首页 > 解决方案 > 卡托皮县寄宿生

问题描述

您如何在 Cartopy 中绘制美国县界?

绘制州和国家边界非常简单

ax.add_feature(cfeature.BORDERS.with_scale('50m'))
ax.add_feature(cfeature.STATES.with_scale('50m'))

但我似乎找不到类似的方法来添加县界。这是 Basemap 的优点之一。

标签: pythoncartopy

解决方案


考虑到 cartopy 绘制 shapefile 的能力,这个问题基本上可以归结为“我在哪里可以找到美国县的轮廓?”。

在http://www.naturalearthdata.com/forums/topic/us-county-shape-file/的自然地球论坛上提出了类似的问题。它指向http://nationalatlas.gov/mld/countyp.html的一个位置,不幸的是,该位置存在一定程度的比特腐烂。一个快速的谷歌建议现在可以在以下位置找到:

https://nationalmap.gov/small_scale/atlasftp.html?openChapters=chpbound#chpbound

我决定下载其中一个县 shapefile:

https://prd-tnm.s3.amazonaws.com/StagedProducts/Small-scale/data/Boundaries/countyl010g_shp_nt00964.tar.gz

有了它,我使用 cartopy 的 shapereader 来获取几何图形,并创建了一个自定义功能,然后可以将其添加到轴上:

import cartopy.crs as ccrs
import cartopy.feature as cfeature
import cartopy.io.shapereader as shpreader
import matplotlib.pyplot as plt


reader = shpreader.Reader('countyl010g.shp')

counties = list(reader.geometries())

COUNTIES = cfeature.ShapelyFeature(counties, ccrs.PlateCarree())

plt.figure(figsize=(10, 6))
ax = plt.axes(projection=ccrs.PlateCarree())

ax.add_feature(cfeature.LAND.with_scale('50m'))
ax.add_feature(cfeature.OCEAN.with_scale('50m'))
ax.add_feature(cfeature.LAKES.with_scale('50m'))
ax.add_feature(COUNTIES, facecolor='none', edgecolor='gray')

ax.coastlines('50m')

ax.set_extent([-83, -65, 33, 44])
plt.show()

美国县

这源自https://scitools.org.uk/cartopy/docs/v0.14/examples/feature_creation.html中的示例,该示例构造 a NaturalEarthFeature,而不是 a ShapelyFeature,但除此之外,原理几乎相同。

希望对你有用。


推荐阅读