Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions app/api/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@
from typing import List, Dict, Any

from fastapi import Depends, APIRouter, HTTPException, status
from fastapi.encoders import jsonable_encoder
from fastapi.responses import JSONResponse
from geojson import Feature, FeatureCollection
from sqlalchemy.ext.asyncio import AsyncSession



from ..schemas.tree import (
StreetTreeResponse
)
from ..dependencies import get_session
from ..services.tree import (
get_tree_by_id
get_tree_by_id,
get_tree_by_species
)

from geoalchemy2.shape import to_shape
Expand Down Expand Up @@ -72,4 +77,24 @@ async def fetch_tree_by_id(
"registration_date": tree.registration_date.isoformat() if tree.registration_date else None,
"type": tree.type,
"geom": geojson_dict,
}
}

@route_street_tree.get(
'/species',
response_model=List,
tags=['Strassenbaeume'],
description='Retrieves street tree details based there species.'
)
async def fetch_tree_by_species(
session: AsyncSession = Depends(get_session)
):
rows = await get_tree_by_species(session)
response = jsonable_encoder(rows)

try:
return JSONResponse(content=response)
except IndexError:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail='Not found'
)
37 changes: 37 additions & 0 deletions app/services/tree.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.sql import text
from sqlmodel import select
from app.models.tree import StreetTreeRegister

Expand All @@ -10,3 +11,39 @@ async def get_tree_by_id(session: AsyncSession, tree_id: int):
row = result.scalars().first()

return row

async def get_tree_by_species(session: AsyncSession):
stmt = text('''
SELECT
tr.id,
tr.tree_number,
tr.street,
tr.species,
tr.type,
ROUND(ST_X(ST_Transform(tr.geom, 4326))::numeric, 6) AS lon,
ROUND(ST_Y(ST_Transform(tr.geom, 4326))::numeric, 6) AS lat,
CASE
WHEN tr.species ILIKE '%Tilia%' THEN 1
WHEN tr.species ILIKE '%Acer%' THEN 2
WHEN tr.species ILIKE '%Quercus%' THEN 3
WHEN tr.species ILIKE '%Fagus%' THEN 4
WHEN tr.species ILIKE '%Betula%' THEN 5
WHEN tr.species ILIKE '%Carpinus%' THEN 6
ELSE 0
END AS species_index
FROM flensburg.street_tree_register tr
WHERE tr.type = 'bestand'
AND NOT EXISTS (
SELECT 1
FROM flensburg.street_tree_register gef
WHERE gef.type = 'gefaellt'
AND gef.tree_number = tr.tree_number
AND gef.street = tr.street
)
ORDER BY tr.species
''')

result = await session.execute(stmt)
rows = result.mappings().all()

return [dict(row) for row in rows]
Loading