|
POST
|
Caleb, Your suggestion exp = '"{0}" = {1}'.format(field, ElValue) worked after I finally figured out else I was doing wrong. It helps if the search and select from layer are the same layer. DOH! Thank you as well, James. Now off to find the rest of the bugs in this beast.
... View more
06-07-2013
10:46 AM
|
0
|
0
|
841
|
|
POST
|
Caleb, Thanks for you reply. Yes GridValue is and integer--actually it will most likely be a float value or either or: i.e. ElValue can equal 118, or 118.5, etc. The code should actually look like this: field = "ELEVATION"
cursor = arcpy.SearchCursor(GAGELayer)
for row in cursor:
ElValue = float(row.getValue(field))
exp = "\"" + field + "\" = " + ElValue + ""\"
arcpy.SelectLayerByAttribute_management (GAGEMergeLayer, "NEW_SELECTION", exp) I tried exp = '"{0}" = {1}'.format(field, GridValue) and I'm still getting: ExecuteError: ERROR 000358: Invalid expression
... View more
06-07-2013
09:46 AM
|
0
|
0
|
841
|
|
POST
|
I am trying to create and expression that uses the value of the ELEVATION field in a feature class to select out a single feature and export it to its own feature class; essentially "ELEVATION" = 3 where the value '3' is a variable. Needless to say, I'm having difficulty with the expression--I don???t have a firm grasp on the whole "\"" thing. Here's what I have at the moment. field = "ELEVATION" cursor = arcpy.SearchCursor(GAGELayer) for row in cursor: ElValue = float(row.getValue(field)) exp = "\"" + field + "\" = " + GridValue + ""\" arcpy.SelectLayerByAttribute_management (GAGEMergeLayer, "NEW_SELECTION", exp) I get it if '3' was a constant. (I'm not real clear what "\"" means exactly. does "\"" = "" or = "? And how do you reverse it for the end of an expression?) Something like this exp = "\"" + field + "\" = 3" I'm not sure how to take that and make the expression work with '3' as a variable. Any help (and a little explanation) would be greatly appreciated.
... View more
06-07-2013
09:08 AM
|
0
|
5
|
1320
|
|
POST
|
Thanks guys! I found a solution to my issue, but I'm going to have to rewrite the script to handle SDE database pathnames, so I will give these options a shot.
... View more
01-23-2013
02:52 AM
|
0
|
0
|
3468
|
|
POST
|
I am trying to automate a naming process that takes the file name from a list of file paths to name other folders, geodatabases, zipfiles, etc. The problem I'm running into is that, I assume, I am trying to run string functions on list object and I can't figure out how to get the script to do what I need it to do. For example, I have the file path C:\TEMP\Durham.shp. I want to name a geodatabase, folder, and zipfile "Durham". Instead I get a folder and geodatabase named "C". Can someone take a look at my script and tel me what I'm doing wrong? The following script is that last incarnation of several attempts. import arcpy, os import zipfile from arcpy import env from os.path import basename BASELAYERS = arcpy.GetParameterAsText(0) CLIPLAYERS = arcpy.GetParameterAsText(1) OUTFOLDERMASTER = arcpy.GetParameterAsText(2) FILETYPES = ["*.shp", "*.dbf", "*.shx", "*.prj", "*.sbn", "*.sbx", "*.xml"] env.workspace = OUTFOLDERMASTER for LAYERS in CLIPLAYERS: LAYERSSTR = ''.join(LAYERS) BASENAME = LAYERSSTR.split("\\")[-1] FOLDER = os.path.join(OUTFOLDERMASTER, BASENAME) os.makedirs(FOLDER) PGDBNAME = "NCFlood_Effective_" + BASENAME + "_PGDB.mdb" arcpy.CreatePersonalGDB_management(FOLDER, PGDBNAME, "9.3") PGDB = os.path.join(FOLDER,PGDBNAME) for FILES in BASELAYERS: FEATURENAMESTR = ''.join(FILES) FEATURENAME = os.path.basename(FEATURENAMESTR) SHAPEFILENAME = FEATURENAME + ".shp" SHAPEFILE = os.path.join(FOLDER, SHAPEFILENAME) arcpy.Clip_analysis(FILES, LAYERS, FEATURENAME) arcpy.Clip_analysis(FILES, LAYERS, SHAPEFILE) ZIPFILELIST = [] for TYPE in FILETYPES: ZIPFILELIST.extend(glob.glob(FOLDER)) ZIPFILENAME = os.path.join(FOLDER, "NCFlood_Effective_" + FOLDERNAME + "_SHP.zip") ARCHIVE = zipfile.ZipFile(ZIPFILENAME, "w") for ZIP in ZIPFILELIST: ARCHIVE.write(ZIP, os.path.basename(ZIP), zipfile.ZIP_DEFLATED) ARCHIVE.close() I have also tried BASENAME = os.path.basename(LAYERS) FOLDERNAME = os.path.splitext(BASENAME)[0]
... View more
01-18-2013
06:18 AM
|
0
|
3
|
4410
|
|
POST
|
Mathew & Jake, Thanks for the quick replies. I was just coming back to add additional info to this post about running the script from Catalog. I discovered that if I hit either button often and fast enough it will complete because somewhere between the clicks the message box becomes the active dialog and the main script window becomes inactive allowing the script to finish its job. If only there was a way to do this pragmatically... I kind of figured that I was going to have to find another way around this a couple hours ago. But like I said earlier, once I started down this path, I wanted to finish it because everything I'd read said this should work (BTW the semi-colons are remnants from experimentation I forgot to remove). I guess I will just have the code check for the files and use AddWarning to instruct the user to download the files like you both suggested. A little disappointing, but at lease it will work.
... View more
09-27-2012
09:45 AM
|
0
|
0
|
2300
|
|
POST
|
I'm trying out pop-up message windows and am not having much luck. I want to ask the user if certain files exist before beginning a process then instruct the user to download the files if they don't already exist. I opted to throw up a message box that asks if the files exist using the win32api.MessageBox / win32con.MB_YESNO functions, then if the answer is 'no' another message box using win32con.MB_OK. The script runs and a message box appears with the yes/no buttons, but nothing happens if you click either button. I actually have to click the 'Close' button on the main script dialog before only the 'no' button will work in the message box. But then I get a process canceled by user error. While I'm sure that there is another way to do this, now that I've started down this path I'm determined to figure it out. Anybody got any ideas? I think I've checked every link on 7 pages of Google results and still have no idea what I'm doing wrong. [HTML]import arcpy, win32com.client, win32api, win32con from win32con import MB_OK, MB_YESNO result = win32api.MessageBox(0, "Do the files exist?.", "Shapefiles", win32con.MB_YESNO); if result == win32con.IDYES: pass else: win32api.MessageBox(0, "Please downloaded and restart the script.", "Shapefiles", win32con.MB_OK); arcpy.AddMessage("did this work?")[/HTML]
... View more
09-27-2012
08:48 AM
|
0
|
3
|
2762
|
|
POST
|
Nope, wait. Looks like I DIDN'T specify dbo.DEFAULT as the version but sde.DEFAULT instead. I should have uploaded the script to begin with. Thanks Jake for pointing that out.
... View more
09-25-2012
04:17 AM
|
0
|
0
|
859
|
|
POST
|
Yep, I am specifying dbo.DEFAULT as the version parameter. Once the database connection is created, I have to manually edit the connection properties and specify dbo.DEFAULT as the version again before it will connect. [HTML]database = r"Database Connections\CENSUS.sde" if arcpy.Exists(database): pass else: arcpy.CreateArcSDEConnectionFile_management ("Database Connections", "CENSUS.sde", "CENSUS", "sde:sqlserver:00.00.000.000", "CENSUS", "DATABASE_AUTH", "USER", "PASS", "SAVE_USERNAME", "sde.DEFAULT", "SAVE_VERSION")[/HTML]
... View more
09-25-2012
04:10 AM
|
0
|
0
|
859
|
|
POST
|
I am trying to make a database connection with the CreateArcSDEConnectionFile_management function in python. Everything works fine EXCEPT that I have to manually change the connection details to dbo.DEFAULT before the connection is usable (I have to do the same even if I am creating the connection through Catalog--Click the Change button in the Connection details section, Choose the dbo.DEFAULT, Click OK on the Connection Details dialog box to close it.) Is this a known issue? Is there a work around that can be scripted so that the user does not need to exit the script and manually perform this step?
... View more
09-25-2012
03:13 AM
|
0
|
4
|
3141
|
|
POST
|
Ok, this has got under my skin a little Don't you hate when that happens? 😉 I'll give this a go as well.
... View more
08-29-2012
10:47 AM
|
0
|
0
|
1693
|
|
POST
|
Thank you clthompson. I appreciate your help. I'll give cursors another look-see, may this time they'll click.
... View more
08-29-2012
08:43 AM
|
0
|
0
|
1693
|
|
POST
|
mzcoyle and clthompson, Thanks for your suggestions. Not to discourage you from making additional cursor suggestions (I'll given them a go if you provide them), but I've never had much luck with search cursors and usually try to avoid them--I just can't seem to wrap my head around them. I always end up with an infinite loop or receive some kind of error. I tried both of your suggestions and neither worked. With mzcoyle's I receive "exceptions.AttributeError: Cursor: Error in parsing arguments." With clthompson's I receive "exceptions.RuntimeError: Row: Field fname does not exist." The bottom code I provided will run and populate the empty MAXSURGE field, however it populates the field with 0's after giving me "The calculate value is invalid for the row with ObjectID = 0..." error. If I try to concatenate the list object into a string value to add to my max() formula, e.g.: SLOSHSTRING = str(fieldNameList)
calculation = "\"max(" + SLOSHSTRING + ")\""
arcpy.CalculateField_management (LAYERS, "MAXSURGE", calculation, "PYTHON", "") I receive "The calculate value is invalid for the row" error. However if I hard code the max() calculation, e.g.: arcpy.CalculateField_management (LAYERS, "MAXSURGE", "max( !SURGE90!, !SURGE90_1!, !SURGE90_12!, !SURGE90_13!, !SURGE90_14!, !MAXSURGE!)", "PYTHON", "") it works just fine and fairly quickly on 18,000+ rows. I guess I'm to figure out how to get python to think that it's getting arcpy.CalculateField_management (LAYERS, "MAXSURGE", "max( !SURGE90!, !SURGE90_1!, !SURGE90_12!, !SURGE90_13!, !SURGE90_14!, !MAXSURGE!)", "PYTHON", "") when it receives this arcpy.CalculateField_management (LAYERS, "MAXSURGE", max({ListObject}), "PYTHON", "") Suggestions? With this or with cursors. Thanks.
... View more
08-29-2012
07:40 AM
|
0
|
0
|
1693
|
|
POST
|
I need some assistance with the syntax for a CalculateField_management calculation as I have still yet to master all the python syntax rules. What I am trying to do is find the max value in a variable number of fields in order to populate another field. I am using the ListFields function to discover the desired fields to choose from, but getting that list into the formula is giving me some difficulty. import arcpy, os, string LAYERS = arcpy.GetParameterAsText(0) SLOSHFILEDS = [f.name for f in arcpy.ListFields(LAYERS,"","DOUBLE") arcpy.CalculateField_management (LAYERS, "MAXSURGE", max(SLOSHFILEDS)) I have tried any number of different string combinations for the max() calc to no avail (not that this particular variation shows that). Adding/Changing the following to the script doesn't give me the syntax error that I would recieve with the above, but it does give me a "The calculate value is invalid for the row with ObjectID = 0..." x 18,526 (or however many rows are in my table) and then does nothing to the table except populate my MAXSURGE field with 0's. SLOSHFILEDS = arcpy.ListFields(LAYERS,"","DOUBLE") fieldNameList = [] for field in SLOSHFILEDS: if not field.required: fieldNameList.append(field.name) arcpy.CalculateField_management (LAYERS, "MAXSURGE", max(fieldNameList)) Hard coding the field names into the formula works great, but of course, I will not always have the same number of fields or same field names to work with. Any help would be appreciated. -John
... View more
08-29-2012
05:04 AM
|
0
|
9
|
1889
|
| Title | Kudos | Posted |
|---|---|---|
| 2 | 07-17-2023 01:26 PM | |
| 1 | 05-26-2015 07:18 AM | |
| 1 | 05-28-2015 05:03 AM | |
| 1 | 04-11-2019 09:19 AM | |
| 1 | 01-12-2016 07:22 AM |
| Online Status |
Offline
|
| Date Last Visited |
06-13-2024
09:06 PM
|