0

I have the following code:

import arcpy
from arcpy.sa import *


arcpy.env.workspace="D:/..." 
sp="StartPoint.shp"          #Dataset that defines the zones
zone_field="FID"             #Field that holds the values that define each zone.
value_raster="UDEM.tif"  #Raster that contains the values on which to calculate a statistic
x_shift = 1                  #increment of x coordinate of the point
TableName = "ZStT"             #Name of output table
arcpy.env.overwriteOutput = True
outTable = ZonalStatisticsAsTable(sp, zone_field, value_raster, TableName, "NODATA", "MEAN")
arcpy.AddField_management(TableName, "parameter", "FLOAT", 3, "", "", "", "NON_NULLABLE", "NON_REQUIRED", "") 
cursor=arcpy.da.SearchCursor(outTable, ["MEAN"]) 

for row in cursor:
    i = 0
    value = row[0]        #Create a cursor to query value in position of the start point
    while (value == 0):       
        cursor2 = arcpy.da.UpdateCursor(sp, ["SHAPE@XY"]) #Move the point 1 meter to right
        for row in cursor2:
            cursor2.updateRow([[row[0][0]+(x_shift), row[0][1]]])
            i += 1      #"Moved step" plus one 

        outTable = ZonalStatisticsAsTable(sp, zone_field, value_raster, TableName, "NODATA", "MEAN") 
        cursor = arcpy.da.SearchCursor(outTable, ["MEAN"]) #query the raster value in the point's location
        for row in cursor:
            value = row[0]

    if value != 0:          #if(True)break the loop
       cursor3=arcpy.da.UpdateCursor(outTable, ["parameter"]) #parameter=i/value
       for row in cursor3:
           row[0] = (i/value)
           cursor3.updateRow(row)

The purpose of function is to iterate over coordinate change until the raster value in one single point's location meet the need (!=0),then put the parameter (moved step/raster value) in table output"ZStT".

Part of this question utilizes code in other question posted by me, but previous one accentuate how to change coordinate and this one focus on how to loop element. They're asking different stuff and I'm specifically told if there's new question post another one.

How to execute this function to element (point) of input layer separately since process to whole layers lead to change of all element simultaneously.

Current Table

  • @Joe.Z, BERA meant how to format your question as you are asking here. I have changed it to make it more readable. Please review to see if the indentation levels and the loops are correct... – fatih_dur Aug 21 '18 at 8:21
  • I think SHAPE@ token should be useful but I can't find connection between them. – Joe.Z Aug 21 '18 at 9:11
  • It is still unclear what you are trying to do. You have a Point feature class, and you want to move each Point east by increments of 1 until the underlying raster has a value that is not zero? What are the Points and raster in real Life? – BERA Aug 21 '18 at 9:33
  • That's exactly what I mean.Raster is a urban DEM(digital elevation model), I set its ground elevation to 0 while the building elevation unchanged(real height,not zero), start point is a random spot among buildings .I just want to gauge distance between the point and its nearest building eastward(or any direction ), and get parameter:distance/height.(Ps:this parameter is given by Japanese architect Yoshinobu Ashihara as way to evaluate spatial openness) it's my work at school and i can only think of this measure since the point is random and I'm new to script.Hope it doesn't sound funny lol. – Joe.Z Aug 21 '18 at 14:08
  • 3
    Possible duplicate of Changing coordinate of point shapefile in ArcGIS using ArcPy? – FelixIP Aug 21 '18 at 19:56
0

I think using cursors is not the best way of solving your problem. You will need many nested cursors and alot of code.

Try:

  1. Convert raster to Points using Raster To Point (or raster to polygon if you want features exactly like your raster pixels)
  2. From this Select the Points with elevation > 0 m
  3. Spatial Join the selection to your random Points with match option CLOSEST and specify a distance field name

In your output you will have distance to and height of nearest building for each Point. The distance will the closest in any direction, not closest east/west/North/South. If you want closest direction in a specified direction there are other ways of solving that.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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