I have a rectangular matrix of geographic climate data with a 1d row of latitude coordinates (size= 834) and a 1d column with longitude coordinates (size= 1115). They are both in unit of m (EPSG:32630) and I would like to transform to lat/lon. When I try to do:

    from pyproj import Proj, transform

    inProj = Proj('epsg:32630')
    outProj = Proj('epsg:4326')
    x1 = precip.lon.values
    x2 = precip.lat.values
    x2, y2 = transform(inProj, outProj, precip.lon.values, precip.lat.values)
    print (x2, y2)

I get the error:

Traceback (most recent call last):
  File "<input>", line 4, in <module>
  File "<conda_env>/lib/python3.7/site-packages/pyproj/transformer.py", line 759, in transform
    ).transform(xx=x, yy=y, zz=z, tt=tt, radians=radians, errcheck=errcheck)
  File "<conda_env>/lib/python3.7/site-packages/pyproj/transformer.py", line 446, in transform
  File "pyproj/_transformer.pyx", line 459, in pyproj._transformer._Transformer._transform
pyproj.exceptions.ProjError: x, y, z, and time must be same size

Does anyone know how I can transform from PCS to GCS with python without having equal amount of latitude and longitude values?

  • I guess you need to generate x, y coordinates for each cell of your grid and transform those to get the best result. Alternatively, you can do that for edge cells only and regenerate the matrix from that in another CRS, but keep in mind that you might face issues with the spherical nature of the earth. – martinfleis Jun 18 at 10:08
  • You probably want something like rioxarray if you have a grid: corteva.github.io/rioxarray/stable/examples/reproject.html – snowman2 Jun 19 at 22:05

Thanks to the comment from Martin, I did what he suggested first. I guess there is no way to just reproject latitude and longitude seperately and not if they are uneven.

    x1 = precip.lon.values
    y1 = precip.lat.values
    y1 = y1.reshape(-1,1)

#Generate two equal matrices with longitude and latitude.
    ones = np.ones((834,1115))
    xlon = np.multiply(ones, x1)
    ylat = np.multiply(ones, y1)
    lat, lon = transform(inProj, outProj, xlon, ylat)

This did solve my problem but I am still confused why it needs to be xlon, ylat, but this worked out and gave the right projection in the end. I tried first transform(inProj, outProj, ylat, xlon) but did not give any correct projection. If somebody knows what is going on I would like to know. I could not find any explanation in the docs.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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