简体   繁体   中英

Geopandas .to_crs only converts one coordinate of Point object

I need to convert the CRS of a GeoDataFrame from EPSG:2145 to EPSG:3857. When I use the.to_crs method, it will only transform the x coordinate, leaving the y in the old format.

My original gdf with EPSG:2145

gdf
    ipere       geometry
    670     POINT (-73.629 45.572)
    671     POINT (-73.569 45.506)
    672     POINT (-73.629 45.572)
    673     POINT (-73.607 45.565)
    771     POINT (-73.636 45.580)

gdf.crs

<Projected CRS: EPSG:2145>
Name: NAD83(CSRS98) / MTM zone 8
Axis Info [cartesian]:
- E(X)[east]: Easting (metre)
- N(Y)[north]: Northing (metre)
Area of Use:
- name: Canada - Quebec - between 75°W and 72°W.
- bounds: (-75.0, 44.99, -72.0, 62.53)
...

Then I try to convert

gdf2 = gdf.to_crs(3857)

And I check:

gdf2
        ipere       geometry
        670     POINT (-8486769.994 45.572)
        671     POINT (-8486769.933 45.506)
        672     POINT (-8486769.994 45.572)
        673     POINT (-8486769.971 45.565)
        771     POINT (-8486770.001 45.580)

As you see, the geometry. Any idea why is this happening?

  • this investigation shows that the starting CRS is not EPSG:2145 but is EPSG:4326
  • you could use set_crs() to change your geodataframe to EPSG:4326 to start
import io
import shapely.wkt
import pandas as pd
import geopandas as gpd

df = pd.read_csv(io.StringIO("""    ipere       geometry
    670     POINT (-73.629 45.572)
    671     POINT (-73.569 45.506)
    672     POINT (-73.629 45.572)
    673     POINT (-73.607 45.565)
    771     POINT (-73.636 45.580)"""), sep="\s\s+", engine="python")

for crs in ["EPSG:4326","EPSG:2145"]:
    gdf = gpd.GeoDataFrame(df, geometry=df["geometry"].apply(shapely.wkt.loads), crs=crs)
    print(crs)
    print(gdf.to_crs("EPSG:3857").to_markdown(index=False))

EPSG:4326

ipere geometry
670 POINT (-8196342.78761794 5712025.204171824)
671 POINT (-8189663.618170344 5701535.712440695)
672 POINT (-8196342.78761794 5712025.204171824)
673 POINT (-8193893.758820487 5710912.098368462)
771 POINT (-8197122.024053493 5713297.494982241)

EPSG:2145

ipere geometry
670 POINT (-8486769.993448919 45.49609174562639)
671 POINT (-8486769.933511838 45.42971666048591)
672 POINT (-8486769.993448919 45.49609174562639)
673 POINT (-8486769.971471995 45.48905196902809)
771 POINT (-8486770.000441579 45.50413721057778)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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