3

I am using Arc 10.1 and I am working on a script that takes a value from a field within one shape file and updates a field in another shape file based on its location. Basically I have smaller parcels that reside within larger areas and I want to assign the name of the larger area to a field within the smaller parcels.

The issue I am having is that when I run the script, every parcel ends up having the same "area". I believe its because its essentially writing each area to the parcel and then rewriting it again and again until there are no more areas to loop through. Can someone provide some advice?

#Make a feature layer for the neighourhoods
arcpy.MakeFeatureLayer_management(neighbourhood, "neighbourhoodLayer")
arcpy.MakeFeatureLayer_management(parcel, "parcelLayerTest1")
#Setup cursor to update new NBHD field
with arcpy.da.UpdateCursor(parcel, (parcelIDField,nbhdField)) as parRows:
    for par in parRows:
        # Create a query string for the current patrol zone 
        #parcelIDstring = par[0]
        #queryString = '"' + str(parcelIDField) + '" =' + "'" + str(parcelIDstring) + "'"

        #Select parcels based on location contained within neighbourhood
        arcpy.SelectLayerByLocation_management("parcelLayerTest1", "HAVE_THEIR_CENTER_IN", neighbourhood)

        nbhdTable = arcpy.SearchCursor(neighbourhood)
        for nbhd in nbhdTable:
            nbhdValue = nbhd.getValue("Neighbourh")
            if nbhdValue:
                par[1] = nbhdValue
                parRows.updateRow(par)

2 Answers 2

4

The problem with your code lies in the logical flow and that you are selecting all the parcel features each time your arcpy.SelectLayerByLocation_management is called. You should re-work your code flow to:

  1. First cursor/select each neighborhood feature (within cursor use select by attribute)
  2. Select by location using neighbohood selected features to select parcel features
  3. Perform update cursor on selected parcel features or use Calculate Field tool to perform the value update grabbed from step 1 value
2
  • So if I understand you correctly. 1) Searchcursor on neighbourhoods 2)select by attribute based on current nbhd in the loop 3)further select parcels based on location within selected nbhd 4)perform the update Commented Mar 5, 2013 at 17:33
  • @Ryan Spencer, Yes that should work.
    – artwork21
    Commented Mar 5, 2013 at 20:38
1

Try using spatial join instead. It will grab the name of the larger area for you and join it to the smaller areas. If you just need to do it once, I would just use the GUI tool, as setting up the field maps and join rules to make things tidy can be a bit cumbersome in Python for a one-time use. If you do want to automate it, you should read up on the FieldMap() and FieldMappings() objects in arcpy.mapping. These aren't strictly necessary, but it allows you to only include the fields that you want in your output feature class.

2
  • 1
    I could use spatial join, I'm just wondering if there is any downfall in using that method. I know my code is close to getting the right value its just that it seems to be not using the neighbourhood value for the the neighbourhood selected in the the "select by location" process. GUI will not be useful as I will be using this to automate the process as it will happen frequently. Commented Mar 5, 2013 at 15:12
  • I've never had a problem using spatial join, so long as the correct "match option" is specified. In your case, I think you would want to use the "WITHIN" match option. The one limitation I can think of is that spatial join requires you to create a new shapefile. Commented Mar 5, 2013 at 16:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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