diff --git a/API/raw_data.py b/API/raw_data.py index 015c9e91..6f28b1ab 100644 --- a/API/raw_data.py +++ b/API/raw_data.py @@ -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 diff --git a/src/query_builder/builder.py b/src/query_builder/builder.py index 924a1745..056c840b 100644 --- a/src/query_builder/builder.py +++ b/src/query_builder/builder.py @@ -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): @@ -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", @@ -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 = [] diff --git a/src/validation/models.py b/src/validation/models.py index 0329f8fe..adbe06db 100644 --- a/src/validation/models.py +++ b/src/validation/models.py @@ -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={ diff --git a/tests/test_app.py b/tests/test_app.py index 8ba906f2..585d9898 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -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, @@ -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, @@ -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]], @@ -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,