Compute Line Length from Longitude Latitude Geometry




Pay Notebook Creator: Roy Hyunjin Han0
Set Container: Numerical CPU with TINY Memory for 10 Minutes 0
Total0
In [28]:
# CrossCompute
longitude_latitude_wkt = 'LINESTRING (-73.935242 40.730610, -71.057083 42.361145)'
In [29]:
# pip install shapely
# pip install utm
# pip install geotable
In [30]:
from shapely import wkt

line_wkt = longitude_latitude_wkt
line_geometry = wkt.loads(line_wkt)
line_geometry
Out[30]:
<shapely.geometry.linestring.LineString at 0x7fa29c68ae80>
In [31]:
point_geometry = line_geometry.centroid
point_longitude = point_geometry.x
point_latitude = point_geometry.y
In [32]:
import utm
utm_zone_number, utm_zone_letter = utm.from_latlon(point_latitude, point_longitude)[-2:]
In [33]:
utm_zone_number
Out[33]:
18
In [34]:
utm_zone_letter
Out[34]:
'T'
In [35]:
from geotable.projections import get_utm_proj4
utm_proj4 = get_utm_proj4(utm_zone_number, utm_zone_letter)
utm_proj4
Out[35]:
'+proj=utm +zone=18 +ellps=WGS84 +datum=WGS84 +units=m +no_defs'
In [36]:
from geotable.projections import get_transform_shapely_geometry, LONGITUDE_LATITUDE_PROJ4

f = get_transform_shapely_geometry(LONGITUDE_LATITUDE_PROJ4, utm_proj4)
In [37]:
f(line_geometry).wkt
Out[37]:
'LINESTRING (589912.1585135038 4509398.080315071, 824714.0257488572 4697409.588074385)'
In [38]:
utm_line_geometry = f(line_geometry)
In [39]:
line_length_in_meters = utm_line_geometry.length
line_length_in_meters
Out[39]:
300799.341600242
In [40]:
from geopy.distance import geodesic
geodesic((40.730610, -73.935242), (42.361145, -71.057083)).meters
# UTM is accurate only for local distances
Out[40]:
300743.5902721274
In [41]:
print(f'utm_zone_number = {utm_zone_number}')
print(f'utm_zone_letter = {utm_zone_letter}')
print(f'utm_proj4 = {utm_proj4}')
print(f'line_utm_wkt = {utm_line_geometry.wkt}')
print(f'line_length_in_meters = {line_length_in_meters}')
utm_zone_number = 18
utm_zone_letter = T
utm_proj4 = +proj=utm +zone=18 +ellps=WGS84 +datum=WGS84 +units=m +no_defs
line_utm_wkt = LINESTRING (589912.1585135038 4509398.080315071, 824714.0257488572 4697409.588074385)
line_length_in_meters = 300799.341600242