TransWikia.com

First call to transform() fails with inf, all subsequent calls are OK - what could be the reason?

Geographic Information Systems Asked by Vitaliy on March 19, 2021

I’m trying to get to the bottom of the following (weird) problem. When applying pyproj.Transformer transformation to a certain polygon, it fails the first time with (inf,inf) throughout. Second and all subsequent calls to the same work just fine!

I saw similar issues with inf which had to do with inappropriate CRS (out of bounds), but this is not the case here.

The question was originally prompted by geopandas’ to_crs() failing occasionally; similar question by someone else here.

Example

import shapely, shapely.geometry, shapely.speedups, shapely.ops, pyproj
print('Shapely version:',shapely.__version__)
print('Shapely speedups enabled:',shapely.speedups.enabled)
#shapely.speedups.disable() # makes no difference
print('Pyproj version:',pyproj.__version__)
print('PROJ version:',pyproj.proj_version_str)

xy = [(532303, 181814), (532190, 181854), (532192, 181861), (532199, 181863), 
      (532195, 181874), (532183, 181870), (532163, 181864), (532162, 181868), 
      (532309, 181915), (532294, 181828), (532306, 181820)] #, (532303, 181814)]
geom = shapely.geometry.Polygon(xy)
OSGB1936 = pyproj.CRS('EPSG:27700')
WGS84 = pyproj.CRS('EPSG:4326')

proj = pyproj.Transformer.from_crs(OSGB1936, WGS84, always_xy=True)
func = proj.transform

# First call fails with inf, all subsequent calls are OK:
for ii in range(3):
    tp = shapely.ops.transform(func, geom)
    print(tp.exterior.coords[0])

Output:

Shapely version: 1.7.1
Shapely speedups enabled: True
Pyproj version: 2.6.1.post1
PROJ version: 7.1.0
(inf, inf)
(-0.0929010345368997, 51.519248856167074)
(-0.0929010345368997, 51.519248856167074)

One Answer

@snowman2 's comment fixes the problem; viz.: explicitly setting

import os
os.environ['PROJ_NETWORK'] = 'OFF'

in the preamble resolves the issue. Thanks!

Also verified that the original problem is solved (i.e., geopandas' to_crs() works fine with or without pygeos)

Correct answer by Vitaliy on March 19, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP