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

Commit

Permalink
+ Filters for date, hashtags, area. Refactor and fix for bootstrappin…
Browse files Browse the repository at this point in the history
…g script
  • Loading branch information
emi420 committed Oct 18, 2023
1 parent 13422f2 commit 5d23c52
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 100 deletions.
240 changes: 150 additions & 90 deletions python/dbapi/api/raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,91 +40,148 @@ def tagsQueryFilter(tagsQuery, table):
query += "OR {0}.tags->>'{1}' IS NOT NULL".format(table, keyValue[0])
return query

class Raw:
def __init__(self, db):
self.underpassDB = db
def getGeoType(table):
if table == "ways_poly":
return "Polygon"
elif table == "ways_line":
return "LineString"
return "Node"

def getPolygons(
self,
def geoFeaturesQuery(
area = None,
tags = None,
hashtag = None,
responseType = "json",
page = None
):
dateTo = None,
dateFrom = None,
page = 0,
table = None):

geoType = getGeoType(table)
query = "with t_ways AS ( \
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 \
SELECT '" + geoType + "' as type, " + table + ".osm_id as id, " + table + ".timestamp, geom as geometry, tags, status, hashtags, editor, created_at FROM " + table + " \
LEFT JOIN validation ON validation.osm_id = " + table + ".osm_id \
LEFT JOIN changesets c ON c.id = " + table + ".changeset \
WHERE \
{0} {1} {2} \
{0} {1} {2} {3} {4} \
), \
t_features AS ( \
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(\"geom\", ST_GeomFromText('POLYGON(({0}))', 4326) )".format(area) if area else "1=1 ",
"AND (" + tagsQueryFilter(tags, "ways_poly") + ")" if tags else "",
"ORDER BY ways_poly.timestamp DESC LIMIT " + str(RESULTS_PER_PAGE),
"AND (" + tagsQueryFilter(tags, table) + ")" if tags else "",
"AND " + table + ".changeset IN (SELECT c.id FROM changesets c where jsonb_path_exists(to_jsonb(hashtags), '$[*] ? (@ like_regex \"^{0}\")') GROUP BY C.id)".format(hashtag) if hashtag else "",
"AND created at >= {0} AND created_at <= {1}".format(dateFrom, dateTo) if dateFrom and dateTo else "",
"LIMIT " + str(RESULTS_PER_PAGE),
)
return query

def listFeaturesQuery(
area = None,
tags = None,
hashtag = None,
page = 0,
dateFrom = None,
dateTo = None,
table = None):

geoType = getGeoType(table)
if table == "nodes":
osmType = "node"
else:
osmType = "way"

query = "with t_ways AS ( \
SELECT '" + osmType + "' as type, '" + geoType + "' as geotype, " + table + ".osm_id as id, ST_X(ST_Centroid(geom)) as lat, ST_Y(ST_Centroid(geom)) as lon, " + table + ".timestamp, tags, status, created_at FROM " + table + " \
LEFT JOIN validation ON validation.osm_id = " + table + ".osm_id \
LEFT JOIN changesets c ON c.id = " + table + ".changeset \
WHERE \
{0} {1} {2} {3} {4} \
), 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(\"geom\", ST_GeomFromText('POLYGON(({0}))', 4326) )".format(area) if area else "1=1 ",
"AND (" + tagsQueryFilter(tags, table) + ")" if tags else "",
"AND " + table + ".changeset IN (SELECT c.id FROM changesets c where jsonb_path_exists(to_jsonb(hashtags), '$[*] ? (@ like_regex \"^{0}\")') GROUP BY C.id)".format(hashtag) if hashtag else "",
"AND created_at >= '{0}' AND created_at <= '{1}'".format(dateFrom, dateTo) if (dateFrom and dateTo) else "",
"AND created_at IS NOT NULL ORDER BY created_at DESC LIMIT " + str(RESULTS_PER_PAGE_LIST) + (" OFFSET {0}".format(page * RESULTS_PER_PAGE_LIST) if page else ""),
)
return self.underpassDB.run(query, responseType, True)
return query

class Raw:
def __init__(self, db):
self.underpassDB = db

def getPolygons(
self,
area = None,
tags = None,
hashtag = None,
responseType = "json",
dateFrom = None,
dateTo = None,
page = None
):
return self.underpassDB.run(geoFeaturesQuery(
area,
tags,
hashtag,
page,
dateFrom,
dateTo,
"ways_poly"
), responseType, True)

def getLines(
self,
area = None,
tags = None,
hashtag = None,
responseType = "json",
dateFrom = None,
dateTo = None,
page = None
):
query = "with t_ways AS ( \
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} \
), \
t_features AS ( \
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(\"geom\", ST_GeomFromText('POLYGON(({0}))', 4326) )".format(area) if area else "1=1 ",
"AND (" + tagsQueryFilter(tags, "ways_line") + ")" if tags else "",
"ORDER BY ways_line.timestamp DESC LIMIT " + str(RESULTS_PER_PAGE),
)
return self.underpassDB.run(query, responseType, True)
return self.underpassDB.run(geoFeaturesQuery(
area,
tags,
hashtag,
page,
dateFrom,
dateTo,
"ways_line"
), responseType, True)

