Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I'm trying to work with the shapefile on python, the file " donnees.shp" contains 3d points I need to extract attributes from this file but my code returns an error:

>>> import shapefile   
>>> r = shapefile.Reader("donnees.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 this error? what am I missing?

thank you for you answer

share|improve this question
    
That shape has no z value. I would think that the library you're using would have a different method to access it if it is there. What library are yo using? –  Geoist Aug 9 '12 at 12:54
    
I'm using the shapefile library : pyshp.googlecode.com/svn/trunk/shapefile.py –  A.H Aug 9 '12 at 13:18
1  
Given that the POINTZ type's code is 11, and the code you're receiving is 1 or POINT, I would anticipate there is a problem with the shapefile. I just created a shapefile with an explicit set of Z values and had no problems accessing them with the script provided. –  Geoist Aug 9 '12 at 14:56
    
Ok thant you for you answer ... I must apply the script on a 3d shapefile (shapefiles that have a z component.) to be sure that the problem provides from "donnees.shp" ... Now, do you have an example or do you know some sources where I can find that 3d shapefile? thanks –  A.H Aug 10 '12 at 7:25
    
realy thank you for your help, you're right, there was a problem with the shapefile, I created a new file with z-values and it runs now ! –  user9503 Aug 13 '12 at 10:32
add comment

1 Answer

The method you're using to access the values is correct according to the developer's wiki and the tests I've recently done:

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]

To create a 'Z' enabled shapefile, just specify that you want Z values during the creation process:

enter image description here

enter image description here

Please be aware of the following procedures for editing Z values in your test cases.

  1. Edit mode
  2. Select and right click the feature to edit
  3. Select 'Edit Verticies'
  4. Click the 'Sketch Properties' button
  5. Modify the value in the Z column
  6. Save an exit edit mode

You can also use steps 1-3 above to determine if your shapefile actually has 'Z' values or not.

Once you've done that, make sure you're opening the correct shapefile with shapefile.Reader(). If in doubt, use an absolute, rather than relative, path.

(FYI, I was also able to get 'M' values to work with this process as well.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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