Skip to content

Commit

Permalink
feat: coordonnees et zones dans les parametres
Browse files Browse the repository at this point in the history
  • Loading branch information
dhdaines committed Feb 19, 2024
1 parent 2bc3f3e commit 33930b9
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 12 deletions.
40 changes: 33 additions & 7 deletions js/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,6 @@ function updateInfo(info, coords) {
maxZoom: 16,
duration: 500,
});
const [lon, lat] = coords;
history.replaceState(null, "", "?g=" + encodeURIComponent(`${lat},${lon}`));
}

function infoError(txt: string) {
Expand All @@ -150,6 +148,7 @@ async function getInfo(coords: GeoJSONPosition) {
if (response.ok) {
const info = await response.json();
updateInfo(info, coords);
history.replaceState(null, "", "?g=" + encodeURIComponent(`${lat},${lon}`));
}
else if (response.status == 404) {
infoError(`L'endroit choisi ne se situe pas à Sainte-Adèle. Veuillez réessayer.`);
Expand All @@ -161,6 +160,24 @@ endroit à proximité.`);
}
}

async function getZone(zone: string) {
const url = `${ZONALDA_API_URL}/z/${zone}`;
const response = await fetch(url);
if (response.ok) {
const info = await response.json();
updateInfo(info, info.point);
history.replaceState(null, "", "?z=" + encodeURIComponent(zone))
return info.point;
}
else if (response.status == 404) {
infoError(`La zone ${zone} n'existe pas. Veuillez réessayer.`);
}
else {
infoError(`Les informations n’ont pu être trouvées pour l'endroit choisi à cause
d'un problème avec la base géomatique.`);
}
}

autocomplete.on('select', async (location: GeoJSONFeature) => {
const pos = location.geometry as GeoJSONPoint;
source.clear(true);
Expand Down Expand Up @@ -212,13 +229,22 @@ window.addEventListener("load", async () => {
zonage = metadata.zonage;
}
const urlParams = new URLSearchParams(window.location.search);
const centroid = urlParams.get("g");
let centroid = null;
const gps = urlParams.get("g");
const zone = urlParams.get("z");
if (gps !== null) {
const [lat, lon] = gps.split(",").map(parseFloat);
centroid = [lon, lat];
getInfo(centroid);
}
else if (zone !== null) {
centroid = await getZone(zone);
console.log(centroid);
}
if (centroid !== null) {
const [lat, lon] = centroid.split(",").map(parseFloat);
const coords = [lon, lat];
const projPos = fromLonLat(coords);
const projPos = fromLonLat(centroid);
source.clear(true);
source.addFeature(new Feature(new Point(projPos)));
view.animate({center: projPos});
getInfo(coords);
}
});
21 changes: 16 additions & 5 deletions zonalda/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from pandas import Series
from shapely import Point # type: ignore

VERSION = "0.0.1"
VERSION = "0.0.2"
THISDIR = Path(__file__).parent
LOGGER = logging.getLogger("zonalda")

Expand All @@ -27,12 +27,23 @@ class Zonalda:
def __init__(self):
self.ville = geopandas.read_file(THISDIR / "sainte-adele.geojson").iloc[0]
self.districts = geopandas.read_file(THISDIR / "districts.geojson")
self.zonage = geopandas.read_file(THISDIR / "zonage.geojson")
zonage = geopandas.read_file(THISDIR / "zonage.geojson")
self.zonage = zonage.assign(ZONE=zonage["ZONE"].str.replace(" ", ""))
self.collectes = geopandas.read_file(THISDIR / "collectes.geojson")

def __call__(
self, latitude: float, longitude: float
) -> tuple[Series | None, Series | None, Series | None,]:
def __getitem__(self, name: str) -> tuple[float, float]:
"""Trouver le centroïde d'une zone par son nom."""
zones = self.zonage.loc[self.zonage["ZONE"] == name]
if zones.empty:
raise KeyError("Zone not found: %s" % name)
c = zones.iloc[0].geometry.centroid
return c.y, c.x

def __call__(self, latitude: float, longitude: float) -> tuple[
Series | None,
Series | None,
Series | None,
]:
"""Chercher les informations citoyennes pour un emplacement."""
p = Point(longitude, latitude)
if not self.ville.geometry.contains(p):
Expand Down
21 changes: 21 additions & 0 deletions zonalda/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,13 @@ class Emplacement(BaseModel):
district: District | None
collecte: Collecte | None
zone: Zone | None
point: tuple[float, float]

@classmethod
def from_wgs84(self, latitude: float, longitude: float) -> "Emplacement":
"""
Chercher les informations géomatiques pour un emplacement GPS.
"""
district, zone, collecte = zonalda(latitude, longitude)
return self(
district=District(numero=district["id"], conseiller=district["Conseiller"])
Expand All @@ -68,8 +72,17 @@ def from_wgs84(self, latitude: float, longitude: float) -> "Emplacement":
)
if zone is not None
else None,
point=(longitude, latitude),
)

@classmethod
def from_zone(self, zone: str) -> "Emplacement":
"""
Localiser le centroïde d'une zone et retourner les autres informations.
"""
latitude, longitude = zonalda[zone]
return self.from_wgs84(latitude, longitude)


@api.exception_handler(MunicipalityError)
async def municipality_error_handler(request, exc):
Expand All @@ -81,6 +94,14 @@ async def ll(latitude: float, longitude: float):
return Emplacement.from_wgs84(latitude, longitude)


@api.get("/z/{zone}")
async def zz(zone: str):
try:
return Emplacement.from_zone(zone)
except KeyError:
return None


GEOAPIFY_URL = "https://api.geoapify.com/v1/geocode/autocomplete"
client = httpx.AsyncClient()

Expand Down

0 comments on commit 33930b9

Please sign in to comment.