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
%cd C:\Users\Rutger\Downloads\newengland
C:\Users\Rutger\Downloads\newengland
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
with open('NEWENGLAND_POLY_ogr.json', 'r') as f:
geo_source = GeoJSONDataSource(geojson=f.read())
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)