From 38b54338be246183740560f2f0ecc09a5d85db4a Mon Sep 17 00:00:00 2001 From: kshitijrajsharma Date: Fri, 5 Apr 2024 09:20:19 +0545 Subject: [PATCH] feat(countries): adds cid country get endpoint --- API/raw_data.py | 7 +++++++ src/app.py | 18 ++++++++++++++++++ src/query_builder/builder.py | 8 ++++++++ 3 files changed, 33 insertions(+) diff --git a/API/raw_data.py b/API/raw_data.py index 441def4c..78752b54 100644 --- a/API/raw_data.py +++ b/API/raw_data.py @@ -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): diff --git a/src/app.py b/src/app.py index 40a7b3a2..4489df75 100644 --- a/src/app.py +++ b/src/app.py @@ -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, @@ -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 diff --git a/src/query_builder/builder.py b/src/query_builder/builder.py index 73f6f5d9..211199ea 100644 --- a/src/query_builder/builder.py +++ b/src/query_builder/builder.py @@ -17,11 +17,14 @@ # 1100 13th Street NW Suite 800 Washington, D.C. 20005 # """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 @@ -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"