Skip to content

Commit

Permalink
fix: clip to boundary
Browse files Browse the repository at this point in the history
  • Loading branch information
rsavoye committed Oct 5, 2023
1 parent a1d0943 commit d0fd5c9
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions osm_rawdata/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def __init__(self,
self.url = "https://raw-data-api0.hotosm.org/v1"
self.headers = {"accept": "application/json", "Content-Type": "application/json"}
else:
log.info("Opening database connection to: %s" % self.uri['dbhost'])
log.info(f"Opening database connection to: {self.uri['dbname']}")
connect = "PG: dbname=" + self.uri['dbname']
if 'dbname' in self.uri and self.uri['dbname'] is not None:
connect = f"dbname={self.uri['dbname']}"
Expand Down Expand Up @@ -250,7 +250,7 @@ def createSQL(self,
allgeom (bool): Whether to return centroids or all the full geometry
Returns:
query (FeatureCollection): the json
(FeatureCollection): the json
"""
sql = list()
query = ""
Expand Down Expand Up @@ -343,14 +343,16 @@ def queryLocal(self,
query (FeatureCollection): the results of the query
"""
features = list()
sql = f"DROP VIEW IF EXISTS ways_view;CREATE VIEW ways_view AS SELECT * FROM ways_poly WHERE ST_CONTAINS(ST_GeomFromEWKT('SRID=4326;{boundary.wkt}'), geom)"
self.dbcursor.execute(sql)
sql = f"DROP VIEW IF EXISTS nodes_view;CREATE VIEW nodes_view AS SELECT * FROM nodes WHERE ST_CONTAINS(ST_GeomFromEWKT('SRID=4326;{boundary.wkt}'), geom)"
self.dbcursor.execute(sql)
sql = f"DROP VIEW IF EXISTS lines_view;CREATE VIEW lines_view AS SELECT * FROM ways_line WHERE ST_CONTAINS(ST_GeomFromEWKT('SRID=4326;{boundary.wkt}'), geom)"
self.dbcursor.execute(sql)
sql = f"DROP VIEW IF EXISTS relations_view;CREATE TEMP VIEW relations_view AS SELECT * FROM nodes WHERE ST_CONTAINS(ST_GeomFromEWKT('SRID=4326;{boundary.wkt}'), geom)"
self.dbcursor.execute(sql)
# if no boundary, it's already been setup
if boundary:
sql = f"DROP VIEW IF EXISTS ways_view;CREATE VIEW ways_view AS SELECT * FROM ways_poly WHERE ST_CONTAINS(ST_GeomFromEWKT('SRID=4326;{boundary.wkt}'), geom)"
self.dbcursor.execute(sql)
sql = f"DROP VIEW IF EXISTS nodes_view;CREATE VIEW nodes_view AS SELECT * FROM nodes WHERE ST_CONTAINS(ST_GeomFromEWKT('SRID=4326;{boundary.wkt}'), geom)"
self.dbcursor.execute(sql)
sql = f"DROP VIEW IF EXISTS lines_view;CREATE VIEW lines_view AS SELECT * FROM ways_line WHERE ST_CONTAINS(ST_GeomFromEWKT('SRID=4326;{boundary.wkt}'), geom)"
self.dbcursor.execute(sql)
sql = f"DROP VIEW IF EXISTS relations_view;CREATE TEMP VIEW relations_view AS SELECT * FROM nodes WHERE ST_CONTAINS(ST_GeomFromEWKT('SRID=4326;{boundary.wkt}'), geom)"
self.dbcursor.execute(sql)

if query.find(" ways_poly ") > 0:
query = query.replace("ways_poly", "ways_view")
Expand Down

0 comments on commit d0fd5c9

Please sign in to comment.