Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create APIs for address risk #131

Merged
merged 9 commits into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,7 @@ node_modules
# Sensitive config
.env.development.local
.env
.env.local
.env.local

# IDEs
.vscode/
24 changes: 24 additions & 0 deletions backend/api/routers/liquefaction_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from fastapi import Depends, HTTPException, APIRouter
from ..tags import Tags
from sqlalchemy.orm import Session
from geoalchemy2 import functions as geo_func
from backend.database.session import get_db
from ..schemas.liquefaction_schemas import (
LiquefactionFeature,
Expand Down Expand Up @@ -41,3 +42,26 @@ async def get_liquefaction_zones(db: Session = Depends(get_db)):
LiquefactionFeature.from_sqlalchemy_model(zone) for zone in liquefaction_zones
]
return LiquefactionFeatureCollection(type="FeatureCollection", features=features)


@router.get("/is-in-liquefaction-zone", response_model=bool)
async def is_in_liquefaction_zone(
lat: float, lon: float, db: Session = Depends(get_db)
):
"""
Check if a point is in a liquefaction zone.

Args:
lat (float): Latitude of the point.
lon (float): Longitude of the point.
db (Session): The database session dependency.

Returns:
bool: True if the point is in a liquefaction zone, False otherwise.
"""
query = db.query(LiquefactionZone).filter(
LiquefactionZone.geometry.ST_Contains(
geo_func.ST_SetSRID(geo_func.ST_GeomFromText(f"POINT({lon} {lat})"), 4326)
)
)
return db.query(query.exists()).scalar()
22 changes: 22 additions & 0 deletions backend/api/routers/seismic_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from fastapi import Depends, HTTPException, APIRouter
from ..tags import Tags
from sqlalchemy.orm import Session
from geoalchemy2 import functions as geo_func
from backend.database.session import get_db
from ..schemas.seismic_schemas import (
SeismicFeature,
Expand Down Expand Up @@ -39,3 +40,24 @@ async def get_seismic_hazard_zones(db: Session = Depends(get_db)):

features = [SeismicFeature.from_sqlalchemy_model(zone) for zone in seismic_zones]
return SeismicFeatureCollection(type="FeatureCollection", features=features)


@router.get("/is-in-seismic-zone", response_model=bool)
async def is_in_seismic_zone(lat: float, lon: float, db: Session = Depends(get_db)):
"""
Check if a point is in a liquefaction zone.

Args:
lat (float): Latitude of the point.
lon (float): Longitude of the point.
db (Session): The database session dependency.

Returns:
bool: True if the point is in a liquefaction zone, False otherwise.
"""
query = db.query(SeismicHazardZone).filter(
SeismicHazardZone.geometry.ST_Contains(
geo_func.ST_SetSRID(geo_func.ST_GeomFromText(f"POINT({lon} {lat})"), 4326)
)
)
return db.query(query.exists()).scalar()
20 changes: 20 additions & 0 deletions backend/api/routers/soft_story_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from ..tags import Tags
from sqlalchemy.orm import Session
from backend.database.session import get_db
from geoalchemy2 import functions as geo_func
from backend.api.schemas.soft_story_schemas import (
SoftStoryFeature,
SoftStoryFeatureCollection,
Expand Down Expand Up @@ -39,3 +40,22 @@ async def get_soft_stories(db: Session = Depends(get_db)):

features = [SoftStoryFeature.from_sqlalchemy_model(story) for story in soft_stories]
return SoftStoryFeatureCollection(type="FeatureCollection", features=features)


@router.get("/is-soft-story", response_model=bool)
async def is_soft_story(lat: float, lon: float, db: Session = Depends(get_db)):
"""
Check if a point is a soft story property.

Args:
lat (float): Latitude of the point.
lon (float): Longitude of the point.
db (Session): The database session dependency.

Returns:
bool: True if the point is a soft story property, False otherwise.
"""
query = db.query(SoftStoryProperty).filter(
SoftStoryProperty.point == geo_func.ST_GeomFromText(f"POINT({lon} {lat})", 4326)
)
return db.query(query.exists()).scalar()
22 changes: 22 additions & 0 deletions backend/api/routers/tsunami_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from fastapi import Depends, HTTPException, APIRouter
from ..tags import Tags
from sqlalchemy.orm import Session
from geoalchemy2 import functions as geo_func
from backend.database.session import get_db
from backend.api.schemas.tsunami_schemas import TsunamiFeature, TsunamiFeatureCollection
from backend.api.models.tsunami import TsunamiZone
Expand Down Expand Up @@ -38,3 +39,24 @@ async def get_tsunami_zones(db: Session = Depends(get_db)):
raise HTTPException(status_code=404, detail="No tsunami zones found")
features = [TsunamiFeature.from_sqlalchemy_model(zone) for zone in tsunami_zones]
return TsunamiFeatureCollection(type="FeatureCollection", features=features)


@router.get("/is-in-tsunami-zone", response_model=bool)
async def is_in_tsunami_zone(lat: float, lon: float, db: Session = Depends(get_db)):
"""
Check if a point is in a tsunami zone.

Args:
lat (float): Latitude of the point.
lon (float): Longitude of the point.
db (Session): The database session dependency.

Returns:
bool: True if the point is in a tsunami zone, False otherwise.
"""
query = db.query(TsunamiZone).filter(
TsunamiZone.geometry.ST_Contains(
geo_func.ST_SetSRID(geo_func.ST_GeomFromText(f"POINT({lon} {lat})"), 4326)
)
)
return db.query(query.exists()).scalar()
31 changes: 31 additions & 0 deletions backend/api/tests/test_liquefaction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""
Test the API of liquefaction_api.py.
"""

import pytest
from fastapi.testclient import TestClient

# Will the .. be stable?
from ..main import app


@pytest.fixture
def client():
return TestClient(app)


def test_is_in_liquefaction_zone(client):
lat, lon = [37.779759, -122.407436]
response = client.get(
f"/api/liquefaction-zones/is-in-liquefaction-zone?lat={lat}&lon={lon}"
)
assert response.status_code == 200
assert response.json() # True

# These should not be in liquefaction zones
wrong_lat, wrong_lon = [0.0, 0.0]
response = client.get(
f"/api/liquefaction-zones/is-in-liquefaction-zone?lat={wrong_lat}&lon={wrong_lon}"
)
assert response.status_code == 200
assert not response.json() # False
15 changes: 15 additions & 0 deletions backend/api/tests/test_seismic.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,18 @@ def test_get_seismic_hazard_zones(client):
print(response_dict)
assert response.status_code == 200
assert len(response_dict) == 2


def test_is_in_seismic_zone(client):
lat, lon = [37.779759, -122.407436]
response = client.get(f"/api/seismic-zones/is-in-seismic-zone?lat={lat}&lon={lon}")
assert response.status_code == 200
assert response.json() # True

# These should not be in a seismic hazard zone
wrong_lat, wrong_lon = [0.0, 0.0]
response = client.get(
f"/api/seismic-zones/is-in-seismic-zone?lat={wrong_lat}&lon={wrong_lon}"
)
assert response.status_code == 200
assert not response.json() # False
16 changes: 15 additions & 1 deletion backend/api/tests/test_soft_story.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

# Will the .. be stable?
from ..main import app
from ..schemas.geo import Polygon


@pytest.fixture
Expand Down Expand Up @@ -41,3 +40,18 @@ def test_get_soft_story(client):
assert response.status_code == 200
# Temporary guaranteed failure until test is written
assert False


def test_is_soft_story(client):
lat, lon = [-122.446575165, 37.766034349]
response = client.get(f"/api/soft-story/is-soft-story?lat={lat}&lon={lon}")
assert response.status_code == 200
assert response.json() # True

# These should not be soft stories
wrong_lat, wrong_lon = [0.0, 0.0]
response = client.get(
f"/api/soft-story/is-soft-story?lat={wrong_lat}&lon={wrong_lon}"
)
assert response.status_code == 200
assert not response.json() # False
16 changes: 12 additions & 4 deletions backend/api/tests/test_tsunami.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,16 @@ def test_get_tsunami_polygon(client):
assert False


def test_get_tsunami_risk(client):
response = client.get("/api/tsunami-risk/addresss")
def test_is_in_tsunami_zone(client):
lat, lon = [37.759039, -122.509515]
response = client.get(f"/api/tsunami/is-in-tsunami-zone?lat={lat}&lon={lon}")
assert response.status_code == 200
# Temporary guaranteed failure until test is written
assert False
assert response.json() # True

# These should not be in our tsunami zone
wrong_lat, wrong_lon = [0.0, 0.0]
response = client.get(
f"/api/tsunami/is-in-tsunami-zone?lat={wrong_lat}&lon={wrong_lon}"
)
assert response.status_code == 200
assert not response.json() # False
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pandas==2.2.3
pathspec==0.12.1
platformdirs==4.3.6
pluggy==1.5.0
pre_commit==4.0.1
psycopg2-binary==2.9.9
pydantic==2.9.0
pydantic-settings==2.5.2
Expand Down
Loading