0

Following my predecessors (here and here) I am trying to append new data to shapefile using Pyshp. The specifics of my problem is the need to save the file with the same name (without creating a new file).

This simple example based on Pyshp documentation failes on the attempt of the edited file saving:

#!/usr/bin/python3.2

import shapefile

#create file
w = shapefile.Writer(shapefile.POINT)
w.field("Lon", "N", 20, 10)
w.field("Lat", "N", 20, 10)
w.field("Z", "N", 20, 10)
w.point(0.0, 0.0)
w.record(0.0, 0.0, 0.0)
w.save("test")

#append new record
e = shapefile.Editor(shapefile="test")            <-- Existing file is opened for editing
e.point(1.0, 1.0)
e.record(1.0, 1.0, 1.0)
e.save("test")                                    <-- Script falls out

Here is my traceback:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-29-e3063921c803> in <module>()
----> 1 e.save("out2")

/usr/local/lib/python3.2/dist-packages/pyshp-1.2.0-py3.2.egg/shapefile.py in save(self, target, shp, shx, dbf)
   1026                 target = temp.name
   1027                 generated = True
-> 1028             self.saveShp(target)
   1029             self.shp.close()
   1030             self.saveShx(target)

/usr/local/lib/python3.2/dist-packages/pyshp-1.2.0-py3.2.egg/shapefile.py in saveShp(self, target)
    983             self.shapeType = self._shapes[0].shapeType
    984         self.shp = self.__getFileObj(target)
--> 985         self.__shapefileHeader(self.shp, headerType='shp')
    986         self.__shpRecords()
    987 

/usr/local/lib/python3.2/dist-packages/pyshp-1.2.0-py3.2.egg/shapefile.py in __shapefileHeader(self, fileObj, headerType)
    713         z = self.zbox()
    714         # Measure
--> 715         m = self.mbox()
    716         try:
    717             f.write(pack("<4d", z[0], z[1], m[0], m[1]))

/usr/local/lib/python3.2/dist-packages/pyshp-1.2.0-py3.2.egg/shapefile.py in mbox(self)
    685     def mbox(self):
    686         """Returns the current m extremes for the shapefile."""
--> 687         return self.__mbox(self._shapes)
    688 
    689     def __shapefileHeader(self, fileObj, headerType='shp'):

/usr/local/lib/python3.2/dist-packages/pyshp-1.2.0-py3.2.egg/shapefile.py in __mbox(self, shapes, shapeTypes)
    671             except IndexError:
    672                 pass
--> 673         return [min(m), max(m)]
    674 
    675     def bbox(self):

TypeError: unorderable types: NoneType() < int()

Thank you

1

It may be an issue with the Editor class. Try this approach which uses a Reader and Writer object to do the same thing: http://geospatialpython.com/2013/10/editing-shapefile.html

  • I finally checked this. Works just perfect. Thank you for a nice lib! – Vitaly Isaev Oct 27 '13 at 15:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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