Skip to content

Instantly share code, notes, and snippets.

@urschrei
Last active May 8, 2018 15:19
Show Gist options
  • Save urschrei/d7bcd6523117699e4ff96ccfba9300b6 to your computer and use it in GitHub Desktop.
Save urschrei/d7bcd6523117699e4ff96ccfba9300b6 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# needs Pandas, Geopandas, Shapely. Should work on 2.7.x and 3.6.x
# this will only dump properties that are open or partially open today
# this can be adjusted by commenting out or altering the values given to isin()
from datetime import date
import geopandas as gp
import pandas as pd
from shapely.geometry import Point, LineString, Polygon, MultiPolygon, MultiPoint, box
df = pd.read_json("https://www.nationaltrust.org.uk/search/data/all-places")
# flip it
df = df.transpose()
# filter by openingTimes value, so anything that's closed isn't dumped
# possible values: ['Open today', 'Closed today', 'Partially open today', nan]
# dropping nan values may give a false negative, but it's better than a false positive
df = df.loc[df['openingTimeStatus'].isin([u'Open today', u'Partially open today'])]
# GeoJSON CRS
crs = {'init': 'epsg:4326'}
today = date.today()
# split lon and lat
df['longitude'] = df.location.apply(lambda c: c['longitude'])
df['latitude'] = df.location.apply(lambda c: c['latitude'])
# build points
geometry = [Point(xy) for xy in zip(df.longitude, df.latitude)]
# drop columns we don't need
df.drop(
['longitude', 'latitude', 'location', 'displayable', 'alternativeImages'],
axis=1,
inplace=True
)
# create GeoDataFrame
gdf = gp.GeoDataFrame(df, crs=crs, geometry=geometry)
# fix image path
gdf['imagePath'] = 'https://www.nationaltrust.org.uk' + gdf['imagePath'].astype(str)
# dump to GeoJSON
gdf.to_file(
"/Users/sth/dev/national_trust/national_trust_allplaces_%s.geojson" % today.strftime("%Y%m%d"),
driver="GeoJSON"
)
Display the source blob
Display the rendered blob
Raw
Loading
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