0

I tried to select each feature in the layer "temp" and use the selected feature to select features in the other layer "Railway" based on location and then do some calculations with the "Railway" layer using the values in the "temp" layer. Here is the code:

import arcpy
fc = "C:/Users/xkuai2/Downloads/temp/temp.shp"
cursor = arcpy.SearchCursor(fc)
for row in cursor:
    count = 0
    Vol = row.getValue("Volume")
    arcpy.SelectLayerByAttribute_management("temp", "NEW_SELECTION", "FID" = count)
    arcpy.SelectLayerByLocation_management("Railway", "WITHIN", "temp", , "NEW_SELECTION")
    arcpy.CalculateField_management("Railway", "Volume", "[Volume] + Vol", "VB")

But I came across with the error message:

Parsing error SyntaxError: invalid syntax (line 5)

I checked the code and have no idea what goes wrong.

1

To try and debug this I would run the test below:

import arcpy
fc = "C:/Users/xkuai2/Downloads/temp/temp.shp"
cursor = arcpy.SearchCursor(fc)
for row in cursor:
    count = 0
    Vol = row.getValue("Volume")
    print Vol

This will mean your testing cycles are quicker than when doing the additional Geoprocessing.

Your syntax up to that point looks OK to me so I am wondering whether there may be a special character in one of rows 4-6 - perhaps try re-typing each, or copy/pasting your code out to Notepad and back.

If you still see the error, then look closer at your syntax below:

arcpy.SelectLayerByAttribute_management("temp", "NEW_SELECTION", "FID" = count)

by running the tool from its tool dialog, and then using Results | Results window to Copy As Python Snippet to compare the syntax it produces with what you are using.

  • Thank you for answering. I've tried printing "Vol" values and it works. And yeah I think there seems to be problems with the "SelectLayerByAttribute_management" function: it's already in the "for" loop but I still used the "FID" values to select. The error might be logic instead of syntax. – PacmanKX Jul 31 '15 at 19:08
1

I'll try to do a little better job of explaining.

This line is not necessary:

arcpy.SelectLayerByAttribute_management("temp", "NEW_SELECTION", "FID" = count)

Because you can instead just use the SearchCursor to loop through every feature in the temp layer, and use each feature's geometry object in the SelectByLocation.

The geom variable is getting the geometry object for the current feature in the for loop. So imagine FID 1, on the first for loop geom will hold the geometry object for that first feature. This geometry object can then be used in the SelectByLocation function.

import arcpy

fc = "C:/Users/xkuai2/Downloads/temp/temp.shp"

#file path to the railway layer is located
railway = "C:/Users/xkuai2/Downloads/temp/railway.shp"
output = "C:/Users/xkuai2/Downloads/temp/output.shp"

arcpy.MakeFeatureLayer_management(railway, "railway_fl")

cursor = arcpy.SearchCursor(fc)

for row in cursor:
    #geom holds the geometry object of the feature for the current row
    geom = row.getValue("SHAPE")
    vol = row.getValue("Volume")

    #the selectbylocation function can take the geom as input, so here we are
    #selecting all railways that are within the geometry of the current temp feature
    arcpy.SelectLayerByLocation_management("railway_fl", "WITHIN", geom)

    #you should probably double check your expression
    arcpy.CalculateField_management("railway_fl", "Volume", "[Volume] + vol", "VB")

arcpy.CopyFeatures_management("railway_fl", output)
arcpy.Delete_management("railway_fl")
  • Thank you for answering. I'm confused with the "geom": does it make sure that only one feature in the "temp" feature is selected and that feature is used to select in the "Railway" feature? – PacmanKX Jul 31 '15 at 19:09
  • @PacmanKX I've updated my answer with some more details. Hopefully it helps. But, yeah, what you said sounds correct. geom holds the geometry of the feature of the current row, which is then used in the SelectByLocation to select any railways that are within the geometry object. – ianbroad Jul 31 '15 at 21:25
  • Great! Very detailed explanation! I'll try that. Thank you! – PacmanKX Jul 31 '15 at 21:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.