Skip to content

Commit

Permalink
added commands
Browse files Browse the repository at this point in the history
  • Loading branch information
xrotwang committed May 31, 2021
1 parent 75fd7bc commit 53ca334
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 3 deletions.
19 changes: 16 additions & 3 deletions rantanenurageocommands/geojson.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,28 @@
def register(parser):
# Allow filtering by language-only, dialect-only, list of glottocodes
parser.add_argument('-o', '--output', type=PathType(must_exist=False), default=None)
parser.add_argument('--callback', default=None)
parser.add_argument('--branches', default=None, type=lambda s: s.split(','))


def run(args):
res = dict(type='FeatureCollection', features=[])
cldf = Dataset().cldf_reader()
features = {r['Language_ID']: r['SpeakerArea'] for r in cldf['areas.csv']}
#for l in cldf.objects('LanguageTable'):
res['features'] = list(features.values())

def filter(l):
res = True
if args.branches:
res = res and l['Branch'] in args.branches
return res

lids = [l['ID'] for l in cldf['LanguageTable'] if filter(l)]
res['features'] = [f for lid, f in features.items() if lid in lids]
if args.output:
dump(res, args.output, indent=4)
if args.callback:
args.output.write_text('{}({});'.format(
args.callback, json.dumps(res)), encoding='utf8')
else:
dump(res, args.output, indent=4)
else:
print(json.dumps(res, indent=4))
29 changes: 29 additions & 0 deletions rantanenurageocommands/htmlmap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""
"""
import shutil
import pathlib
import argparse
import webbrowser

from clldutils.clilib import PathType

from . import geojson


def register(parser):
# Allow filtering by language-only, dialect-only, list of glottocodes
parser.add_argument(
'-o', '--output', type=PathType(must_exist=False, type='dir'), default=pathlib.Path('.'))
parser.add_argument('--callback', default='parseGeojson', help=argparse.SUPPRESS)
parser.add_argument('--branches', default=None, type=lambda s: s.split(','))


def run(args):
outdir = args.output
if not outdir.exists():
outdir.mkdir()
args.output = outdir / 'languages.js'
geojson.run(args)
shutil.copy(pathlib.Path(__file__).parent / 'index.html', outdir)
webbrowser.open(str(outdir / 'index.html'))
86 changes: 86 additions & 0 deletions rantanenurageocommands/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<!DOCTYPE html>
<html>
<head>
<title>The Uralic languages</title>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css"
integrity="sha512-xodZBNTC5n17Xt2atTPuE1HxjVMSvLVW9ocqUKLsCC5CXdbqCmblAshOMAS6/keqq/sMZMZ19scR4PsZChSR7A=="
crossorigin=""/>
<script src="https://unpkg.com/[email protected]/dist/leaflet.js"
integrity="sha512-XQoYMqMTK8LvdxXYG3nZ448hOEQiglfqkJs1NOQV44cWnUrBc8PkAOcXy20w0vlaXaVUearIOBhiXZ5V3ynxwA=="
crossorigin=""></script>
<style>
html, body {
height: 100%;
margin: 0;
}

#map {
width: 100%;
height: 800px;
}
</style>
</head>
<body>

<div id='map'></div>
<script>
var langs;
function parseGeojson(d) {langs = d}
</script>
<script src="languages.js" type="text/javascript"></script>
<script>
var highlight;
var map = L.map('map').setView([39.74739, 10], 3);
var group = new L.featureGroup;
var languages = {};

L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 19,
attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);

function onEachFeature(feature, layer) {
layer.on({
click: function (e) {
if (highlight !== undefined) {
highlight.setStyle({'fillColor': '#999'});
}
e.target.setStyle({'fillColor': '#f00'});
highlight = e.target;
}
});

var popupContent = "<p> " + feature.properties.Language;
if (feature.properties.Dialect) {
popupContent += ' [' + feature.properties.Dialect + ']';
}
popupContent += "</p>";
layer.bindPopup(popupContent);

group.addLayer(layer);
if (languages[feature.properties.Language] === undefined) {
languages[feature.properties.Language] = new L.featureGroup;
languages[feature.properties.Language].addTo(map);
}
languages[feature.properties.Language].addLayer(layer)
}

L.geoJSON(langs, {
onEachFeature: onEachFeature,
style: function (feature) {
return {
weight: 2,
color: "#999",
opacity: 1,
fillColor: "#B0DE5C",
fillOpacity: 0.8
}
}
}).addTo(map);
map.fitBounds(group.getBounds());
L.control.layers({}, languages).addTo(map);
</script>
</body>
</html>

0 comments on commit 53ca334

Please sign in to comment.