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

Commit

Permalink
+ New endpoints for getting all features
Browse files Browse the repository at this point in the history
  • Loading branch information
emi420 committed Sep 12, 2023
1 parent e60eca1 commit 8ab3322
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 17 deletions.
143 changes: 132 additions & 11 deletions python/dbapi/api/raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def getPolygons(
page = None
):
query = "with t_ways AS ( \
SELECT ways_poly.osm_id as id, ways_poly.timestamp, geom as geometry, tags, status FROM ways_poly \
SELECT 'Polygon' as type, ways_poly.osm_id as id, ways_poly.timestamp, geom as geometry, tags, status FROM ways_poly \
LEFT JOIN validation ON validation.osm_id = ways_poly.osm_id \
WHERE \
{0} {1} {2} {3} \
Expand All @@ -62,7 +62,7 @@ def getLines(
page = None
):
query = "with t_ways AS ( \
SELECT ways_line.osm_id as id, ways_line.timestamp, geom as geometry, tags, status FROM ways_line \
SELECT 'LineString' as type, ways_line.osm_id as id, ways_line.timestamp, geom as geometry, tags, status FROM ways_line \
LEFT JOIN validation ON validation.osm_id = ways_line.osm_id \
WHERE \
{0} {1} {2} {3} \
Expand All @@ -85,27 +85,74 @@ def getNodes(
key = None,
value = None,
hashtag = None,
responseType = "json"
responseType = "json",
page = None
):
query = "with t_nodes AS ( \
SELECT nodes.osm_id as id, geom as geometry, tags, status FROM nodes \
SELECT 'Point' as type, nodes.osm_id as id, geom as geometry, tags, status FROM nodes \
LEFT JOIN validation ON validation.osm_id = nodes.osm_id \
WHERE \
ST_Intersects(\"geom\", \
ST_GeomFromText('POLYGON(({0}))', 4326) \
) {1} {2} \
{0} {1} {2} {3} \
), \
t_features AS ( \
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,
"ST_Intersects(\"geom\", ST_GeomFromText('POLYGON(({0}))', 4326) )".format(area) if area else "1=1 ",
"and nodes.tags ? '{0}'".format(key) if key and not value else "",
"and nodes.tags->'{0}' ~* '^{1}'".format(key, value) if key and value else "",
"ORDER BY nodes.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 getAll(
self,
area = None,
key = None,
value = None,
hashtag = None,
responseType = "json",
page = None
):

polygons = self.getPolygons(
area,
key,
value,
hashtag,
responseType,
page)

lines = self.getLines(
area,
key,
value,
hashtag,
responseType,
page)

nodes = self.getNodes(
area,
key,
value,
hashtag,
responseType,
page)

result = {'type': 'FeatureCollection', 'features': []}

if polygons['features']:
result['features'] = result['features'] + polygons['features']

if lines['features']:
result['features'] = result['features'] + lines['features']

elif nodes['features']:
result['features'] = result['features'] + nodes['features']

return result

