GeoPandas Set CRS on Points

后端 未结 3 1311
甜味超标
甜味超标 2020-12-28 15:43

Given the following GeoDataFrame:

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


        
3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-28 16:30

    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(h.Lon, h.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)
    

提交回复
热议问题