2

I have a series of LineString that I want to have the length in meters. I use the shapely.geometry module, and importing LineString.

I've seen this post, explaining how to do it, but it seems using Django : Get LineString length in meters (Python, GEODjango)

# Create a line string
>>> line = LineString([(0, 0), (360, 0)])

# Specify srid using [WGS84][1]
>>> line.srid = 4326

# Transform into projected coordinate system (using web mercator)
>>> line.transform(3857)

# Line length. This is zero within numerical precision, because the start 
# point (0, 0) is the same as the end point (0, 360)) on a map.
>>> line.length
>>> 1.1329847282581795e-08

# An example forcing the line through the other side of the world,
# and specifying srid on definition.
>>> line = LineString([(0, 0), (180, 0), (360, 0)], srid=4326)
>>> line.tansform(3857)
>>> line.length
>>> 40075016.6855784

I try to use this post, but it makes me the following error :

'LineString' object has no attribute 'lenght'

IS there any module to import?

4
  • 2
    Never trust the distance returned by Web Mercator. Unless you're measuring along the equator, it is effectively useless.
    – Vince
    Apr 27 '20 at 16:36
  • Why @Vince? Do you have any suggestion in return? Apr 28 '20 at 10:21
  • The defects of distance measurement in Web Mercator are well known, but Google Maps used it, so it became a standard for mapping, despite the infinite distance from the Equator to the poles. Since the example is only measuring the Equator, it is demonstrating implicit wrapping, but since almost any other projection is better for distance (and custom equidistant or equal area projections are most appropriate), documenting this quirk is of little real value. Your error is simple spelling (t before h).
    – Vince
    Apr 28 '20 at 11:26
  • Ok interesting ! I used another system to calculate it, I will show it on answer to this question. And for the answer, yes, it's true, I didn't see that... Thanks a lot !! Apr 28 '20 at 12:24
1

Just a point of clarification;
with pyproj 2.6.0 (which is bind to the newest Proj at least in version 6.2.0) you can even use:

from pyproj import Proj, Transformer
import shapely.ops as sp_ops
geom =  # your_shapely_geometry_here
my_transformer = Transformer.from_crs('EPSG:4326', 'EPSG:3857', always_xy=True)
geom_transformed = sp_ops.transform(my_transformer.transform, geom)

You don't need the partial anymore, I mean.

You can check both this and this.

1

To answer the problem, I finally used this :

line1 = LineString([(0, 0), (180, 0), (360, 0)])
project = partial(pyproj.transform, pyproj.Proj('EPSG:4326'), pyproj.Proj('EPSG:32633'))
line2 = transform(project, line1)
total_lenght = line2.length

and it apparently works.

0

'LineString' object has no attribute 'lenght'

You might want to try length, not lenght.

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.