Python 在中用图像覆盖/填充国家边界
是否有一种方法可以使用与使用自定义库的R解决方案类似的图像填充国家/地区: 我有一个解决方案,面部颜色是填充的,例如下面意大利是蓝色的。不过,我想加上意大利国旗。Python中有没有一种方法(搜索后我没有找到多少)或者需要QGIS之类的东西:Python 在中用图像覆盖/填充国家边界,python,matplotlib,geospatial,matplotlib-basemap,cartopy,Python,Matplotlib,Geospatial,Matplotlib Basemap,Cartopy,是否有一种方法可以使用与使用自定义库的R解决方案类似的图像填充国家/地区: 我有一个解决方案,面部颜色是填充的,例如下面意大利是蓝色的。不过,我想加上意大利国旗。Python中有没有一种方法(搜索后我没有找到多少)或者需要QGIS之类的东西: #create a map where I can load images in to fill the countries import cartopy import cartopy.crs as ccrs import matplotlib.pyplo
#create a map where I can load images in to fill the countries
import cartopy
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import cartopy.io.shapereader as shpreader
flag = "italy.png" #this is a locally saved png.
plt.figure(figsize=(15, 15)) #size of plot
ax = plt.axes(projection=cartopy.crs.TransverseMercator(25))
ax.add_feature(cartopy.feature.BORDERS, linestyle='-', alpha=1)
ax.coastlines(resolution='110m') #simplifies the border lines
ax.add_feature(cartopy.feature.OCEAN, facecolor="#40e0d0") #colour of ocean
# ax.gridlines() #adds global grid lines
ax.set_extent ((-7.5, 50, 34, 69), cartopy.crs.PlateCarree()) #makes it european
shpfilename = shpreader.natural_earth(resolution='110m',
category='cultural',
name='admin_0_countries')
for country in shpreader.Reader(shpfilename).records():
if country.attributes['NAME_LONG'] == "Italy":
ax.add_geometries(country.geometry, ccrs.PlateCarree(),
facecolor="blue",
#no attribute like this img= "fd",
label=country.attributes['NAME_LONG'])
plt.show()
任何帮助,非常感谢 这是一个演示代码,可以满足您的需要。事实上,
cartopy logo
使用此技术创建
import cartopy
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import cartopy.io.shapereader as shpreader
import matplotlib.patches as mpatches
import numpy as np
imdat1 = plt.imread('flag-of-italy.jpg', format='jpg') # use your flag
plt.figure(figsize=(8, 8))
ax = plt.axes(projection=cartopy.crs.TransverseMercator(25))
ax.add_feature(cartopy.feature.BORDERS, linestyle='-', alpha=1)
ax.coastlines(resolution='110m')
ax.add_feature(cartopy.feature.OCEAN, facecolor="#40e0d0")
# ax.gridlines() #adds global grid lines
ax.set_extent ((-7.5, 50, 24, 69), cartopy.crs.PlateCarree())
shpfilename = shpreader.natural_earth(resolution='110m',
category='cultural',
name='admin_0_countries')
italy_ctry = None #use this to grab italy's
for country in shpreader.Reader(shpfilename).records():
if country.attributes['NAME_LONG'] == "Italy":
italy_ctry = country
ax.add_geometries(country.geometry, ccrs.PlateCarree(),
facecolor="none",
alpha=0.7,
zorder=2,
label=country.attributes['NAME_LONG'])
# create mpatch from `italy` geometry
cg = italy_ctry.geometry
cg2 = cg.simplify(0.02)
if cg2.geometryType()=='MultiPolygon':
# if == `Polygon`, dont need to loop
for ea in cg2.geoms:
cg2xy = ea.exterior.xy # tuple of (x,y)
xys = []
for ea in zip(cg2xy[0], cg2xy[1]):
#print(ea[0],ea[1])
xys.append([ea[0],ea[1]])
# add a patch
poly = mpatches.Polygon(xys, closed=True, ec='r', \
lw=2, fc='yellow', \
transform=ccrs.PlateCarree(), \
alpha=0.5, zorder=30)
plate_carree_transform = ccrs.PlateCarree()._as_mpl_transform(ax)
xtent1 = (6.519950, 17.122259, 35.783370, 47.962952)
imdat2 = ax.imshow(imdat1, origin='upper', extent=xtent1, \
transform=ccrs.PlateCarree(), \
zorder=15, alpha=.9)
##imdat2 = ax.stock_img() #for testing
imdat2.set_clip_path(mpatches.Path(xys), transform=plate_carree_transform)
pass
plt.show()
采样图(随使用的标志而变化):
供参考。对于一个新用户,在你获得25分之前,你不能投票支持我的答案吗?声誉积分。但您可以将其标记为“已接受”,并获得“+2”的声誉。谢谢。从何处提取覆盖范围的xtent1值?如果它是动态的,比如说我用西班牙取代了意大利,那么这些价值观肯定也必须改变吗?thanks@Andrew每个
country.geometry
都提供了可以处理的坐标,以获得其LL和UR角。我没有在代码中这样做,而是在地图上阅读它们。这是一个很好的问题,你可以作为一个新问题发布。谢谢,LL和UR:左下角和右上角?我猜另外两个是右下和右上?只是想确认一下,你在地图上读它们是什么意思?您是如何从country.geometry的输出中计算这些数字的?谢谢,我会在确认后问这个问题谢谢,你从哪里得到的/你是如何计算xtent1的数字的?这是另一个答案还是类似的?thanks@Andrew对于意大利的准确LL/UR(角)边界,可以使用cg.bounds
并以度为单位获取(lonmin、latmin、lonmax、latmax)。在我的代码中,我使用了它的粗略值。