#!/usr/bin/env python # coding: utf-8 # In[1]: from itertools import chain as ch from bokeh.io import output_file, show, output_notebook from bokeh.models import ( ColumnDataSource, Range1d, HoverTool, LogColorMapper ) from bokeh.palettes import Viridis6 as palette from bokeh.plotting import figure from bokeh.models import GeoJSONDataSource from bokeh.tile_providers import CARTODBPOSITRON import shapefile import json # In[2]: output_notebook() # In[3]: get_ipython().run_line_magic('cd', 'C:\\Users\\Rutger\\Downloads\\newengland') # In[4]: def geojson_create(file_name, outfile): reader = shapefile.Reader(file_name) fields = reader.fields[1:] states = set([record[1] for record in reader.records()]) #print states field_names = [field[0] for field in fields] buffer = [] for sr in reader.shapeRecords(): atr = dict(zip(field_names, sr.record)) geom = sr.shape.__geo_interface__ #a = [list(x) for x in y for y in geom['coordinates']] b = list(ch(*geom['coordinates'])) a = [[list(x) for x in b]] #print a geom['coordinates'] = a buffer.append(dict(type="Feature", geometry = geom, properties = atr)) #print buffer[0] with open(outfile, 'w') as geojson: geojson.write(json.dumps({'type': "GeometryCollection", 'features': buffer}, indent=2) +'\n') return states # In[5]: with open('NEWENGLAND_POLY_ogr.json', 'r') as f: geo_source = GeoJSONDataSource(geojson=f.read()) # In[6]: x_range = Range1d(start=-30000, end=700000) y_range = Range1d(start=600000, end=1600000) p = figure(tools='wheel_zoom,pan', x_range=x_range, y_range=y_range, title='title', width=900, height=450) p.axis.visible = False p.patches('xs', 'ys', fill_color="red", line_color="black", line_width=1.5, source=geo_source) show(p) # In[ ]: