Skip to content

Commit

Permalink
Added st_within_feature
Browse files Browse the repository at this point in the history
  • Loading branch information
kshitijrajsharma committed Aug 25, 2023
1 parent ebbb640 commit 842293b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 72 deletions.
54 changes: 0 additions & 54 deletions API/raw_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,57 +444,3 @@ def get_countries(q: str = ""):
@version(1)
def get_osm_feature(osm_id: int):
return RawData().get_osm_feature(osm_id)


# @router.post("/snapshot/plain/", response_model=FeatureCollection)
# @version(1)
# def get_current_snapshot_as_plain_geojson(
# request: Request,
# params: SnapshotParamsPlain = Body(
# default={},
# examples={
# "normal": {
# "summary": "Example : Country Boundary",
# "description": "**Query** to extract administrative boundary of nepal in plain geojson format",
# "value": {
# "select": ["name"],
# "where": [
# {"key": "admin_level", "value": ["2"]},
# {"key": "boundary", "value": ["administrative"]},
# {"key": "name:en", "value": ["Nepal"]},
# ],
# "joinBy": "AND",
# "lookIn": ["relations"],
# },
# },
# "second": {
# "summary": "Example : City Boundary",
# "description": "**Query** to extract city bounadry in plain geojson format",
# "value": {
# "select": ["name"],
# "where": [
# {"key": "admin_level", "value": ["7"]},
# {"key": "boundary", "value": ["administrative"]},
# {"key": "name", "value": ["Pokhara"]},
# ],
# "joinBy": "AND",
# "lookIn": ["relations"],
# },
# },
# },
# ),
# ):
# """Simple API to get osm features as geojson for small region. This is designed only for querying small data for large data follow /snapshot/

# Params ::

# bbox: Optional List = takes xmin, ymin, xmax, ymax uses srid=4326
# select: List = this is select query you can pass [*] to select all attribute
# where: List[WhereCondition] = [{'key': 'building', 'value': ['*']},{'key':'amenity','value':['school','college']}]
# join_by: Optional[JoinFilterType] = or/ and
# look_in: Optional[List[OsmFeatureType]] = ["nodes", "ways_poly","ways_line","relations"] : tables name


# """
# result = RawData(params).extract_plain_geojson()
# return result
13 changes: 9 additions & 4 deletions src/query_builder/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ def get_query_as_geojson(query_list, ogr_export=None):
return final_query


def create_geom_filter(geom):
def create_geom_filter(geom, geom_lookup_by="ST_intersects"):
"""generates geometry intersection filter - Rawdata extraction"""
geometry_dump = dumps(dict(geom))
return f"""ST_intersects(ST_GEOMFROMGEOJSON('{geometry_dump}'), geom)"""
return f"""{geom_lookup_by}(geom,ST_GEOMFROMGEOJSON('{geometry_dump}'))"""


def format_file_name_str(input_str):
Expand Down Expand Up @@ -201,7 +201,10 @@ def extract_geometry_type_query(
):
"""used for specifically focused on export tool , this will generate separate queries for line point and polygon can be used on other datatype support - Rawdata extraction"""

