Coordinates – Creating and Reading Z Values (Coordinates) Using PyShp Effectively

coordinatespyshpz-value

I am trying to parse an input shapefile which consists of multiple polygons and want to create a new shapefile from this with added z values. Each polygon of the input shapefile is defined by multiple points, whereas each of these is defined by an x and y coordinate.

I am creating a new shapefile with x, y (simply copied from input shapefile) and z coordinates like so:

s = shapefile.Writer()
s.autoBalance = 1
s.shapeType = 15 // PolygonZ
pointlist = [[12345.123, 45678.123, 10.0], [23456.123, 34567.123, 10.0]]
s.poly(shapeType=s.shapeType, parts=[pointlist])
s.save("test.shp")

But now when I want to read this created shapefile I can't see the defined z values:

s = shapefile.Reader("test.shp")
print(s.shapes()[0].points) // [[12345.123, 45678.123]]

What am I missing that the z values aren't included? I have also tried to define the polygon with at least 4 points (where the last point is identical to the first one) – exactly like the docs are stating to do this, but this didn't help either.

When I am trying to read the z value of the first point of a polygon with QGIS I can easily see my specified values:

z(start_point($geometry)) // 10

Best Answer

If you examine GeospatialPython.com: Working with Elevation Values in Shapefiles (the blog of Joel Lawhead, creator of the Pyshp module)

When you read 3D shapefiles the elevation values will be stored separately....And know that the z values are stored in the "z" property of each shape instead of being integrated with the x,y value lists.

From the supplied example:

r = shapefile.Reader("MyPolyZ")
first = r.shapes()[0]
dir(first)
['__doc__', '__geo_interface__', '__init__', '__module__', 'bbox', 'parts', 'points', 'shapeType', 'z']
first.points
[(-89.0, 33.0), (-90.0, 31.0), (-91.0, 30.0), (-89.0, 33.0)]
first.z
[12.0, 11.0, 12.0, 12.0]