1

I am trying to compare county borders in two shapefiles and create a third showing how (if at all) those borders changed. To do so, I'm using arcpy.DetectFeatureChanges_management.

I have tried the following code:

# Detect changes in county boundaries over time
import arcpy

#Set working directory
cd = r"C:\Users\folder"
arcpy.env.workspace = cd
arcpy.env.overwriteOutput = True

#Set local variables
file1 = r"county_shapefiles\county_2000.shp"
file2 = r"county_shapefiles\county_2005.shp"
output = r"county_shapefiles\county_changes.shp"

#Create changes shapefile
arcpy.DetectFeatureChanges_management(file1, file2, output, "500 Feet")

but I get the error 000366: invalid geometry type. The error occurs twice, so I assume it is related to the two shapefiles I use as input.

I consulted the Help from Esri (http://desktop.arcgis.com/en/arcmap/10.3/tools/data-management-toolbox/detect-feature-changes.htm), but in their example code they use shapefiles (.shp), too, so this should not be the problem.

Any ideas?


Update: Following ShaunO's suggestion I tried to use the intersect function to do the same. I adapted his code in the following way:

file1 = r"county_shapefiles\county_2000.shp"
file2 = r"county_shapefiles\county_2005.shp"

arcpy.MakeFeatureLayer_management(file1,"lyr1")
arcpy.MakeFeatureLayer_management(file2,"lyr2")

#make an empty set to hold outputs from intersect tool

CountyIDs = set()

with arcpy.da.SearchCursor("lyr1", "COUNTY") as cursor1:
    for row in cursor1:
        with arcpy.da.SearchCursor("lyr2", "COUNTY") as cursor2:
            for rows in cursor2:
                #create sliver with intersect toool for each county
                arcpy.Intersect_analysis(["lyr1", "lyr2"], rows[0]+".shp")
                #Add Interset outputs to set for merge
                CountyIDs.add(rows[0])

#finally, merge all the outputs
arcpy.Merge_management(CountyIDs,"county_changes.shp")

#delete layers to release them from memory
arcpy.Delete_management(lyr1)
arcpy.Delete_management(lyr2)

I have changed some things that produced errors before (no guarantee I changed anything for the better. Still new to ArcPy/Python). Now the code does not produce any error anymore, but it does not finish either. Is this due to a programming error on my behalf, or simply because intersecting close to 4000 counties, and hence adding up 4000 shapefiles, is a very time-consuming or inefficient task?

  • 1
    I think this tool can only accept line features as inputs, not polygons or points: " Finds where the update line features spatially match..." – geoJshaun Nov 28 '16 at 17:34
  • ^I agree with @ShaunO or perhaps there are geometry errors with the 2 inputs – user32496 Nov 28 '16 at 17:38
2

I think it would better serve you to use the intersect tool under Analysis. This will produce the polygon slivers of the border changes. You'll have to use two SearchCursors to cycle through the County IDs of both shapefiles so the tool doesn't perform the process on the wrong county since there is multi-contiguity. Lastly you'll have to merge the outputs together in a single file. It would also be preferable to convert your shapefiles to layers for the process.

Something like this (you'll have to play around with it but it gives the basic idea, I think):

file1 = r"county_shapefiles\county_2000.shp"
file2 = r"county_shapefiles\county_2005.shp"

#make feature layers
arcpy.MakeFeatureLayer_management(file1,lyr1)
arcpy.MakeFeatureLayer_management(file2,lyr2)

#make an empty set to hold outputs from intersect tool
CountyIDs = set()

#make search cursors to iterate through lyr files
with arcpy.da.SearchCursor(lyr1, CountyID) as cursor1:
    for row in cursor1:
        with arcpy.da.SearchCursor(lyr2, CountyID)  as cursor2:
            for rows in cursor2:
                #create sliver with intersect tool for each county
                arcpy.Intersect_analysis(lyr1,lyr2, rows[0]+".shp")
                #Add Interset outputs to set for merge
                CountyIds.add(rows[0])

#finally, merge all the outputs
arcpy.Merge_management(CountyIDs,"county_changes.shp")

#delete layers to release them from memory
arcpy.Delete_management(lyr1)
arcpy.Delete_management(lyr2)
  • Thank you for the suggestion. I have not got it working yet but it sounds promising. I will update the question with a follow-up question on your code, if that's okay. – eigenvector Nov 29 '16 at 7:36
  • Yes, that would take an extremely long time to process. I recommend using .gdb instead of shapefiles. It should be more efficient and will avoid any issues with the set values not containing file extensions. If you change your default workspace to a file geodatabase this should no longer be a problem. – geoJshaun Nov 29 '16 at 16:18

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.