I am using the arcpy module for arcGIS to implement a peano curve algorithm and provide each object in the GIS Project with a spatial order value.
I have currently defined the Peano curve but need to write cursor functions that will compute and add the outputs to the new field after calling the Peano.
This is the code that I have so far. Areas related to the question are in bold. Thank you!
#return the fractional part from a double number
def GetFractionalPart(dbl):
return dbl - math.floor(dbl)
#Return the peano curve coordinate for a given x, y value
def Peano(x,y,k):
if (k==0 or (x==1 and y==1):
return 0.5
if x <= 0.5:
if y <= 0.5:
quad = 0
elif y <= 0.5:
quad = 3
else:
quad = 2
subpos = Peano(2 * abs(x - 0.5), 2 * abs(y - 0.5), k-1)
if (quad == 1 or quad == 3):
subpos = 1 - subpos
return GetFractionalPart((quad + subpos - 0.5)/4.0)
#Import modules and create geoprocessor
import arcpy
arcpy.env.OverwriteOutput = True
#Prepare two inputs as parameters
inp_fc = arcpy.GetParameterAsText(0)
PeanoOrder_fld = arcpy.GetParameterAsText(1)
#Add the double field
arcpy.AddField_management(inp_fc, PeanoOrder_fld, "DOUBLE")
#Get the extent for the feature class
desc = arcpy.Describe(inp_fc)
extent = desc.Extent
xmin = extent.XMin
ymin = extent.YMin
xmax = extent.XMax
ymax = extent.YMax
#Compute constants to scale the coordinates
dx = xmax - xmin
dy = ymax - ymin
if dx >= dy:
offsetx = 0.0
offsety = (1.0-dy/dx)/2.0
scale = dx
else:
offsetx = (1.0 - dx/dy)/2.0
offsety = 0.0
scale = dy
**#Get each object and compute its Peano curve spatial order
#Create an update cursor
rows = arcpy.UpdateCursor(inp_fc)
rows.___?___
row = rows.next( )**
#get the X,Y coordinate for each feature
#If a polygon use centroid, If a point use point itself
while row:
if desc.ShapeType.lower() in ["polyline", "polygon"]:
pnt = row.shape.centroid
else:
pnt = row.shape.getPart(0)
#Normalization
unitx = (pnt.X - xmin) / scale + offsetx
unity = (pnt.Y - ymin) / scale + offset
**#Call the Peano Function and add to attribute field
peanoPos = Peano(unitx, unity, 32)
row.__?__
rows.__?__
row =__?___
del row, rows**