Fichiers
2026-10-11 01:57:14 +02:00

133 lignes
8.6 KiB
Python

import json,re,unicodedata,math
osm=json.load(open('_brut/osm.json',encoding='utf8'))['elements']
ent=json.load(open('_brut/wd_ent.json',encoding='utf8'))
bb=json.load(open('_brut/wd_bbox.json',encoding='utf8'))
poly=json.load(open('_brut/pnr.geojson',encoding='utf8'))['coordinates']
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.lower()).encode('ascii','ignore').decode())
def pele(v):
if v is None: return None
m=re.match(r'^\s*(-?\d+(?:[.,]\d+)?)\s*(m)?\s*$',v)
return round(float(m.group(1).replace(',','.')),1) if m else None
def typ(t,name):
n=t.get('natural');tu=t.get('tourism')
if n=='peak': return 'sommet'
if n=='saddle' or t.get('mountain_pass')=='yes': return 'pas' if re.match(r"(?i)^pas\b",name) else 'col'
if n in('cliff','rock','stone','arete','ridge'): return 'rocher'
if tu=='viewpoint': return 'belvedere'
if n=='cave_entrance': return 'grotte'
if tu in('alpine_hut','wilderness_hut') or t.get('amenity')=='shelter': return 'refuge'
return 'autre'
def wdalt(e):
out=[]
for c in e.get('claims',{}).get('P2044',[]):
try:
a=c['mainsnak']['datavalue']['value'];u=a['unit']
v=float(a['amount'])
if u.endswith('Q3710'): v=v*0.3048
out.append(round(v,1))
except KeyError: pass
return out
pts=[];seen=set()
for e in osm:
t=e['tags'];name=t['name'].strip()
lat=e.get('lat') or e['center']['lat'];lon=e.get('lon') or e['center']['lon']
key=(e['type'],e['id'])
if key in seen: continue
seen.add(key)
p={'id':f"osm-{e['type']}-{e['id']}",'nom':name,'type':typ(t,name),'lat':round(lat,6),'lon':round(lon,6),
'altitude_m':None,'altitude_sources':[],'sources':[f"https://www.openstreetmap.org/{e['type']}/{e['id']}"],
'commune_secteur':None,'statut':'ok','note':'','osm_tags':{k:t[k] for k in('natural','tourism','amenity','mountain_pass','cave:ref','ref','alt_name','old_name') if k in t}}
notes=[]
oe=pele(t.get('ele'))
if t.get('ele') and oe is None: notes.append(f"ele OSM non numerique: {t['ele']!r}")
if oe is not None: p['altitude_sources'].append({'source':'OpenStreetMap ele','valeur':oe})
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(' ','_'))
for v in wdalt(w): p['altitude_sources'].append({'source':'Wikidata P2044','valeur':v})
lab=w.get('labels',{}).get('fr',{}).get('value')
if 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'
elif lab and lab!=name: notes.append(f"libelle Wikidata: {lab!r}")
try:
c=w['claims']['P625'][0]['mainsnak']['datavalue']['value']
dkm=math.hypot((c['latitude']-lat)*111,(c['longitude']-lon)*111*math.cos(math.radians(lat)))
if dkm>0.5: notes.append(f"position Wikidata a {dkm:.1f} km de OSM");p['statut']='conflit'
except (KeyError,IndexError): pass
vals=[a['valeur'] for a in p['altitude_sources']]
if vals:
p['altitude_m']=vals[0]
if max(vals)-min(vals)>2: p['statut']='conflit';notes.append('altitudes divergentes: '+', '.join(f"{a['source']}={a['valeur']}" for a in p['altitude_sources']))
elif p['statut']=='ok': p['statut']='a_verifier';notes.append('altitude non sourcee')
if not inpnr(lat,lon): notes.append('hors polygone PNR (limite)')
p['note']='; '.join(notes);pts.append(p)
# lieux-dits OSM (place=locality) au nom de relief
NT=[('^Pas ','pas'),('^(Col|Brèche|Porte) ','col'),('^(Pic|Mont|Cime|Bec|Tête|Sommet|Pointe|Crête|Dent|Aiguille) ','sommet'),('^Rochers? |^Roc ','rocher'),('^Belvédère ','belvedere'),('^(Grotte|Scialet|Gouffre) ','grotte'),('^(Cabane|Refuge) ','refuge'),('^(Plateau|Hauts) ','autre')]
for e in json.load(open('_brut/loc.json',encoding='utf8'))['elements']:
t=e['tags'];name=t['name'].strip();lat=e.get('lat') or e['center']['lat'];lon=e.get('lon') or e['center']['lon']
if any(norm(p['nom'])==norm(name) and abs(p['lat']-lat)<0.01 and abs(p['lon']-lon)<0.014 for p in pts): continue
ty=next(ty for rx,ty in NT if re.match(rx,name+' '))
oe=pele(t.get('ele'))
pts.append({'id':f"osm-{e['type']}-{e['id']}",'nom':name,'type':ty,'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':None,'statut':'a_verifier','note':'OSM place=%s : type deduit du nom, position = lieu-dit (pas forcement le point exact)'%t['place']+('' if oe is not None else '; altitude non sourcee'),'osm_tags':{'place':t['place']}})
# Wikidata bbox items non lies a OSM
linked={s.rsplit('/',1)[1] for p in pts for s in p['sources'] if 'wikidata' in s}
wmap={}
for b in bb:
q=b['item']['value'].rsplit('/',1)[1]
if q in linked: continue
m=re.match(r'Point\(([-\d.]+) ([-\d.]+)\)',b['coord']['value']);lon,lat=float(m[1]),float(m[2])
r=wmap.setdefault(q,{'q':q,'lab':b['itemLabel']['value'],'lat':lat,'lon':lon,'alts':set(),'cls':set(),'fw':b.get('frwiki',{}).get('value')})
if 'alt' in b: r['alts'].add(round(float(b['alt']['value']),1))
r['cls'].add(b['cls']['value'].rsplit('/',1)[1])
wd_only=0;wd_matched=0
for r in wmap.values():
if not inpnr(r['lat'],r['lon']) or re.match(r'^Q\d+$',r['lab']): continue
# rapprochement par nom et distance
best=None
for p in pts:
d=math.hypot((p['lat']-r['lat'])*111,(p['lon']-r['lon'])*111*math.cos(math.radians(r['lat'])))
if d<1.5 and (norm(r['lab'])==norm(p['nom']) or norm(p['nom']) in norm(r['lab']) or norm(r['lab']) in norm(p['nom'])): best=(p,d);break
if best:
p,d=best;wd_matched+=1
p['sources'].append('https://www.wikidata.org/wiki/'+r['q']+' (rapproche par nom/position)')
if r['fw']: p['sources'].append(r['fw'])
for v in sorted(r['alts']): p['altitude_sources'].append({'source':'Wikidata P2044','valeur':v})
vals=[a['valeur'] for a in p['altitude_sources']]
if vals:
if p['altitude_m'] is None: p['altitude_m']=vals[0]; p['statut']='ok' if p['statut']=='a_verifier' else p['statut']; p['note']=p['note'].replace('altitude non sourcee','altitude Wikidata seule')
if max(vals)-min(vals)>2: p['statut']='conflit';p['note']+=('; ' if p['note'] else '')+'altitudes divergentes: '+', '.join(f"{a['source']}={a['valeur']}" for a in p['altitude_sources'])
continue
wd_only+=1
SOM={'Q8502','Q207326','Q1437459','Q1596836'}
t='sommet' if r['cls']&SOM else 'col' if 'Q133056' in r['cls'] else 'grotte' if r['cls']&{'Q35509','Q1210950'} else 'autre'
if t=='col' and re.match(r'(?i)^pas\b',r['lab']): t='pas'
alts=sorted(r['alts'])
pts.append({'id':'wd-'+r['q'],'nom':r['lab'],'type':t,'lat':round(r['lat'],6),'lon':round(r['lon'],6),'altitude_m':alts[0] if alts else None,
'altitude_sources':[{'source':'Wikidata P2044','valeur':v} for v in alts],'sources':['https://www.wikidata.org/wiki/'+r['q']]+([r['fw']] if r['fw'] else []),
'commune_secteur':None,'statut':'conflit' if len(alts)>1 and alts[-1]-alts[0]>2 else 'a_verifier','note':'present dans Wikidata, absent de la requete OSM (a verifier sur carte)','osm_tags':{}})
for p in pts:
p['sources']=list(dict.fromkeys(p['sources']))
u=[];[u.append(a) for a in p['altitude_sources'] if a not in u];p['altitude_sources']=u
segs=[s for s in p['note'].split('; ') if s and not s.startswith('altitudes divergentes')]
vals=[a['valeur'] for a in u]
if vals and max(vals)-min(vals)>2:
segs.append('altitudes divergentes: '+', '.join(f"{a['source']}={a['valeur']}" for a in u))
p['note']='; '.join(segs)
pts.sort(key=lambda p:(p['type'],norm(p['nom'])))
json.dump(pts,open('vercors_points.json','w',encoding='utf8'),ensure_ascii=False,indent=1)
import collections
print(len(pts),collections.Counter(p['type'] for p in pts),collections.Counter(p['statut'] for p in pts),'wd_only',wd_only,'wd_matched',wd_matched)
for p in pts:
if p['statut']=='conflit': print('C',p['nom'],p['type'],p['note'])