# Telechargement des POI du PNR du Vercors (Overpass + Wikidata), requetes decoupees avec pauses. import json, os, time, urllib.request, urllib.parse UA = 'VercorsInventaire/1.0 (inventaire personnel non commercial; contact sthimisbad@gmail.com)' OUT = os.path.join(os.path.dirname(__file__), '_brut') AREA = 'area(id:3605555268)->.a;' # relation 5555268 = PNR du Vercors Q = { 'lieux': 'nwr(area.a)[place~"^(town|village|hamlet|isolated_dwelling|suburb|neighbourhood|locality)$"][name];', 'eau': 'nwr(area.a)[amenity~"^(drinking_water|fountain|water_point|watering_place|lavoir)$"];nwr(area.a)[natural=spring];nwr(area.a)[man_made~"^(water_well|water_tap)$"];nwr(area.a)[building=lavoir];', 'patrimoine': 'nwr(area.a)[historic][name];nwr(area.a)[tourism~"^(attraction|museum|artwork|gallery|theme_park|zoo|picnic_site)$"][name];nwr(area.a)[amenity=place_of_worship][name];nwr(area.a)[heritage][name];nwr(area.a)[tourism=viewpoint][name];nwr(area.a)[natural=cave_entrance][name];nwr(area.a)[natural=waterfall][name];nwr(area.a)[waterway=waterfall][name];', 'abris': 'nwr(area.a)[amenity=shelter][name];nwr(area.a)[tourism~"^(alpine_hut|wilderness_hut)$"][name];', 'commerces': 'nwr(area.a)[shop];', 'services': 'nwr(area.a)[amenity~"^(restaurant|cafe|bar|pub|fast_food|pharmacy|post_office|bank|atm|fuel|townhall|doctors|clinic|marketplace|toilets|charging_station|library|ice_cream|biergarten)$"];nwr(area.a)[tourism~"^(hotel|guest_house|hostel|chalet|camp_site|caravan_site|apartment|motel)$"];nwr(area.a)[tourism=information][information=office];nwr(area.a)[office=tourism];nwr(area.a)[craft][name];', } def post(url, data, hdr=None): r = urllib.request.Request(url, data=urllib.parse.urlencode(data).encode(), headers={'User-Agent': UA, **(hdr or {})}) return urllib.request.urlopen(r, timeout=300).read() for k, body in Q.items(): f = os.path.join(OUT, f'osm_{k}.json') if os.path.exists(f): continue q = f'[out:json][timeout:180];{AREA}({body});out tags center;' raw = None for essai in range(4): srv = ('https://overpass-api.de/api/interpreter', 'https://overpass.kumi.systems/api/interpreter')[essai % 2] try: raw = post(srv, {'data': q}); json.loads(raw); break except Exception as e: raw = None; print(k, 'erreur', srv, e, flush=True); time.sleep(60) if raw is None: print(k, 'ECHEC, fichier non ecrit'); continue open(f, 'wb').write(raw); print(k, len(json.loads(raw)['elements'])); time.sleep(20) # Wikidata : elements OSM portant une balise wikidata qids = set() for k in Q: for e in json.load(open(os.path.join(OUT, f'osm_{k}.json'), encoding='utf8'))['elements']: q = e.get('tags', {}).get('wikidata', '').split(';')[0].strip() if q.startswith('Q'): qids.add(q) f = os.path.join(OUT, 'wd_ent.json') if not os.path.exists(f): ent = {}; L = sorted(qids) for i in range(0, len(L), 50): u = 'https://www.wikidata.org/w/api.php?' + urllib.parse.urlencode({'action': 'wbgetentities', 'ids': '|'.join(L[i:i+50]), 'props': 'labels|descriptions|claims|sitelinks', 'languages': 'fr', 'sitefilter': 'frwiki', 'format': 'json'}) ent.update(json.loads(urllib.request.urlopen(urllib.request.Request(u, headers={'User-Agent': UA}), timeout=120).read())['entities']); time.sleep(2) json.dump(ent, open(f, 'w', encoding='utf8'), ensure_ascii=False); print('wd_ent', len(ent)) # Wikidata SPARQL : monuments proteges (P1435) et communes (P31 Q484170) dans la bbox elargie a 5.00 E f = os.path.join(OUT, 'wd_sparql.json') if not os.path.exists(f): sp = '''SELECT ?item ?itemLabel ?itemDescription ?coord ?kind ?osmn ?osmw ?osmr ?frwiki WHERE { SERVICE wikibase:box { ?item wdt:P625 ?coord. bd:serviceParam wikibase:cornerSouthWest "Point(5.00 44.65)"^^geo:wktLiteral. bd:serviceParam wikibase:cornerNorthEast "Point(5.75 45.30)"^^geo:wktLiteral. } { ?item wdt:P1435 ?h. BIND("patrimoine" AS ?kind) } UNION { ?item wdt:P31 wd:Q484170. BIND("commune" AS ?kind) } OPTIONAL { ?item wdt:P11693 ?osmn } OPTIONAL { ?item wdt:P10689 ?osmw } OPTIONAL { ?item wdt:P402 ?osmr } OPTIONAL { ?frwiki schema:about ?item; schema:isPartOf } SERVICE wikibase:label { bd:serviceParam wikibase:language "fr". } }''' raw = urllib.request.urlopen(urllib.request.Request('https://query.wikidata.org/sparql?' + urllib.parse.urlencode({'query': sp, 'format': 'json'}), headers={'User-Agent': UA, 'Accept': 'application/sparql-results+json'}), timeout=120).read() b = json.loads(raw)['results']['bindings']; json.dump(b, open(f, 'w', encoding='utf8'), ensure_ascii=False); print('wd_sparql', len(b))