0

This question already has an answer here:

I am trying to update Z values of the vertices of the polygon shape. The script bellow works for polygons without multiparts and donuts. If I run this script for polygon shape that contains multiparts and donuts, I get error. How can I make it work?

My script:

import arcpy

fc = r"C:\Scratch\fcTest.shp"

# 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 not pnt:
                    arr.add(None)
                    continue

                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])

Error:

 Traceback (most recent call last):
 File "C:\Scratch\fcTest.py", line 21, in <module>
    if pnt.Z == 0:
  AttributeError: 'NoneType' object has no attribute 'Z'
   >>> 

marked as duplicate by Vince, TomazicM, whyzar, Jochen Schwarze, xunilk Aug 1 at 18:15

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • @Vince i tried my best but i think i am doing some thing wrong in the loop. its erasing all gaps/donuts in the polygons. is it possible for you to amend the script? – Ram Jul 31 at 9:39
  • I'm not going to fix this script when I already fixed the other. If you have questions, they should be on code that correctly handles the None between subparts, which it the fundamental issue in the answer. – Vince Jul 31 at 9:43
  • I amended the script what you mentioned above, but its not working in this case. Hope you can undestand my problem. see amended script above – Ram Jul 31 at 9:54
  • Your error is incompatible with the code. If you update one you need to update the other. – Vince Jul 31 at 10:15
  • yes, you are correct. now i dont have error but i am getting het polygons without multiparts/donuts. This is not what i expect, and more over i dont know how to update the other. – Ram Jul 31 at 10:53
1

I suggest you to give the explode_to_points if the UpdateCursror a try in this case.

If you set it to True, each row of your cursor will be each vertex of your polygons.

I did not tested it, but you can try something like the below:

import arcpy

fc = r"C:\Scratch\fcTest.shp"

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

with arcpy.da.UpdateCursor(fc, ["SHAPE@Z"], explode_to_points=True) as cursor:
    for row in cursor:
        if row[0] == 0:
            row[0] = 1.0
        cursor.updateRow(row)
  • While returning multiple rows per feature in a SearchCursor can make some sense, in an UpdateCursor it seems insane -- Wouldn't this corrupt the geometry on updateRow? Actually, with the shape@Z, an update shouldn't be possible at all. – Vince Jul 31 at 10:42
  • @umbe 1987, your method is good for 0 values and for lines. but for polygons if i increase with values 2, the first vertex is getting 4. – Ram Jul 31 at 10:49
  • I don't think it will be a problem. I solved a similar problem in the past. Here is the link: gis.stackexchange.com/a/177380/9518. – umbe1987 Jul 31 at 10:49
  • Sorry just fixed an issue in the code. – umbe1987 Jul 31 at 10:50
  • 1
    @umbe 1987 sorry, it was my mistake. i just tried, your method is working thank you – Ram Jul 31 at 11:01

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