Skip to content

Commit

Permalink
Merge pull request #247 from hotosm/feature/country_fetch_cid
Browse files Browse the repository at this point in the history
Feature : Fetch Countries by cid
  • Loading branch information
kshitijrajsharma authored Apr 5, 2024
2 parents 6cdfc61 + 38b5433 commit 9221be6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
7 changes: 7 additions & 0 deletions API/raw_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,13 @@ def get_countries(q: str = ""):
return result


@router.get("/countries/{cid}/")
@version(1)
def get_specific_country(cid: int):
result = RawData().get_country(cid)
return result


@router.get("/osm_id/")
@version(1)
def get_osm_feature(osm_id: int):
Expand Down
18 changes: 18 additions & 0 deletions src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
extract_geometry_type_query,
generate_polygon_stats_graphql_query,
get_countries_query,
get_country_cid,
get_country_from_iso,
get_country_geom_from_iso,
get_osm_feature_query,
Expand Down Expand Up @@ -847,6 +848,23 @@ def get_countries_list(self, q):
self.cur.close()
return FeatureCollection(features=features)

def get_country(self, q):
"""Gets specific country from the database
Args:
cid (_type_): country cid
Returns:
featurecollection: geojson of country
"""
query = get_country_cid(q)
self.cur.execute(query)
get_fetched = self.cur.fetchall()
self.cur.close()
if len(get_fetched) < 1:
return "Not found"
return orjson.loads(get_fetched[0][0])

def get_osm_feature(self, osm_id):
"""Returns geometry of osm_id in geojson
Expand Down
8 changes: 8 additions & 0 deletions src/query_builder/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@
# 1100 13th Street NW Suite 800 Washington, D.C. 20005
# <[email protected]>
"""Page Contains Query logic required for application"""
# Standard library imports
import re
from json import dumps, loads

# Third party imports
from geomet import wkt

# Reader imports
from src.config import USE_DUCK_DB_FOR_CUSTOM_EXPORTS
from src.config import logger as logging
from src.validation.models import SupportedFilters, SupportedGeometryFilters
Expand Down Expand Up @@ -777,6 +780,11 @@ def get_countries_query(q):
return query


def get_country_cid(cid):
query = f"Select ST_AsGeoJSON(cf.*) FROM countries cf where cid = {cid}"
return query


def get_osm_feature_query(osm_id):
select_condition = (
"osm_id, tableoid::regclass AS osm_type, tags,changeset,timestamp,geom"
Expand Down

0 comments on commit 9221be6

Please sign in to comment.