pyproj FutureWarning: '+init=<authority>:<code>' syntax is deprecated

Viewed 9037

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]))
2 Answers

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)

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]))
Related