坐标参考系统
CRS(坐标参考系统 , Coordinates Reference System)非常重要,其意义在于GeoSeries或GeoDataFrame对象中的几何形状是任意空间中的坐标集合。 CRS可以使Python的坐标与地球上的具体实物点相关联。
使用proj4字符串代码的方法引用CRS。你可以从 https://www.spatialreference.org 或 https://remote-sensing.org/ 找到最常用的代码。
例如,最常用的CRS之一是WGS84纬度 - 经度投影。 这个投影的proj4表示通常可以用多种不同的方式引用相同CRS:
"+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"
通用的投影可以通过EPSG代码来引用,因此这个通用的投影也可以使用proj4字符串调用
"+init=epsg:4326".
GeoPandas包含有CRS的表示方法,包括proj4字符串本身
("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
或在字典中分解的参数:
{'proj': 'latlong', 'ellps': 'WGS84', 'datum': 'WGS84', 'no_defs': True}
此外,也可直接采用EPSG代码完成一些功能。
以下为参考值,是一些非常常见的投影和它们的proj4字符串:
- WGS84纬度/经度:
+proj=longlat+ellps=WGS84+datum=WGS84+no_defs
或+init=epsg:4326
- UTM区域(北):“+ proj = utm + zone = 33 + ellps = WGS84 + datum = WGS84 + units = m + no_defs”
- UTM Zones(南):“+ proj = utm + zone = 33 + ellps = WGS84 + datum = WGS84 + units = m + no_defs + south”
设置投影
投影有两个操作方法:设置投影和重新投影。
当地理坐标有坐标数据(x-y值),但没有关于这些坐标如何指向实际位置的信息时,需要设置投影。 如果没有设置CRS,地理信息的几何操作虽可以工作,但不会变换坐标,导出的文件也可能无法被其他软件正常理解。
注意,大多数情况你不需要设置投影。使用 from_file()
命令加载的数据会始终包括投影信息。
您可以通过crs属性来查看当前CRS对象: my_geoseries.crs
。
然而,您可能会获得不包含投影的数据。在这种情况下,您必须设置CRS,以便地理数据库作出解释坐标的合理操作。
例如,将纬度和经度的电子表格手动转换为GeoSeries,可以通过WGS84纬度 - 经度CRS分配给 crs
属性的方法来设置投影:
my_geoseries.crs = {'init' :'epsg:4326'}
首先加载数据:
import os
# os.environ['PROJ_LIB'] = '/git/usr/miniforge/envs/python312/share/proj'
import geopandas as gpd
wdata_path = './ne-data/ne_110m_admin_0_countries.shp'
world = gpd.read_file(wdata_path)
# world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
# Check original projection
# (it's Platte Carre! x-y are long and lat)
world.crs
旧版本中使用如下方式声明:
world.crs = {'init': 'epsg:4326'}
/opt/conda/lib/python3.12/site-packages/pyproj/crs/crs.py:141: FutureWarning: '+init=<authority>:<code>' syntax is deprecated. '<authority>:<code>' is the preferred initialization method. When making the change, be mindful of axis order changes: https://pyproj4.github.io/pyproj/stable/gotchas.html#axis-order-changes-in-proj-6 in_crs_string = _prepare_from_proj_string(in_crs_string)
新版本更直接:
world.crs = 'epsg:4326'
查看结果 :
world.plot()
<Axes: >
Reproject to Mercator (after dropping Antartica)。
设置投影可以使用: world = world.to_crs({'init': 'epsg:3395'})
,
但现在更推荐使用如下方式:
world = world.to_crs('epsg:3857')
world.plot();
world.to_crs(epsg=3395) # would also work
geometry | |
---|---|
0 | MULTIPOLYGON (((20037508.343 -1800679.237, 200... |
1 | POLYGON ((3774143.866 -105050.44, 3792946.708 ... |
2 | POLYGON ((-964649.018 3185897.152, -964597.245... |
3 | MULTIPOLYGON (((-13674486.249 6242596, -136894... |
4 | MULTIPOLYGON (((-13674486.249 6242596, -133583... |
... | ... |
172 | POLYGON ((2096126.508 5735055.661, 2096127.988... |
173 | POLYGON ((2234260.104 5220640.91, 2204305.52 5... |
174 | POLYGON ((2292095.761 5110826.439, 2284604.344... |
175 | POLYGON ((-6866186.192 1196928.987, -6802177.4... |
176 | POLYGON ((3432408.751 388270.175, 3334408.389 ... |
177 rows × 1 columns
world.plot();