python to snap based on attribute

5209
9
Jump to solution
08-10-2015 04:51 PM
KathleenWallis
Occasional Contributor II

I need to move some points that were geocoded to the place they actually are on the map.
I have a feature class called Meters that needs to be snapped (or x,y updated) to that they are moved to the corresponding point in a feature class called GPSpoints.
Based on the thread found here

Re: Snap points to lines based on attribute matching 

I tried the code below., however I keep getting the error "Layer Name or Table View: Dataset Meter_layer does not exist or is not supported"

Any help would be greatly appreciated.

import arcpy

arcpy.env.workspace = "C:\\Users\\kwallis\\Desktop\\METERS"

Meter_layer = arcpy.MakeFeatureLayer_management("C:\Users\kwallis\Desktop\METERS\Meters.shp", "Meters_lyr")
field_Meters = "ACCOUNTID"

GPSpnts_layer = arcpy.MakeFeatureLayer_management("C:\\Users\\kwallis\\Desktop\\METERS\\GPSpnts.shp", "GPSpnts_lyr")
field_GPSpnts = "ACCOUNTID" 

#creating cursor for a list of all the names in Meter layer
cursor_Meters = arcpy.SearchCursor(Meter_layer)

#empty list for Meter names
list_Meter_names = []

#filling the list with names from Meter layer
for row in cursor_Meters:
    list_Meter_names .append(row.getValue(field_Meters))

                                            
#another empty list for storing  only unique names from list_Meter_names
s = []
for i in list_Meter_names :
       if i not in s:
          s.append(i)
                                             

#selecting Meters and GPSpnts by name:
for name in s:
     layer1 = arcpy.management.SelectLayerByAttribute("Meter_layer", "NEW_SELECTION", "ACCOUNTID=" +"'"+name+"'")
     layer2 = arcpy.management.SelectLayerByAttribute("GPSpnts_layer", "NEW_SELECTION", "ACCOUNTID=" +"'"+name+"'")
     arcpy.Snap_edit(layer1, [layer2, "VERTEX", "30 feet"])
arcpy.management.SelectLayerByAttribute(Meter_layer , "CLEAR_SELECTION")
arcpy.management.SelectLayerByAttribute(GPSpnts_layer , "CLEAR_SELECTION")

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
DarrenWiens2
MVP Honored Contributor

First, you called it "Meters_lyr", then refer to "Meter_lyr". Make the same.

To clarify, you made a variable called, "Meter_lyr", that held a layer called, "Meters_lyr". Then, you tried to refer to a layer named, "Meter_lyr" when none existed.

View solution in original post

9 Replies
DarrenWiens2
MVP Honored Contributor

First, you called it "Meters_lyr", then refer to "Meter_lyr". Make the same.

To clarify, you made a variable called, "Meter_lyr", that held a layer called, "Meters_lyr". Then, you tried to refer to a layer named, "Meter_lyr" when none existed.

KathleenWallis
Occasional Contributor II

Quick response !
I have been looking at this too long.

I also  referred to it as ""Meter_layer: instead of "Meter_lyr" in the SelectLayerByAttribute line.
Thanks so much for the quick help

Katy
.

0 Kudos
DannyBittner1
New Contributor II

I tested this out and it turns out that Snap_edit requires a second pair of square brackets. The outside brackets represent the table of snap environments, and the inner brackets represent a single snap environment inside the table.

arcpy.Snap_edit(layer1, [[layer2, "VERTEX", "30 feet"]])

Without that change, you'll see the following error: "Parameters are not valid. ERROR 000800: The value is not a member of END | VERTEX | EDGE. ERROR 000800: The value is not a member of END | VERTEX | EDGE. ERROR 000800: The value is not a member of END | VERTEX | EDGE. Failed to execute (Snap)."

KathleenWallis
Occasional Contributor II

PLEASE SEE THIS UPDATE

Thanks to Danny Bittner's help there is a simplified version of what I was wanting to do. This snaps the actual points themselves. Note you are not in an edit session so make sure you really want the points to move because there is no "undo". The version below does not make a .lyr temporary layer.

# Import arcpy module

import arcpy

arcpy.env.workspace = "C:\\Users\\kwallis\\Desktop\\METERS"

print "creating cursor..."

#creating cursor for a list of all the names in Meter layer

cursor_Meters = arcpy.SearchCursor("Meters")

print "emptying list for Meter names..."

