Browse Source

Practice 3 using the provided json as-is

master
Nathan Bergey 5 months ago
parent
commit
b4b82b0f74
  1. 35
      docs/practices.md
  2. 33
      test/test_example.py

35
docs/practices.md

@ -79,3 +79,38 @@ FROM japan_segments
```
![Kagoshima segments](./img/kagoshima_segments.png)
```sql
SELECT id FROM japan_segments
WHERE ST_Within(
bounds,
ST_SetSRID(ST_GeomFromGeoJSON('{
"type": "Polygon",
"coordinates": [
[
[
130.27313232421875,
30.519681272749402
],
[
131.02020263671875,
30.519681272749402
],
[
131.02020263671875,
30.80909017893796
],
[
130.27313232421875,
30.80909017893796
],
[
130.27313232421875,
30.519681272749402
]
]
]
}'), 4326)
);
```

33
test/test_example.py

@ -169,7 +169,40 @@ class TestExample(DbTest):
os.path.join(PATH_TO_SQL_DIR, "japan_segments.sql")
)
# Taking this task very literally and using the GeoJSON
# directly with PostGIS `ST_GeomFromGeoJSON`
sql = """
SELECT id FROM japan_segments
WHERE ST_Within(
bounds,
ST_SetSRID(ST_GeomFromGeoJSON('{
"type": "Polygon",
"coordinates": [
[
[
130.27313232421875,
30.519681272749402
],
[
131.02020263671875,
30.519681272749402
],
[
131.02020263671875,
30.80909017893796
],
[
130.27313232421875,
30.80909017893796
],
[
130.27313232421875,
30.519681272749402
]
]
]
}'), 4326)
);
"""
with conn.cursor(cursor_factory=RealDictCursor) as cur:
cur.execute(sql)

Loading…
Cancel
Save