Skip to content
This repository has been archived by the owner on Oct 5, 2024. It is now read-only.

Commit

Permalink
REST API: list of new mapped features + quality issues
Browse files Browse the repository at this point in the history
  • Loading branch information
emi420 committed Aug 30, 2023
1 parent 59297cb commit 44c51c9
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 14 deletions.
1 change: 1 addition & 0 deletions python/dbapi/api/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def run(self, query, responseType = 'json', singleObject = False):
item[column] = row[index]
results.append(item)
cur.close()

if singleObject:
return results[0]['result']
return results
81 changes: 70 additions & 11 deletions python/dbapi/api/raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

from .db import UnderpassDB

RESULTS_PER_PAGE = 25

class Raw:
def __init__(self, db):
self.underpassDB = db
Expand All @@ -29,22 +31,24 @@ def getPolygons(
key = None,
value = None,
hashtag = None,
responseType = "json"
responseType = "json",
page = None
):
query = "with t_ways AS ( \
SELECT raw_poly.osm_id, geometry, tags, status FROM raw_poly \
SELECT raw_poly.osm_id as id, raw_poly.timestamp, geometry, tags, status FROM raw_poly \
LEFT JOIN validation ON validation.osm_id = raw_poly.osm_id \
WHERE \
ST_Intersects(\"geometry\", \
ST_GeomFromText('POLYGON(({0}))', 4326) \
) {1} {2} \
{0} {1} {2} {3} \
), \
t_features AS ( \
SELECT jsonb_build_object( 'type', 'Feature', 'id', t_ways.osm_id, 'properties', to_jsonb(t_ways) - 'geometry' - 'osm_id' , 'geometry', ST_AsGeoJSON(geometry)::jsonb ) AS feature FROM t_ways \
) SELECT jsonb_build_object( 'type', 'FeatureCollection', 'features', jsonb_agg(t_features.feature) ) as result FROM t_features;".format(
area,
SELECT jsonb_build_object( 'type', 'Feature', 'id', id, 'properties', to_jsonb(t_ways) \
- 'geometry' , 'geometry', ST_AsGeoJSON(geometry)::jsonb ) AS feature FROM t_ways \
) SELECT jsonb_build_object( 'type', 'FeatureCollection', 'features', jsonb_agg(t_features.feature) ) \
as result FROM t_features;".format(
"ST_Intersects(\"geometry\", ST_GeomFromText('POLYGON(({0}))', 4326) )".format(area) if area else "1=1 ",
"and raw_poly.tags ? '{0}'".format(key) if key and not value else "",
"and raw_poly.tags->'{0}' ~* '^{1}'".format(key, value) if key and value else "",
"ORDER BY raw_poly.timestamp DESC LIMIT " + str(RESULTS_PER_PAGE) + " OFFSET {0}".format(page * RESULTS_PER_PAGE) if page else "",
)
return self.underpassDB.run(query, responseType, True)

Expand All @@ -57,19 +61,74 @@ def getNodes(
responseType = "json"
):
query = "with t_nodes AS ( \
SELECT raw_node.osm_id, geometry, tags, status FROM raw_node \
SELECT raw_node.osm_id as id, geometry, tags, status FROM raw_node \
LEFT JOIN validation ON validation.osm_id = raw_node.osm_id \
WHERE \
ST_Intersects(\"geometry\", \
ST_GeomFromText('POLYGON(({0}))', 4326) \
) {1} {2} \
), \
t_features AS ( \
SELECT jsonb_build_object( 'type', 'Feature', 'id', t_nodes.osm_id, 'properties', to_jsonb(t_nodes) - 'geometry' - 'osm_id' , 'geometry', ST_AsGeoJSON(geometry)::jsonb ) AS feature FROM t_nodes \
) SELECT jsonb_build_object( 'type', 'FeatureCollection', 'features', jsonb_agg(t_features.feature) ) as result FROM t_features;".format(
SELECT jsonb_build_object( 'type', 'Feature', 'id', id, 'properties', to_jsonb(t_nodes) \
- 'geometry' - 'osm_id' , 'geometry', ST_AsGeoJSON(geometry)::jsonb ) AS feature FROM t_nodes \
) SELECT jsonb_build_object( 'type', 'FeatureCollection', 'features', jsonb_agg(t_features.feature) ) \
as result FROM t_features;".format(
area,
"and raw_node.tags ? '{0}'".format(key) if key and not value else "",
"and raw_node.tags->'{0}' ~* '^{1}'".format(key, value) if key and value else "",
)
return self.underpassDB.run(query, responseType, True)

