Pages

Monday, March 21, 2011

Working with Elevation Values in Shapefiles

I've had several questions about working with elevation values in shapefiles.  Awhile back I  posted a brief example in the wiki on the google code project site and felt it should be included here so this information is more visible.


Elevation values, known as "Z" values allow you to create 3-dimensional shapefiles. Working with elevation values in the Python Shapefile Library is easy but requires a step which hasn't been well documented until now. Make sure you are working with at least version 1.0 (revision 24 in the repository) as previous versions have a bug that prevents elevation values from being written properly as well as another bug which occurs when creating certain polygons.

The shapefile specification allows for three types of 3-dimensional shapefiles:
PointZ - a 3D point shapefile, PolylineZ - a 3D line shapefile, and PolygonZ - a 3D polygon shapefile.

The most important thing to remember is to explicitly set the shape type of each record to the correct shape type or it will default to a 2-dimensional shape. Eventually the library will automatically detect the elevation type.
Here is a brief example of creating a PolygonZ shapefile:

import shapefile
w = shapefile.Writer(shapeType=shapefile.POLYGONZ)
# OR you can type
# w = shapefile.Writer(shapeType=15)
w.poly([[[-89.0, 33, 12], [-90, 31, 11], [-91, 30, 12]]], shapeType=15)
w.field("NAME")
w.record("PolyZTest")
w.save("MyPolyZ")

When you read 3D shapefiles the elevation values will be stored separately:

>>>import shapefile
>>>r = shapefile.Reader("MyPolyZ")
>>>r.shapes()[0].points
[[-89.0, 33.0], [-90.0, 31.0], [-91.0, 30.0]]
>>>r.shapes()[0].z
[12, 11, 12]

So remember: explicitly set the shapetype for each shape you add to ensure the z values are maintained. 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.

2 comments:

  1. I'm trying to work with shapefile which is a very interesting module,
    the file " onnees.shp" contains 3d points
    I need to extract attributes from this file
    but my code returns me an error:

    >>> import shapefie
    >>> r = shapefie.Reader("dalle_T1.shp")
    >>> r.shapes()[0].shapeType
    1
    >>> r.shapes()[0].points
    [[841001.0, 6399999.0]]
    >>> r.shapes()[0].z
    Traceback (most recent call last):
    File "", line 1, in
    r.shapes()[0].z
    AttributeError: _Shape instance has no attribute 'z'


    what is the nature of tir error?
    what am I missing?

    ReplyDelete
    Replies
    1. I'm sorry , I made a confusion, the file's name is: "dalle_T1.shp"
      and shapefile is replacing the shapefile( i just renamed it)

      thank you for your answer
      A.H

      Delete