POST
|
Hi Everybody: I am writing a python script to perform data validation for data editor using ArcMap, and want to make it into a tool in ArcToolbox. If the tool is executed from ArcMap with a MXD file open, is it possible to let it detect the active data connection in the MXD and directly use it to retrieve data? Thank you. Try import arcpy.mapping print arcpy.mapping.MapDocument("CURRENT").filePath
... View more
04-16-2012
09:36 PM
|
0
|
0
|
263
|
POST
|
For the script as below, can I directly use fieldname, rather than newName in SQL? (so delete newName = arcpy.AddFieldDelimiters("C:/Data", fieldName) ...) If not, why? When to use AddFieldDelimiters? Any difference to ValidateFieldName? Thanks indeed!! import arcpy from arcpy import env fieldName = arcpy.GetParameterAsText(0) wkspace = arcpy.GetParameterAsText(1) in_features = arcpy.GetParameterAsText(2) out_feat_class = arcpy.GetParameterAsText(3) stateVal = arcpy.GetParameterAstext(4) # AddFieldDelimieters will return a field name will the proper # field delimiters for the workspace specified. # newName = arcpy.AddFieldDelimiters("C:/Data", fieldName) # Use delimited field for Select tool SQL expression # sqlExp = newName + " = " + stateVal env.workspace = wkspace arcpy.Select_analysis(in_features, out_feat_class, sqlExp) The AddFieldDelimiters will add the correct surrounds for the field depending on the source location for example [field] 'field' field etc as different workspaces need different ones. Use AddFieldDelimiters when constructing a SQL statement as these save you some work in getting it right. ValidateFieldName removes invalid characters form a field name (usually user typed) so it will conform and not cause code to crash by replacing invalid characters with an underscore. hth Stephen
... View more
04-16-2012
07:46 PM
|
0
|
0
|
510
|
POST
|
This may be simple but I am only a beginner. Does anyone have a Python script to populate a field based on the code of another field in the same table? Or do have any recommendations where I might find a script? Thank you Pam Hi Pam In python use an updateCursor to access and update the rows then work row by row making your changes as required. Here is something to get you started:
import arcpy
cur = arcpy.UpdateCursor(r"c:\your file") #put in your file path here
for row in cur:
row.fieldName1 = row.fieldName2 #here I am copying the data from one field to another, just add your own code here.
cur.updateRow(row) #transfer the row changes from memory to the table
Hope this helps Stephen
... View more
04-16-2012
07:38 PM
|
0
|
0
|
333
|
POST
|
The del command deletes a reference to say a cursor and thus frees up some memory and more importantly releases the resource being used. Consider a script where you open am updateCursor on a featureclass and thus lock it for others and your script runs for 20+ hours but only needs to update the featureclass and then does nothing with it for the remainder of the script, you have a featurecalss now ties up for 20+ hrs, if you use del and delete the reference to it and unlock it is is available for use then and users don't have to await the script's end. Hope this helps Stephen
... View more
04-15-2012
07:22 PM
|
0
|
0
|
240
|
POST
|
So let's look at what you are doing fcs = gp.ListFeatureClasses()
fc = fcs.next()
while fc:
print fc
fc = fcs.next()
fcs.reset()
fc = fcs.next() With this code block (above) you are simply printing out the names of the fcs found and not processing anything, fcs.reset() does nothing as it is not implemented so don't use it. so the code below isn't going to do much work as fc will not hold a value.
print gp.GetMessages()
while fc:
print fc
infc= fc
OutRaster = outshp + "/" + str(fc)
print gp.GetMessages()
gp.ExtractByMask_sa(Inraster, infc, OutRaster)
print gp.GetMessages() If this did work infc would only be the last fc recorded so the mask will only be on that one and not all of them, Why are you using gp and arcpy, just use arcpy. try you code for just 1 fc first and build on that. import arcpy
arcpy.CheckOutExtension("Spatial")
fcs = gp.ListFeatureClasses()
Inraster = "D:/DdAaTtAa/Data/image/"
for fc in fcs:
print fc
infc= fc
OutRaster = outshp + "/" + str(fc)
gp.ExtractByMask_sa(Inraster, infc)
outExtractByMask.save(OutRaster) which will give you a mask with the last fc Once you have this working to work on all fc in the list you will need to combine (union/merge...) them into 1 and then apply the mask with this new fc. Hope this helps to get you started if you haven't figured it out already.
... View more
04-02-2012
02:05 PM
|
0
|
0
|
304
|
POST
|
If you are running this from within an ArcMap/Catalog session you can also create a tool script dialog box and attach your script to it to get the input that way and the dialog will ensure it is a workspace as well.
... View more
03-22-2012
05:31 PM
|
0
|
0
|
235
|
POST
|
Hi Jake Looks like the environment setting 'Same as Display' works (even or one layer loaded into a fresh mxd that wasn't working). After a look through the help files it looks like this isn't something I can set in python code, do you know of a way to set this in code? I can only see the ability to set the projection in code. Thanks Stephen
... View more
03-21-2012
02:17 PM
|
0
|
0
|
620
|
POST
|
hmm, and then I do a retest of the theory and it now refuses to work again...
... View more
03-20-2012
09:11 PM
|
0
|
0
|
620
|
POST
|
I think I have worked out what the problem is, the selectlayerbylocation doesn't work when the layer coordinate system is different to the dataframe's in that is one is projected and the other is geographic then the layer that is different refuses to be selected. When I started with a fresh mxd it all worked as expected. Is this a bug or "as designed"?
... View more
03-20-2012
08:59 PM
|
0
|
0
|
620
|
POST
|
This is my setup: [ATTACH=CONFIG]12881[/ATTACH] The full path is to a point feature class D:\GeoFabric\HR_Catchments\HR_Catchments.gdb\HR_Catchments\AHGFNode
... View more
03-20-2012
01:55 PM
|
0
|
0
|
620
|
POST
|
Interestingly if I apply a GetCount_management to the selection layer it returns 0 records even though there are 40308 features within, the feature set returns the correct number of features. Further testing- if I add the polygon fc and run the script the correct number of features is returned by the count but on a second run straight after the count is back to 0, in both cases still no selected polygons. The only hint I get (and this may not be relevant) is that the tool messages include this: Executing: SelectLayerByLocation AHGFContractedCatchment INTERSECT in_memory\{68DDA71F-1C9E-4477-A9EE-4746D9B0F4E3} # NEW_SELECTION Which may mean that this is becoming an in memory process and not transferred to the activeview? As you can see my code is practically the same as your suggestion:
import arcpy
features = arcpy.GetParameterAsText(0)
layertoSelect = arcpy.GetParameterAsText(1)
result = arcpy.GetCount_management(layertoSelect)
arcpy.AddMessage("x = " + str(result.getOutput(0)))
arcpy.SelectLayerByLocation_management(layertoSelect,"INTERSECT",features,"","NEW_SELECTION")
for m in range(0,arcpy.GetMessageCount()-1):
arcpy.AddMessage(arcpy.GetMessage(m))
arcpy.RefreshActiveView()
Stephen
... View more
03-19-2012
02:06 PM
|
0
|
0
|
620
|
POST
|
Hi Jake Thanks for your response and suggestion, unfortunately this matches what I have tried and although the code completes successfully nothing is selected in the polygon featureclass or shapefile underneath either onscreen or in the attribute table. Is there something else I need to do? Stephen
... View more
03-19-2012
01:40 PM
|
0
|
0
|
620
|
POST
|
The string module allows you to search for substrings. os.path.basename and os.path.dirname will break up the mxd name and path.
... View more
03-18-2012
08:40 PM
|
0
|
0
|
210
|
Online Status |
Offline
|
Date Last Visited |
11-11-2020
02:23 AM
|