将 fiona 结构展平为散景字典
flatten a fiona structure to dictionary for bokeh
我有一个 fiona 数据结构
http://toblerity.org/fiona/manual.html
我试着把它弄平,这样我就可以在散景中绘制它。
的缩短版本及其邮政编码边界形状文件
下面是一条记录,例如来自 fiona 的邮政编码 43452,缩短版本
{'geometry': {'coordinates': [[(-83.674464, 41.331119),
(-83.67444499999999,41.331123999999996),
(-83.67215, 41.331634)
]],
'type': 'Polygon'},
'id': '1',
'properties': OrderedDict([('ZCTA5CE10', '43452'),
('GEOID10', '43452'),
('CLASSFP10', 'B5'),
('MTFCC10', 'G6350'),
('FUNCSTAT10', 'S'),
('ALAND10', 121783676),
('AWATER10', 13437379),
('INTPTLAT10', '+41.5157923'),
('INTPTLON10', '-082.9809454')]),
'type': 'Feature'}
https://docs.bokeh.org/en/latest/docs/gallery/texas.html
我看例子来自Bokeh example
我需要这样的结构
{
'zipcode':[43452] , 'rate':[1]
,'x':[[83.674464,-83.67444499999999,-83.67215]]
,'y':[[41.331119,41.331123999999996,41.331634]]
}
因为这是一条记录,如果我想做两个邮政编码,它会是这样的
{
'zipcode':[43452,41111] , 'rate':[1,2]
,'x':[[83.674464,-83.67444499999999,-83.67215],[66.6,-77.7,88.9]]
,'y':[[41.331119,41.331123999999996,41.331634],[66.2,-77.2,88.3]]
}
我想我可以一次做一个循环和一个记录,然后将每一行追加到字典结构中。看起来很难看,我想有更简单的方法吗?
我用来压平的代码...丑..
xx=[[*x] for x in zip(*[(1,2),(3,4),(5,6)])]
kk=0
for feat in c:
kk=kk+1
#pprint.pprint(feat)
if (kk>1):
break
#xx=[[*x] for x in zip(*feat['geometry']['coordinates'][0][1])]
#pprint.pprint(feat['geometry']['coordinates'][0])
tt=feat['geometry']['coordinates'][0]
xx=[[*x] for x in zip(*tt)]
x_cor=xx[0]
y_cor=xx[1]
print (x_cor)
print(y_cor)
#print(xx)
绘制地理数据时使用 Bokeh 的 GeoJSONDataSource
更容易。下面的示例显示了如何使用 Fiona and/or GeoPandas 读取和转换 shapefile,您可以使用其中任何一个。并用 Bokeh 绘制结果。
进口
import geopandas as gpd
import fiona
import json
from bokeh.io import show, output_notebook
from bokeh.models import GeoJSONDataSource, LogColorMapper
from bokeh.palettes import Viridis6 as palette
from bokeh.plotting import figure
输入数据
shapefile = 'cb_2016_us_county_500k.shp' # from census.gov
正在使用 Geopandas 加载数据
# read all counties
counties = gpd.read_file(shapefile)
# filter only Texas
texas = counties.query('STATEFP=="48"')
# Export to geojson
geojson = texas.to_json()
或与菲奥娜一起阅读
# open shapefile
with fiona.open(r'D:\Data\US_counties\cb_2016_us_county_500k.shp') as f:
# filter Texas
texas = list(filter(lambda x: x['properties']['STATEFP'] == "48", f.values()))
# export to geojson
geojson = json.dumps({'type': 'FeatureCollection', 'features': texas})
使用散景绘图
color_column = 'ALAND'
geo_source = GeoJSONDataSource(geojson=geojson)
p = figure(title="Texas")
p.patches('xs', 'ys', fill_alpha=0.7,
fill_color={'field': color_column, 'transform': LogColorMapper(palette=palette)},
line_color='black', line_width=0.5, source=geo_source)
show(p)
我有一个 fiona 数据结构 http://toblerity.org/fiona/manual.html
我试着把它弄平,这样我就可以在散景中绘制它。
的缩短版本及其邮政编码边界形状文件下面是一条记录,例如来自 fiona 的邮政编码 43452,缩短版本
{'geometry': {'coordinates': [[(-83.674464, 41.331119),
(-83.67444499999999,41.331123999999996),
(-83.67215, 41.331634)
]],
'type': 'Polygon'},
'id': '1',
'properties': OrderedDict([('ZCTA5CE10', '43452'),
('GEOID10', '43452'),
('CLASSFP10', 'B5'),
('MTFCC10', 'G6350'),
('FUNCSTAT10', 'S'),
('ALAND10', 121783676),
('AWATER10', 13437379),
('INTPTLAT10', '+41.5157923'),
('INTPTLON10', '-082.9809454')]),
'type': 'Feature'}
https://docs.bokeh.org/en/latest/docs/gallery/texas.html
我看例子来自Bokeh example 我需要这样的结构
{
'zipcode':[43452] , 'rate':[1]
,'x':[[83.674464,-83.67444499999999,-83.67215]]
,'y':[[41.331119,41.331123999999996,41.331634]]
}
因为这是一条记录,如果我想做两个邮政编码,它会是这样的
{
'zipcode':[43452,41111] , 'rate':[1,2]
,'x':[[83.674464,-83.67444499999999,-83.67215],[66.6,-77.7,88.9]]
,'y':[[41.331119,41.331123999999996,41.331634],[66.2,-77.2,88.3]]
}
我想我可以一次做一个循环和一个记录,然后将每一行追加到字典结构中。看起来很难看,我想有更简单的方法吗?
我用来压平的代码...丑..
xx=[[*x] for x in zip(*[(1,2),(3,4),(5,6)])]
kk=0
for feat in c:
kk=kk+1
#pprint.pprint(feat)
if (kk>1):
break
#xx=[[*x] for x in zip(*feat['geometry']['coordinates'][0][1])]
#pprint.pprint(feat['geometry']['coordinates'][0])
tt=feat['geometry']['coordinates'][0]
xx=[[*x] for x in zip(*tt)]
x_cor=xx[0]
y_cor=xx[1]
print (x_cor)
print(y_cor)
#print(xx)
绘制地理数据时使用 Bokeh 的 GeoJSONDataSource
更容易。下面的示例显示了如何使用 Fiona and/or GeoPandas 读取和转换 shapefile,您可以使用其中任何一个。并用 Bokeh 绘制结果。
进口
import geopandas as gpd
import fiona
import json
from bokeh.io import show, output_notebook
from bokeh.models import GeoJSONDataSource, LogColorMapper
from bokeh.palettes import Viridis6 as palette
from bokeh.plotting import figure
输入数据
shapefile = 'cb_2016_us_county_500k.shp' # from census.gov
正在使用 Geopandas 加载数据
# read all counties
counties = gpd.read_file(shapefile)
# filter only Texas
texas = counties.query('STATEFP=="48"')
# Export to geojson
geojson = texas.to_json()
或与菲奥娜一起阅读
# open shapefile
with fiona.open(r'D:\Data\US_counties\cb_2016_us_county_500k.shp') as f:
# filter Texas
texas = list(filter(lambda x: x['properties']['STATEFP'] == "48", f.values()))
# export to geojson
geojson = json.dumps({'type': 'FeatureCollection', 'features': texas})
使用散景绘图
color_column = 'ALAND'
geo_source = GeoJSONDataSource(geojson=geojson)
p = figure(title="Texas")
p.patches('xs', 'ys', fill_alpha=0.7,
fill_color={'field': color_column, 'transform': LogColorMapper(palette=palette)},
line_color='black', line_width=0.5, source=geo_source)
show(p)