Tuesday 29 October 2019

Changing coordinate of point shapefile in ArcGIS using ArcPy?



I'm working on coding a function in ArcGIS trying to measure the number of pixels between a point (feature class) and its nearest building horizontally within a UDEM raster, then return the number.


Here is my idea:



  1. Execute Zonal Statistics as Table and create a cursor to see whether the value in output table equals 0(the elevation of ground in my UDEM are set to 0 while elevation of building remain the real value)


  2. If so(value=0), make x coordinate plus one, which means move the point 1 meter to the right(the resolution is 1×1) and refresh the coordinate of the point.

  3. Iterate over step2 until the value doesn't equal 0,thus showing the location of point has moved to the "boundary" and I'll know the distance.


My problem now is having no idea how to perform Step2.


I tried some code to change its location but doesn't work.



Answer



To move a point in a feature class you can use the da.UpdateCursor and SHAPE@XY token. Select the point/points you want to move and then:


import arcpy
in_features = 'Pointlayer' #Change to match your layer name
x_shift = 1

y_shift = 1
with arcpy.da.UpdateCursor(in_features, ['SHAPE@XY']) as cursor:
for row in cursor:
cursor.updateRow([[row[0][0] + (x_shift),
row[0][1] + (y_shift)]])

With no selection all points will be moved. Backup your data before you try the code.


enter image description here


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...