-
Notifications
You must be signed in to change notification settings - Fork 557
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
Replace deprecated To/From WKT/WKB functions with Reader/Writers #63
Comments
I've been poking around the reader and writer APIs, and they are actually straight-forward, and offers nice flexibility. See the API docs and WKTWriterTest.cpp to get an idea of the writer's for usage and examples of output. I think it would be a good idea to make a WKT writer a persistent object, with properties exposed in, say,
I don't know if |
Thanks for digging in to this @mwtoews. I'm +1 on adding a WKT writer class, but -1 on a module level instance of it. I regret the module state that I've already saddled on Shapely, and don't want to add more. A new class actually gives us a way out: wkt.dump (and dumps) could create their own writer instance as needed or take one from the caller (as with json.dump) or programs could create and reuse their own writers. |
Moving the classes/objects off
In BTW, I'm half done implementing this, but I haven't pushed any code out yet. I'm mostly fumbling around with |
@mwtoews For what it's worth, I'm working on a lib which provides GeoJSON <--> WKB/WKT, including support for 2d, 3d, and 4d geometry. It's pure Python, so no mucking around with the GEOS API. https://github.com/larsbutler/geomet It's still a work in progress, though--I don't have all of the geometry types implemented. |
Thanks @larsbutler I'll have a look. @sgillies suggested using a Python WKT/B implementation in #32 I'm not sure if there are performance benefits with the GEOS C API reader/writer, but there is certainly more flexibility and simplicity with implementing a pure Python solution. |
Mostly implemented here: mwtoews@7bd61da |
I've been thinking about doing a few things. First, the default reader/writers are created to:
To change the properties for the default writers, one would need to: from shapely.geos import lgeos
lgeos.wkt_writer.trim = False
lgeos.wkb_writer.big_endian = False Should a copy of the default writer objects be put somewhere else? What about the default writers' default output settings? Currently, I have it to set a different WKT output for GEOS >=3.3.0, using Second, to allow keywords to WK[TB]Writer's wkt_writer = WKTWriter(lgeos, trim=True, output_dimension=3)
wkb_writer = WKBWriter(lgeos, output_dimension=3, big_endian=False) Furthermore, allow these keywords to be used by shapely.wk[tb].dumps: from shapely.geometry import Point
from shapely.wkt import dumps
a = Point(1,2,3)
dumps(a) # POINT Z (1 2 3)
dumps(a, trim=True, output_dimension=3) # POINT Z (1 2 3)
dumps(a, trim=True, output_dimension=2) # POINT (1 2)
dumps(a, trim=False, output_dimension=2) # POINT (1.0000000000000000 2.0000000000000000)
a.wkt # POINT Z (1 2 3) # using default writer Here, dumps would create a new writer, set output options, write data, then it would destroy the writer. The default writer is an independent object, with different default settings (discussed above). Looking at json.dumps, it looks normal to have several keyword parameters for the writer implementation. |
It would be nice to have an ability to limit number of decimal digits in wkt. Unfortunately, I don't know if geos is capable if such trim. |
@georgthegreat under the working code in my https://github.com/mwtoews/shapely/tree/v13 branch this could be done by setting from shapely.geos import lgeos
lgeos.wkt_writer.rounding_precision = 4 |
Thanks! |
Some random remarks:
|
- New classes in geos.py, with persistent objects in geos.lgeos - Use __del__() methods for classes that need to be cleaned up - Move atexit register to a decorator at the bottom - Writers implement get/set properties, where appropriate - Use a different defaults for WKT Writer for GEOS 3.3.0 and up, to output 3 dimensions and trim extra decimals - Set WKB Writer to have default output dimensions of 3 (#64) - Deprecate older To/From WKT/WKB functions in geometry.base, particularly: - geom_from_wkt(data) - geom_to_wkt(ob) - geom_from_wkb(data) - geom_to_wkb(ob) - to_wkb(self) - to_wkt(self) - Keeping deserialize_wkb(data) for now, since I can't figure out how to avoid circular imports using the reader; only used for 'EMPTY' - Geometry objects now have a 'wkb_hex' property, for convenience - Test suites run using older WKT formatting, so these don't need to be modified; all tests appear to pass
According to the GEOS C API,
GEOSGeomToWKT
andGEOSGeomFromWKT
(used in ctypes_declarations.py) are deprecated (since 3.0.0-CAPI-1.4.1), and should use the new WKT Reader and Writer functions. The same applies to WKB.This idea was sparked in #32
The text was updated successfully, but these errors were encountered: