Skip to content

Commit

Permalink
fix: make zone lookup reliable
Browse files Browse the repository at this point in the history
  • Loading branch information
dhdaines committed Jul 10, 2024
1 parent c1c62c8 commit 3f171db
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 11 deletions.
25 changes: 21 additions & 4 deletions zonalda/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,30 @@ def __init__(self):
self.zonage = zonage.assign(ZONE=zonage["ZONE"].str.replace(" ", ""))
self.collectes = geopandas.read_file(THISDIR / "collectes.geojson")

def __getitem__(self, name: str) -> tuple[float, float]:
"""Trouver le centroïde d'une zone par son nom."""
def __getitem__(self, name: str) -> tuple[
Series | None,
Series | None,
Series | None,
]:
"""Trouver les informations pour une zone."""
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
zone = zones.iloc[0]
district, collecte = None, None
districts = self.districts.loc[self.districts.contains(zone.geometry)]
if len(districts) > 1:
LOGGER.warning("Plusieurs districts trouvé pour %s: %s", name, districts)
if len(districts):
district = districts.iloc[0]
collectes = self.collectes.loc[self.collectes.contains(zone.geometry)]
if len(collectes) > 1:
LOGGER.warning(
"Plusieurs zones de collectes trouvé pour %s: %s", name, collectes
)
if len(collectes):
collecte = collectes.iloc[0]
return district, zone, collecte

def __call__(self, latitude: float, longitude: float) -> tuple[
Series | None,
Expand Down
33 changes: 26 additions & 7 deletions zonalda/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ class Emplacement(BaseModel):
point: tuple[float, float]

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

@classmethod
def from_zone(self, zone: str) -> "Emplacement":
"""
Localiser le centroïde d'une zone et retourner les autres informations.
def from_zone(cls, zone: str) -> "Emplacement":
"""Localiser le centroïde d'une zone et retourner les autres informations.
Notez que le centroïde d'une zone peut très bien ne pas se
trouver dans la zone elle-même!
"""
latitude, longitude = zonalda[zone]
return self.from_wgs84(latitude, longitude)
district, zone, collecte = zonalda[zone]
return cls(
district=District(numero=district["id"], conseiller=district["Conseiller"])
if district is not None
else None,
collecte=Collecte(jour=collecte["jour"], couleur=collecte["couleur"])
if collecte is not None
else None,
zone=Zone(
zone=zone["ZONE"],
milieu=zone["Types"],
description=zone["Descr_Type"],
# FIXME: thoroughly unnecessary JSON parsing
geometry=json.loads(shapely.to_geojson(zone["geometry"])),
)
if zone is not None
else None,
point=(zone.geometry.centroid.x, zone.geometry.centroid.y),
)


@api.exception_handler(MunicipalityError)
Expand Down

0 comments on commit 3f171db

Please sign in to comment.