Skip to content

Commit

Permalink
fix: accomoder des resultats vides
Browse files Browse the repository at this point in the history
  • Loading branch information
dhdaines committed Dec 8, 2023
1 parent 1fe9284 commit c5594ee
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
22 changes: 15 additions & 7 deletions js/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function getCurrentPosition(): Promise<GeolocationPosition> {

const [lon1, lat1, lon2, lat2] = sainteAdeleBBox;
const adresse = document.getElementById("adresse");
if (adresse == null)
if (adresse === null)
throw "Element not found: adresse";
const autocomplete = new GeocoderAutocomplete(
adresse,
Expand Down Expand Up @@ -72,6 +72,8 @@ window.addEventListener("load", async () => {
});

function categorieTexte(info) {
if (info === null)
return "inconnu";
const { zone, milieu, description } = info;
const [categorie, souscategorie] = milieu.split(".");
const cat = zonage.categorie_milieu[categorie];
Expand All @@ -85,6 +87,8 @@ function categorieTexte(info) {
}

function milieuTexte(info) {
if (info === null)
return "inconnu";
const { zone, milieu, description } = info;
const mil = zonage.milieu[milieu];
if (mil !== undefined) {
Expand All @@ -96,12 +100,16 @@ function milieuTexte(info) {
}

function collecteTexte(info) {
if (info === null)
return "inconnue";
const { couleur, jour } = info;
return `${jour} <a target="_blank" href="${COLLECTES_URL}">(calendrier PDF)</a>`;
}

function conseilTexte(info) {
return `<a href="mailto:district${info.numero}@vdsa.ca">${info.conseiller}</a>`;
if (info === null)
return "inconnu";
return `conseillier: <a href="mailto:district${info.numero}@vdsa.ca">${info.conseiller}</a>`;
}

const geoformat = new GeoJSON();
Expand All @@ -111,12 +119,12 @@ const zonelayer = new VectorLayer<any>({ // stfu tsc
});
function updateInfo(info) {
const infoDiv = document.getElementById("info");
if (infoDiv == null)
if (infoDiv === null)
throw "Element not found: info";
const { zone, district, collecte } = info;
infoDiv.innerHTML = `<table>
<tr><td>District</td><td>${district.numero}: ${conseilTexte(district)}<td></tr>
<tr><td>Zone</td><td>${zone.zone}</td></tr>
<tr><td>District</td><td>${district.numero} (${conseilTexte(district)})<td></tr>
<tr><td>Zone</td><td>${zone.zone ?? "inconnue"}</td></tr>
<tr><td>Catégorie</td><td>${categorieTexte(zone)}<td></tr>
<tr><td>Milieu</td><td>${milieuTexte(zone)}<td></tr>
<tr><td>Collecte</td><td>${collecteTexte(collecte)}<td></tr>
Expand All @@ -134,7 +142,7 @@ function updateInfo(info) {

function infoError(txt: string) {
const infoDiv = document.getElementById("info");
if (infoDiv == null)
if (infoDiv === null)
throw "Element not found: info";
infoDiv.innerHTML = `<p>${txt}</p>`;
}
Expand Down Expand Up @@ -182,7 +190,7 @@ async function geolocateMe() {
}

const locate = document.getElementById("locate");
if (locate == null)
if (locate === null)
throw "Element not found: locate";
locate.addEventListener("click", geolocateMe);

Expand Down
11 changes: 7 additions & 4 deletions zonalda/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from pathlib import Path

import geopandas # type: ignore
from pandas import Series
from shapely import Point # type: ignore

VERSION = "0.0.1"
Expand All @@ -29,7 +30,9 @@ def __init__(self):
self.zonage = geopandas.read_file(THISDIR / "zonage.geojson")
self.collectes = geopandas.read_file(THISDIR / "collectes.geojson")

def __call__(self, latitude: float, longitude: float):
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 All @@ -40,19 +43,19 @@ def __call__(self, latitude: float, longitude: float):
districts = self.districts.loc[self.districts.contains(p)]
if len(districts) > 1:
LOGGER.warning("Plusieurs districts trouvé pour %s: %s", p, districts)
if districts:
if len(districts):
district = districts.iloc[0]
zones = self.zonage.loc[self.zonage.contains(p)]
if len(zones) > 1:
LOGGER.warning("Plusieurs zones trouvé pour %s: %s", p, zones)
if zones:
if len(zones):
zone = zones.iloc[0]
collectes = self.collectes.loc[self.collectes.contains(p)]
if len(collectes) > 1:
LOGGER.warning(
"Plusieurs zones de collectes trouvé pour %s: %s", p, collectes
)
if collectes:
if len(collectes):
collecte = collectes.iloc[0]
return district, zone, collecte

Expand Down
6 changes: 3 additions & 3 deletions zonalda/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ def from_wgs84(self, latitude: float, longitude: float) -> "Emplacement":
district, zone, collecte = zonalda(latitude, longitude)
return self(
district=District(numero=district["id"], conseiller=district["Conseiller"])
if district
if district is not None
else None,
collecte=Collecte(jour=collecte["jour"], couleur=collecte["couleur"])
if collecte
if collecte is not None
else None,
zone=Zone(
zone=zone["ZONE"],
Expand All @@ -66,7 +66,7 @@ def from_wgs84(self, latitude: float, longitude: float) -> "Emplacement":
# FIXME: thoroughly unnecessary JSON parsing
geometry=json.loads(shapely.to_geojson(zone["geometry"])),
)
if zone
if zone is not None
else None,
)

Expand Down

0 comments on commit c5594ee

Please sign in to comment.