zhikrullah / pyshp

Automatically exported from code.google.com/p/pyshp
MIT License
0 stars 0 forks source link

Null shapes raising exception on save #52

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
How to reproduce:

w = shapefile.Writer(shapeType=3)
w.null()
w.poly(shapeType=3, parts=[[[122,37,4,9], [117,36,3,4]], [[115,32,8,8], 
[118,20,6,4], [113,24]]])
w.save("foo")

Expected behavior:
A shapefile should be written

Actual behavior:
Exception thrown

Possible fix:

__shpFileLength should initialize nParts and nPoints to 0 each time through the 
`for s in self._shapes` loop.

    def __shpFileLength(self):
        """Calculates the file length of the shp file."""
        # Start with header length
        size = 100
        # Calculate size of all shapes
        for s in self._shapes:
            # Add in record header and shape type fields
            size += 12
            nParts = 0
            nPoints = 0

__bbox should skip over null shapes. I also added support for the case when all 
the shapes are null shapes.

    def __bbox(self, shapes):
        x = []
        y = []
        for s in shapes:
            if s.shapeType == 0:
                continue
            px, py = list(zip(*s.points))[:2]
            x.extend(px)
            y.extend(py)

        # handle the case when there are only null shapes
        if x == y == []:
            return [0, 0, 0, 0]
        return [min(x), min(y), max(x), max(y)]

Original issue reported on code.google.com by m...@pdx.edu on 25 Jul 2013 at 4:41

GoogleCodeExporter commented 8 years ago

Original comment by jlawh...@geospatialpython.com on 25 Jul 2013 at 7:15