Created
January 16, 2017 17:02
-
-
Save grischard/d1ef9f458baa42518e1f9fd4e21f46b1 to your computer and use it in GitHub Desktop.
Proxy to reproject luref geojson from opendata.vdl.lu
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
from flask import Flask | |
from flask import request | |
from flask import Response | |
import json | |
from pyproj import Proj | |
from pyproj import transform | |
import requests | |
app = Flask(__name__) | |
LUREF = Proj("+init=EPSG:2169") | |
WGS84 = Proj(proj='latlong', datum='WGS84') | |
def __reproject(_p): | |
ll = transform(LUREF, WGS84, _p[0], _p[1]) | |
return [round(ll[0], 7), round(ll[1], 7)] # 7 decimals | |
def reproject(target_url): | |
geojson_object = requests.get(target_url).json() | |
geojson_object.pop("crs", None) # drop crs object | |
for f in geojson_object['features']: | |
f.pop("bbox", None) # drop bbox object | |
if f["geometry"]["type"] == "Point": | |
if f["geometry"]["coordinates"][0] < 1000: | |
continue # Probably not LUREF | |
p = f["geometry"]["coordinates"] | |
p = __reproject(p) | |
elif f["geometry"]["type"] == "Polygon": | |
if f["geometry"]["coordinates"][0][0][0] < 1000: | |
continue # Probably not LUREF | |
for r in f["geometry"]["coordinates"]: | |
for p in r: | |
p = __reproject(p) | |
else: | |
if f["geometry"]["coordinates"][0][0] < 1000: | |
continue # Probably not LUREF | |
for p in f["geometry"]["coordinates"]: | |
p = __reproject(p) | |
return geojson_object | |
@app.route('/<path:path>') | |
def catch_all(path): | |
# Target URL is everything after the url root | |
target_url = request.url[len(request.url_root):] | |
resp = Response(response=json.dumps(reproject(target_url)), | |
status=200, mimetype="application/json") | |
resp.headers.add('Access-Control-Allow-Origin', '*') | |
return resp | |
@app.route('/favicon.ico') | |
def favicon(): | |
return Response(response="404 not found", status=404) | |
@app.route('/') | |
def hello(): | |
vdl = "http://opendata.vdl.lu/odaweb/" | |
catalogjson = requests.get(vdl + "?describe=1").json() | |
catalog = [ | |
'<li><a href="/{vdl}?cat={id}">{name}</a></li>' | |
.format(vdl, id=item["id"], name=item["i18n"]["fr"]["name"]) | |
for item in sorted( | |
catalogjson["data"], key=lambda k: k['i18n']['fr']['name'] | |
) | |
] | |
return """<!doctype html><html><head> | |
<title>LUREF to WGS84 GeoJSON converter</title> | |
</head><body> | |
<h2>LUREF to WGS84 GeoJSON converter microservice</h2> | |
<p>Translates GeoJSON URL from LUREF to WGS84.</p> | |
<h4>What do I do with this?</h4> | |
<pThe output is valid WGS84 geojson, which you can easily | |
<a href="http://leafletjs.com/examples/geojson.html"> | |
display on a map</a></p> | |
<h4>Extra features</h4> | |
<ul><li>Deletes redundant VdL 'crs' and 'bbox' objects</li> | |
<li>Sanity check, if X < 1000 it's probably not LUREF because | |
that point is way outside of Luxembourg, and we won't convert it. | |
<h4>Examples to try</h4> | |
<p>The <a | |
href="https://data.public.lu/en/organizations/ville-de-luxembourg"> | |
City of Luxembourg</a> publishes data in LUREF:</p> | |
<p><ul>{}</ul></p> | |
<p><emph>Mat <span style="color:red">♥</span> codéiert. Hat tip: | |
<a href="https://twitter.com/jaykayone">@jaykayone</a>. | |
</emph></p> | |
</body></html>""".format(''.join(catalog)) | |
if __name__ == '__main__': | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment