Skip to content

Instantly share code, notes, and snippets.

@brendancol
Last active April 23, 2021 17:38
Show Gist options
  • Save brendancol/06c60ff9c708edbca72710bbc951e249 to your computer and use it in GitHub Desktop.
Save brendancol/06c60ff9c708edbca72710bbc951e249 to your computer and use it in GitHub Desktop.
Translating Alaska and Hawaii (Bokeh + Shapely)
import json
from bokeh.io import output_file
from bokeh.plotting import figure, show
from bokeh.models import GeoJSONDataSource
from shapely import affinity
from shapely.geometry import mapping, shape
def get_geojson():
'''
Returns
-------
str - geojson with Alaska and Hawaii translated to fit in extent of Continental US.
states geojson available at:
http://s3.amazonaws.com/bokeh_data/us_states_simplified_albers.geojson
'''
with open('us_states_simplified_albers.geojson') as us_states_file:
geojson = json.loads(us_states_file.read())
for f in geojson['features']:
if f['properties']['NAME'] == 'Alaska':
geom = affinity.translate(shape(f['geometry']), xoff=1.2e6, yoff=-4.8e6)
geom = affinity.scale(geom, .4, .4)
geom = affinity.rotate(geom, 30)
f['geometry'] = mapping(geom)
elif f['properties']['NAME'] == 'Hawaii':
geom = affinity.translate(shape(f['geometry']), xoff=4.6e6, yoff=-1.1e6)
geom = affinity.rotate(geom, 30)
f['geometry'] = mapping(geom)
return json.dumps(geojson)
fig = figure(plot_width=1250, plot_height=600, x_range=(-3e6, 3e6), y_range=(-2e6, 1.5e6))
fig.grid.grid_line_alpha = 0
fig.axis.visible = False
fig.patches(xs='xs', ys='ys', line_color='white', source=GeoJSONDataSource(geojson=get_geojson()), alpha=.85)
output_file('translate_map.html')
show(fig)
@brendancol
Copy link
Author

screen shot 2016-07-27 at 5 04 35 pm

@chris-vecchio
Copy link

Thanks for this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment