python - 将多边形框添加到cartopy python
问题描述
问:我想在我的 Cartopy 地图上绘制一个由longitude
max/min 和latitude
max/min 定义的框。
进口:
import cartopy
import cartopy.feature as cpf
from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter
from shapely import geometry
from collections import namedtuple
from shapely.geometry.polygon import LinearRing
我有我的 Region 对象来定义框的边界。
Region = namedtuple('Region',field_names=['region_name','lonmin','lonmax','latmin','latmax'])
region = Region(
region_name="all_region",
lonmin = 32.6,
lonmax = 51.8,
latmin = -5.0,
latmax = 15.2,
)
sub_region = Region(
region_name="highlands_region",
lonmin=35,
lonmax=40,
latmin=5.5,
latmax=12.5
)
我的职能
plot_polygon
andadd_sub_region_box
函数不起作用,因为它们都不会产生边界框。我收到一条错误消息,add_sub_region_box
但plot_polygon
没有返回任何内容。
def plot_geog_location(region, lakes=False, borders=False, rivers=False):
""" use cartopy to plot the region (defined as a namedtuple object)
Arguments:
---------
: region (Region namedtuple)
region of interest bounding box defined in engineering/regions.py
: lakes (bool)
show lake features
: borders (bool)
show lake features
: rivers (bool)
show river features (@10m scale from NaturalEarth)
"""
lonmin,lonmax,latmin,latmax = region.lonmin,region.lonmax,region.latmin,region.latmax
ax = plt.figure().gca(projection=cartopy.crs.PlateCarree())
ax.add_feature(cpf.COASTLINE)
if borders:
ax.add_feature(cpf.BORDERS, linestyle=':')
if lakes:
ax.add_feature(cpf.LAKES)
if rivers:
# assert False, "Rivers are not yet working in this function"
water_color = '#3690f7'
river_feature = get_river_features()
ax.add_feature(river_feature)
ax.set_extent([lonmin, lonmax, latmin, latmax])
# plot the lat lon labels
# https://scitools.org.uk/cartopy/docs/v0.15/examples/tick_labels.html
# https://stackoverflow.com/questions/49956355/adding-gridlines-using-cartopy
xticks = np.linspace(lonmin, lonmax, 5)
yticks = np.linspace(latmin, latmax, 5)
ax.set_xticks(xticks, crs=cartopy.crs.PlateCarree())
ax.set_yticks(yticks, crs=cartopy.crs.PlateCarree())
lon_formatter = LongitudeFormatter(zero_direction_label=True)
lat_formatter = LatitudeFormatter()
ax.xaxis.set_major_formatter(lon_formatter)
ax.yaxis.set_major_formatter(lat_formatter)
fig = plt.gcf()
return fig, ax
def plot_polygon(ax, sub_region):
"""
https://groups.google.com/forum/#!topic/scitools-iris/LxR0EbQolyE
Note:
----
order is important:
lower-left, upper-left, upper-right, lower-right
2 -- 3
| |
1 -- 4
"""
# ax = fig.axes[0]
lons = [sub_region.latmin, sub_region.latmin, sub_region.latmax, sub_region.latmax]
lats = [sub_region.lonmin, sub_region.lonmax, sub_region.lonmax, sub_region.lonmin]
ring = LinearRing(list(zip(lons, lats)))
ax.add_geometries([ring], cartopy.crs.PlateCarree(), facecolor='b', edgecolor='black', alpha=0.5)
return ax
def add_sub_region_box(ax, subregion):
""" """
geom = geometry.box(minx=subregion.lonmin,maxx=subregion.lonmax,miny=subregion.latmin,maxy=subregion.latmax)
ax.add_geometries(geom, crs=cartopy.crs.PlateCarree(), alpha=0.3)
return ax
这两个功能都不起作用!任何帮助将不胜感激。
运行函数:
fig, ax = plot_geog_location(region,borders=True, lakes=True, rivers=False)
plot_polygon(ax, sub_region)
fig, ax = plot_geog_location(region,borders=True, lakes=True, rivers=False)
add_sub_region_box(ax, sub_region)
解决方案
推荐阅读
- asp.net-mvc - Asp.Net MVC 不隐藏 URL 中的参数名称
- hadoop - 为什么即使映射器和减速器的数量设置为 1,hive 仍将 2 个部分文件写入 hdfs
- sql - 如何通过 IF 语句中的 sql 命令更新表中的记录
- flutter - 如何在 Flutter 发布模式下“摇树”内部 QA 页面?
- python - POST 请求不影响本地主机服务器中的 HTML 输出
- redis - 有没有办法在集群上刷新,以便从数据库中删除主从的所有密钥
- java - 如何通过所有线程发送消息?
- three.js - 视图矢量和法线之间的角度?
- python - 过滤掉熊猫数据框中A或B列上不存在的数字?
- python - 从 txt 文件中删除特定行