自动根据GeoJSON属性使用geopandas设置轮廓和填充颜色。

huangapple go评论69阅读模式
英文:

Automatically set outline and fill color based on GeoJSON properties with geopandas

问题

我正在制作一个程序,从风暴预测中心(SPC)获取过去的对流展望的GeoJSON数据,并使用geopandas绘制它。根据我的当前代码,它能够正确地将展望绘制到地图上。但是,着色不正确。我注意到SPC返回的GeoJSON包含了类别的轮廓和填充颜色数据 - (在properties字段中)

是否可以使用properties中的strokefill数据自动着色每个MultiPolygon

我的当前代码如下(假设已导入所有包)

我尝试查阅geopandas文档,但没有说明如何使用geojson中的字段来着色多边形。

英文:

I am making a program that retrieves GeoJSON data from past convective outlooks from the Storm Prediction Center (SPC) and plot it using geopandas. With my current code, it is able to plot outlooks correctly onto a map. However, the coloring isn't right. I noticed that the GeoJSON returned by the SPC included outline and fill coloring data for the categories - (in properties field)

{"type": "FeatureCollection", "features": [{"type": "Feature", "geometry": {"type": "MultiPolygon", "coordinates": ...}, "properties": {"DN": 2, "VALID": "202109010100", "EXPIRE": "202109011200", "ISSUE": "202109010042", "LABEL": "TSTM", "LABEL2": "General Thunderstorms Risk", "stroke": "#55BB55", "fill": "#C1E9C1"}}, {"type": "Feature", "geometry": {"type": "MultiPolygon", "coordinates": ...}, "properties": {"DN": 3, "VALID": "202109010100", "EXPIRE": "202109011200", "ISSUE": "202109010042", "LABEL": "MRGL", "LABEL2": "Marginal Risk", "stroke": "#005500", "fill": "#66A366"}}, {"type": "Feature", "geometry": {"type": "MultiPolygon", "coordinates": ...}, "properties": {"DN": 4, "VALID": "202109010100", "EXPIRE": "202109011200", "ISSUE": "202109010042", "LABEL": "SLGT", "LABEL2": "Slight Risk", "stroke": "#DDAA00", "fill": "#FFE066"}}]} 

Is it possible to use the stroke and fill data in properties to automatically color every MultiPolygon?

My current code is below (assume that all packages are imported)

outlook = "https://www.spc.noaa.gov/products/outlook/archive/2021/day1otlk_20210901_0100_cat.lyr.geojson"
world = geopandas.read_file(
    geopandas.datasets.get_path('naturalearth_lowres')
)
df = geopandas.read_file(outlook)
ax = world.plot(color='white', edgecolor='#333333',linewidth=0.3)
print(type(df))
s = geopandas.GeoDataFrame(df)
s.plot(ax=ax,markersize=0.7,figsize=(1000,1000))
ax.set_xlim(-140, -70) # focus on continental US
ax.set_ylim(25, 50) # focus on continental US
plt.savefig('outlook.jpg', dpi=360) # save as outlook.jpg

I tried looking in the geopandas documentation but it didn't state how to use fields in geojson to color polygons.

答案1

得分: 1

你离最终结果只差一个 kwargplot 接受 Series 作为 color 参数:

> colorstr, np.array, pd.Series(默认:None)如果指定,所有对象将以统一的颜色着色。

s.plot(ax=ax, color=s["fill"], markersize=0.7, figsize=(1000,1000)) # <-  color=s["fill"](此处添加)

输出:

自动根据GeoJSON属性使用geopandas设置轮廓和填充颜色。

英文:

You're one kwarg away from the final result. plot accepts Series as an argument for color :

> colorstr, np.array, pd.Series (def: None) If specified, all objects
> will be colored uniformly.

s.plot(ax=ax, color=s["fill"], markersize=0.7, figsize=(1000,1000)) # <-  color=s["fill"] (added here)

Output :

自动根据GeoJSON属性使用geopandas设置轮廓和填充颜色。

huangapple
  • 本文由 发表于 2023年2月7日 02:06:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75365019.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定