-
Notifications
You must be signed in to change notification settings - Fork 2
/
api_habitations.py
43 lines (32 loc) · 1.34 KB
/
api_habitations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# api_habitations
import os, time, json
from typing import Optional, List
from pydantic import BaseModel
from fastapi.responses import FileResponse
from fastapi import HTTPException, Header, Path
import pandas as pd
from geosadak_api_launch import app
import commonfuncs as cf
import dbconnect
from globalvars import logIP
#########
@app.get("/API/habitations", tags=["habitations"])
def habitations(STATE_ID: str, BLOCK_ID: str, DISTRICT_ID: Optional[str]=None, interservice:Optional[bool] = False, X_Forwarded_For: Optional[str] = Header(None)):
if not interservice: logIP(X_Forwarded_For, 'habitations', limit=10)
whereParams = []
whereParams.append(f""" "STATE_ID" = '{STATE_ID}' """)
if DISTRICT_ID:
whereParams.append(f""" "DISTRICT_I" = '{DISTRICT_ID}' """)
whereParams.append(f""" "BLOCK_ID" = '{BLOCK_ID}' """)
s1 = f"""select id, "HAB_ID", "STATE_ID", "DISTRICT_I","BLOCK_ID", "HAB_NAME", "TOT_POPULA",
ST_Y(geometry) as latitude,
ST_X(geometry) as longitude
from habitation
where {' AND '.join(whereParams)}
order by "HAB_NAME"
"""
df1 = dbconnect.makeQuery(s1, output='df', keepCols=True)
df1['latitude'] = df1['latitude'].apply(lambda x: round(x,5))
df1['longitude'] = df1['longitude'].apply(lambda x: round(x,5))
returnD = {'data': df1.to_csv(index=False)}
return returnD