GeoPandas Set CRS on Points

随声附和 提交于 2019-12-21 07:55:56

问题


Given the following 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):

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. Its 'geometry' values looked like this after the .to_crs method was applied:

    POLYGON ((-5973.005380655156 3399.646267693398... and when I try the above with the hg GeoDataFrame, they still look like regular lat/long coordinates.

  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).

  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:

    ValueError: Cannot transform naive geometries. Please set a crs on the object first.

Thanks in advance!


回答1:


As of 2018 setting the CRS in GeoPandas is as simple as:

gdf.crs = {'init' :'epsg:4326'}

where gdf is a geopandas.geodataframe.GeoDataFrame

so in the example above it would be:

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

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

hg.crs = {'init' :'epsg:4326'}  
# ^ comment out to get a "Cannot transform naive geometries" error below

# project to merkator
hg.to_crs({'init': 'epsg:3395'})

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



回答2:


The answer was here all along:

hg=hg.to_crs(c.crs)

This sets the crs for hg to that of c.



来源:https://stackoverflow.com/questions/38961816/geopandas-set-crs-on-points

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!