Wednesday 22 April 2015

arcpy - Updating Location of Point Features using XY Fields?


I have changed the coordinates of couple points in the attribute table and I want to update the location. I have found the script that is working in field calculator.


Does anybody know how to write it using a Python script in ArcGIS Desktop (not in Field Calculator)?


def XYsetVALUE( shape, X_value, Y_value):

point = shape.getPart(0)
point.X = X_value
point.Y = Y_value
return point

__esri_field_calculator_splitter__
XYsetVALUE ( !SHAPE!, !X_COORD!, !Y_COORD! )

COORDINATE FIELD TYPE ------ STRING !!!!



Answer




Use the UpdateCursor for this with the SHAPE@X and SHAPE@Y tokens. This will change the data so backup before executing:


import arcpy

feature_class=r'C:\Default.gdb\Points' #Change to match your data
fields=['X_coord','Y_coord'] #Change to match your column names containing x and y coordinates

with arcpy.da.UpdateCursor(feature_class,['SHAPE@X','SHAPE@Y']+fields) as cursor:
for row in cursor:
row[0]=float(row[2])
row[1]=float(row[3])

cursor.updateRow(row)

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...