From b4b82b0f7439a13d5ae49f48bca5c46dab2de0b3 Mon Sep 17 00:00:00 2001 From: Nathan Bergey Date: Tue, 13 Sep 2022 22:25:59 -0400 Subject: [PATCH] Practice 3 using the provided json as-is --- docs/practices.md | 35 +++++++++++++++++++++++++++++++++++ test/test_example.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/docs/practices.md b/docs/practices.md index e59cbc2..0e9a672 100644 --- a/docs/practices.md +++ b/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) +); +``` \ No newline at end of file diff --git a/test/test_example.py b/test/test_example.py index 8a74cef..c7d450c 100644 --- a/test/test_example.py +++ b/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)