wrong behavior for autoBalance=1?

18 views
Skip to first unread message

Mondo

unread,
Jun 15, 2011, 11:34:36 AM6/15/11
to Geospatial Python

Hi PyShp users,

First of all, big thanks to Joel and his company for making this and
letting us
use it -- I'm very grateful.

Just a quick question about the autoBalance functionality. Suppose I
do this:

import shapefile as shp
w = shp.Writer(shp.POLYLINE)
w.field('FIRST_FLD','C','40')
w.field('SECOND_FLD','C','40')
w.autoBalance = 1
w.line(parts=[[[1,5],[5,5],[5,1],[3,3],[1,1]]])
w.poly(parts=[[[1,3],[5,3]]], shapeType=shp.POLYLINE)


Now, If I do this:

len(w.records)

I would expect this to be a list with length 2. But instead of that,
it's empty.
Any thoughts? Is auto-balancing not working in this version (i.e.,
1.0.1, dated 2011.03.03)

Thanks again,

M


Geospatial Python

unread,
Jun 15, 2011, 12:24:23 PM6/15/11
to Geospatial Python
M,

The autobalance feature currently only applies to the Editor class
which inherits from the Writer class. And currently the Editor class
only works with existing shapefiles.

However you could easily hack this feature into the Writer class by
copying the "balance" method from the Editor class into the Writer
class. Then you would need to copy the last two lines of the
"Editor.point" method into the Writer.point, Writer.poly, and
Writer.line methods. The lines I'm describing in Editor.point are the
following:
if self.autoBalance:
self.balance()

Finally you would need to add the "autoBalance" attribute to the
Writer.__init__ method.

The other more scenic route would be to modify the Editor class to
fully inherit from the Writer class to create shapefiles. To do this
you would need an "else" clause at the end of Editor.__init__() which
simply calls the Writer.__init__() method. Then you would need to
override the point, line, and poly methods of the Writer class to add
the autobalance call. And there are probably a few other "gotchas" in
this modification that I haven't thought of yet.

Other options include just remembering to add the records when you add
shapes for creating your own external functions to add shapes which
balance things for you.

Thanks for the kind words,
Joel
Reply all
Reply to author
Forward
0 new messages