Update fields and redraw points based on updated fields

481
4
03-29-2020 09:25 AM
KyleDrew
New Contributor
import arcpy
from arcpy.sa import *
from arcpy import env
arcpy.CheckOutExtension("spatial")
main_dir = "F:\Independent_Study\\"
flow_dir = main_dir + "Flow"
env.workspace = main_dir
env.overwriteOutput = True
threshold = 3000
dem = Raster("F:\\Independent_Study\\Newfiles\\Newfiles\\UDW1_dtm.tif")
#invert DEM
dem_neg = dem * -1
gps_pts = "F:\\Independent_Study\\UDW_with_UTM_Coordinates\\UDW_with_UTM_Coordinates.shp"
#run flow direction on DEM
neg_Direction = FlowDirection(dem_neg)
neg_Direction.save(flow_dir + "\\neg_flow_direction.tif")
#Extracts values at GPS Points
direction_value = ExtractValuesToPoints(gps_pts, neg_Direction, flow_dir + "\\direction_values.shp")
values = arcpy.ListFields(direction_value)
#create an update cursor to update the X and Y coordinate values based on flow direction.
cursor = arcpy.da.UpdateCursor(flow_dir + "\\direction_values.shp", ["*"])
#Save spatial reference information
spatial_reference = arcpy.Describe(flow_dir + "\\direction_values.shp").spatialReference
#Create new feature class for updated values
arcpy.CreateFeatureclass_management(flow_dir, "\\updated_dir_values.shp", "POINT", flow_dir + "\\direction_values.shp", "DISABLED", "DISABLED", spatial_reference)
cur = arcpy.InsertCursor(flow_dir + "\\updated_dir_values.shp",["SHAPE@"])
#for loop that iterates through feature class and modifies X and Y coordinates based on the flow direction at that point
for row in cursor:
                       #East
    if (row [10] == 1):
        row[9] = int(row[9]) + 1
        cursor.updateRow(row)
        print "EAST"
                        #SE
    elif (row [10] == 2):
        row[8] = int(row[8]) - 1
        row[9] = int(row[9]) + 1
        cursor.updateRow(row)
                
        print "SOUTH-EAST"
                    #SW
    elif (row [10] == 8):
        row[8] = int(row[8]) - 1
        row[9] = int(row[9]) - 1
        cursor.updateRow(row)
        print "SOUTHWEST"
                        #NW
    elif (row [10] == 32):
        row[8] = int(row[8]) + 1
        row[9] = int(row[9]) - 1
        cursor.updateRow(row)
        print "NORTHWEST"
                        #NE
    elif (row [10] == 128):
        row[8] = int(row[8]) + 1
        row[9] = int(row[9]) + 1
        cursor.updateRow(row)
        print "NORTHEAST"
                        #North
    elif (row [10] == 64):
        row[8] = int(row[8]) + 1
        cursor.updateRow(row)
        print "NORTH"
                        #South
    elif (row [10] == 4):
        row[8] = int(row[8]) - 1
        cursor.updateRow(row)
        print "SOUTH"
                        #West
    elif (row [10] == 16):
        row[9] = int(row[9]) - 1
        cursor.updateRow(row)
        print "WEST"

arcpy.Append_management(flow_dir + "\\direction_values.shp", flow_dir + "\\updated_dir_values.shp", "TEST","","")

Hi, there, I'm trying to run a for-loop that iterates through a shape feature class of GPS points, extracts the value for the flow direction at that point, and modifies the x (UTM_E) and y (UTM_N) coordinates based on which direction the flow path will take. I'm trying to then redraw that point based on the new x and y coordinates, but I'm having trouble writing the code that actually redraws the points using the new coordinate values, which are stored as fields in the feature class. And finally, I want to put that into another loop that it updates the values and redraws them N number of times.

Thank you!

0 Kudos
4 Replies
DavidPike
MVP Frequent Contributor

I've not studies the code thoroughly but i'm guessing it's updating the values as expected?

I may be misinterpreting, but you're aware that the visible attribute table has nothing to do with the actual positions of the points? You would need to update the geometry object ("SHAPE@" token in a cursor) or use the update fields to create a new feature through a tool like add xy data.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Including code as screenshots will greatly cut down on the number of people that look at your question.  Please paste the code into GeoNet and use Posting code with Syntax Highlighting on GeoNet‌, or include it as an attachment if the code is too lengthy to paste.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

In your code.jpg screenshot, you are mixing different types of cursors together.  You are creating an Insert Cursor but trying to use it like an Update Cursor.

Thanks for posting the code, I realize now I misread it earlier.  Although you are not mixing cursor syntax, you are creating an insert cursor and never using it.  Is the insert cursor being used later by some other code?

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Regarding:

I'm having trouble writing the code that actually redraws the points using the new coordinate values, which are stored as fields in the feature class.

Regarding coordinate values that are stored as fields in a feature class, they are just copies of coordinate values and aren't dynamically linked to the actual coordinate values (there could be some exceptions with views in enterprise geodatabases but that doesn't apply here).  Your current code is updating the copied value and not the actual values, which explains by your points are moving after you make updates with a cursor.

As David Pike‌ commented earlier, you will need to use one of the SHAPE tokens (can be SHAPE@ or others) to retrieve and update the actual coordinates.

0 Kudos