1

I have a two line shapefiles (Trace.shp and Snap.shp). There is a GIS_ID field in both files. Now I want to snap "Snap.shp" to "Trace.shp" according to GIS_ID value. I have tried to use Snap tool for ArcGIS Desktop but I am not sure about the results because using that solution I am not sure that line is snapped to the right line from another layer with the same GIS_ID. I want to do that using ArcGIS 10.4

  • 1
    Are you using Snap tool under editing toolbox? I would try to select the same GIS_ID in both feature classes and run snap tool. You should be able to do this by joining them and iterate feature selection in ModelBuilder, or by using arcpy. – BERA Dec 13 '18 at 12:46
  • @BERA Yes, I am using Snap tool under editing toolbox. Could you give me more details about that solution and add it as answer? – user122678 Dec 13 '18 at 13:16
3

You can use arcpy to iterate over each ID, create feature layers with the same ID's and then snap together lines which share the same id. Change inputs, field name, and snapping options and then execute in python window of ArcMap. Backup your data Before you try it.

import arcpy
arcpy.env.overwriteOutput = True
arcpy.env.addOutputsToMap = False

arcpy.env.workspace = r'C:\Default.gdb' #Or a folder with shapefiles
fc_to_snap = 'Roads2' #If shapefile, add .shp
fc_to_snap_to = 'Roads' #If shapefile, add .shp

idfield = 'Roadname' #Change to match the name of your ID field. The name and field type must be the same in both feature classes.

all_ids = list({i[0] for i in arcpy.da.SearchCursor(fc_to_snap,idfield)})

for unique_id in all_ids:
    sql = """{0}='{1}'""".format(arcpy.AddFieldDelimiters(fc_to_snap, idfield), unique_id) #If your ID field is not a string, remove the single quotes surrounding {1}
    arcpy.MakeFeatureLayer_management(in_features=fc_to_snap, out_layer='tosnaplyr',where_clause=sql)
    arcpy.MakeFeatureLayer_management(in_features=fc_to_snap_to, out_layer='tosnaptolyr', where_clause=sql)
    if arcpy.GetCount_management(in_rows='tosnaplyr').getOutput(0) > 0 and arcpy.GetCount_management(in_rows='tosnaptolyr').getOutput(0) > 0:
        arcpy.Snap_edit(in_features='tosnaplyr', snap_environment=[['tosnaptolyr',"END","100 Meters"]])

enter image description here

  • But I have a 2000 lines. How to do that with this number of lines? – user122678 Dec 14 '18 at 7:14
  • I have about 2000 lines, but when I run a script I get a couple of new layers and after that get the error message. – user122678 Dec 14 '18 at 7:21
  • ERROR999999: Error executing function – user122678 Dec 14 '18 at 7:26
  • 1
    Sorry. Now I see that in one layer it is "double" but in another one it is string. – user122678 Dec 14 '18 at 7:32
  • 1
    Now that works. Great! Will let you know when I finish. – user122678 Dec 14 '18 at 7:39

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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