【发布时间】:2020-02-13 05:56:48
【问题描述】:
我想使用 CDS(ColumnDataSource) 因为我将使用滑块更新地图,我知道我可以使用 GeoJsonDataSource 并且可以使用 geojson 更新它,尽管我会将它嵌入到 webapp尽管我宁愿使用 CDS,但 geojson 会非常重视它,因为我对整个事情有更多的控制权。 还有另一种方法可以在不溶解它的情况下获得“几何”列(涉及大量内存)? 所以当它是一个多多边形时,补丁不会加载它,这只会给我简单的多边形。如果有人可以帮助我。 提前致谢! 数据可在此处获得scroll down and select 2008 Local Electoral Areas
import pandas as pd
import numpy as np
import geopandas as gpd
from bokeh.io import (output_notebook, show, curdoc, output_file)
from bokeh.plotting import figure
from bokeh.models import GeoJSONDataSource, LinearColorMapper, ColorBar, ColumnDataSource
from bokeh.models.glyphs import MultiPolygons
from copy import deepcopy
from bokeh.palettes import Pastel2, viridis, inferno, magma, Paired, Spectral, brewer, Greens, YlGn
import matplotlib.pyplot as plt
output_notebook()
geoIrl = gpd.read_file('Census2011_Local_Electoral_Areas_2008.shp')
irlg = geoIrl[['COUNTY','geometry']]
irl = irlg.dissolve(by='COUNTY', aggfunc='sum')
dfirl = deepcopy(irl)
#extracting the xs and ys
def getPolyCoords(row, geom, coord_type):
"""Returns the coordinates ('x|y') of edges/vertices of a Polygon/others"""
# Parse the geometries and grab the coordinate
geometry = row[geom]
#print(geometry.type)
if geometry.type=='Polygon':
if coord_type == 'x':
# Get the x coordinates of the exterior
# Interior is more complex: xxx.interiors[0].coords.xy[0]
return list( geometry.exterior.coords.xy[0] )
elif coord_type == 'y':
# Get the y coordinates of the exterior
return list( geometry.exterior.coords.xy[1] )
if geometry.type in ['Point', 'LineString']:
if coord_type == 'x':
return list( geometry.xy[0] )
elif coord_type == 'y':
return list( geometry.xy[1] )
if geometry.type=='MultiLineString':
all_xy = []
for ea in geometry:
if coord_type == 'x':
all_xy.append(list( ea.xy[0] ))
elif coord_type == 'y':
all_xy.append(list( ea.xy[1] ))
return all_xy
if geometry.type=='MultiPolygon':
all_xy = []
for ea in geometry:
if coord_type == 'x':
all_xy.append(list( ea.exterior.coords.xy[0] ))
elif coord_type == 'y':
all_xy.append(list( ea.exterior.coords.xy[1] ))
return all_xy
else:
# Finally, return empty list for unknown geometries
return []
dfirl['xs'] = dfirl.apply(getPolyCoords, geom='geometry', coord_type='x', axis=1)
dfirl['ys'] = dfirl.apply(getPolyCoords, geom='geometry', coord_type='y', axis=1)
rX = 'CARLOW', 'CAVAN', 'CLARE', 'CORK', 'DONEGAL', 'DUBLIN', 'GALWAY','KERRY', 'KILDARE', 'KILKENNY', 'LAOIS', 'LEITRIM', 'LIMERICK','LONGFORD', 'LOUTH', 'MAYO', 'MEATH', 'MONAGHAN', 'OFFALY', 'ROSCOMMON', 'SLIGO', 'TIPPERARY', 'WATERFORD', 'WESTMEATH', 'WEXFORD', 'WICKLOW'
srcmap = ColumnDataSource(data=dict(x=rX, y02=df_map['2002'], y06=df_map['2006'], y11=df_map['2011'], y16=df_map['2016'], xs=df_map['xs'], ys=df_map['ys']))
pc = figure(title = 'test', tools = '', x_axis_location = None, y_axis_location = None)
pc.patches('xs', 'ys', fill_alpha = 0.7, line_width = 0.5, source = srcmap)
#pc.grid.grid_line_color=None
show(pc)
如果我单独选择一个多面体,我将能够使用补丁来绘制它。
xstest =df_map.loc['DUBLIN']['xs']
ystest = df_map.loc['DUBLIN']['ys']
pc = figure(title = 'test', tools = '', x_axis_location = None, y_axis_location = None)
pc.patches(xs=xstest, ys=ystest, fill_alpha = 0.7, line_width = 0.5)
show(pc)
【问题讨论】:
标签: python gis bokeh geojson patch