0

I am trying to update the Z value of a polygon using update Cursor and it just doesn't work!

I have been exploring this site and trying different codes without success.

I have a shapefile with polygons that represents buildings, and some of the vertices have Z values, others have z=0.

What am I doing wrong?

import arcpy

sheet_name = "RPN_2609"
GDB_path = r"D:\Buildings_Height\Case_Study\Part_B\{}.gdb".format(sheet_name)
bldg = GDB_path + "//" + "RPN_2609_BLDG_No_Zero"

with arcpy.da.UpdateCursor(bldg, ["SHAPE@","OID@"]) as cursor:
  for row in cursor:
      for vertice in row[0]:
          for pnt in vertice:
            try:
                if pnt.Z == 0:
                    pnt.Z = 1
                    cursor.updateRow(row)
            except:
                pass
1

Your code is flawed and not updating the geometry correctly. You need to build a NEW POLYGON and explicitly state it has Z values and insert that back into the FeatureClass. The following code updated my single part test data as expected:

import arcpy

fc = r"C:\Scratch\fGDB_Scratch.gdb\fcTest"

# Get Spatial Reference of dataset
desc = arcpy.Describe(fc)
sr = desc.spatialReference

with arcpy.da.UpdateCursor(fc, ["SHAPE@"]) as cursor:
    for row in cursor:

        # Get Geometry
        geom = row[0]

        # Create an empty array object
        arr = arcpy.Array()

        # Read points, if Z is found to be zero reset it to 1
        for part in geom:
            for pnt in part:
                if pnt.Z == 0:
                    pnt.Z = 1.0

                 # Add updated point to Array
                arr.add(pnt)

        # Construct new polygon geometry from array whilst setting spatial reference and indicating it has Z
        newpoly = arcpy.Polygon(arr,sr,True)

        #Update row
        cursor.updateRow([newpoly])
  • 1
    You really ought to use the SpatialReference of the source in the Polygon constructor, or it may truncate coordinate values to thousandths before the updateRow is executed. – Vince Jul 22 at 11:43
  • 1
    @Vince, very good point, me just being slack, I shall adjust code accordingly! – Hornbydd Jul 22 at 13:53
  • thanks! i wrote something similar and it works! :) – noa Jul 24 at 7:24
  • 1
    Also the for pnt in part needs a test for if not pnt to add a None and continue; see gis.stackexchange.com/questions/330288/… – Vince Jul 28 at 17:22

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.