geom_filter = create_geom_filter(params.geometry)
geom_filter = create_geom_filter(
params.geometry,
"ST_within" if params.use_st_within is True else "ST_intersects",
)
select_condition = f"""osm_id ,tags,changeset,timestamp , {'ST_Centroid(geom) as geom' if params.centroid else 'geom'}""" # this is default attribute that we will deliver to user if user defines his own attribute column then those will be appended with osm_id only
schema = {
"osm_id": "int64",
Expand Down Expand Up @@ -466,7 +469,9 @@ def raw_currentdata_extraction_query(
country_export=False,
):
"""Default function to support current snapshot extraction with all of the feature that export_tool_api has"""
geom_filter = f"""ST_intersects(ST_GEOMFROMGEOJSON('{geometry_dump}'), geom)"""

geom_lookup_by = "ST_within" if params.use_st_within is True else "ST_intersects"
geom_filter = create_geom_filter(params.geometry, geom_lookup_by)

base_query = []

Expand Down
4 changes: 4 additions & 0 deletions src/validation/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ class RawDataCurrentParams(BaseModel):
centroid: Optional[bool] = Field(
default=False, description="Exports centroid of features as geom"
)
use_st_within: Optional[bool] = Field(
default=False,
description="Exports features which are exactly inside the passed polygons (ST_WITHIN) By default features which are intersected with passed polygon is exported",
)
filters: Optional[Filters] = Field(
default=None,
example={
Expand Down
28 changes: 14 additions & 14 deletions tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,22 @@ def test_rawdata_current_snapshot_geometry_query():
from
nodes
where
ST_intersects(ST_GEOMFROMGEOJSON('{"coordinates": [[[84.92431640625, 27.766190642387496], [85.31982421875, 27.766190642387496], [85.31982421875, 28.02592458049937], [84.92431640625, 28.02592458049937], [84.92431640625, 27.766190642387496]]], "type": "Polygon"}'), geom) and (tags ->> 'amenity' IN ( 'shop' , 'toilet' ))) t0 UNION ALL select ST_AsGeoJSON(t1.*) from (select
ST_intersects(geom,ST_GEOMFROMGEOJSON('{"coordinates": [[[84.92431640625, 27.766190642387496], [85.31982421875, 27.766190642387496], [85.31982421875, 28.02592458049937], [84.92431640625, 28.02592458049937], [84.92431640625, 27.766190642387496]]], "type": "Polygon"}')) and (tags ->> 'amenity' IN ( 'shop' , 'toilet' ))) t0 UNION ALL select ST_AsGeoJSON(t1.*) from (select
osm_id ,version,tags,changeset,timestamp,geom
from
ways_line
where
ST_intersects(ST_GEOMFROMGEOJSON('{"coordinates": [[[84.92431640625, 27.766190642387496], [85.31982421875, 27.766190642387496], [85.31982421875, 28.02592458049937], [84.92431640625, 28.02592458049937], [84.92431640625, 27.766190642387496]]], "type": "Polygon"}'), geom)) t1 UNION ALL select ST_AsGeoJSON(t2.*) from (select
ST_intersects(geom,ST_GEOMFROMGEOJSON('{"coordinates": [[[84.92431640625, 27.766190642387496], [85.31982421875, 27.766190642387496], [85.31982421875, 28.02592458049937], [84.92431640625, 28.02592458049937], [84.92431640625, 27.766190642387496]]], "type": "Polygon"}'))) t1 UNION ALL select ST_AsGeoJSON(t2.*) from (select
osm_id ,version,tags,changeset,timestamp,geom
from
ways_poly
where
ST_intersects(ST_GEOMFROMGEOJSON('{"coordinates": [[[84.92431640625, 27.766190642387496], [85.31982421875, 27.766190642387496], [85.31982421875, 28.02592458049937], [84.92431640625, 28.02592458049937], [84.92431640625, 27.766190642387496]]], "type": "Polygon"}'), geom)) t2 UNION ALL select ST_AsGeoJSON(t3.*) from (select
ST_intersects(geom,ST_GEOMFROMGEOJSON('{"coordinates": [[[84.92431640625, 27.766190642387496], [85.31982421875, 27.766190642387496], [85.31982421875, 28.02592458049937], [84.92431640625, 28.02592458049937], [84.92431640625, 27.766190642387496]]], "type": "Polygon"}'))) t2 UNION ALL select ST_AsGeoJSON(t3.*) from (select
osm_id ,version,tags,changeset,timestamp,geom
from
relations
where
ST_intersects(ST_GEOMFROMGEOJSON('{"coordinates": [[[84.92431640625, 27.766190642387496], [85.31982421875, 27.766190642387496], [85.31982421875, 28.02592458049937], [84.92431640625, 28.02592458049937], [84.92431640625, 27.766190642387496]]], "type": "Polygon"}'), geom)) t3"""
ST_intersects(geom,ST_GEOMFROMGEOJSON('{"coordinates": [[[84.92431640625, 27.766190642387496], [85.31982421875, 27.766190642387496], [85.31982421875, 28.02592458049937], [84.92431640625, 28.02592458049937], [84.92431640625, 27.766190642387496]]], "type": "Polygon"}'))) t3"""
query_result = raw_currentdata_extraction_query(
validated_params,
g_id=None,
Expand Down Expand Up @@ -96,22 +96,22 @@ def test_rawdata_current_snapshot_normal_query():
from
nodes
where
ST_intersects(ST_GEOMFROMGEOJSON('{"coordinates": [[[84.92431640625, 27.766190642387496], [85.31982421875, 27.766190642387496], [85.31982421875, 28.02592458049937], [84.92431640625, 28.02592458049937], [84.92431640625, 27.766190642387496]]], "type": "Polygon"}'), geom)) t0 UNION ALL select ST_AsGeoJSON(t1.*) from (select
ST_intersects(geom,ST_GEOMFROMGEOJSON('{"coordinates": [[[84.92431640625, 27.766190642387496], [85.31982421875, 27.766190642387496], [85.31982421875, 28.02592458049937], [84.92431640625, 28.02592458049937], [84.92431640625, 27.766190642387496]]], "type": "Polygon"}'))) t0 UNION ALL select ST_AsGeoJSON(t1.*) from (select
osm_id ,version,tags,changeset,timestamp,geom
from
ways_line
where
ST_intersects(ST_GEOMFROMGEOJSON('{"coordinates": [[[84.92431640625, 27.766190642387496], [85.31982421875, 27.766190642387496], [85.31982421875, 28.02592458049937], [84.92431640625, 28.02592458049937], [84.92431640625, 27.766190642387496]]], "type": "Polygon"}'), geom)) t1 UNION ALL select ST_AsGeoJSON(t2.*) from (select
ST_intersects(geom,ST_GEOMFROMGEOJSON('{"coordinates": [[[84.92431640625, 27.766190642387496], [85.31982421875, 27.766190642387496], [85.31982421875, 28.02592458049937], [84.92431640625, 28.02592458049937], [84.92431640625, 27.766190642387496]]], "type": "Polygon"}'))) t1 UNION ALL select ST_AsGeoJSON(t2.*) from (select
osm_id ,version,tags,changeset,timestamp,geom
from
ways_poly
where
ST_intersects(ST_GEOMFROMGEOJSON('{"coordinates": [[[84.92431640625, 27.766190642387496], [85.31982421875, 27.766190642387496], [85.31982421875, 28.02592458049937], [84.92431640625, 28.02592458049937], [84.92431640625, 27.766190642387496]]], "type": "Polygon"}'), geom)) t2 UNION ALL select ST_AsGeoJSON(t3.*) from (select
ST_intersects(geom,ST_GEOMFROMGEOJSON('{"coordinates": [[[84.92431640625, 27.766190642387496], [85.31982421875, 27.766190642387496], [85.31982421875, 28.02592458049937], [84.92431640625, 28.02592458049937], [84.92431640625, 27.766190642387496]]], "type": "Polygon"}'))) t2 UNION ALL select ST_AsGeoJSON(t3.*) from (select
osm_id ,version,tags,changeset,timestamp,geom
from
relations
where
ST_intersects(ST_GEOMFROMGEOJSON('{"coordinates": [[[84.92431640625, 27.766190642387496], [85.31982421875, 27.766190642387496], [85.31982421875, 28.02592458049937], [84.92431640625, 28.02592458049937], [84.92431640625, 27.766190642387496]]], "type": "Polygon"}'), geom)) t3"""
ST_intersects(geom,ST_GEOMFROMGEOJSON('{"coordinates": [[[84.92431640625, 27.766190642387496], [85.31982421875, 27.766190642387496], [85.31982421875, 28.02592458049937], [84.92431640625, 28.02592458049937], [84.92431640625, 27.766190642387496]]], "type": "Polygon"}'))) t3"""
query_result = raw_currentdata_extraction_query(
validated_params,
g_id=None,
Expand Down Expand Up @@ -148,22 +148,22 @@ def test_attribute_filter_rawdata():
from
ways_line
where
ST_intersects(ST_GEOMFROMGEOJSON('{"coordinates": [[[83.502574, 27.569073], [83.502574, 28.332758], [85.556417, 28.332758], [85.556417, 27.569073], [83.502574, 27.569073]]], "type": "Polygon"}'), geom) and (tags ->> 'building' = 'yes')) t0 UNION ALL select ST_AsGeoJSON(t1.*) from (select
ST_intersects(geom,ST_GEOMFROMGEOJSON('{"coordinates": [[[83.502574, 27.569073], [83.502574, 28.332758], [85.556417, 28.332758], [85.556417, 27.569073], [83.502574, 27.569073]]], "type": "Polygon"}')) and (tags ->> 'building' = 'yes')) t0 UNION ALL select ST_AsGeoJSON(t1.*) from (select
osm_id , tags ->> 'name' as name , geom
from
relations
where
ST_intersects(ST_GEOMFROMGEOJSON('{"coordinates": [[[83.502574, 27.569073], [83.502574, 28.332758], [85.556417, 28.332758], [85.556417, 27.569073], [83.502574, 27.569073]]], "type": "Polygon"}'), geom) and (tags ->> 'building' = 'yes') and (geometrytype(geom)='MULTILINESTRING')) t1 UNION ALL select ST_AsGeoJSON(t2.*) from (select
ST_intersects(geom,ST_GEOMFROMGEOJSON('{"coordinates": [[[83.502574, 27.569073], [83.502574, 28.332758], [85.556417, 28.332758], [85.556417, 27.569073], [83.502574, 27.569073]]], "type": "Polygon"}')) and (tags ->> 'building' = 'yes') and (geometrytype(geom)='MULTILINESTRING')) t1 UNION ALL select ST_AsGeoJSON(t2.*) from (select
osm_id ,version,tags,changeset,timestamp,geom
from
ways_poly
where
(grid = 1187 OR grid = 1188) and (ST_intersects(ST_GEOMFROMGEOJSON('{"coordinates": [[[83.502574, 27.569073], [83.502574, 28.332758], [85.556417, 28.332758], [85.556417, 27.569073], [83.502574, 27.569073]]], "type": "Polygon"}'), geom)) and (tags ->> 'building' = 'yes')) t2 UNION ALL select ST_AsGeoJSON(t3.*) from (select
(grid = 1187 OR grid = 1188) and (ST_intersects(geom,ST_GEOMFROMGEOJSON('{"coordinates": [[[83.502574, 27.569073], [83.502574, 28.332758], [85.556417, 28.332758], [85.556417, 27.569073], [83.502574, 27.569073]]], "type": "Polygon"}'))) and (tags ->> 'building' = 'yes')) t2 UNION ALL select ST_AsGeoJSON(t3.*) from (select
osm_id ,version,tags,changeset,timestamp,geom
from
relations
where
ST_intersects(ST_GEOMFROMGEOJSON('{"coordinates": [[[83.502574, 27.569073], [83.502574, 28.332758], [85.556417, 28.332758], [85.556417, 27.569073], [83.502574, 27.569073]]], "type": "Polygon"}'), geom) and (tags ->> 'building' = 'yes') and (geometrytype(geom)='POLYGON' or geometrytype(geom)='MULTIPOLYGON')) t3"""
ST_intersects(geom,ST_GEOMFROMGEOJSON('{"coordinates": [[[83.502574, 27.569073], [83.502574, 28.332758], [85.556417, 28.332758], [85.556417, 27.569073], [83.502574, 27.569073]]], "type": "Polygon"}')) and (tags ->> 'building' = 'yes') and (geometrytype(geom)='POLYGON' or geometrytype(geom)='MULTIPOLYGON')) t3"""
query_result = raw_currentdata_extraction_query(
validated_params,
g_id=[[1187], [1188]],
Expand Down Expand Up @@ -221,12 +221,12 @@ def test_and_filters():
from
ways_poly
where
ST_intersects(ST_GEOMFROMGEOJSON('{"coordinates": [[[36.70588085657477, 37.1979648807274], [36.70588085657477, 37.1651408422983], [36.759267544807194, 37.1651408422983], [36.759267544807194, 37.1979648807274], [36.70588085657477, 37.1979648807274]]], "type": "Polygon"}'), geom) and (tags ->> 'destroyed:building' = 'yes' AND tags ->> 'damage:date' = '2023-02-06')) t0 UNION ALL select ST_AsGeoJSON(t1.*) from (select
ST_intersects(geom,ST_GEOMFROMGEOJSON('{"coordinates": [[[36.70588085657477, 37.1979648807274], [36.70588085657477, 37.1651408422983], [36.759267544807194, 37.1651408422983], [36.759267544807194, 37.1979648807274], [36.70588085657477, 37.1979648807274]]], "type": "Polygon"}')) and (tags ->> 'destroyed:building' = 'yes' AND tags ->> 'damage:date' = '2023-02-06')) t0 UNION ALL select ST_AsGeoJSON(t1.*) from (select
osm_id , tags ->> 'building' as building , tags ->> 'destroyed:building' as destroyed_building , tags ->> 'damage:date' as damage_date , tags ->> 'name' as name , tags ->> 'source' as source , geom
from
relations
where
ST_intersects(ST_GEOMFROMGEOJSON('{"coordinates": [[[36.70588085657477, 37.1979648807274], [36.70588085657477, 37.1651408422983], [36.759267544807194, 37.1651408422983], [36.759267544807194, 37.1979648807274], [36.70588085657477, 37.1979648807274]]], "type": "Polygon"}'), geom) and (tags ->> 'destroyed:building' = 'yes' AND tags ->> 'damage:date' = '2023-02-06') and (geometrytype(geom)='POLYGON' or geometrytype(geom)='MULTIPOLYGON')) t1"""
ST_intersects(geom,ST_GEOMFROMGEOJSON('{"coordinates": [[[36.70588085657477, 37.1979648807274], [36.70588085657477, 37.1651408422983], [36.759267544807194, 37.1651408422983], [36.759267544807194, 37.1979648807274], [36.70588085657477, 37.1979648807274]]], "type": "Polygon"}')) and (tags ->> 'destroyed:building' = 'yes' AND tags ->> 'damage:date' = '2023-02-06') and (geometrytype(geom)='POLYGON' or geometrytype(geom)='MULTIPOLYGON')) t1"""
query_result = raw_currentdata_extraction_query(
validated_params,
g_id=None,
Expand Down

0 comments on commit 842293b

Please sign in to comment.