Skip to content

Commit

Permalink
Garde la tag OSM duquel le nom est issue, priorise le nom OSM par defaut
Browse files Browse the repository at this point in the history
  • Loading branch information
frodrigo committed Jan 29, 2025
1 parent 55a5342 commit bf83615
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 29 deletions.
20 changes: 19 additions & 1 deletion bano/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Nom:
def __init__(
self,
nom,
nom_tag,
fantoir,
nature,
source,
Expand All @@ -26,6 +27,7 @@ def __init__(
self.code_dept = hp.get_code_dept_from_insee(code_insee)
self.code_insee_ancienne_commune = code_insee_ancienne_commune
self.nom = nom.replace("\t", " ")
self.nom_tag = nom_tag
self.nom_ancienne_commune = nom_ancienne_commune
self.fantoir = fantoir[0:9] if fantoir else None
self.nature = nature
Expand Down Expand Up @@ -64,7 +66,7 @@ def _as_csv_format_bano(self, correspondance):
fantoir = remplace_fantoir_ban(correspondance, self.niveau, self.fantoir)
else:
fantoir = self.fantoir
return f"{fantoir}\t{self.nom}\t{self.nature}\t{self.code_insee}\t{self.code_dept}\t{self.code_insee_ancienne_commune if self.code_insee_ancienne_commune else ''}\t{self.nom_ancienne_commune if self.nom_ancienne_commune else ''}\t{self.source}"
return f"{fantoir}\t{self.nom}\t{self.nom_tag}\t{self.nature}\t{self.code_insee}\t{self.code_dept}\t{self.code_insee_ancienne_commune if self.code_insee_ancienne_commune else ''}\t{self.nom_ancienne_commune if self.nom_ancienne_commune else ''}\t{self.source}"

def add_fantoir(self, topo):
if not self.fantoir:
Expand Down Expand Up @@ -104,6 +106,7 @@ def charge_noms_osm_hors_numeros(self):
for (
provenance,
name,
name_tag,
tags,
libelle_suffixe,
code_insee_ancienne_commune,
Expand All @@ -114,6 +117,7 @@ def charge_noms_osm_hors_numeros(self):
self.add_nom(
Nom(
name,
name_tag,
tags.get("ref:FR:FANTOIR"),
nature,
"OSM",
Expand All @@ -126,6 +130,7 @@ def charge_noms_osm_hors_numeros(self):
self.add_nom(
Nom(
name,
name_tag,
tags["ref:FR:FANTOIR"],
nature,
"OSM",
Expand Down Expand Up @@ -199,6 +204,7 @@ def enregistre(self, correspondance):
columns=(
"fantoir",
"nom",
"nom_tag",
"nature",
"code_insee",
"code_dept",
Expand Down Expand Up @@ -462,6 +468,7 @@ def noms_des_adresses(self, noms):
noms.add_nom(
Nom(
a.voie,
None,
a.fantoir,
"voie",
a.source,
Expand All @@ -474,6 +481,7 @@ def noms_des_adresses(self, noms):
noms.add_nom(
Nom(
a.place,
None,
a.fantoir,
"place",
a.source,
Expand Down Expand Up @@ -556,6 +564,7 @@ def __init__(
lon,
lat,
nom,
nom_tag,
fantoir=None,
code_insee_ancienne_commune=None,
nom_ancienne_commune=None,
Expand All @@ -567,6 +576,7 @@ def __init__(
self.lat = round(lat, 6)
self.nature = nature
self.nom = nom.replace("\t", " ")
self.nom_tag = nom_tag
self.nom_normalise = hp.normalize(nom)
self.fantoir = fantoir[0:9] if fantoir else None
self.code_insee_ancienne_commune = code_insee_ancienne_commune
Expand Down Expand Up @@ -627,6 +637,7 @@ def charge_points_nommes_lieux_dits_cadastre(self):
x,
y,
hp.format_toponyme(nom),
None,
code_insee_ancienne_commune=code_insee_ancienne_commune,
nom_ancienne_commune=nom_ancienne_commune,
)
Expand All @@ -641,6 +652,7 @@ def charge_points_nommes_centroides_osm(self):
x,
y,
nom,
nom_tag,
code_insee_ancienne_commune,
fantoir,
nom_ancienne_commune,
Expand All @@ -658,6 +670,7 @@ def charge_points_nommes_centroides_osm(self):
x,
y,
nom,
nom_tag,
code_insee_ancienne_commune=code_insee_ancienne_commune,
fantoir=single_fantoir,
nom_ancienne_commune=nom_ancienne_commune,
Expand All @@ -673,6 +686,7 @@ def charge_points_nommes_place_osm(self):
x,
y,
nom,
nom_tag,
code_insee_ancienne_commune,
fantoir,
nom_ancienne_commune,
Expand All @@ -688,6 +702,7 @@ def charge_points_nommes_place_osm(self):
x,
y,
nom,
nom_tag,
code_insee_ancienne_commune=code_insee_ancienne_commune,
fantoir=single_fantoir,
nom_ancienne_commune=nom_ancienne_commune,
Expand Down Expand Up @@ -715,6 +730,7 @@ def charge_points_nommes_numeros_ban(self):
x,
y,
nom,
None,
code_insee_ancienne_commune=code_insee_ancienne_commune,
fantoir=fantoir,
nom_ancienne_commune=nom_ancienne_commune,
Expand All @@ -730,6 +746,7 @@ def noms_des_points_nommes(self, noms):
noms.add_nom(
Nom(
a.nom,
None,
a.fantoir,
a.nature,
a.source,
Expand All @@ -742,6 +759,7 @@ def noms_des_points_nommes(self, noms):
noms.add_nom(
Nom(
a.nom,
a.nom_tag,
a.fantoir,
a.nature,
a.source,
Expand Down
29 changes: 23 additions & 6 deletions bano/sql/charge_noms_voies_lieux-dits_OSM.sql
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
SELECT DISTINCT provenance,
name,
name_tag,
tags,
libelle_suffixe,
a9.code_insee,
a9.nom,
nature
FROM (SELECT 1::integer AS provenance,
pt.way,
UNNEST(ARRAY[pt.name,pt.alt_name,pt.old_name]) as name,
name_osm.name,
name_osm.name_tag,
tags,
CASE
WHEN pt.place='' THEN 'voie'::text
Expand All @@ -16,26 +18,41 @@ FROM (SELECT 1::integer AS provenance,
FROM (SELECT way FROM planet_osm_polygon WHERE "ref:INSEE" = '__code_insee__') p
JOIN (SELECT * FROM planet_osm_point WHERE ("ref:FR:FANTOIR" !='' OR place != '') AND name != '') pt
ON pt.way && p.way AND
ST_Intersects(pt.way, p.way)
ST_Intersects(pt.way, p.way),
UNNEST(
ARRAY [pt.name,pt.alt_name,pt.old_name],
ARRAY ['name','alt_name','old_name']
) AS name_osm(name,name_tag)
UNION ALL
SELECT 2,
l.way,
UNNEST(ARRAY[l.name,l.alt_name,l.old_name]) as name,
name_osm.name,
name_osm.name_tag,
tags,
'voie'
FROM (SELECT way FROM planet_osm_polygon WHERE "ref:INSEE" = '__code_insee__') p
JOIN (SELECT * FROM planet_osm_line WHERE highway != '' AND name != '') l
ON p.way && l.way AND ST_Contains(p.way, l.way)
ON p.way && l.way AND ST_Contains(p.way, l.way),
UNNEST(
ARRAY [l.name,l.alt_name,l.old_name],
ARRAY ['name','alt_name','old_name']
) AS name_osm(name,name_tag)
UNION ALL
SELECT 3,
pl.way,
UNNEST(ARRAY[pl.name,pl.alt_name,pl.old_name]) as name,
name_osm.name,
name_osm.name_tag,
tags,
'voie'
FROM (SELECT way FROM planet_osm_polygon WHERE "ref:INSEE" = '__code_insee__') p
JOIN (SELECT * FROM planet_osm_polygon WHERE (highway||"ref:FR:FANTOIR" != '' OR landuse = 'residential' OR amenity = 'parking') AND name != '') pl
ON pl.way && p.way AND
ST_Intersects(pl.way, p.way)) l
ST_Intersects(pl.way, p.way),
UNNEST(
ARRAY [pl.name,pl.alt_name,pl.old_name],
ARRAY ['name','alt_name','old_name']
) AS name_osm(name,name_tag)
) l
LEFT OUTER JOIN suffixe h
ON ST_Intersects(l.way, h.geometrie)
LEFT OUTER JOIN (SELECT * FROM polygones_insee_a9 where insee_a8 = '__code_insee__') a9
Expand Down
20 changes: 16 additions & 4 deletions bano/sql/charge_noms_voies_relation_OSM.sql
Original file line number Diff line number Diff line change
@@ -1,29 +1,41 @@
SELECT DISTINCT provenance,
name,
name_tag,
tags,
libelle_suffixe,
a9.code_insee,
a9.nom,
'voie'::text
FROM (SELECT 4::integer AS provenance,
UNNEST(ARRAY[l.name,l.alt_name,l.old_name]) as name,
name_osm.name,
name_osm.name_tag,
l.way,
r.tags
FROM (SELECT way FROM planet_osm_polygon WHERE "ref:INSEE" = '__code_insee__') p
JOIN (SELECT name,alt_name,old_name,tags,osm_id,way FROM planet_osm_line WHERE highway != '' AND name != '') l
ON p.way && l.way AND ST_Contains(p.way, l.way)
JOIN planet_osm_rels r
ON r.osm_id = l.osm_id
ON r.osm_id = l.osm_id,
UNNEST(
ARRAY [l.name,l.alt_name,l.old_name],
ARRAY ['name','alt_name','old_name']
) AS name_osm(name,name_tag)
UNION ALL
SELECT 5,
UNNEST(ARRAY[l.name,l.alt_name,l.old_name]) as name,
name_osm.name,
name_osm.name_tag,
l.way,
r.tags
FROM (SELECT way FROM planet_osm_polygon WHERE "ref:INSEE" = '__code_insee__') p
JOIN (SELECT name,alt_name,old_name,tags,osm_id,way FROM planet_osm_polygon WHERE highway != '' AND name != '') l
ON p.way && l.way AND ST_Contains(p.way, l.way)
JOIN planet_osm_rels r
ON r.osm_id = l.osm_id) l
ON r.osm_id = l.osm_id,
UNNEST(
ARRAY [l.name,l.alt_name,l.old_name],
ARRAY ['name','alt_name','old_name']
) AS name_osm(name,name_tag)
) l
LEFT OUTER JOIN (SELECT * FROM suffixe WHERE code_insee = '__code_insee__') h
ON ST_Intersects(way, h.geometrie)
LEFT OUTER JOIN (SELECT * FROM polygones_insee_a9 where insee_a8 = '__code_insee__') a9
Expand Down
1 change: 1 addition & 0 deletions bano/sql/charge_noms_voies_relation_bbox_OSM.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
SELECT DISTINCT provenance,
name,
'name' AS name_tag,
tags,
libelle_suffixe,
a9.code_insee,
Expand Down
36 changes: 28 additions & 8 deletions bano/sql/charge_points_nommes_centroides_OSM.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ WITH
lignes_brutes
AS
(SELECT l.way,
unnest(array[l.name,l.alt_name,l.old_name]) AS name,
name_osm.name,
name_osm.name_tag,
COALESCE(a9.code_insee,'xxxxx') as insee_jointure,
a9.code_insee insee_ac,
unnest(array["ref:FR:FANTOIR","ref:FR:FANTOIR:left","ref:FR:FANTOIR:right"]) AS fantoir,
Expand All @@ -12,14 +13,19 @@ FROM (SELECT way FROM planet_osm_polygon WHERE "ref:INSEE" = '__code_insee__'
JOIN planet_osm_line l
ON ST_Intersects(l.way, p.way)
LEFT OUTER JOIN (SELECT * FROM polygones_insee_a9 WHERE insee_a8 = '__code_insee__') a9
ON ST_Intersects(l.way, a9.geometrie)
ON ST_Intersects(l.way, a9.geometrie),
UNNEST(
ARRAY [l.name,l.alt_name,l.old_name],
ARRAY ['name','alt_name','old_name']
) AS name_osm(name,name_tag)
WHERE (l.highway != '' OR
l.waterway = 'dam') AND
l.highway NOT IN ('bus_stop','platform') AND
l.name != ''
UNION ALL
SELECT ST_PointOnSurface(l.way),
unnest(array[l.name,l.alt_name,l.old_name]) AS name,
name_osm.name,
name_osm.name_tag,
COALESCE(a9.code_insee,'xxxxx') as insee_jointure,
a9.code_insee insee_ac,
"ref:FR:FANTOIR" AS fantoir,
Expand All @@ -29,13 +35,18 @@ FROM (SELECT geometrie FROM polygones_insee WHERE code_insee = '__code_insee_
JOIN planet_osm_polygon l
ON ST_Intersects(l.way, p.geometrie)
LEFT OUTER JOIN (SELECT * FROM polygones_insee_a9 WHERE insee_a8 = '__code_insee__') a9
ON ST_Intersects(l.way, a9.geometrie)
ON ST_Intersects(l.way, a9.geometrie),
UNNEST(
ARRAY [l.name,l.alt_name,l.old_name],
ARRAY ['name','alt_name','old_name']
) AS name_osm(name,name_tag)
WHERE (l.highway||"ref:FR:FANTOIR" != '' OR l.landuse = 'residential' OR l.amenity = 'parking') AND
l.highway NOT IN ('bus_stop','platform') AND
l.name != ''
UNION ALL
SELECT l.way,
unnest(array[l.name,l.alt_name,l.old_name]) AS name,
name_osm.name,
name_osm.name_tag,
COALESCE(a9.code_insee,'xxxxx') as insee_jointure,
a9.code_insee insee_ac,
"ref:FR:FANTOIR" AS fantoir,
Expand All @@ -45,7 +56,11 @@ FROM (SELECT way FROM planet_osm_polygon WHERE "ref:INSEE" = '__code_insee__'
JOIN planet_osm_rels l
ON ST_Intersects(l.way, p.way)
LEFT OUTER JOIN (SELECT * FROM polygones_insee_a9 WHERE insee_a8 = '__code_insee__') a9
ON ST_Intersects(l.way, a9.geometrie)
ON ST_Intersects(l.way, a9.geometrie),
UNNEST(
ARRAY [l.name,l.alt_name,l.old_name],
ARRAY ['name','alt_name','old_name']
) AS name_osm(name,name_tag)
WHERE l.member_role = 'street' AND
l.name != ''),
lignes_noms
Expand All @@ -68,28 +83,31 @@ lignes_agregees
AS
(SELECT ST_LineMerge(ST_Collect(way_line)) way,
name,
name_tag,
insee_ac,
insee_jointure,
fantoir,
nom_ac
FROM lignes_noms_rang
WHERE rang = 1
GROUP BY 2,3,4,5,6),
GROUP BY 2,3,4,5,6,7),
centroide_lignes_agregees
AS
(SELECT ST_Centroid(ST_LineMerge(ST_Collect(way_line))) way,
name,
name_tag,
insee_ac,
insee_jointure,
fantoir,
nom_ac
FROM lignes_noms_rang
WHERE rang = 1
GROUP BY 2,3,4,5,6),
GROUP BY 2,3,4,5,6,7),
resultat
AS
(SELECT ST_SetSRID(ST_ClosestPoint(lignes_agregees.way,centroide_lignes_agregees.way),4326) point,
lignes_agregees.name,
lignes_agregees.name_tag,
lignes_agregees.insee_ac,
lignes_agregees.fantoir,
lignes_agregees.nom_ac
Expand Down Expand Up @@ -131,6 +149,7 @@ ON ST_Intersects(c.point, a9.geometrie))
SELECT ST_x(point),
ST_y(point),
name,
name_tag,
insee_ac,
fantoir,
nom_ac
Expand All @@ -139,6 +158,7 @@ UNION ALL
SELECT ST_x(point),
ST_y(point),
name,
'name' AS name_tag,
insee_ac,
fantoir,
nom_ac
Expand Down
Loading

0 comments on commit bf83615

Please sign in to comment.