# Construit vercors_poi.json (meme format de base que ../vercors_points.json, champs POI en plus). import json, re, math, os, unicodedata, collections H = os.path.dirname(os.path.abspath(__file__)); B = os.path.join(H, '_brut') ld = lambda p: json.load(open(p, encoding='utf8')) poly = ld(os.path.join(H, '..', '_brut', 'pnr.geojson'))['coordinates'] old = ld(os.path.join(H, '..', 'vercors_points.json')) old_ids = {p['id'] for p in old} def pip(x, y, ring): ins = False; n = len(ring) for i in range(n): x1, y1 = ring[i]; x2, y2 = ring[i-1] if (y1 > y) != (y2 > y) and x < (x2-x1)*(y-y1)/(y2-y1)+x1: ins = not ins return ins def inpnr(lat, lon): return any(pip(lon, lat, p[0]) and not any(pip(lon, lat, h) for h in p[1:]) for p in poly) def norm(s): return re.sub(r'[^a-z0-9]', '', unicodedata.normalize('NFKD', (s or '').lower()).encode('ascii', 'ignore').decode()) def dist(a, b, c, d): return math.hypot((a-c)*111, (b-d)*111*math.cos(math.radians(a))) def pele(v): m = re.match(r'^\s*(-?\d+(?:[.,]\d+)?)\s*(m)?\s*$', v or '') return round(float(m.group(1).replace(',', '.')), 1) if m else None SHOP = {'bakery': 'boulangerie', 'pastry': 'boulangerie', 'convenience': 'epicerie', 'general': 'epicerie', 'deli': 'epicerie', 'greengrocer': 'epicerie', 'farm': 'producteur', 'supermarket': 'supermarche', 'butcher': 'boucherie', 'cheese': 'fromagerie', 'wine': 'cave', 'alcohol': 'cave', 'beverages': 'cave', 'sports': 'sport', 'outdoor': 'sport', 'bicycle': 'sport', 'ski': 'sport'} AMEN = {'restaurant': 'restaurant', 'fast_food': 'restaurant', 'cafe': 'cafe_bar', 'bar': 'cafe_bar', 'pub': 'cafe_bar', 'biergarten': 'cafe_bar', 'ice_cream': 'cafe_bar', 'pharmacy': 'pharmacie', 'post_office': 'poste', 'bank': 'banque_dab', 'atm': 'banque_dab', 'fuel': 'carburant', 'townhall': 'mairie', 'doctors': 'sante', 'clinic': 'sante', 'marketplace': 'marche', 'toilets': 'toilettes', 'charging_station': 'borne_recharge', 'library': 'bibliotheque'} HEB = {'hotel': 'hotel', 'motel': 'hotel', 'guest_house': 'chambre_hotes_gite', 'chalet': 'chambre_hotes_gite', 'apartment': 'chambre_hotes_gite', 'hostel': 'gite_auberge', 'camp_site': 'camping', 'caravan_site': 'camping'} PLACE = {'town': 'ville', 'village': 'village', 'hamlet': 'hameau', 'isolated_dwelling': 'habitat_isole', 'suburb': 'quartier', 'neighbourhood': 'quartier', 'locality': 'lieu_dit'} EAU = {'lavoir': 'lavoir', 'fountain': 'fontaine', 'drinking_water': 'eau_potable', 'water_point': 'point_eau', 'watering_place': 'abreuvoir'} def classe(t): """(categorie, type) ; ordre de priorite : eau > patrimoine specifique > commerces > services > lieux.""" a = t.get('amenity'); tu = t.get('tourism') if a == 'lavoir' or t.get('building') == 'lavoir': return 'eau', 'lavoir' if a in EAU: return 'eau', EAU[a] if t.get('natural') == 'spring': return 'eau', 'source' if t.get('man_made') == 'water_well': return 'eau', 'puits' if t.get('man_made') == 'water_tap': return 'eau', 'eau_potable' if tu == 'information' or t.get('office') == 'tourism': return 'service', 'office_tourisme' if tu in HEB: return 'service', HEB[tu] if a in AMEN: return 'service', AMEN[a] if 'shop' in t: return 'commerce', SHOP.get(t['shop'], 'commerce_autre') if 'craft' in t: return 'commerce', 'artisan' if tu in ('alpine_hut', 'wilderness_hut'): return 'abri', 'refuge' if a == 'shelter': return 'abri', 'abri' if tu == 'viewpoint': return 'patrimoine', 'belvedere' if t.get('natural') == 'cave_entrance': return 'patrimoine', 'grotte' if t.get('natural') == 'waterfall' or t.get('waterway') == 'waterfall': return 'patrimoine', 'cascade' if a == 'place_of_worship': return 'patrimoine', 'edifice_religieux' if tu in ('museum', 'gallery'): return 'patrimoine', 'musee' if tu == 'artwork': return 'patrimoine', 'oeuvre' if tu == 'picnic_site': return 'patrimoine', 'aire_pique_nique' if tu in ('attraction', 'theme_park', 'zoo'): return 'patrimoine', 'site_touristique' if 'historic' in t or 'heritage' in t: return 'patrimoine', 'patrimoine_historique' if t.get('place') in PLACE: return ('lieu_habite' if t['place'] != 'locality' else 'lieu_dit'), PLACE[t['place']] return None, None PUBLIC_CONTACT = {'commerce', 'service', 'patrimoine', 'abri'} # jamais pour lieux habites / habitat isole KEEP = ('amenity', 'shop', 'tourism', 'historic', 'heritage', 'natural', 'man_made', 'place', 'craft', 'building', 'drinking_water', 'cuisine', 'fee', 'stars', 'capacity', 'access', 'seasonal', 'wikidata', 'wikipedia', 'alt_name', 'old_name') def first(t, *ks): for k in ks: if t.get(k): return t[k] return None ent = ld(os.path.join(B, 'wd_ent.json')) if os.path.exists(os.path.join(B, 'wd_ent.json')) else {} pts = []; seen = set(); skipped_old = 0; hors = 0 for f in sorted(x for x in os.listdir(B) if x.startswith('osm_')): for e in ld(os.path.join(B, f))['elements']: key = f"osm-{e['type']}-{e['id']}" if key in seen: continue seen.add(key) if key in old_ids: skipped_old += 1; continue t = e.get('tags', {}); cat, ty = classe(t) if not cat: continue lat = e.get('lat') or e['center']['lat']; lon = e.get('lon') or e['center']['lon'] name = (t.get('name') or '').strip() notes = []; statut = 'ok' if not name: name = f"({ty.replace('_', ' ')} sans nom)"; statut = 'a_verifier'; notes.append('sans nom dans OSM') if cat == 'lieu_dit': statut = 'a_verifier'; notes.append('OSM place=locality : lieu-dit, pas forcement habite') oe = pele(t.get('ele')) p = {'id': key, 'nom': name, 'type': ty, 'categorie': cat, 'lat': round(lat, 6), 'lon': round(lon, 6), 'altitude_m': oe, 'altitude_sources': [{'source': 'OpenStreetMap ele', 'valeur': oe}] if oe is not None else [], 'sources': [f"https://www.openstreetmap.org/{e['type']}/{e['id']}"], 'commune_secteur': t.get('addr:city'), 'statut': statut, 'note': '', 'description': t.get('description') or t.get('description:fr'), 'description_source': 'OpenStreetMap description' if (t.get('description') or t.get('description:fr')) else None, 'horaires': t.get('opening_hours'), 'site_web': None, 'telephone': None, 'email': None, 'osm_tags': {k: t[k] for k in KEEP if k in t}} if cat in PUBLIC_CONTACT: p['site_web'] = first(t, 'website', 'contact:website', 'url'); p['telephone'] = first(t, 'phone', 'contact:phone', 'contact:mobile') p['email'] = first(t, 'email', 'contact:email') if ty == 'chambre_hotes_gite': # souvent chez l'habitant : pas de telephone/email, nom a relire p['telephone'] = p['email'] = None; notes.append("hebergement chez l'habitant possible : telephone/email non repris, verifier que le nom n'est pas celui d'un particulier") if cat in ('eau',) and t.get('drinking_water'): notes.append('eau potable (OSM drinking_water=%s)' % t['drinking_water']) q = t.get('wikidata', '').split(';')[0].strip() if q and q in ent and 'missing' not in ent[q]: w = ent[q]; p['sources'].append('https://www.wikidata.org/wiki/' + q) fw = w.get('sitelinks', {}).get('frwiki') if fw: p['sources'].append('https://fr.wikipedia.org/wiki/' + fw['title'].replace(' ', '_')) d = w.get('descriptions', {}).get('fr', {}).get('value') if d: if p['description']: notes.append(f'description Wikidata: {d!r}') else: p['description'] = d; p['description_source'] = 'Wikidata ' + q lab = w.get('labels', {}).get('fr', {}).get('value') if lab and statut != 'ok' and not t.get('name'): notes.append(f'nom Wikidata (OSM sans nom): {lab!r}') elif lab and norm(lab) != norm(name) and norm(name) not in norm(lab) and norm(lab) not in norm(name): notes.append(f'nom Wikidata different: {lab!r}'); p['statut'] = 'conflit' try: c = w['claims']['P625'][0]['mainsnak']['datavalue']['value'] dk = dist(c['latitude'], c['longitude'], lat, lon); lim = 2.0 if cat == 'lieu_habite' else 0.5 if dk > lim: notes.append(f'position Wikidata a {dk:.1f} km de OSM'); p['statut'] = 'conflit' except (KeyError, IndexError): pass if not inpnr(lat, lon): notes.append('hors polygone PNR (limite)'); hors += 1 p['note'] = '; '.join(notes); pts.append(p) # Wikidata SPARQL (monuments proteges, communes) wd_link = wd_only = 0 byosm = {p['id']: p for p in pts} allq = {s.rsplit('/', 1)[1] for p in pts + old for s in p['sources'] if 'wikidata.org' in s} rows = ld(os.path.join(B, 'wd_sparql.json')) if os.path.exists(os.path.join(B, 'wd_sparql.json')) else [] wm = {} for b in rows: q = b['item']['value'].rsplit('/', 1)[1] m = re.match(r'Point\(([-\d.]+) ([-\d.]+)\)', b['coord']['value']) r = wm.setdefault(q, {'q': q, 'lab': b['itemLabel']['value'], 'desc': b.get('itemDescription', {}).get('value'), 'lat': float(m[2]), 'lon': float(m[1]), 'kind': set(), 'osm': set(), 'fw': b.get('frwiki', {}).get('value')}) r['kind'].add(b['kind']['value']) for k, ty in (('osmn', 'node'), ('osmw', 'way'), ('osmr', 'relation')): if k in b: r['osm'].add(f"osm-{ty}-{b[k]['value']}") for r in wm.values(): if r['q'] in allq or re.match(r'^Q\d+$', r['lab']) or not inpnr(r['lat'], r['lon']): continue tgt = next((byosm[o] for o in r['osm'] if o in byosm), None); how = 'identifiant OSM declare dans Wikidata' if not tgt: cats = ('lieu_habite',) if r['kind'] == {'commune'} else ('patrimoine', 'eau', 'abri', 'lieu_habite', 'lieu_dit') c = [(dist(p['lat'], p['lon'], r['lat'], r['lon']), p) for p in pts if p['categorie'] in cats and (norm(p['nom']) == norm(r['lab']) or (len(norm(p['nom'])) > 5 and (norm(p['nom']) in norm(r['lab']) or norm(r['lab']) in norm(p['nom']))))] c = [x for x in c if x[0] < (3 if 'commune' in r['kind'] else 1)] if c: tgt = min(c, key=lambda x: x[0])[1]; how = 'rapproche par nom/position' if tgt: wd_link += 1; tgt['sources'].append(f"https://www.wikidata.org/wiki/{r['q']} ({how})") if r['fw']: tgt['sources'].append(r['fw']) if r['desc'] and not tgt['description']: tgt['description'] = r['desc']; tgt['description_source'] = 'Wikidata ' + r['q'] if 'patrimoine' in r['kind']: tgt['note'] = '; '.join(x for x in (tgt['note'], 'protege au titre des monuments historiques ou equivalent (Wikidata P1435)') if x) continue wd_only += 1 pts.append({'id': 'wd-' + r['q'], 'nom': r['lab'], 'type': 'village' if r['kind'] == {'commune'} else 'patrimoine_historique', 'categorie': 'lieu_habite' if r['kind'] == {'commune'} else 'patrimoine', 'lat': round(r['lat'], 6), 'lon': round(r['lon'], 6), 'altitude_m': None, 'altitude_sources': [], 'sources': ['https://www.wikidata.org/wiki/' + r['q']] + ([r['fw']] if r['fw'] else []), 'commune_secteur': None, 'statut': 'a_verifier', 'note': 'present dans Wikidata seulement' + (' (P1435 protection patrimoniale)' if 'patrimoine' in r['kind'] else ' (commune)'), 'description': r['desc'], 'description_source': 'Wikidata ' + r['q'] if r['desc'] else None, 'horaires': None, 'site_web': None, 'telephone': None, 'email': None, 'osm_tags': {}}) # Doublons possibles (on signale, on ne supprime pas) ndup = 0 def mark(p, other, why): global ndup p.setdefault('doublon_possible', []) if other not in p['doublon_possible']: p['doublon_possible'].append(other); ndup += 1 p['note'] = '; '.join(x for x in (p['note'], f'doublon possible ({why}) : {other}') if x) if p['statut'] == 'ok': p['statut'] = 'conflit' grid = collections.defaultdict(list) for p in pts: grid[(round(p['lat'], 2), round(p['lon'], 2))].append(p) for p in pts: for dx in (-0.01, 0, 0.01): for dy in (-0.01, 0, 0.01): for o in grid[(round(p['lat']+dx, 2), round(p['lon']+dy, 2))]: if o is p: continue d = dist(p['lat'], p['lon'], o['lat'], o['lon']) named = not p['nom'].startswith('(') if named and norm(p['nom']) == norm(o['nom']) and p['categorie'] == o['categorie'] and d < 0.3: mark(p, o['id'], f'meme nom a {d*1000:.0f} m') elif p['categorie'] == 'eau' == o['categorie'] and d < 0.015: mark(p, o['id'], f'point d eau a {d*1000:.0f} m') for p in pts: # recouvrement avec l'inventaire des reliefs if p['type'] in ('belvedere', 'grotte', 'refuge', 'abri'): for o in old: if norm(o['nom']) == norm(p['nom']) and dist(p['lat'], p['lon'], o['lat'], o['lon']) < 0.5: mark(p, o['id'], 'deja dans vercors_points.json') for p in pts: p['sources'] = list(dict.fromkeys(p['sources'])) ORD = ['lieu_habite', 'lieu_dit', 'eau', 'patrimoine', 'abri', 'commerce', 'service'] pts.sort(key=lambda p: (ORD.index(p['categorie']), p['type'], norm(p['nom']))) out = os.path.join(H, 'vercors_poi.json') json.dump(pts, open(out, 'w', encoding='utf8'), ensure_ascii=False, indent=1) # statistiques st = collections.OrderedDict() for p in pts: s = st.setdefault((p['categorie'], p['type']), collections.Counter()); s['n'] += 1; s[p['statut']] += 1 for k in ('horaires', 'description', 'site_web', 'telephone'): if p.get(k): s[k] += 1 if not p['nom'].startswith('('): s['nomme'] += 1 print(len(pts), 'points ; deja dans vercors_points:', skipped_old, '; hors polygone:', hors, '; WD rattaches:', wd_link, '; WD seuls:', wd_only, '; marques doublon:', ndup) print('|categorie|type|total|nommes|ok|a_verifier|conflit|horaires|description|site web|telephone|') for (c, t), s in st.items(): print(f"|{c}|{t}|{s['n']}|{s['nomme']}|{s['ok']}|{s['a_verifier']}|{s['conflit']}|{s['horaires']}|{s['description']}|{s['site_web']}|{s['telephone']}|") tot = collections.defaultdict(collections.Counter) for (c, t), s in st.items(): tot[c].update(s) for c, s in tot.items(): print('TOTAL', c, dict(s)) json.dump({f'{c}/{t}': dict(s) for (c, t), s in st.items()}, open(os.path.join(B, 'stats.json'), 'w'), indent=1) # carte autonome s = json.dumps(pts, ensure_ascii=False, separators=(',', ':')).replace('