Skip to content

Instantly share code, notes, and snippets.

@mthh
Last active January 5, 2018 10:56
Show Gist options
  • Save mthh/bbeedc8a422607726eddfb34fc951acd to your computer and use it in GitHub Desktop.
Save mthh/bbeedc8a422607726eddfb34fc951acd to your computer and use it in GitHub Desktop.
Location of stuff related to "Commune de Paris"
license: gpl-3.0
border: no

Location of stuff related to "Commune de Paris"

Extracted from OSM (© OpenStreetMap Contributors):

  • commemorative plaques whose inscription contains "commune" or "1871",
  • streets named after "communards" or "versaillais".
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from overpy import Overpass
import ujson as json
def rec_insc(prefix, tags):
temp, n = [], 1
while True:
insc = tags.get('{}:{}'.format(prefix, n))
if not insc: break
temp.append(insc)
n += 1
return ' '.join(temp)
def reconstruct_inscription(tags):
if 'inscription' in tags.keys():
tags['complete_inscription'] = tags['inscription']
elif 'inscription:fr' in tags.keys():
tags['complete_inscription'] = tags['inscription:fr']
elif 'inscription:1' in tags.keys():
tags['complete_inscription'] = rec_insc('inscription', tags)
elif 'inscription:fr:1' in tags.keys():
tags['complete_inscription'] = rec_insc('inscription:fr', tags)
elif 'inscription:url' in tags.keys():
print('Only "inscription:url" in tags')
else:
pass
# print('No inscription in tags')
overpass_api = Overpass()
result = overpass_api.query('''[out:json];
area[name = "Paris"]->.a;
(
node(area.a)[memorial=plaque];
); out;''')
nodes = result.get_nodes()
possible_inscription_keys = set()
_ = [
[possible_inscription_keys.add(k)
for k in n.tags.keys() if 'inscription' in k]
for n in nodes]
nodes_commune = []
features = []
for n in nodes:
reconstruct_inscription(n.tags)
if 'complete_inscription' in n.tags:
txt = n.tags['complete_inscription'].lower()
if 'commune' in txt or '1871 ' in txt:
nodes_commune.append(n)
features.append({
"type": "Feature",
"properties": n.tags,
"geometry": {
"type": "Point",
"coordinates": [n.lon, n.lat]}
})
with open('plaques_commune.geojson', 'wb') as f:
f.write(json.dumps({
"type": "FeatureCollection", "features": features}).encode())
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.41.0/mapbox-gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.41.0/mapbox-gl.css' rel='stylesheet' />
<style>
body {
margin: 0;
padding: 0;
}
#cont {
width: 800px;
height: 600px;
margin: auto;
}
#map {
width: 100%;
height: 100%;
}
.mapboxgl-popup {
max-width: 400px;
font: 12px 'Helvetica Neue', Arial, Helvetica, sans-serif;
}
</style>
</head>
<body>
<div id="cont"><div id='map'></div>
</div>
<script>
mapboxgl.accessToken = 'pk.eyJ1IjoibXRoaCIsImEiOiJjaXBkcmYyOHUwMDBndWVuZnU5YWdka2w2In0.fbZYSGMQgjc2B3_LdWvRbA';
let map;
d3.queue(2)
.defer(d3.json, 'plaques_commune.geojson')
.defer(d3.json, 'communards_streets.geojson')
.await(prepare)
function prepare(error, points, lines) {
if (error) throw error;
map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/dark-v9',
center:[2.332697, 48.859230],
zoom: 11.379,
});
map.on('load', function() {
map.addLayer({
id: 'streets',
type: 'line',
source: { type: 'geojson', data: lines },
paint: {
'line-color': 'red',
'line-width': 3.3,
'line-opacity': 1,
},
layout: {
'line-join': 'round',
'line-cap': 'round',
},
});
map.addLayer({
id: 'plaques',
type: 'circle',
source: { type: 'geojson', data: points },
paint: {
'circle-color': 'darkred',
'circle-opacity': 0.80,
'circle-radius': 4,
'circle-stroke-color': 'white',
'circle-stroke-width': 1,
'circle-stroke-opacity': 0.80,
},
layout: {},
});
const popup = new mapboxgl.Popup({
closeButton: false,
closeOnClick: false
});
map.on('mousemove', (e) => {
let _features = map.queryRenderedFeatures(e.point, {
layers: ['plaques', 'streets']
});
map.getCanvas().style.cursor = (_features.length) ? 'pointer' : '';
if (!_features.length) {
popup.remove();
return;
}
let ft = _features[0];
if (ft.geometry.type === 'Point') {
popup.setLngLat(ft.geometry.coordinates)
.setHTML(ft.properties.complete_inscription)
.addTo(map);
} else {
popup.setLngLat(d3.geoCentroid(ft.geometry))
.setHTML(ft.properties.name)
.addTo(map);
}
});
});
}
</script>
</body>
</html>
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment