You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
desregistradores/server/src/api/main.py

100 lines
2.8 KiB

# BUILT-INS
import json
# VENDOR
from fastapi import FastAPI, UploadFile, File
import aiofiles
import spatialite
app = FastAPI()
@app.get("/")
async def index():
return {"message": "Beinvguda al portal de les desregistradores"}
@app.get("/districte/{id}")
async def get_districte(id: int):
pass
@app.get("/districtes/{west}/{north}/{east}/{south}")
async def get_districtes(west: float, north: float, east: float, south: float):
db = spatialite.connect('desregistradores.sqlite')
cur = db.execute('''SELECT id, districte, nom, AsGeoJSON(wkb_geometry)
FROM districtes
WHERE Intersects(wkb_geometry, GeomFromText('POLYGON ((
{east} {north},
{east} {south},
{west} {south},
{west} {north},
{east} {north}
))', 4326));'''.format(west=west, north=north, east=east, south=south))
features = [{
"type": "Feature",
"geometry": json.loads(row[3]),
"properties": {"id": row[0], "districte": row[1], "nom": row[2]}
} for row in cur.fetchall()]
return {"type": "FeatureCollection", "features": features}
@app.get("/barri/{id}")
async def get_barri(id: int):
pass
@app.get("/barris/{west}/{north}/{east}/{south}")
async def get_barris(west: float, north: float, east: float, south: float):
db = spatialite.connect('desregistradores.sqlite')
cur = db.execute('''SELECT id, districte, nom, AsGeoJSON(wkb_geometry)
FROM barris
WHERE Intersects(wkb_geometry, GeomFromText('POLYGON ((
{east} {north},
{east} {south},
{west} {south},
{west} {north},
{east} {north}
))', 4326));'''.format(west=west, north=north, east=east, south=south))
features = [{
"type": "Feature",
"geometry": json.loads(row[3]),
"properties": {"id": row[0], "districte": row[1], "nom": row[2]}
} for row in cur.fetchall()]
return {"type": "FeatureCollection", "features": features}
@app.get("/parcele/{id}")
async def get_parcele(id: int):
pass
@app.get('/parceles/{west}/{north}/{east}/{south}')
async def get_parceles(west: float, north: float, east: float, south: float):
db = spatialite.connect('desregistradores.sqlite')
cur = db.execute('''SELECT id, refcat, AsGeoJSON(wkb_geometry)
FROM parceles
WHERE Intersects(wkb_geometry, GeomFromText('POLYGON((
{east} {north},
{east} {south},
{west} {south},
{west} {north},
{east} {north}
))', 4326));'''.format(west=west, north=north, east=east, south=south))
features = [{
"type": "Feature",
"geometry": json.loads(row[2]),
"properties": {"id": row[0], "refcat": row[1]}
} for row in cur.fetchall()]
return {"type": "FeatureCollection", "features": features}
@app.post("/upload")
async def upload(file: UploadFile = File(...)):
return {"filename": file.filename}