def getPolygonsList(
self,
area = None,
Expand All @@ -119,7 +166,7 @@ def getPolygonsList(
page = 1

query = "with t_ways AS ( \
SELECT ways_poly.osm_id as id, ST_X(ST_Centroid(geom)) as lat, ST_Y(ST_Centroid(geom)) as lon, ways_poly.timestamp, tags, status FROM ways_poly \
SELECT 'way' as type, ways_poly.osm_id as id, ST_X(ST_Centroid(geom)) as lat, ST_Y(ST_Centroid(geom)) as lon, ways_poly.timestamp, tags, status FROM ways_poly \
LEFT JOIN validation ON validation.osm_id = ways_poly.osm_id \
WHERE \
{0} {1} {2} {3} \
Expand All @@ -133,6 +180,33 @@ def getPolygonsList(
)
return self.underpassDB.run(query, responseType, True)

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

query = "with t_lines AS ( \
SELECT 'way' as type, ways_line.osm_id as id, ST_X(ST_Centroid(geom)) as lat, ST_Y(ST_Centroid(geom)) as lon, ways_line.timestamp, tags, status FROM ways_line \
LEFT JOIN validation ON validation.osm_id = ways_line.osm_id \
WHERE \
{0} {1} {2} {3} \
), t_features AS ( \
SELECT to_jsonb(t_lines) as feature from t_lines \
) SELECT jsonb_agg(t_features.feature) as result FROM t_features;".format(
"ST_Intersects(\"geom\", ST_GeomFromText('POLYGON(({0}))', 4326) )".format(area) if area else "1=1 ",
"and ways_line.tags ? '{0}'".format(key) if key and not value else "",
"and ways_line.tags->'{0}' ~* '^{1}'".format(key, value) if key and value else "",
"ORDER BY ways_line.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,
Expand All @@ -146,7 +220,7 @@ def getNodesList(
page = 1

query = "with t_nodes AS ( \
SELECT nodes.osm_id as id, ST_X(ST_Centroid(geom)) as lat, ST_Y(ST_Centroid(geom)) as lon, tags, status FROM nodes \
SELECT 'node' as type, nodes.osm_id as id, ST_X(ST_Centroid(geom)) as lat, ST_Y(ST_Centroid(geom)) as lon, tags, status FROM nodes \
LEFT JOIN validation ON validation.osm_id = nodes.osm_id \
WHERE {0} {1} {2} {3} \
), \
Expand All @@ -158,4 +232,51 @@ def getNodesList(
"and nodes.tags->'{0}' ~* '^{1}'".format(key, value) if key and value else "",
"ORDER BY nodes.timestamp DESC LIMIT " + str(RESULTS_PER_PAGE) + " OFFSET {0}".format(page * RESULTS_PER_PAGE) if page else "",
)
return self.underpassDB.run(query, responseType, True)
return self.underpassDB.run(query, responseType, True)

def getAllList(
self,
area = None,
key = None,
value = None,
hashtag = None,
responseType = "json",
page = None
):

polygons = self.getPolygonsList(
area,
key,
value,
hashtag,
responseType,
page)

lines = self.getLinesList(
area,
key,
value,
hashtag,
responseType,
page)

nodes = self.getNodesList(
area,
key,
value,
hashtag,
responseType,
page)

result = []

if polygons:
result = result + polygons

if lines:
result = result + lines

if nodes:
result = result + nodes

return result
26 changes: 24 additions & 2 deletions python/restapi/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ def getNodes(request: RawRequest):
results = rawer.getNodes(
area = request.area,
key = request.key or "",
value = request.value or ""
value = request.value or "",
page = request.page
)
return results

Expand All @@ -177,7 +178,18 @@ def getLines(request: RawRequest):
results = rawer.getLines(
area = request.area,
key = request.key or "",
value = request.value or ""
value = request.value or "",
page = request.page
)
return results

@app.post("/raw/all")
def getLines(request: RawRequest):
results = rawer.getAll(
area = request.area,
key = request.key or "",
value = request.value or "",
page = request.page
)
return results

Expand All @@ -199,4 +211,14 @@ def getNodesList(request: RawRequest):
value = request.value or "",
page = request.page
)
return results

@app.post("/raw/allList")
def getAllList(request: RawRequest):
results = rawer.getAllList(
area = request.area or None,
key = request.key or "",
value = request.value or "",
page = request.page
)
return results
6 changes: 2 additions & 4 deletions src/bootstrap/bootstrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ void startProcessingWays(const underpassconfig::UnderpassConfig &config) {

// TODO: improve duplicate code
int total = queryraw->getWaysCount(QueryRaw::polyTable);
std::cout << "Processing polygons (" + std::to_string(total) + ")..." << std::endl;
if (total > 0) {
int count = 0;
long lastid = 0;
Expand All @@ -87,12 +86,11 @@ void startProcessingWays(const underpassconfig::UnderpassConfig &config) {
db->query(task->query);
lastid = wayTask.lastid;
count += wayTask.processed;
std::cout << "\r" << "Processing : " << count << "/" << total << " (" << percentage << "%)";
std::cout << "\r" << "Processing polygons: " << count << "/" << total << " (" << percentage << "%)";
}
}

total = queryraw->getWaysCount(QueryRaw::lineTable);
std::cout << "Processing lines (" + std::to_string(total) + ")..." << std::endl;
if (total > 0) {
int count = 0;
long lastid = 0;
Expand All @@ -109,7 +107,7 @@ void startProcessingWays(const underpassconfig::UnderpassConfig &config) {
db->query(task->query);
lastid = wayTask.lastid;
count += wayTask.processed;
std::cout << "\r" << "Processing : " << count << "/" << total << " (" << percentage << "%)";
std::cout << "\r" << "Processing lines: " << count << "/" << total << " (" << percentage << "%)";
}
}

Expand Down

0 comments on commit 8ab3322

Please sign in to comment.