如何在 Python 中创建一个网格字段,其中包含全球每个像素的国家名称,分辨率为 0.1°x0.1°?

问题描述

我需要一个分辨率为 0.1°x0.1° 的整个世界的网格区域。每个像素都应包含像素所在国家/地区的名称

我尝试使用 Python 中的 reverse_geocode 包生成该字段。问题是,当您输入国际水域中的某个位置时,该软件包会提供最近国家/地区的名称,而不仅仅是“国际水域”(请参见下面的示例代码)。我也考虑过用google,但我想有超过600万像素待定,商业服务可能不是最好的选择。

import reverse_geocode as rg

# coordinates in atlantic ocean
lat_water = 35
lon_water = -29

coordinates = [(lat_water,lon_water)]
result = rg.search(coordinates)

country = result[0]['country']

print('expected: international waters')
print('from geocode:',country)

# output
# expected: international waters
# from geocode: Portugal

在我的目标分辨率下,有没有一种方法可以在不使用可能包含海洋/无海洋掩膜的外部数据集的情况下做到这一点?

解决方法

使用 cartopy 可能会完成这项工作:

import numpy as np
import shapely
import cartopy.io.shapereader as shpreader

shpfilename = shpreader.natural_earth(
    resolution='50m',category='cultural',name='admin_0_countries')

reader = shpreader.Reader(shpfilename)

cs = list(reader.records())
for lon in np.arange(-179.9,-180.1,0.1):                                                                                                                                     
    for lat in np.arange(-90,90.1,0.1):
        c = list(set([_c.attributes["NAME_EN"] for _c in cs if _c.geometry.contains(shapely.geometry.Point(lon,lat))]))
        if len(c) == 0:
            print(f"{lat:5.1f},{lon:5.1f} nothing")
        else:
            assert len(c) == 1
            print(f"{lat:5.1f},{lon:5.1f} {c[0]}")

不是很优化,但应该可以。