#!/usr/bin/env python # coding: utf-8 # In[17]: # ms-python.python added import os try: os.chdir(os.path.join(os.getcwd(), 'MyGeoScripts')) print(os.getcwd()) except: pass # # In[4]: import geopandas as gpd import pandas as pd shapefile = '../Data/County/County.shp' # read shape file using geopandas county = gpd.read_file(shapefile) county.head() # In[5]: county = gpd.read_file(shapefile)[['COUNTY','geometry']] county.head() # In[6]: datafile = '../Data/County/kewi_water_test_and_results1.csv' df = pd.read_csv(datafile,sep=',') df.head() # In[7]: #take county and water alkalinity levels alkaline_df1 = df[['COUNTY','ALKALINITY_Mg/L']] #renamed second column alkaline_df1.rename(columns={"ALKALINITY_Mg/L" : "ALKALINITY"}, inplace=True) alkaline_df1.head() alkaline_df1.describe() # In[8]: # remove rows with missing values in alkalinity alkaline_df2= alkaline_df1.dropna() alkaline_df2.head() # In[9]: alkaline_df2.dtypes # In[10]: # remove duplicated county names by getting the mean alkalinity of the respective duplicates and having it as one county alkaline_df3 = alkaline_df2.groupby('COUNTY').mean().reset_index() alkaline_df3.head() alkaline_df1.describe() # In[11]: # merge county(geodata) and alkaline_df3(alkaline values) frames # perform left merge to preserve every row on county geodata merged = county.merge(alkaline_df3,how = 'left') # replace NaN values with string NoData merged.fillna('No data',inplace=True) merged.head() # In[12]: import json # read data(a dataframe) to json merged_json = json.loads(merged.to_json()) # convert to string like object json_data = json.dumps(merged_json) # json_data # In[13]: from bokeh.io import output_notebook, show, output_file from bokeh.plotting import figure from bokeh.models import GeoJSONDataSource, LinearColorMapper, ColorBar from bokeh.palettes import brewer #Input GeoJSON source that contains features for plotting. geosource = GeoJSONDataSource(geojson = json_data) palette = brewer['YlGnBu'][8] #Reverse color order so that dark blue is highest alkalinity palette = palette[::-1] #Instantiate LinearColorMapper that linearly maps numbers in a range, into a sequence of colors. color_mapper = LinearColorMapper(palette = palette, low = 1, high = 38600) #Define custom tick labels for color bar. tick_labels = {'0': '0%','9650':'25%', '19300':'50%','28950':'75%', '38600': '100%'} #Create color bar. color_bar = ColorBar(color_mapper=color_mapper, label_standoff=8,width = 500, height = 20, border_line_color=None,location = (0,0), orientation = 'horizontal', major_label_overrides = tick_labels) #Create figure object. p = figure(title = 'Alkalinity of water in Kenyan counties', plot_height = 700 , plot_width = 700, toolbar_location = None) p.xgrid.grid_line_color = None p.ygrid.grid_line_color = None #Add patch renderer to figure. p.patches('xs','ys', source = geosource,fill_color = {'field' :'ALKALINITY', 'transform' : color_mapper}, line_color = 'black', line_width = 0.25, fill_alpha = 1) #Specify figure layout. p.add_layout(color_bar, 'below') #Display figure inline in Jupyter Notebook. output_notebook() #Display figure. show(p) # In[14]: from bokeh.io import curdoc, output_notebook from bokeh.models import Slider, HoverTool from bokeh.layouts import widgetbox, row, column #Input GeoJSON source that contains features for plotting. geosource = GeoJSONDataSource(geojson = json_data) #Define a sequential multi-hue color palette. palette = brewer['BuPu'][9] #Reverse color order so that dark blue is highest alkalinity. palette = palette[::-1] #Instantiate LinearColorMapper that linearly maps numbers in a range, into a sequence of colors. Input nan_color. color_mapper = LinearColorMapper(palette = palette, low = 1, high = 1000, nan_color = '#d9d9d9') #Define custom tick labels for color bar. #tick_labels = {'0': 'No data','200':'200', '19300':'50%','28950':'75%', '38600': '100%'} #Add hover tool hover = HoverTool(tooltips = [ ('Country/region','@COUNTY'),('% ALKALINITY_MG/L', '@ALKALINITY')]) #Create color bar. color_bar = ColorBar(color_mapper=color_mapper, label_standoff=8,width = 1000, height = 20, border_line_color=None,location = (0,0), orientation = 'horizontal', major_label_overrides = tick_labels) #Create figure object. p = figure(title = 'Alkalinity of water in Kenyan counties', plot_height = 750 , plot_width = 700, toolbar_location = None, tools = [hover]) p.xgrid.grid_line_color = None p.ygrid.grid_line_color = None #Add patch renderer to figure. p.patches('xs','ys', source = geosource,fill_color = {'field' :'ALKALINITY', 'transform' : color_mapper}, line_color = 'black', line_width = 0.25, fill_alpha = 1) p.add_layout(color_bar, 'below') # # Make a column layout of widgetbox(slider) and plot, and add it to the current document layout = column(p,widgetbox()) curdoc().add_root(layout) #Display plot inline in Jupyter notebook output_notebook() #Display plot show(layout)