Created
November 25, 2014 01:08
-
-
Save wryun/2d14b0c38b56b163a59f to your computer and use it in GitHub Desktop.
geoalchemy2 using geojson rather than wkb/wkt (wkbelement/wktelement)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import geoalchemy2, geoalchemy2.shape | |
| import shapely.geometry | |
| # Could use geomet rather than pushing this through shapely. | |
| class Geometry(geoalchemy2.Geometry): | |
| """Deal in GeoJSON rather than WKB/WKT""" | |
| def result_processor(self, dialect, coltype): | |
| super_proc = super(Geometry, self).result_processor(dialect, coltype) | |
| def process(v): | |
| return v and shapely.geometry.mapping( | |
| geoalchemy2.shape.to_shape(super_proc(v))) | |
| return process | |
| def bind_processor(self, dialect): | |
| # Watch out. from_shape returns a wkb not a wkt, which | |
| # means geoalchemy2 doesn't apply the SRID. | |
| super_proc = super(Geometry, self).bind_processor(dialect) | |
| def process(v): | |
| return v and super_proc( | |
| geoalchemy2.shape.from_shape(shapely.geometry.shape(value)) | |
| if isinstance(v, dict) else v) | |
| return process |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thank for this great piece of code, but the deserialization part is not working well it gives me this error ProgrammingError: (psycopg2.ProgrammingError) can't adapt type 'WKBElement' [SQL: 'INSERT INTO person (name, point) VALUES (%(name)s, ST_GeomFromEWKT(%(point)s)) RETURNING person.id'] [parameters: {'name': u'TTTT', 'point': <WKBElement at 0x28f0b10; '01010000003cdba337dcc351c06d37c1374d374840'>}]
127.0.0.1 - - [19/Apr/2015 12:17:53] "POST /api/person HTTP/1.1" 400 -