-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support PostGIS as alternative data source to Overpass
- Loading branch information
Showing
8 changed files
with
111 additions
and
16 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#!/usr/bin/env python3 | ||
import json | ||
|
||
import aiopg | ||
import psycopg2 | ||
from psycopg2.extras import NamedTupleCursor | ||
import sentry_sdk | ||
|
||
from overpass import ZOOM_DEFAULT | ||
|
||
|
||
tile_query = """ | ||
SELECT * from soundscape_tile(%(zoom)s, %(tile_x)s, %(tile_y)s) | ||
""" | ||
|
||
|
||
class PostgisClient: | ||
"""A drop-in replacement for OverpassClient that uses a PostGIS server. | ||
The server is assumed to already be populated, including having the | ||
soundscape_tile function installed. | ||
""" | ||
def __init__(self, server, user_agent, cache_dir, cache_days, cache_size): | ||
# all the other args are only used by the OverpassClient | ||
self.server = server | ||
|
||
@sentry_sdk.trace | ||
async def query(self, x, y): | ||
async with aiopg.connect(self.server) as conn: | ||
async with conn.cursor(cursor_factory=NamedTupleCursor) as cursor: | ||
response = await self._gentile_async(cursor, x, y) | ||
return response | ||
|
||
# based on https://github.com/microsoft/soundscape/blob/main/svcs/data/gentiles.py | ||
async def _gentile_async(self, cursor, x, y, zoom=ZOOM_DEFAULT): | ||
try: | ||
await cursor.execute(tile_query, {'zoom': int(zoom), 'tile_x': x, 'tile_y': y}) | ||
value = await cursor.fetchall() | ||
return { | ||
'type': 'FeatureCollection', | ||
'features': list(map(lambda x: x._asdict(), value)) | ||
} | ||
except psycopg2.Error as e: | ||
print(e) | ||
raise |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,8 @@ | |
|
||
from cache import CompressedJSONCache | ||
from overpass import OverpassClient, OverpassResponse | ||
from postgis import PostgisClient | ||
from server import backend_client | ||
|
||
|
||
class TestCompressedJSONCache: | ||
|
@@ -221,3 +223,27 @@ async def test_intersections(self, x, y, overpass_client): | |
) | ||
) | ||
) | ||
|
||
|
||
class TestPostgisClient: | ||
@pytest.mark.parametrize( | ||
"url,expected_type", | ||
[ | ||
["https://overpass.kumi.systems/api/interpreter/", OverpassClient], | ||
["postgres://username:[email protected]:5432/osm/", PostgisClient], | ||
["ftp://example.com/", ValueError], | ||
], | ||
) | ||
def test_url_recognition(self, url, expected_type): | ||
"""We should get an OverpassClient, PostgisClient, or ValueError based on the URL.""" | ||
try: | ||
return_value = backend_client( | ||
url, | ||
"Overscape/0.1", | ||
cache_dir=Path("_test_cache"), | ||
cache_days=7, | ||
cache_size=1e5, | ||
) | ||
except Exception as exc: | ||
return_value = exc | ||
assert type(return_value) is expected_type |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
aiohttp==3.8.4 | ||
aiopg==1.4.0 | ||
osm2geojson==0.2.3 | ||
shapely==2.0.1 | ||
sentry-sdk==1.25.1 |