Greetings,
I am experiencing a bit wierd response as I would like to create a polygon object and enter its z-values using arcpy. It seems all goes fine interms of execution but when I try to query the newly created object, the z-values are set to zeros. I am using ArcGIS Desktop 10.2 on windows 7. Here is the code snippet.
# Import system modules import arcpy import time, datetime from time import strftime import sys, traceback, os from arcpy import env from arcpy import da def showMessage(msgText): arcpy.AddMessage(msgText); print msgText def showErrorMessage(msgText): arcpy.AddError(msgText); print msgText def readGeom(inputFC, fieldNames): ## infc = arcpy.GetParameterAsText(0) infc = inputFC # Enter for loop for each feature # # Print the current multipoint's ID for row in arcpy.da.SearchCursor(infc, fieldNames): # showMessage("Feature {0}:".format(row[0])) partnum = 0 # Step through each part of the feature # for part in row[1]: # Print the part number # showMessage("Part {0}:".format(partnum)) # Step through each vertex in the feature # for pnt in part: if pnt: # Print x,y coordinates of current point # showMessage("{0}, {1}, {2}".format(pnt.X, pnt.Y, pnt.Z)) else: # If pnt is None, this represents an interior ring # showMessage("Interior Ring:") partnum += 1 try: aoiCoords = "5581313.715,3090151.188,88;5581925.211,3085259.218,88;5574281.509,3085259.218,22;5570918.28,3091679.929,55;5575198.75,3093208.669,44" template = r"PathTo_abc_POL" arcpy.AddMessage("Starting Processing... " + strftime("%d%b%y")) spatial_reference = arcpy.Describe(template).spatialReference ## env.workspace = out_path ## arcpy.env.overwriteOutput = True; dummyPoint = arcpy.Point() firstPoint = arcpy.Point() pointsArray = arcpy.Array() coordsTokens = aoiCoords.split(';') loopIndex = 0; for coord in coordsTokens: coordValues = coord.split(',') dummyPoint.X = float(coordValues[0]); dummyPoint.Y = float(coordValues[1]); dummyPoint.Z = float(coordValues[2]); ## showMessage("X, Y, Z: " +coordValues[0] + ", " +coordValues[1] + ", " + coordValues[2]) if(loopIndex ==0): firstPoint = dummyPoint loopIndex = loopIndex + 1 pointsArray.add(dummyPoint) #close the polygon pointsArray.add(firstPoint); has_z = "ENABLED" has_m = "DISABLED" aoiPolygon = arcpy.Polygon(pointsArray, spatial_reference, has_z, has_m) out_path = arcpy.env.scratchGDB test1 = "test1" geometry_type = "POLYGON" env.workspace = out_path arcpy.env.overwriteOutput = True; outFCName = arcpy.CreateFeatureclass_management(out_path, test1, geometry_type, template, has_m, has_z, spatial_reference) aoiWMName = outFCName.getOutput(0) cursor = arcpy.da.InsertCursor(test1, ["SHAPE@"]) cursor.insertRow([aoiPolygon]) del cursor desc = arcpy.Describe(test1) arcpy.AddMessage("0: HasZ: " + str(desc.hasZ) + ", HasM: " + str(desc.hasM)) readGeom(test1, ["OID@", "SHAPE@"]) arcpy.AddMessage("Processing completed") except Exception as e: tb = sys.exc_info()[2] tbinfo=traceback.format_tb(tb)[0] strMessages = "Exception Type is: " + str(sys.exc_type) strMessages = strMessages +"\nException Value is: " + str(sys.exc_value) strMessages = strMessages + "\nFailure Info: " + tbinfo arcpy.AddError(strMessages) arcpy.AddError(e.message)
Can someone kindly share any thought or reference on how to fix it? Thanks in advance. Here are the resuts.
Feature 1:
Part 0:
5581313.715, 3090151.188, 0.0
5581925.211, 3085259.218, 0.0
5574281.509, 3085259.218, 0.0
5570918.28, 3091679.929, 0.0
5575198.75, 3093208.669, 0.0
5581313.715, 3090151.188, 0.0
Solved! Go to Solution.
The problem is probably due to this line:
aoiPolygon = arcpy.Polygon(pointsArray, spatial_reference, has_z, has_m)
When creating a polygon, the "has_z" should be boolean (True, in your case).
When creating the featureclass the "has_z" is string ("ENABLED" in your case).
Change that line to:
aoiPolygon = arcpy.Polygon(pointsArray, spatial_reference, has_z=True, has_m=False)
The problem is probably due to this line:
aoiPolygon = arcpy.Polygon(pointsArray, spatial_reference, has_z, has_m)
When creating a polygon, the "has_z" should be boolean (True, in your case).
When creating the featureclass the "has_z" is string ("ENABLED" in your case).
Change that line to:
aoiPolygon = arcpy.Polygon(pointsArray, spatial_reference, has_z=True, has_m=False)
Just ran this code and it created a PolygonZM (no idea why it is M aware when it is set as False😞
import arcpy aoiCoords = "5581313.715,3090151.188,88;5581925.211,3085259.218,88;5574281.509,3085259.218,22;5570918.28,3091679.929,55;5575198.75,3093208.669,44" fc_out = r"D:\Xander\GeoNet\Polygon3D\test2.shp" pnts = aoiCoords.split(';') arrPnts = arcpy.Array() sr= arcpy.SpatialReference(102100) for pnt in pnts: crds = pnt.split(',') point = arcpy.Point(float(crds[0]), float(crds[1]), float(crds[2])) arrPnts.add(point) polygon = arcpy.Polygon(arrPnts, sr, True, False) lst_polygons = [polygon] arcpy.CopyFeatures_management(lst_polygons, fc_out)
Not that this answers the question why the file is M aware, but the issue seems to be in saving to a shapefile. If you run the code saving to an 'in_memory' feature class, it is PolygonZ.
Called a bug.
Discovered this little ditty...
- buried in the bowels of hell.... aka .... the help files.
- http://resources.arcgis.com/en/help/main/10.2/index.html#/Output_has_M_values/001w00000007000000/
- For shapefiles, storage of M and Z values is closely connected;
if the output has Z, then regardless of this environment setting,
the output will also have M.
Thanks everyone for your comments. Xander pointed it correctly as I somehow was using arcpy.Polygon and arcpy.CreateFeatureclass_management methods in my original script, both methods use the same variables 'has_m and has_z' and I didn't pay attention to them as they both take different values and data types. Once again a round of applaud for Xander .
I forgot to mention that arcpy exception handling didn't help in catching this error.