def getPolygonsList(
self,
area = None,
key = None,
value = None,
hashtag = None,
responseType = "json",
page = None
):
if page == 0:
page = 1

query = "with t_ways AS ( \
SELECT raw_poly.osm_id as id, ST_X(ST_Centroid(geometry)) as lat, ST_Y(ST_Centroid(geometry)) as lon, raw_poly.timestamp, tags, status FROM raw_poly \
LEFT JOIN validation ON validation.osm_id = raw_poly.osm_id \
WHERE \
{0} {1} {2} {3} \
), t_features AS ( \
SELECT to_jsonb(t_ways) as feature from t_ways \
) SELECT jsonb_agg(t_features.feature) as result FROM t_features;".format(
"ST_Intersects(\"geometry\", ST_GeomFromText('POLYGON(({0}))', 4326) )".format(area) if area else "1=1 ",
"and raw_poly.tags ? '{0}'".format(key) if key and not value else "",
"and raw_poly.tags->'{0}' ~* '^{1}'".format(key, value) if key and value else "",
"ORDER BY raw_poly.timestamp DESC LIMIT " + str(RESULTS_PER_PAGE) + " OFFSET {0}".format(page * RESULTS_PER_PAGE) if page else "",
)
return self.underpassDB.run(query, responseType, True)

def getNodesList(
self,
area = None,
key = None,
value = None,
hashtag = None,
responseType = "json",
page = None
):
if page == 0:
page = 1

query = "with t_nodes AS ( \
SELECT raw_node.osm_id as id, ST_X(ST_Centroid(geometry)) as lat, ST_Y(ST_Centroid(geometry)) as lon, tags, status FROM raw_node \
LEFT JOIN validation ON validation.osm_id = raw_node.osm_id \
WHERE {0} {1} {2} {3} \
), \
t_features AS ( \
SELECT to_jsonb(t_nodes) AS feature FROM t_nodes \
) SELECT jsonb_agg(t_features.feature) as result FROM t_features;".format(
"ST_Intersects(\"geometry\", ST_GeomFromText('POLYGON(({0}))', 4326) )".format(area) if area else "1=1 ",
"and raw_node.tags ? '{0}'".format(key) if key and not value else "",
"and raw_node.tags->'{0}' ~* '^{1}'".format(key, value) if key and value else "",
"ORDER BY raw_node.timestamp DESC LIMIT " + str(RESULTS_PER_PAGE) + " OFFSET {0}".format(page * RESULTS_PER_PAGE) if page else "",
)
return self.underpassDB.run(query, responseType, True)
25 changes: 23 additions & 2 deletions python/restapi/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,10 @@ def osmchangeValidate(request: OsmchangeValidateRequest):
@app.post("/raw/polygons")
def getPolygons(request: RawRequest):
results = rawer.getPolygons(
area = request.area,
area = request.area or None,
key = request.key or "",
value = request.value or ""
value = request.value or "",
page = request.page
)
return results

Expand All @@ -168,4 +169,24 @@ def getNodes(request: RawRequest):
key = request.key or "",
value = request.value or ""
)
return results

@app.post("/raw/polygonsList")
def getPolygonsList(request: RawRequest):
results = rawer.getPolygonsList(
area = request.area or None,
key = request.key or "",
value = request.value or "",
page = request.page
)
return results

@app.post("/raw/nodesList")
def getNodesList(request: RawRequest):
results = rawer.getNodesList(
area = request.area or None,
key = request.key or "",
value = request.value or "",
page = request.page
)
return results
3 changes: 2 additions & 1 deletion python/restapi/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class OsmchangeValidateRequest(BaseModel):
check: str

class RawRequest(BaseModel):
area: str
area: str = None
key: str = None
value: str = None
page: int = None
3 changes: 3 additions & 0 deletions utils/raw-underpass.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ CREATE INDEX way_refs_way_id_idx ON public.way_refs (way_id);

CREATE INDEX node_version_idx ON public.raw_node (version);
CREATE INDEX way_version_idx ON public.raw_poly (version);

CREATE INDEX node_timestamp_idx ON public.raw_node(timestamp DESC);
CREATE INDEX way_timestamp_idx ON public.raw_poly(timestamp DESC);

0 comments on commit 44c51c9

Please sign in to comment.