Skip to content
This repository has been archived by the owner on Nov 8, 2018. It is now read-only.

Simplify rendered outline #226

Open
wants to merge 3 commits into
base: odes-client
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions App/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
from operator import itemgetter
from os.path import join, dirname
from threading import Thread
import json, os
import json, os, math

import requests
import uritemplate
import psycopg2
import shapely.geometry

from flask import (
Blueprint, jsonify, Response, render_template, url_for, request, session
Expand Down Expand Up @@ -124,9 +125,28 @@ def wof_geojson(id):
'''
template = 'http://whosonfirst.mapzen.com/spelunker/id/{id}.geojson'
url = uritemplate.expand(template, dict(id=id))
wof_resp = requests.get(url)

headers = {key: val for (key, val) in wof_resp.headers.items()
if key in ('Content-Type', 'Content-Length')}

return Response(wof_resp.content, headers=headers)
while True:
wof_head = requests.head(url)
if wof_head.status_code in (301, 302, 303):
url = wof_head.headers.get('Location')
else:
break

if wof_head.status_code != 200:
return Response('No WoF with ID {}'.format(id), status=404)

if request.args.get('raw') == 'yes':
return Response(url, status=302, headers={'Location': url})

geojson = requests.get(url).json()
geom = shapely.geometry.shape(geojson.get('geometry', {}))
print('Raw {} chars of WKT with area {:.6f}'.format(len(str(geom)), geom.area))

x1, y1, x2, y2 = geom.bounds
hypot = math.hypot(x1 - x2, y1 - y2)
simple = geom.simplify(hypot/100)
print('Simplified to {} chars of WKT with tolerance {:.6f}'.format(len(str(simple)), hypot/100))

geojson['geometry'] = shapely.geometry.mapping(simple)
return jsonify(geojson)
2 changes: 1 addition & 1 deletion App/templates/metro.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ <h2>{{ metro.name }}</h2>
<div id="encompassed">
<h3>{{ wof_name }}</h3>
<p>To clip this extract to the <span class="legend-blue">{{wof_name}} boundary</span>, use this outline:</p>
<a class="link" href="{{ url_for('Metro-Extracts.wof_geojson', id=wof_id)}}">{{wof_name}} GEOJSON</a>
<a class="link" href="{{ url_for('Metro-Extracts.wof_geojson', id=wof_id)}}?raw=yes">{{wof_name}} GEOJSON</a>
</div>
{% endif %}

Expand Down
2 changes: 1 addition & 1 deletion App/templates/odes/extract.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ <h2>{{extract.name or extract.wof.name or extract.id or extract.odes.id}}</h2>
{% if extract.wof.name and extract.wof.id %}
<div id="encompassed" style="margin-top: 40px;">
<p>To clip this extract to the <span class="legend-blue">{{extract.wof.name}} boundary</span>, use this outline:</p>
<a class="link" href="{{ url_for('Metro-Extracts.wof_geojson', id=extract.wof.id)}}">{{extract.wof.name}} GEOJSON</a>
<a class="link" href="{{ url_for('Metro-Extracts.wof_geojson', id=extract.wof.id)}}?raw=yes">{{extract.wof.name}} GEOJSON</a>
</div>
{% endif %}
<h3>Downloads</h3>
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ beautifulsoup4==4.4.1
httmock==1.2.5
psycopg2==2.6.1
mock==2.0.0

Shapely==1.5.16