Select to view content in your preferred language

Polygon Coordinates - Z Value

2377
4
Jump to solution
05-22-2012 08:46 PM
GSKTRYG
Deactivated User
Hi,

Please suggest me that how to get the Z- value of each vertex of a polygon feature.
I need to calculate the average height of a polygon feature.

Thanks
Siva.
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MarcinGasior
Frequent Contributor
This code should help (supports simple polygons, multipart polygons; doesn't support polygons with interior rings):
import arcpy polygonZ = r"C:\tmp\Testing.gdb\PolygonZ" rows = arcpy.SearchCursor(polygonZ) for row in rows:  feature = row.SHAPE #get the geometry into variable   partNum = 0  partCount = feature.partCount    sumZ = 0.0  totalPntCount = 0   #feature can have multiple parts - first goes iteration through parts  while partNum < partCount:   part = feature.getPart(partNum) #the output is Array of points   pnt = part.next() #take first point from Array of points    pntNum = 0   pntCount = part.count    #iterate through points (first point is doubled as last point - last point is excludec)   while pntNum < (pntCount - 1):    valueZ = pnt.Z    sumZ += valueZ    pnt = part.next()    pntNum += 1    totalPntCount += 1    partNum += 1   meanZ = sumZ/totalPntCount  print meanZ  del rows, row


For more information on accessing geometries in arcpy see Reading geometries.

View solution in original post

0 Kudos
4 Replies
MarcinGasior
Frequent Contributor
This code should help (supports simple polygons, multipart polygons; doesn't support polygons with interior rings):
import arcpy polygonZ = r"C:\tmp\Testing.gdb\PolygonZ" rows = arcpy.SearchCursor(polygonZ) for row in rows:  feature = row.SHAPE #get the geometry into variable   partNum = 0  partCount = feature.partCount    sumZ = 0.0  totalPntCount = 0   #feature can have multiple parts - first goes iteration through parts  while partNum < partCount:   part = feature.getPart(partNum) #the output is Array of points   pnt = part.next() #take first point from Array of points    pntNum = 0   pntCount = part.count    #iterate through points (first point is doubled as last point - last point is excludec)   while pntNum < (pntCount - 1):    valueZ = pnt.Z    sumZ += valueZ    pnt = part.next()    pntNum += 1    totalPntCount += 1    partNum += 1   meanZ = sumZ/totalPntCount  print meanZ  del rows, row


For more information on accessing geometries in arcpy see Reading geometries.
0 Kudos
GSKTRYG
Deactivated User
Thanks m.gasior. It worked well.

Siva.
0 Kudos
DaveJohnson
Occasional Contributor
I am not familiar with working with Shape field and was trying to write this on my own.  Big thanks to Gasior, exactly what I needed. 

I believe the result is same as IanKo's Get Average Z tool, which is not rewritten for 10.  I like it better in Python anyway.

Dave
0 Kudos
JarodAbel
Emerging Contributor
Can you expand on how to overwrite the Z value once it is isolated?

I have a polyline that lost its z-values. I have the center line points for the polyline. I am able to match vertices with its centerline point by X,Y but I need to take the Z value from the centerline point and overwrite the Z value for the vertex.

import arcpy, csv, os, sys, traceback
from arcpy import env
arcpy.env.overwriteOutput = True

##try:
#track segment FC
segZ = "C:/Documents and Settings/h6e6v/My Documents/Leader_Work/PTC_DATA/Test/MissingZTest_1.gdb/segs_noZvalue_test"
pntZ = "C:/Documents and Settings/h6e6v/My Documents/Leader_Work/PTC_DATA/Test/MissingZTest_1.gdb/pnts_withZvalue_test"

descs = arcpy.Describe(segZ)
shapefieldnames = descs.ShapeFieldName
descp = arcpy.Describe(pntZ)
shapefieldnamep = descp.ShapeFieldName

newPoint = arcpy.Point()
segUC = arcpy.UpdateCursor(segZ)
for seg in segUC:
    bXYZ = seg.getValue(shapefieldnames)
    partNum = 0
    for part in bXYZ:
        part_list = []
        for item in bXYZ.getPart(partNum):
            if item:
                x = str(item.X)
                ix = x[:14]
                y = str(item.Y)
                iy = y[:14]
                pnts = arcpy.UpdateCursor(pntZ)
                iz = item.Z
                for pnt in pnts:
                    pXYZ = pnt.getValue(shapefieldnamep)
                    point = pXYZ.labelPoint
                    pointX = point.X
                    pointY = point.Y
                    pointZ = point.Z
                    if ix == str(pointX):
                        if iy == str(pointY):
                            print item.ID
                            print ix+" = "+str(pointX)
                            print iy+" = "+str(pointY)
                            print item.Z
                            print str(pointZ)
                            
                        else:
                            pass
                                              
                    else:
                        pass            
            else:
                pass
    

You can see at the bottom (print item.Z and print str(pointZ)) the Z values are there but I need to replace item.Z with str(pointZ).

any thoughts ?

Thanks
0 Kudos