简体   繁体   English

GeoPandas 在点上设置 CRS

[英]GeoPandas Set CRS on Points

Given the following GeoDataFrame:给定以下 GeoDataFrame:

h=pd.DataFrame({'zip':[19152,19047],
               'Lat':[40.058841,40.202162],
               'Lon':[-75.042164,-74.924594]})
crs='none'
geometry = [Point(xy) for xy in zip(h.Lon, h.Lat)]
hg = GeoDataFrame(h, crs=crs, geometry=geometry)
hg

       Lat          Lon     zip     geometry
0   40.058841   -75.042164  19152   POINT (-75.042164 40.058841)
1   40.202162   -74.924594  19047   POINT (-74.924594 40.202162)

I need to set the CRS as I did with another GeoDataFrame (like this):我需要像设置另一个 GeoDataFrame 一样设置 CRS(像这样):

c=c.to_crs("+init=epsg:3857 +ellps=GRS80 +datum=GGRS87 +units=mi +no_defs")

I've tried this:我试过这个:

crs={'init': 'epsg:3857'}

and this:这个:

hg=hg.to_crs("+init=epsg:3857 +ellps=GRS80 +datum=GGRS87 +units=mi +no_defs")

...but no luck. ...但没有运气。

Some important notes:一些重要的注意事项:

  1. The other GeoDataFrame for which the above .to_crs method worked was from a shape file and the geometry column was for polygons, not points.上述 .to_crs 方法适用的另一个 GeoDataFrame 来自形状文件,几何列用于多边形,而不是点。 Its 'geometry' values looked like this after the .to_crs method was applied:应用 .to_crs 方法后,它的“几何”值如下所示:

    POLYGON ((-5973.005380655156 3399.646267693398... and when I try the above with the hg GeoDataFrame, they still look like regular lat/long coordinates. POLYGON ((-5973.005380655156 3399.646267693398 ......当我用 hg GeoDataFrame 尝试上述操作时,它们看起来仍然像常规的经纬度坐标。

  2. If/when this works out, I'll then concatenate these points with the polygon GeoDataFrame in order to plot both (points on top of polygons).如果/当这可行时,我会将这些点与多边形 GeoDataFrame 连接起来,以便绘制两者(多边形顶部的点)。

  3. When I try concatenating the GeoDataFrames first before using the .to_crs method, and then I use the method on both the point and polygon rows at once, I get the following error:当我在使用 .to_crs 方法之前首先尝试连接 GeoDataFrames,然后我同时在点行和多边形行上使用该方法时,出现以下错误:

    ValueError: Cannot transform naive geometries. ValueError:无法转换简单的几何图形。 Please set a crs on the object first.请先在对象上设置一个 crs。

Thanks in advance!提前致谢!

Geopandas API got cleaned up, and now works without surprises. Geopandas API 得到了清理,现在可以正常工作了。 Make sure to use the lastest stable version and read the docs .确保使用最新的稳定版本并阅读文档

Setting the CRS on a GeoDataFrame using its EPSG code is as simple as使用 EPSG 代码在 GeoDataFrame 上设置 CRS 非常简单

gdf.set_crs(epsg=4326, inplace=True)

where gdf is a geopandas.geodataframe.GeoDataFrame .其中gdfgeopandas.geodataframe.GeoDataFrame Watch out for the explicit inplace !注意显式inplace

So in the example above it would be:所以在上面的例子中,它将是:

import pandas as pd
from shapely.geometry import Point
from geopandas import GeoDataFrame

df = pd.DataFrame({'zip':[19152,19047],
               'Lat':[40.058841,40.202162],
               'Lon':[-75.042164,-74.924594]})

geometry = [Point(xy) for xy in zip(df.Lon, df.Lat)]
gdf = GeoDataFrame(df, geometry=geometry)

gdf.set_crs(epsg=4326, inplace=True)
# ^ comment out to get a "Cannot transform naive geometries" error below

# project to merkator
gdf.to_crs(epsg=3395)

     zip        Lat        Lon                          geometry
0  19152  40.058841 -75.042164  POINT (-8353655.485 4846992.030)
1  19047  40.202162 -74.924594  POINT (-8340567.652 4867777.107)

The format for Setting CRS in GeoPandas is now GeoPandas 中设置 CRS 的格式现在是

gdf.crs = "EPSG:4326"

The earlier format is deprecated较早的格式已弃用

ref: https://geopandas.org/projections.html参考: https : //geopandas.org/projections.html

The answer was here all along:答案一直在这里

hg=hg.to_crs(c.crs)

This sets the crs for hg to that of c.这将 hg 的 crs 设置为 c 的 crs。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM