0

I have some sample code to plot a map of Ontario using Bokeh. The code reads in the shapefile and converts it to a geojson file as suggested from examples available in the internet. The shapefile source data is the Ontario census subdivision geographic boundary from the StatsCan website downloaded as a shapefile.

Image screenshot: https://i.stack.imgur.com/xMibh.jpg

The result so far is an empty chart and I can't figure out what's wrong.

The shapefile is loaded first as a geopandas dataframe and converted to geojson.

Apologies for my lack of stackoverflow etiquette. I'm a new user.

%matplotlib inline
import matplotlib.pyplot as plt
import pandas as pd
import geopandas

import os

from bokeh.plotting import figure, output_file, show, save,output_notebook
from bokeh.models import GeoJSONDataSource, LinearColorMapper, ColorBar
from bokeh.palettes import brewer


pd.options.display.max_rows = 10

workspace = r'C:\Users\user\Documents\lcsd000b16a_e'
CSD_LAYER = geopandas.read_file(os.path.join(workspace,r"lcsd000b16a_e.shp"))

ONT_CSD = CSD_LAYER[CSD_LAYER['PRUID']=='35']
ONT_CSD['geometry'].head()

1372    POLYGON ((7202895.13143 1077367.822855, 720382...
1373    POLYGON ((7205717.394285 1098087.974285, 72058...
1374    POLYGON ((7169056.905715 1216085.682855, 71693...
1614    POLYGON ((7162217.717145 948748.982855, 716229...
1809    POLYGON ((7506330.95143 1116872.145715, 750632... 



# # Get the CRS of our grid
CRS = ONT_CSD.crs
print('FROM:' + str(CRS))

ONT_CSD = ONT_CSD.to_crs(epsg=3857) #transform to webmercator
print('TO: '+ str(ONT_CSD.crs))

FROM:{'init': 'epsg:3347'}
TO: {'init': 'epsg:3857', 'no_defs': True}

import json

#read data to json file
ONT_CSD_json = json.loads(ONT_CSD.to_json())

#convert to string like object
ONT_CSD_JSON_DATA = json.dumps(ONT_CSD_json)

#Input GeoJSON source that contains features for plotting.
geosource = GeoJSONDataSource(geojson = ONT_CSD_JSON_DATA)

#Create figure object.
p = figure(title = 'test', plot_height = 600 , plot_width = 950)
p.xgrid.grid_line_color = None
p.ygrid.grid_line_color = None
#Add patch renderer to figure. 

p.patch('xs','ys', source = geosource,
          line_color = 'black', line_width = 1, fill_alpha = 0.75)
Paul H
  • 65,268
  • 20
  • 159
  • 136
  • By the way, I've done more than a few hours of research across Bokeh libraries and online resources, so I'm not posting this question without having done trial and error tests myself. – pythonchimp Jul 14 '19 at 14:33
  • Using Bokeh version 1.2.0 – pythonchimp Jul 14 '19 at 16:58
  • try changing `p.patch` to `p.patches`. – bexi Jul 16 '19 at 13:38
  • 1
    That worked! Big thanks, bexi. I actually had two issues. The first one you already pointed out. The second one was the full Ontario census division are quite large and it takes Bokeh a long time to plot the full map. Jupyter Notebook was actually freezing through this process. I decided to reduce the size to a smaller area around Toronto and finally got a map displayed. – pythonchimp Jul 17 '19 at 01:24
  • Have you tried simplifying geometry a bit? You do not need full precision for Bokeh plotting and it might allow you to plot the whole area as you wanted before. – martinfleis Jul 18 '19 at 08:52
  • martinfleis, yes I've thinking about that as an alternative. Definitely the type of mapping I'm doing doesn't require a boundary with this much detail. As long as it looks okay on a chart that's good enough. Is there a source you'd recommend to get started? – pythonchimp Jul 24 '19 at 22:05

0 Answers0