解决 Geopandas 和 Altair 中的上行绘图和投影问题

问题描述 投票:0回答:1

我正在尝试使用 ONS 的一些数据绘制伦敦的多层地图。

import geopandas as gpd
from shapely import wkt

# Convert the 'geometry' column to shapely geometry objects
london_wards_shp['geometry'] = london_wards_shp['geometry'].apply(wkt.loads)
london_wards_gpd = gpd.GeoDataFrame(london_wards_shp, geometry='geometry')
london_wards_gpd = london_wards_gpd.set_crs(epsg=4326)
london_wards_gpd.plot()

返回错误:

ValueError: aspect must be finite and positive 

我找到了绘图的解决方案:

london_wards_gpd.plot(aspect=1)

但后来我想使用 Altair 来构建图层,一个具有整个图层图的图层。

> london_wards_map = alt.Chart(london_wards_gpd).mark_geoshape(
>     fill=None,  # No fill
>     stroke='darkgray',  # Black stroke
>     strokeWidth=1  # Stroke width ).encode(
>     tooltip='NAME:N'  # Replace 'NAME' with the actual name of the column that contains the ward names ).properties(
>     width=800,
>     height=600 ).project(
>     type='identity' )
> 
> hackney_wards = london_wards_gpd[london_wards_gpd['DISTRICT']
> =='Hackney']
> #hackney_wards = gpd.GeoDataFrame(hackney_wards, geometry='geometry')  # Convert DataFrame to GeoDataFrame
> #hackney_wards = hackney_wards.set_crs(epsg=4326) hackney_layer = alt.Chart(hackney_wards).mark_geoshape(
>     fill='lightgray',  # No fill
>     stroke='darkgray',  # Black stroke
>     strokeWidth=1  # Stroke width ).encode(
>     tooltip='NAME:N'  # Replace 'NAME' with the actual name of the column that contains the ward names ).properties(
>     width=800,
>     height=600 ).project(
>     type='identity' )

london_wards_map + hackney_layer

那为什么是颠倒的呢?

另一个问题。

我想在上面放第三层。

邮政编码_meals.crs = 4326 点 = alt.Chart(postcode_meals).mark_circle(color='#008751').encode( 经度='长:Q', 纬度='lat:Q', size=alt.Size('count:Q',scale=alt.Scale(domain=[postcode_meals['count'].min(),postcode_meals['count'].max()],range=[10, 1000 ])), # 根据需要调整范围 tooltip=['病区', '邮政编码', '计数', '长', '纬度'] ).properties(title='按邮政编码列出的优惠券地图')

text = alt.Chart(postcode_meals).mark_text(dy=-5).encode( 经度='长:Q', 纬度='lat:Q', text='邮政编码' )

将病区边界与点和文本相结合

积分

作为测试图,它看起来是正确的。

但是当我尝试将它们分层时......

不太确定如何诊断这里的投影问题

python geopandas altair
1个回答
0
投票

我找到了第一个问题的答案。翻转数据框可以像这样完成:

定义一个函数来翻转 y 坐标

def flip_geom(geom):
    geom_type = geom.geom_type
    if geom_type == 'Polygon':
        geom = transform(lambda x, y, z=None: (x, -y), geom)
    elif geom_type == 'MultiPolygon':
        geom = transform(lambda x, y, z=None: (x, -y), geom)
    else:
        raise ValueError('Unhandled geometry type: ' + repr(geom_type))
    return geom
© www.soinside.com 2019 - 2024. All rights reserved.