question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

to_crs creates different result from pyproj.transform

See original GitHub issue

I’m not sure this is an issue for GeoPandas or pyproj, but parhaps due to always_xy option for pyproj.transform, to_crs creates different result from pyproj.transform.

Repro

Python and package versions

import sys

import pyproj
import geopandas

print(sys.version)
print(pyproj.__version__)
print(geopandas.__version__)

3.8.0 | packaged by conda-forge | (default, Nov 22 2019, 19:11:19) [Clang 9.0.0 (tags/RELEASE_900/final)]

2.4.2.post1

0.6.2

Reproject by pyproj.transform

x = -567637.0
y = -56204.0

# EPSG 4612: JGD2000, geographical, unit: degree
# https://epsg.io/4612
EPSG4612 = pyproj.Proj("EPSG:4612")
# RPSG 2443: JGD2000 / Japan Plane Rectangular CS I, projected, unit: meter
# https://epsg.io/2443
EPSG2443 = pyproj.Proj("EPSG:2443")

print(pyproj.transform(EPSG2443, EPSG4612, x, y))

(27.878013118155934, 128.92918943580798)

Here, 27.8780 is latitude and 128.929 is longitude.

Reproject by GeoDataFrame.to_crs

gdf = geopandas.GeoDataFrame(
    geometry = geopandas.points_from_xy(x = [x], y = [y]),
    crs = "EPSG:2443"
)
print(gdf.to_crs("EPSG:4612"))

geometry 0 POINT (123.47406 32.34878)

Swapping x and y produce correct result

gdf = geopandas.GeoDataFrame(
    geometry = geopandas.points_from_xy(x = [y], y = [x]),
    crs = "EPSG:2443"
)
print(gdf.to_crs("EPSG:4612"))

geometry 0 POINT (128.92919 27.87801)

Result produced by to_crs is similar for the one produced by transform with always_xy = True

print(pyproj.transform(EPSG2443, EPSG4612, x, y, always_xy=True))

(123.4740611549679, 32.34878175793676)

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:20 (17 by maintainers)

github_iconTop GitHub Comments

4reactions
snowman2commented, Jan 8, 2020

The input CRS has the northing coordinate first.

>>> from pyproj import CRS
>>> CRS("EPSG:2443")
<Projected CRS: EPSG:2443>
Name: JGD2000 / Japan Plane Rectangular CS I
Axis Info [cartesian]:
- X[north]: Northing (metre)
- Y[east]: Easting (metre)
Area of Use:
- name: Japan - zone I
- bounds: (128.17, 26.96, 130.46, 34.74)
Coordinate Operation:
- name: Japan Plane Rectangular CS zone I
- method: Transverse Mercator
Datum: Japanese Geodetic Datum 2000
- Ellipsoid: GRS 1980
- Prime Meridian: Greenwich

And the output has the northing coordinate first.

>>> CRS("EPSG:4612")
<Geographic 2D CRS: EPSG:4612>
Name: JGD2000
Axis Info [ellipsoidal]:
- Lat[north]: Geodetic latitude (degree)
- Lon[east]: Geodetic longitude (degree)
Area of Use:
- name: Japan
- bounds: (122.38, 17.09, 157.65, 46.05)
Datum: Japanese Geodetic Datum 2000
- Ellipsoid: GRS 1980
- Prime Meridian: Greenwich

So, you need to input/output with north first and expect lat first:

>>> from pyproj import Transformer
>>> north = -56204.0
>>> east = -567637.0
>>> trans = Transformer.from_crs(2443, 4612)
>>> lat, lon = trans.transform(north, east)
>>> lat, lon
(32.34878175793677, 123.4740611549679)

However, geopandas uses always_xy, so it expects input x first and output x first always:

>>> transxy = Transformer.from_crs(2443, 4612, always_xy=True)
>>> lon, lat = transxy.transform(east, north)
>>> lon, lat
(123.4740611549679, 32.34878175793677)

Based on this, result_B seems to be correct.

1reaction
snowman2commented, Jan 8, 2020

By “correct”, I mean after transformed the two points should be the same location on a map, or on the earth, right?

Yeah, you’re right. I originally skimmed your question 🙃 - bad habit of mine. I thought the order of the output was swapped as is shown in the example I provided. But, looking at it again, the values do differ. So only one of the two could have been correct.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Converting Individual Points Instead of Entire Geometries ...
to_crs () but as soon as I use Pyproj.Transformer.transform() it becomes clear I am getting different results from these two methods, when they ......
Read more >
python pyproj transform yielding different results for the ...
I get a different answer. I print out inProj both times and both appear to be the same. Run 1: import pyproj import...
Read more >
Transformer - pyproj 3.4.1 documentation - GitHub Pages
The pyproj.Transformer has the capabilities of performing 2D, 3D, and 4D (time) transformations. It can do anything that the PROJ command line programs...
Read more >
Advanced Examples - pyproj 3.4.0 documentation
import numpy as np from pyproj import Transformer, transform transformer ... Results: 160 ms ± 3.68 ms per loop (mean ± std. dev....
Read more >
Transformer — pyproj 2.5.0 documentation - GitHub Pages
CRS or input used to create one) – Projection of output data. ... the transform method will accept as input and return as...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found