POST
|
I need to copy a set of personal geodatabases from a master directory to a local working directory for processing. Here's my Python code: import os import shutil dirlist=os.listdir("Z:\\COMMON\\GIS\\WORK\\Ron\\NEW") dst="C:\\Arcwork\\parcelmdb" mdblist=[] for item in dirlist: [INDENT] if item.lower().endswith("mdb"): mdblist.append(item) [/INDENT] for db in mdblist: [INDENT] shutil.copyfile(db,dst) [/INDENT]However, when run I get an error: Traceback (most recent call last): File "C:\Arcwork\GetMasterMDBList.py", line 11, in <module> shutil.copyfile(item,dst) File "C:\Python26\ArcGIS10.0\lib\shutil.py", line 52, in copyfile fsrc = open(src, 'rb') IOError: [Errno 2] No such file or directory: 'Bolton.mdb' Just so that you understand why your approach didn't work: print the arguments (`db` and `dst`) before you pass them to shutil.copyfile. Probably db is 'Bolton.mdb'. If you pass that to shutil.copyfile it guesses './Bolton.mdb' where '.' is the directory the script was started in - and it doesn't seem that there is a file called 'Bolton.mdb'.
... View more
08-23-2010
09:07 AM
|
0
|
0
|
491
|
POST
|
While waiting for a batch process to finish I had a look at your script. Please take the following as some ideas and remarks, I'm by no means an expert myself and this is supposed to be constsructive criticism 🙂 In some of your show* functions you use variables in the function body which you do not pass to your function as arguments. This works in this case but it is fragile as you have to make sure that your variable from the higher scope which you use in the function is defined before your function is called. Why not: def showMessage(message):
print message
showMessage('my testmessage') instead of def showMessage():
print message
showMessage() # oops
message = 'testing'
It is not usual to use the semicolon syntax in Python. One command, one line - but in the end this is a matter of taste, of course. Please be careful with all these blank except statements. A blank except statement swallows *all* possible python errors and makes it very hard to debug a program. I haven't used my scripts from inside of ArcMap so far, so it might be necessary to do it there. But then again, it might be a good idea to write the traceback at least to your log file. At the moment I do the following in my scripts: try:
gp:Intersect_Analysis('test1.shp; test2.shp', 'out.shp')
except arcgisscripting.ExecuteError:
print gp.GetMessages(2) This has the advantage that it doesn't silently ignore any other errors I'm not aware of (for example the syntax error which can be found in the snippet). It might have disadvantages when starting scripts from ArcMap of which I'm not aware of at the moment but it may be worth a try. In line #68 it seems as if you want to catch an IOError except IOError:
....do some stuff.... From #79 on it seems to me as if the license will always be set to 'ArcView'. In an if-elif- statement not all conditions are checked. After the first condition in your if-elif- tree evaluates as `True` the body of that condition is executed and the rest of the elif-else conditions are skipped: >>> a = True
>>> b = True
>>> if a == True:
... print 'a is True'
... elif b == True:
... print 'b is True'
...
a is True So you need to invert the order of your conditions as ArcInfo is the highest license level - if I'm taken correctly 🙂 In #157: do you want to catch a NameError? I hope you don't find these remarks, well, overbearing.
... View more
08-23-2010
08:55 AM
|
0
|
0
|
317
|
POST
|
Right click on your tool->batch->rigth click on one of your input values -> browse -> select multiple files as you normally do (SHIFT or CTRL) - this works fine for one of your input values.
... View more
08-23-2010
06:19 AM
|
0
|
0
|
270
|
POST
|
You want to call a method. You call objects by using round brackets. clm42's snippet misses a `:` at the end of line 4. Please note that Python is case-sensitive. Your attempt will fail if a file is - for whatever reason - called .MDB, .mDb or .mDB. >>> a = 'test.MDB'
>>> a.endswith('mdb')
False
Therefore you need to "convert" your item variable to lower-case before testing for 'mdb' >>> a = 'test.MDB'
>>> a.lower()
'test.mdb'
>>> a.lower().endswith('mdb')
True
This adds up to: dirlist=os.listdir("c:\\Arcwork")
mdblist=[]
for item in dirlist:
if item.lower().endswith("mdb"):
print item
mdblist.append(item)
print mdblist
... View more
08-20-2010
12:04 PM
|
0
|
0
|
971
|
POST
|
For part a) of your task (find center of an ellipse) i got a solution for a circle which should be the same for an ellipse: import arcgisscripting
gp = arcgisscripting.create(9.3)
print 'Geoprocessor initialised'
circle_path = ur'c:/test/circles.shp'
cursor = gp.SearchCursor(circle_path)
center_points = list()
# let's iterate over all rows in the circle shape
for row in iter(cursor.next, None):
# get the circle polygon
circle = row.shape
# get xmin, xmax etc.
x_min = circle.extent.XMin
y_min = circle.extent.YMin
x_max = circle.extent.XMax
y_max = circle.extent.YMax
center_x = x_max - x_min
center_y = y_max - y_min
center_points.append((center_x, center_y))
print center_points If that works you only need a solution for b)
... View more
08-19-2010
02:11 AM
|
0
|
0
|
1042
|
POST
|
It is hard to track your exact problem down as I don't know the content of your variables. But the following might help you as it is how I have just solved my problem. import arcgisscripting, os.path
gp = arcgisscripting.create(9.3)
input_tables = [u'table_1', u'table_2', u'table_3']
in_dir = u'c:/path_to_my_tables'
out_dir = u'c:/whatever'
# let's use absolute file paths (c:/path_to_my_tables/table_1) and let's use an
# ESRI value table instead of building an insert string ('c:/.../table1;c:/..../table2')
# btw, why does the tool not accept a python list as input argument?
val_tab = gp.CreateObject("ValueTable")
for table in input_tables:
val_tab.AddRow(os.path.join(in_dir, table))
merged_table = os.path.join(out_dir, 'merged_table')
gp.Merge_management(val_tab, merged_table, '')
... View more
08-18-2010
11:57 PM
|
0
|
0
|
725
|
POST
|
Are you sure that this evil blank except statement (normally one says that it is best practice to catch only specific Exceptions - never ALL possible exceptions as this may lead to errors which are hard to track down. But in the ArcGIS world this seems to be...well..different. At least for many 9.3 examples) does not catch some python error which you are not aware of? And just a tip for file name/path operations: You can use forward slashes instead of backwards slashes which may be less error-prone as you don't have to fight with escape sequences path = 'c:/test' instead of path = 'c:\\test' For path operations you might want to try os.path.join: import os.path
base_dir = 'c:/test/shp'
shape_name = 'test.shp'
shape_path = os.path.join(base_dir, shape_name) This is less error-prone as you don't need to take care of missing slashes at the end of base_dir etc. I don't know if this solves your problem, but I will have to do the exact same task tomorrow so maybe I can then give you some more useful information.
... View more
08-18-2010
04:20 PM
|
0
|
0
|
725
|
POST
|
I've had 3 intensive days with ArcGIS 9.3 so far and honestly, I'm getting a little annoyed by its flaws. What could be the reason that a geoprocessing script just hangs? My simple python script does the following (pseudo code) for shape_file in directory:
AddField_management(new_field)
CalculateField_management(new_field, fancy_expression) It does the job fine for some files and then it just hangs when calculating a field. Where hanging means that I can wait for 10 minutes without progress whereas the field of a much larger shape file had been calculated in about 2 minutes. I can abort the script and when I then check the last edited shape file: everything is ok. The new field was added and it was even calculated correctly for all records. So I can adapt my script and restart it each time it hangs. But that is a bad workaround. So I thought: hey, if ArcGIS has problems with external scripts I can build a model for such an easy task. So I did that (just chained AddField and CalculateField together in Modelbuilder) and started a batch process for my last 6 missing files (right click on my model in the toolbox, right click on the input parameter and selecting multiple files gives me multiple rows in the batch grid). AddField was finished for all the six files but none of the six started CalculateField jobs was finished after 20min. The green bar kept moving but nothing happened anymore. I canceld the job and checked the shape files manually: everything was alright, the field was calculated correctly for every shape file. So is there any advice on this? Anything I can do about it? Edit: sry, I think I've posted this in the wrong sub forum.
... View more
08-11-2010
07:58 AM
|
0
|
0
|
667
|
POST
|
Hi Jeff, thanks for your answer. You are right, if I change the NUTS levels to a 'list of values' it gives me the batch grid. But unfortunately this breaks my whole model as the calculation of the NUTS number now happens only once before the Intersect tool is run. Maybe I should just write a python script but I'd like to understand how that would work in Modelbuilder. My initial series approach does work and I hoped that I could just run that several times with different country_code input (well, that's what I thought the batch grid was for :\). So the conclusion is: it is generally not possible to use the batch grid when a series of values is used in your model?
... View more
08-11-2010
02:35 AM
|
0
|
0
|
1136
|
POST
|
Hi all, I want to use calculate value (9.3) in order to build a certain string. The result should be: '%value_of_variable%' (note that I want the `%` to be present *after* the in-line variable substitution) So I created a simple model (Screenshot). My problem is that for the following expressions I get the following results (with the value of String as Hello): My first try gives a result as expected (in-line variable substitution) Expression 1: '%String%' Result 1: Hello Second try: Expression 2: '%%String%%' Result 2: %%String%% Oh well - why? If %% was an escape sequence the result should give %String% and not %%String%%, or am I wrong? And where is this behaviour documented? Third try: Expression 3: '%%%String%%%' Result 3: %%Hello%% Well, almost. So how is it possible to get %Hello% in my example?
... View more
08-11-2010
12:31 AM
|
0
|
0
|
539
|
POST
|
Thanks for your answer but I don't understand what you mean. Which input row? In your screenshot it seems as the batch grid is displayed. But my problem is that I don't get to this batch grid. Instead, the normal input mask is displayed (see screenshots).
... View more
08-10-2010
11:54 PM
|
0
|
0
|
1136
|
POST
|
I have created a rather simple model (find it attached to this post) in ModelBuilder (9.3) and it runs just fine but I want to run it as a batch process. So I right click on the model name in the toolbox, select `batch` - and it gives me the regular input mask instead of the batch grid. Why is that? This is just annoying as I don't get any error message... Any ideas?
... View more
08-10-2010
08:32 AM
|
0
|
5
|
3192
|
Online Status |
Offline
|
Date Last Visited |
11-11-2020
02:23 AM
|