Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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)
share|improve this question
add comment

2 Answers 2

up vote 3 down vote accepted

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
share|improve this answer
    
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 –  Ryan Spencer Mar 5 '13 at 17:33
    
@Ryan Spencer, Yes that should work. –  artwork21 Mar 5 '13 at 20:38
add comment

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.

share|improve this answer
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. –  Ryan Spencer Mar 5 '13 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. –  Jay Guarneri Mar 5 '13 at 16:03
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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