|
POST
|
I'm using Python to run a script, but I've run into a small problem. I'm using triple quotes to form a definition query, but inside the query is a wildcard %, so the interpreter is expecting a variable to format the string. Is there a stroke key that can be used?
mapLyr2.definitionQuery = """"FID" IN (%s) AND "Jurisdicti" NOT LIKE '% Township'""" %dqStr
... View more
10-01-2014
01:27 PM
|
0
|
5
|
5937
|
|
POST
|
My use of Attribute Assistance is very limited. My knowledge of Python is relatively small, but one thing I've done several times is customizing a script to move attribute data from one feature class to another based on spatial selection. I use a search cursor to loop through the layer of data I need extracted, then I query that layer in the loop, followed by a select by location and field calculation. Then I remove the definition query at the end of the loop. This is what I'm using right now to do something similar - I think.
mxd = arcpy.mapping.MapDocument("Current")# Define mxd to current file.
townships = arcpy.da.SearchCursor("Townships", "name")
for township in townships:
tName = township[0]
cName = '''"{0}"'''.format(tName)
mapLyr1.definitionQuery = """"name" = '{0}'""".format(tName)
arcpy.SelectLayerByLocation_management("Layer2", "INTERSECT", "Townships", "0 FEET", "NEW_SELECTION")
arcpy.CalculateField_management("Layer2", "Jurisdicti", cName, "PYTHON")
mapLyr1.definitionQuery = None
This takes the township name and places in Layer2's field. You can do many things with this basic setup, though it can take some time to run when there are hundreds or thousands of features.
... View more
09-24-2014
08:42 AM
|
1
|
0
|
868
|
|
POST
|
I found a work around, but it doesn't solve the question of formatting. If I change the variable to have double quotes it accepts it. I have to format the variable with the quotation marks wrapped in triple apostrophes, like this: cName = '''"{0}"'''.format(tName)
... View more
09-24-2014
08:23 AM
|
0
|
0
|
559
|
|
POST
|
Have you tried the option "Have Their Centroid In?"
arcpy.SelectLayerByLocation_management("Grid", "HAVE_THEIR_CENTER_IN", "Tract", "0 FEET", "NEW_SELECTION")
... View more
09-24-2014
08:11 AM
|
0
|
1
|
868
|
|
POST
|
I'm trying to calculate a field using the Python window, but I can't seem to figure out how to format the function properly. What's the correct syntax for using a string variable as the expression? arcpy.CalculateField_management(mapLyr2, "Jurisdicti", "'{0}'.format(tName)", "PYTHON") I couldn't find an example that answered this question for me. Thanks in advance.
... View more
09-24-2014
08:02 AM
|
0
|
2
|
2684
|
|
POST
|
I created a new mxd with my layers in it and it's working. So I don't know what caused the issue to begin with.
... View more
06-23-2014
06:44 AM
|
0
|
0
|
877
|
|
POST
|
Yeah, it's not the layer name. Is it possible for an mxd to become corrupt that certain functions won't run?
... View more
06-23-2014
06:40 AM
|
0
|
0
|
877
|
|
POST
|
I tried the same script in a different mxd and it worked fine. But it still won't run in the map I created it for. What might cause this?
... View more
06-23-2014
06:32 AM
|
0
|
0
|
877
|
|
POST
|
I'm having an issue getting a loop to run in my map. Something as simple as just trying to print "yay" won't iterate. Here's what I have to even a test that I can't get to run. Am I missing something? mxd = arcpy.mapping.MapDocument("Current")
mapLyr1 = arcpy.mapping.ListLayers(mxd, "sampleLayer") [0]
cursor = arcpy.SearchCursor(mapLyr1)
for row in cursor:
print "yay"
... View more
06-23-2014
06:28 AM
|
0
|
5
|
1217
|
|
POST
|
The probably looks pretty ugly, but I found two examples that I got working to create a list and definition query for the features. Here's the entire script. I did not try del rows2 yet in the first loop. import arcpy
#set map doc and the layer to be used
mxd = arcpy.mapping.MapDocument("Current")
mapLyr1 = arcpy.mapping.ListLayers(mxd, "Parcels_With_PageNum") [0]
mapLyr2 = arcpy.mapping.ListLayers(mxd, "H2O_Pts_Nearest_Address") [0]
ID_Silvis = int()
field = 'NEW_PIN'
fcName = 'H2O_Pts_Nearest_Address'
myList = set([row.getValue(field) for row in arcpy.SearchCursor(fcName)])
field = 'NEW_PIN'
dquery = []
for mapLyr2 in arcpy.mapping.ListLayers(mxd, fcName):
for row in arcpy.SearchCursor(mapLyr2):
dquery.append("'{}'".format(row.NEW_PIN))
mapLyr1.definitionQuery = '"NEW_PIN" in ({})'.format(", ".join(dquery))
rows1 = arcpy.UpdateCursor(mapLyr1, "", "", "NEW_PIN")
for row in rows1:
hydcount = 1
newpin = row.NEW_PIN
print newpin
arcpy.SelectLayerByAttribute_management(mapLyr2, "NEW_SELECTION", "\"NEW_PIN\" = '%s'" %newpin)
rows2 = arcpy.SearchCursor(mapLyr2)
for row2 in rows2:
hydfield = "Hydr_Num" + str(hydcount)
ID_Silvis = row2.ID_Silvis
# arcpy.CalculateField_management(mapLyr1, hydfield, hydID, "PYTHON")
row.setValue(hydfield, ID_Silvis)
hydcount = hydcount + 1
rows1.updateRow(row)
arcpy.SelectLayerByAttribute_management(mapLyr2, "CLEAR_SELECTION")
mapLyr1.definitionQuery = ''
del mxd, row, rows1, mapLyr1, mapLyr2, hydcount, hydfield, row2, rows2, dbquery, field, myList, fcName, field#, hydID
... View more
02-24-2014
03:01 PM
|
0
|
0
|
1222
|
|
POST
|
Since there aren't too many points to parcels in the data, I was able to sort the attribute table of the points and see what the max number of hydrants per parcel there was and so I created 6 or 7 "hydr_x" fields. The counter starts at one and can fill up to that point. You're right about what if I run out of fields, though. Then the counter resets back to 1 at the beginning of the first loop. I tried adding the del row2, rows2 to the end of the first loop, but I get an error saying row2, rows2 isn't defined. For a selection, I was thinking about creating a list of all unique values in the point layer and using that as a definition query. Not all the points are contained within a parcel. But I did use a spatial join of "nearest neighbor" to get a PIN for each point. So select by location wouldn't return all the parcels that need to be selected.
... View more
02-24-2014
09:28 AM
|
0
|
0
|
1222
|
|
POST
|
Thank you for your help Richard. You're right about ID_Silvis. Between revisions that got mixed up. ID_Silvis is the field I need copied. So I went and corrected that and on a single feature with 2 points it seems to be working. The next thing, would be to create a selection of features to run the script on. So right now I have about 340 points, but when I do a join, they only seem to be associated with 90 parcels. So to create a definition query that could select just those 90 parcels and run the script on those instead of 3,500 parcels. I will be reading into how to create a list and using that to create a selection. So far here's what I've got working. import arcpy
#set map doc and the layer to be used
mxd = arcpy.mapping.MapDocument("Current")
mapLyr1 = arcpy.mapping.ListLayers(mxd, "Parcels_With_PageNum") [0]
mapLyr2 = arcpy.mapping.ListLayers(mxd, "H2O_Pts_Nearest_Address") [0]
ID_Silvis = int()
rows1 = arcpy.UpdateCursor(mapLyr1, "", "", "NEW_PIN")
for row in rows1:
hydcount = 1
newpin = row.NEW_PIN
print newpin
arcpy.SelectLayerByAttribute_management(mapLyr2, "NEW_SELECTION", "\"NEW_PIN\" = '%s'" %newpin)
rows2 = arcpy.UpdateCursor(mapLyr2)
for row2 in rows2:
hydfield = "Hydr_Num" + str(hydcount)
ID_Silvis = row2.ID_Silvis
# arcpy.CalculateField_management(mapLyr1, hydfield, hydID, "PYTHON")
row.setValue(hydfield, ID_Silvis)
hydcount = hydcount + 1
rows1.updateRow(row)
# arcpy.SelectLayerByAttribute_management(mapLyr2, "CLEAR_SELECTION")
del mxd, row, rows1, mapLyr1, mapLyr2, hydcount, hydfield, hydID, row2, rows2
... View more
02-24-2014
07:45 AM
|
0
|
0
|
1222
|
|
POST
|
Thank you for that. I kind of restarted what I had yesterday, and I'll past the whole thing this time. I understand the posts I've quickly looked at in that link and will give that consideration when I rework this again. For now this is what I have. And just our of curiousity, I already know you don't recommend the old cursors (read that part) can a nested loop update or set a value to a layer outside of itself? In other words, can "row2" update a field in "row1?" import arcpy, os, time
#set map doc and the layer to be used
mxd = arcpy.mapping.MapDocument("Current")
mapLyr1 = arcpy.mapping.ListLayers(mxd, "Parcels_With_PageNum") [0]
mapLyr2 = arcpy.mapping.ListLayers(mxd, "H2O_Pts_Nearest_Address2") [0]
ID_Silvis = int()
rows1 = arcpy.UpdateCursor(mapLyr1, "", "", "NEW_PIN")
for row in rows1:
hydcount = 1
newpin = row.NEW_PIN
print newpin
arcpy.SelectLayerByAttribute_management(mapLyr2, "NEW_SELECTION", " \"NEW_PIN\" = '%s' " %newpin)
rows2 = arcpy.UpdateCursor(mapLyr2)
for row2 in rows2:
hydfield = "Hydr_Num" + str(hydcount)
hydID = ID_Silvis
row.setValue(hydfield, hydID)
rows1.updateRow(row)
hydcount = hydcount + 1
arcpy.SelectLayerByAttribute_management(mapLyr2, "CLEAR_SELECTION")
del mxd, row, rows1, row2, rows2, mapLyr1, mapLyr2, hydcount, hydfield, hydID I know this code is considered slow, but from what I've learned of Python so far, it's the best I know how to do. But I will read through the page you linked. Thank you.
... View more
02-21-2014
01:02 PM
|
0
|
0
|
1222
|
|
POST
|
I have a script that uses one layer to select features of a second layer in the first loop of the script. When the second layer's features are selected, I have a second loop that is supposed to update the fields on the first layer in the first loop. Is there a specific way of using an updateCursor or setValue to affect the nesting loop? for row1 in rows1:
count = 1
print "row %d" %parcelrow
parcelrow = parcelrow +1
selectcount = 0
currentPIN = row1.getValue(selectField)
arcpy.SelectLayerByAttribute_management(mapLyr1, "NEW_SELECTION", "\"%s\" = '%s'" %(selectField, currentPIN))
print currentPIN
arcpy.SelectLayerByAttribute_management(mapLyr2, "ADD_TO_SELECTION", "\"%s\" = '%s'" %(selectField, currentPIN))
selectcount = int(arcpy.GetCount_management(mapLyr2).getOutput(0))
print "%d points selected" %selectcount
if selectcount == 0:
arcpy.SelectLayerByAttribute_management(mapLyr1, "CLEAR_SELECTION")
pass
else:
for row2 in rows2:
hydrantfield = copytoField + "%d" %count
print "filling field " + hydrantfield
hydrID = row2.getValue(copyField)
#This setValue is supposed to update mayLyr1
row1.setValue(hydrantfield, hydrID)
rows1.updateRow(row1)
count = count + 1
arcpy.SelectLayerByAttribute_management(mapLyr1, "CLEAR_SELECTION")
arcpy.SelectLayerByAttribute_management(mapLyr2, "CLEAR_SELECTION") The second loop seems to get stuck. Shouldn't the loop only run through the selected features then continue back to the first loop?
... View more
02-20-2014
10:07 AM
|
0
|
10
|
1837
|
|
POST
|
I'm working on a set of choropleth maps for planning project areas. We're discussing whether it's better to create maps with percentages of total population or to show density (normalized by acre of census tract) of percentage of population. The maps are just to show where the planned project areas in relation to areas below poverty, etc. are. So is it better or more of a standard just to show where percentages of population demographics are or would it be better to show the density of those areas?
... View more
01-10-2014
10:59 AM
|
0
|
1
|
1021
|
| Title | Kudos | Posted |
|---|---|---|
| 1 | 09-24-2014 08:42 AM | |
| 1 | 01-30-2015 09:21 AM | |
| 1 | 01-30-2015 09:25 AM |
| Online Status |
Offline
|
| Date Last Visited |
11-11-2020
02:24 AM
|