Skip to content

Commit 5824cea

Browse files
committed
Add helper to dump geojson to string
1 parent c9e9d2f commit 5824cea

File tree

6 files changed

+51
-5
lines changed

6 files changed

+51
-5
lines changed

README.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,32 @@ You need to register the extension:
2222
import postgis
2323
postgis.register(mydatabase.get_cursor())
2424

25-
Then you can pass geoms to psycopg:
25+
Then you can pass and get geoms using psycopg:
2626

2727
cursor.execute('INSERT INTO table (geom) VALUES (%s)', [Point(x=1, y=2, srid=4326)])
2828
cursor.execute('SELECT geom FROM points LIMIT 1')
2929
geom = cursor.fetchone()[0]
3030
assert geom.coords == (1, 2)
31+
32+
33+
## Example
34+
35+
> import psycopg2
36+
> from postgis import register, LineString
37+
> db = psycopg2.connect(dbname="test")
38+
> cursor = db.cursor()
39+
> register(cursor)
40+
> cursor.execute('CREATE TABLE IF NOT EXISTS mytable ("geom" geometry(LineString) NOT NULL)')
41+
> cursor.execute('INSERT INTO mytable (geom) VALUES (%s)', [LineString([(1, 2), (3, 4)], srid=4326)])
42+
> cursor.execute('SELECT geom FROM mytable LIMIT 1')
43+
> geom = cursor.fetchone()[0]
44+
> geom
45+
<LineString LINESTRING(1.0 2.0, 3.0 4.0)>
46+
> geom[0]
47+
<Point POINT(1.0 2.0)>
48+
> geom.coords
49+
((1.0, 2.0), (3.0, 4.0))
50+
> geom.geojson
51+
{'coordinates': ((1.0, 2.0), (3.0, 4.0)), 'type': 'LineString'}
52+
> str(geom.geojson)
53+
'{"type": "LineString", "coordinates": [[1, 2], [3, 4]]}'

postgis/geojson.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import json
2+
3+
4+
class GeoJSON(dict):
5+
6+
def __str__(self):
7+
return json.dumps(self)

postgis/geometry.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from psycopg2 import extensions as _ext
22
from .reader import EWKBReader, Typed
3+
from .geojson import GeoJSON
34

45

56
class Geometry(object, metaclass=Typed):
@@ -50,10 +51,10 @@ def wkt(self):
5051

5152
@property
5253
def geojson(self):
53-
return {
54+
return GeoJSON({
5455
'type': self.name,
5556
'coordinates': self.coords
56-
}
57+
})
5758

5859

5960
def register(cursor):

postgis/geometrycollection.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from .geometry import Geometry
2+
from .geojson import GeoJSON
23

34

45
class GeometryCollection(Geometry):
@@ -42,7 +43,7 @@ def wkt_coords(self):
4243

4344
@property
4445
def geojson(self):
45-
return {
46+
return GeoJSON({
4647
'type': self.name,
4748
'geometries': [g.geojson for g in self]
48-
}
49+
})

tests/test_linestring.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ def test_linestring_geojson():
2020
"coordinates": ((1, 2), (3, 4))}
2121

2222

23+
def test_linestring_geojson_as_string():
24+
line = LineString(((1, 2), (3, 4)))
25+
geojson = str(line.geojson)
26+
assert '"type": "LineString"' in geojson
27+
assert '"coordinates": [[1, 2], [3, 4]]' in geojson
28+
29+
2330
def test_geom_should_compare_with_coords():
2431
assert ((30, 10), (10, 30), (40, 40)) == LineString(((30, 10), (10, 30), (40, 40))) # noqa
2532

tests/test_point.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ def test_point_geojson():
2020
assert point.geojson == {"type": "Point", "coordinates": (1, 2)}
2121

2222

23+
def test_point_geojson_as_string():
24+
point = Point(1, 2)
25+
geojson = str(point.geojson)
26+
assert '"type": "Point"' in geojson
27+
assert '"coordinates": [1, 2]' in geojson
28+
29+
2330
def test_point_should_compare_with_coords():
2431
assert (-1.123456789, 2.987654321) == Point(-1.123456789, 2.987654321)
2532

0 commit comments

Comments
 (0)