36

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!

3 Answers 3

69

Geopandas API got cleaned up, and now works without surprises. 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

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

where gdf is a geopandas.geodataframe.GeoDataFrame. Watch out for the explicit 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)
4
  • 2
    GeoDataFrame(df, geometry=geometry, crs='EPSG:4326') would also work
    – Ufos
    Oct 28, 2020 at 23:34
  • @JanPisl, it does work on the latest stable version of geopandas (0.8.2). What do you mean by "no longer valid"? What version of python and geopandas are you using?
    – Ufos
    Feb 1, 2021 at 13:31
  • 1
    Ok sorry my bad! I use 0.7. Let me correct myself: If you get *** AttributeError: 'GeoDataFrame' object has no attribute 'set_crs' , try answer from shyam
    – Jan Pisl
    Feb 1, 2021 at 14:13
  • any way only with shapely ? Aug 4, 2021 at 13:51
14

The format for Setting CRS in GeoPandas is now

gdf.crs = "EPSG:4326"

The earlier format is deprecated

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

1
  • in such cases it's better to suggest and edit to an already accepted answer, or, at least, leave a comment. I've updated my answer, changes should go live once someone gets to review them.
    – Ufos
    Sep 28, 2020 at 17:00
2

The answer was here all along:

hg=hg.to_crs(c.crs)

This sets the crs for hg to that of c.

4
  • 4
    The answer is also wrong. to_crs transforms from one CRS to another, while .crs = {...} sets the initial crs.
    – Ufos
    Aug 30, 2018 at 18:03
  • @Ufos you say "also", are you suggesting that all answers in this thread are incorrect?
    – baxx
    Feb 22, 2020 at 15:56
  • @baxx my other comment got removed as the link in the answer was fixed. The comment was "the link is dead". So, now "also" does not make sense, but I cannot edit the comment.
    – Ufos
    Feb 26, 2020 at 18:47
  • 1
    If you check the for-now-working link, you will see that there to_crs is used to transform the coordinates, not to set the CRS.
    – Ufos
    Feb 26, 2020 at 18:50

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.