def getNodes(
self,
area = None,
tags = None,
hashtag = None,
responseType = "json",
dateFrom = None,
dateTo = None,
page = None
):
query = "with t_nodes AS ( \
SELECT 'Point' as type, nodes.osm_id as id, nodes.timestamp, geom as geometry, tags, status FROM nodes \
LEFT JOIN validation ON validation.osm_id = nodes.osm_id \
WHERE \
{0} {1} {2} \
), \
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(
"ST_Intersects(\"geom\", ST_GeomFromText('POLYGON(({0}))', 4326) )".format(area) if area else "1=1 ",
"AND (" + tagsQueryFilter(tags, "nodes") + ")" if tags else "",
"ORDER BY nodes.timestamp DESC LIMIT " + str(RESULTS_PER_PAGE),
)
return self.underpassDB.run(query, responseType, True)
return self.underpassDB.run(geoFeaturesQuery(
area,
tags,
hashtag,
page,
dateFrom,
dateTo,
"nodes"
), responseType, True)


def getAll(
self,
area = None,
tags = None,
hashtag = None,
responseType = "json",
dateFrom = None,
dateTo = None,
page = None
):

Expand All @@ -133,20 +190,26 @@ def getAll(
tags,
hashtag,
responseType,
dateFrom,
dateTo,
page)

lines = self.getLines(
area,
tags,
hashtag,
responseType,
dateFrom,
dateTo,
page)

nodes = self.getNodes(
area,
tags,
hashtag,
responseType,
dateFrom,
dateTo,
page)

result = {'type': 'FeatureCollection', 'features': []}
Expand All @@ -168,79 +231,70 @@ def getPolygonsList(
tags = None,
hashtag = None,
responseType = "json",
dateFrom = None,
dateTo = None,
page = None
):

query = "with t_ways AS ( \
SELECT 'way' as type, 'Polygon' as geotype, 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} \
), 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(\"geom\", ST_GeomFromText('POLYGON(({0}))', 4326) )".format(area) if area else "1=1 ",
"AND (" + tagsQueryFilter(tags, "ways_poly") + ")" if tags else "",
"ORDER BY ways_poly.timestamp DESC LIMIT " + str(RESULTS_PER_PAGE_LIST) + " OFFSET {0}".format(page * RESULTS_PER_PAGE_LIST) if page else "",
)
return self.underpassDB.run(query, responseType, True)
return self.underpassDB.run(listFeaturesQuery(
area,
tags,
hashtag,
page,
dateFrom,
dateTo,
"ways_poly"
), responseType, True)

def getLinesList(
self,
area = None,
tags = None,
hashtag = None,
responseType = "json",
dateFrom = None,
dateTo = None,
page = None
):
if page == 0:
page = 1
return self.underpassDB.run(listFeaturesQuery(
area,
tags,
hashtag,
page,
dateFrom,
dateTo,
"ways_line"
), responseType, True)

query = "with t_lines AS ( \
SELECT 'way' as type, 'LineString' as geotype, 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} \
), 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 (" + tagsQueryFilter(tags, "ways_line") + ")" if tags else "",
"ORDER BY ways_line.timestamp DESC LIMIT " + str(RESULTS_PER_PAGE_LIST) + " OFFSET {0}".format(page * RESULTS_PER_PAGE_LIST) if page else "",
)
return self.underpassDB.run(query, responseType, True)

def getNodesList(
self,
area = None,
tags = None,
hashtag = None,
responseType = "json",
dateFrom = None,
dateTo = None,
page = None
):
if page == 0:
page = 1
return self.underpassDB.run(listFeaturesQuery(
area,
tags,
hashtag,
page,
dateFrom,
dateTo,
"nodes"
), responseType, True)

query = "with t_nodes AS ( \
SELECT 'node' as type, 'Point' as geotype, nodes.osm_id as id, ST_X(ST_Centroid(geom)) as lat, ST_Y(ST_Centroid(geom)) as lon, nodes.timestamp, tags, status FROM nodes \
LEFT JOIN validation ON validation.osm_id = nodes.osm_id \
WHERE {0} {1} {2} \
), \
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(\"geom\", ST_GeomFromText('POLYGON(({0}))', 4326) )".format(area) if area else "1=1 ",
"AND (" + tagsQueryFilter(tags, "nodes") + ")" if tags else "",
"ORDER BY nodes.timestamp DESC LIMIT " + str(RESULTS_PER_PAGE_LIST) + " OFFSET {0}".format(page * RESULTS_PER_PAGE_LIST) if page else "",
)
return self.underpassDB.run(query, responseType, True)

def getAllList(
self,
area = None,
tags = None,
hashtag = None,
responseType = "json",
dateFrom = None,
dateTo = None,
page = None
):

Expand All @@ -249,20 +303,26 @@ def getAllList(
tags,
hashtag,
responseType,
dateFrom,
dateTo,
page)

lines = self.getLinesList(
area,
tags,
hashtag,
responseType,
dateFrom,
dateTo,
page)

nodes = self.getNodesList(
area,
tags,
hashtag,
responseType,
dateFrom,
dateTo,
page)

result = []
Expand Down
Loading

0 comments on commit 5d23c52

Please sign in to comment.