微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

地图投影:如何转换 shapefile

如何解决地图投影:如何转换 shapefile

我从 Natural Earth 下载了“Admin 0- countries (Download countries 4.67MB v4.1.0)”数据集。如果我使用 Aspose SHP Viewer 查看下载的数据集,我会看到:

enter image description here

但是,我希望能够转换每个坐标,以便我可以创建自己的地图投影。我该怎么做?

我查看了 Fiona 和 pyshp,但我不确定如何才能做到这一点。

import fiona
import matplotlib
import matplotlib.pyplot as plt
from math import log,tan,pi,radians


def mercator_proj(lon,lat):
    lat,lon = map(radians,[lat,lon])

    R = 6371000
    lon_0 = 0
    x = R*(lon-lon_0)
    y = R*log(tan(pi/4+lat/2))
    return (x,y)

shape = fiona.open("ne_10m_admin_0_countries.shp")
# print(shape.schema)

k = iter(shape)

x,y = [],[]
for i in range(150): #first 150 countries
    s = next(k)
    for lis in s["geometry"]["coordinates"]:
        for thing in lis[0]: #it always appears as a nested list of single element,[[]]
            if not isinstance(thing,tuple): #occasionally there is a random float
                continue
            _x,_y = mercator_proj(*thing)
            x.append(_x)
            y.append(_y)
            # print(_x,_y)

colors = ["green"]
color_indices = [0 for i in range(len(x))]
colormap = matplotlib.colors.ListedColormap(colors)
plt.scatter(x,y,c=color_indices,cmap=colormap)
plt.show()

产生以下结果:

enter image description here

相反,我希望能够复制上面的图片。它也应该适用于其他预测(而不仅仅是 mercator_proj)。

编辑: “我希望能够转换每个坐标,以便我可以创建自己的地图投影”,我的意思是我希望能够使用我自己的数学定义公式。大多数地图投影都有公式,例如下面的墨卡托投影:

enter image description here

这需要一个纬度和经度,并返回 2D 地图上的 x,y 值。

解决方法

我不是 100% 确定您要做什么,但我相信您可以这样做:

import geopandas as gpd

# Location of the zip file on disk
shp_file = 'ne_10m_admin_0_countries.zip'

# Loading the data into a GeoDataFrame
my_geodata = gpd.read_file(shp_file)

# Transforming the data's projection/coordinate system 
# to EPSG 4326 (which is the default system used in the 
# Aspose viewer you posted.)
my_geodata = my_geodata.to_crs('epsg:4326')

# Plotting the data
my_geodata.plot()

上面的代码产生下图: enter image description here

请注意,您需要安装 GeoPandas 库。它在后台使用 Fiona,可以轻松处理从一个投影到另一个投影的转换。

它还使操作数据库的其他部分变得非常容易。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?