#empty list for Meter names

list_Meter_names = []

print "filling the list with names from Meter layer..."

#filling the list with names from Meter layer

for row in cursor_Meters:

    list_Meter_names .append(row.getValue("ACCOUNTID"))

print "another empty list..."                                              

#another empty list for storing  only unique names from list_Meter_names

s = []

for i in list_Meter_names :

      if i not in s:

          s.append(i)

                                            

print "selecting Meters and GPSpnts by name..."

#selecting Meters and GPSpnts by name:

for name in s:

    print "selecting  ....", name

    layer1 = arcpy.SelectLayerByAttribute_management("Meters", "NEW_SELECTION", "ACCOUNTID="+"'"+name+"'")

    layer2 = arcpy.SelectLayerByAttribute_management ("GPSpnts", "NEW_SELECTION", "ACCOUNTID=" +"'"+name+"'")

  

    print "executing snap"  

    arcpy.Snap_edit("Meters", [["GPSpnts", "VERTEX", "90 feet"]])

print "Clear the selection and refresh the active view...."

# Clear the selection and refresh the active view  

arcpy.SelectLayerByAttribute_management(Meters, "CLEAR_SELECTION")

arcpy.SelectLayerByAttribute_management(GPSpnts, "CLEAR_SELECTION")

print "done !!"

BrianVann
New Contributor II

Greetings,

I have trying to snap points to a line only where an attribute matches between the two, however, python skills are limited. I have a point shapefile called snappingtooltest with the field "LRSLRM" that contains a matching attribute and a feature class called Funct_Cl_3 with a field named "RCLINK" that contains the attribute to match to. I am running ArcGIS through the Citrix environment.

Here is my code  with the inserted values I have:

import arcpy
arcpy.env.workspace = "\\gdot.ad.local\gdot\Operations\Traffic_Operations\tmc\TSafety\Maps\FunctionalClass\TestingData\snappingtooltest"
print "creating cursor..." #creating cursor for a list of all the names in crash layer
cursor_snappingtooltest = arcpy.SearchCursor("snappingtooltest")  
print "emptying list for snappingtooltest names..." #empty list for names
list_snappingtooltest_names = []
print "filling the list with names from snappingtooltest layer..."#filling the list with names from snappingtooltest layer
for row in cursor_snappingtooltest:
    list_snappingtooltest_names.append(row.getValue("LRSLRM") 
print "another empty list..." 
#another empty list for storing only unique names from list_snappingtooltest_names
s = []
for i in list_snappingtooltest_names:
    if i not in s:
        s.append(i)
print "selecting snappingtooltest and Funct_Cl_3 by name..." #selecting snappingtooltest and Funct_CL3 by name:
for name in s:
     print "selecting ....",name
     layer1 = arcpy.SelectLayerByAttribute_management("snappingtooltest", "NEW_SELECTION", "LRSLRM="+"'"+name+"'")
     layer2 = arcpy.SelectLayerByAttribute_management ("Funct_Cl_3", "NEW_SELECTION", "RCLINK="+"'"+name+"'")
     print "executing snap"  
     arcpy.Snap_edit("snappingtooltest", [["Funct_Cl_3", "Edge", "350 feet"]])
print "Clear the selection and refresh the active view...." # Clear the selection and refresh the active view  
arcpy.SelectLayerByAttribute_management(snappingtooltest, "CLEAR_SELECTION")
arcpy.SelectLayerByAttribute_management("Funct_Cl_3", "CLEAR_SELECTION")
print "done !!" 

And, at this point anyway and I know there will be more, here is the error I am getting:

Parsing error SyntaxError: invalid syntax (line 10)

Thanks for the help!!

0 Kudos
NeilAyres
MVP Alum

Firstly try formatting your code block correctly. I am sure someone will look at it then :

Code Formatting... the basics++ | GeoNet 

0 Kudos
DarrenWiens2
MVP Honored Contributor

You're missing the final close parenthesis in Line 9:

list_snappingtooltest_names.append(row.getValue("LRSLRM"))
BrianVann
New Contributor II

Darren,

Yes, that was it!!

I felt like I made a touchdown in a football game after watching it begin to run!

Thanks for the help!

RebeccaStrauch__GISP
MVP Emeritus

Brian, If Darren's comment doesn't fix you issue, you should branch you port to a new question, since Kathleen's question has already be resolved.  A moderator can help with this if needed.