Skip to content

Instantly share code, notes, and snippets.

@paregorios
Created September 12, 2016 14:49
Show Gist options
  • Save paregorios/a9c5c5ac56879b3334ef4a8356ca510b to your computer and use it in GitHub Desktop.
Save paregorios/a9c5c5ac56879b3334ef4a8356ca510b to your computer and use it in GitHub Desktop.
import json
from shapely.geometry import MultiPoint, MultiPolygon, Point, Polygon, shape
with open(args.placefile, 'r') as f:
pj = json.load(f)
places = []
for i, pd in enumerate(pj['@graph']):
try:
if i >= limit:
break
except TypeError:
pass
locations = []
for f in pd['features']:
l = {
'geometry': shape(f['geometry']),
'id': f['id'],
'title': f['properties']['title'],
'description': f['properties']['description'],
'uri': f['properties']['link'],
'precision': f['properties']['location_precision'],
}
ll = [z for z in pd['locations'] if z['id'] == l['id']][0]
l['time-periods'] = [z['timePeriod'] for z in ll['attestations']]
locations.append(l)
points = []
for l in locations:
g = l['geometry']
if type(g) == Point:
points.append(g)
elif type(g) == Polygon:
points.extend(
[Point(coord) for coord in g.boundary._get_coords()])
else:
raise Exception('boom')
points = MultiPoint(points)
places.append({
'locations': locations,
'id': pd['id'],
'title': pd['title'],
'description': pd['description'],
'representative-point': Point(pd['reprPoint']),
'types': pd['placeTypes'],
'connects-with': pd['connectsWith'],
'uri': pd['uri'],
'convex-hull': points.convex_hull,
'envelope': points.envelope,
'type': 'feature'
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment