Skip to main content
Updated code to ensure spatial reference is set during construction of polygon
Source Link
Hornbydd
  • 44.6k
  • 5
  • 42
  • 84

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,Nonesr,True)

        #Update row
        cursor.updateRow([newpoly])

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"

with arcpy.da.UpdateCursor(fc, ["SHAPE@"]) as cursor:
    for row in cursor:
        geom = row[0]
        arr = arcpy.Array()
        for part in geom:
            for pnt in part:
                if pnt.Z == 0:
                    pnt.Z = 1.0
                arr.add(pnt)
        newpoly = arcpy.Polygon(arr,None,True)
        cursor.updateRow([newpoly])

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])
Source Link
Hornbydd
  • 44.6k
  • 5
  • 42
  • 84

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"

with arcpy.da.UpdateCursor(fc, ["SHAPE@"]) as cursor:
    for row in cursor:
        geom = row[0]
        arr = arcpy.Array()
        for part in geom:
            for pnt in part:
                if pnt.Z == 0:
                    pnt.Z = 1.0
                arr.add(pnt)
        newpoly = arcpy.Polygon(arr,None,True)
        cursor.updateRow([newpoly])