2

I would like to perform snapping using Arcpy for a lot of files in all the lines of the shapefiles that don't connect to the nearest other lines, so they do.

I tried to do it in geopandas using Python but I can't make it work.

Since I don't know much about ArcPy I will show you what I have done in geopandas:

import geopandas as gpd
from shapely.ops import snap
s=gpd.read_file(r'C:\Users\user\Desktop\ag_eus')
result=snap(s,s,0.5)

How this code would be converted in ArcPy?

I have done this process in regular Arcmap and worked great but how to do it in ArcPy so it can work with multiple shapefiles in a loop. If you can explain what you do too, would be great.

1 Answer 1

4

The snapping that you are looking for seems to be equivalent to the integrate tool. Beware of this tool because it modifies the input data. So you should make a copy of your fc first.

import arcpy
arcpy.env.workspace = "c:/geodatabase.gdb/" #define workspace (would be a directory if you have a set a shapefiles)
for fc in arcpy.ListFeatureClasses(): #listfeature class create a python list with the names of all feature classes in the workspace
    arcpy.CopyFeatures_management(fc, fc + "snapped") #copies each fc in a new fc with the name + "snapped. With shapefiles you would do fc[:-4] +"snapped.shp" as output name.
    arcpy.Integrate_management(fc + "snapped", 0.5)#or fc[:-4] +"snapped.shp" as output name for shapefile

Note that integrate is the "quick and dirty" snapping tool for ArcGIS. You could also select one of the Editing tools in the editing toolbox for more specific fixes (e.g. snap arcpy.Snap_edit() or extent), but those tools require at least a standard licence.

3
  • Can you also explain what are you doing because ArcPy documentation was no that helpful for me.For example where do you declare the shapefiles that you will use? Commented Jul 20, 2018 at 14:42
  • Is integrate suited for lines? Commented Jul 20, 2018 at 14:56
  • yes, but you can use snap if you want more rules and a focus on the last segments only.
    – radouxju
    Commented Jul 21, 2018 at 20:16

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.