Skip to content

Commit

Permalink
Seismic + fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
elucherini committed Dec 23, 2024
1 parent 8346376 commit 6bf3535
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
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()
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

0 comments on commit 6bf3535

Please sign in to comment.