3

Open Street Map (pyproj). How to solve syntax issue? has a similar question and the answers there did not help me.

I am using the helper class below a few hundred times and my console gets flooded with warnings:

/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pyproj/crs/crs.py:53: FutureWarning: '+init=<authority>:<code>' syntax is deprecated. '<authority>:<code>' is the preferred initialization method. When making the change, be mindful of axis order changes: https://pyproj4.github.io/pyproj/stable/gotchas.html#axis-order-changes-in-proj-6
  return _prepare_from_string(" ".join(pjargs))

https://pyproj4.github.io/pyproj/stable/gotchas.html#axis-order-changes-in-proj-6

When i try to follow the hint by using:

return transform(Proj('epsg:4326'), Proj('epsg:3857'), lon,lat)

I get some (inf,inf) results in cases where the original code worked. What is the proper way to avoid the syntax error but get the same results?

shows the old syntax but no code example for a compatible new statement.

https://github.com/pyproj4/pyproj/issues/224 states:

  *What is the preferred way of loading EPSG CRSes now?

use "EPSG:XXXX" in source_crs or target_crs arguments of proj_create_crs_to_crs() when creating a transformation, or as argument of proj_create() to instanciate a CRS object*

What does this mean as a code example?

from pyproj import Proj, transform

class Projection:
    @staticmethod
    def wgsToXy(lon,lat):
        return transform(Proj(init='epsg:4326'), Proj(init='epsg:3857'), lon,lat)

    @staticmethod
    def pointToXy(point):
        xy=point.split(",")
        return Projection.wgsToXy(float(xy[0]),float(xy[1]))
Wolfgang Fahl
  • 15,016
  • 11
  • 93
  • 186

2 Answers2

6

In order to keep using the old syntax (feeding the transformer (Lon,Lat) pairs) you can use the always_xy=True parameter when creating the transformer object:

from pyproj import Transformer
transformer = Transformer.from_crs(4326, 3857, always_xy=True)
points = [
    (6.783333, 51.233333),  # Dusseldorf
    (-122.416389, 37.7775)  # San Francisco
]
for pt in transformer.itransform(points):
    print(pt)

Output

(755117.1754412088, 6662671.876828446)
(-13627330.088231295, 4548041.532457043)
Ionut Ticus
  • 2,683
  • 2
  • 17
  • 25
0

This is my current guess for the fix:

    #e4326=Proj(init='epsg:4326')
    e4326=CRS('EPSG:4326')
    #e3857=Proj(init='epsg:3857')
    e3857=CRS('EPSG:3857')

Projection helper class

from pyproj import Proj, CRS,transform

class Projection:
    '''
    helper to project lat/lon values to map
    '''
    #e4326=Proj(init='epsg:4326')
    e4326=CRS('EPSG:4326')
    #e3857=Proj(init='epsg:3857')
    e3857=CRS('EPSG:3857')

    @staticmethod
    def wgsToXy(lon,lat):
        t1=transform(Projection.e4326,Projection.e3857, lon,lat)
        #t2=transform(Proj('epsg:4326'), Proj('epsg:3857'), lon,lat)
        return t1

    @staticmethod
    def pointToXy(point):
        xy=point.split(",")
        return Projection.wgsToXy(float(xy[0]),float(xy[1]))
Wolfgang Fahl
  • 15,016
  • 11
  • 93
  • 186
  • Use **always_xy**; quoting pyproj [docs](https://pyproj4.github.io/pyproj/stable/api/transformer.html): `The axis order may be swapped if the source and destination CRS’s are defined as having the first coordinate component point in a northerly direction (See PROJ FAQ on axis order). You can check the axis order with the pyproj.crs.CRS class. If you prefer to keep your axis order as always x,y, you can use the always_xy option when creating the pyproj.transformer.Transformer.` – Ionut Ticus Apr 08 '20 at 18:43
  • 1
    @Ionut Ticus - that's greek to me. I was interested in the syntax deprecation workaround – Wolfgang Fahl Apr 09 '20 at 14:41