Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a WKT/WKB __repr__/__str__ methods to pysal polygons & chains #812

Closed
ljwolf opened this issue May 26, 2016 · 3 comments
Closed

Add a WKT/WKB __repr__/__str__ methods to pysal polygons & chains #812

ljwolf opened this issue May 26, 2016 · 3 comments

Comments

@ljwolf
Copy link
Member

ljwolf commented May 26, 2016

Our point geometries already implement something close to the WKT standard:

>>> pysal.cg.Point((10,5))
(10.0, 5.0)

writing this shouldn't be too onerous. But, if in place, it would allow us to do something like:

>>> df = ps.pdio.read_files('NAT.shp')
>>> df['new_column'] = do_spatial_analysis()
>>> df.to_csv('NAT.csv')
>>> data = pandas.read_csv('NAT.csv')
>>> data['geometry'] = data.geometry.apply(pysal.core.utils.WKTParser.Polygon)

which could be quite nice. Currently, this is the type of behavior adopted in shapely, where shapes's __str__ method provides wkt and their __reduce__ method (used by pickle) provides wkb.

@parulsethi
Copy link

parulsethi commented Feb 20, 2017

Hi,
I am interested in adding this, but I'm not much familiar with pysal workflow. could you please elaborate this a bit more, maybe an example about what output is required for polygon/chain. Like, what resultant format of string representation for polygon?

@ljwolf ljwolf removed the GSOC label Feb 22, 2017
@ljwolf
Copy link
Member Author

ljwolf commented Feb 22, 2017

Hey! Thanks for your interest!

This involves providing a correct mapping from our polygon type (in pysal.cg.shapes) to the Open Geographic Consortium's Well-known text standard for representing geometries.

IIRC, the blocker for this was that our polygon type, pysal.cg.shapes.Polygon, did not preserve the mapping for which holes nest within each exterior ring. All the other shape types with corresponding types in WKT can be implemented using a string template or regular expressions.

In order to generate valid WKT for multipolygons with holes, each exterior polygon ring in polygon.parts needs to get paired with the holes in polygon.holes contained inside of that exterior ring. This could be done by checking whether a point on the boundary of each hole is within each exterior ring using Polygon.contains_point, assuming the polygon boundary does not intersect itself.

@parulsethi
Copy link

Thanks for the comprehensive explanation!

and if I understand correctly, I'd need to check only a single point out of a hole's vertices, rather than checking for all vertices of a hole to be inside the exterior ring, as no intersection is being assumed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants