johajippijupp
johajippijupp

Reputation: 25

Shapely/Pyproj transformation throws OverflowError - determining length of LineString

I'm working in a Jupyter Notebook and deleted code I thought I didn't need the other. Now I get an overflow error, when running the notebook. I'm pretty sure the code used to work just fine and the problem is caused by me stupidly deleting stuff.

Anyway, I can't seem to find what is missing and would really appreciate help. I'm using a list with coordinates, convert them to a linestring and then transform them. Finally, I lookup the length.

import pyproj
from pyproj import Transformer
from shapely.ops import transform
from shapely.geometry import LineString

route = [[41.875562, -87.624421], [29.949932, -90.070116], [40.712728, -74.006015]]

ls = LineString(route) 

project = pyproj.Transformer.from_proj(
    pyproj.Proj(init='epsg:4326'),
    pyproj.Proj(init='epsg:3857'))

ls_metric = transform(project.transform, ls) 

ls_metric_length = round(ls_metric.length / 1000)

This returns

OverflowError: cannot convert float infinity to integer

The problem arises already with ls_metric which doesn't generate a LineString.

Upvotes: 1

Views: 672

Answers (1)

Ionut Ticus
Ionut Ticus

Reputation: 2789

I ran your code and got this warning:

FutureWarning: '+init=<authority>:<code>' syntax is deprecated.
'<authority>:<code>' is the preferred initialization method

Sure enough I changed the pyproj Transformer and got a result:

project = pyproj.Transformer.from_proj(
    pyproj.Proj('epsg:4326'),
    pyproj.Proj('epsg:3857'))

gives a length of 3984 km. I used the latest versions in a venv:

pyproj==2.6.0
Shapely==1.7.0

The warning above also gives another important note regarding axis order changes; in short:

pyproj.Proj('epsg:4326') works with [lat,lng], [lat,lng] ...
pyproj.Proj(init='epsg:4326') works with [lng,lat], [lng,lat] ...

the first one being the preferred way while the second is deprecated.

Upvotes: 1

Related Questions