I want to know linestrings (epsg:4326) length in meters instead of degrees. I know that I can use proj4 for that.
Here is my code, if you interested.
project = partial(
pyproj.transform,
pyproj.Proj(init='EPSG:4326'),
pyproj.Proj(init='EPSG:3395')
)
project2 = partial(
pyproj.transform,
pyproj.Proj(init='EPSG:3395'),
pyproj.Proj(init='EPSG:4326')
)
def distance_in_meters(linestring):
return transform(project, linestring).length
def interpolate(distance, linestring):
current_ratio = 0
points = []
step = distance / distance_in_meters(linestring)
while current_ratio < 1:
point = linestring.interpolate(current_ratio, normalized=True)
current_ratio += step
points.append(point)
return points
distance_between_points = 249.67
total_distance_in_meters = distance_between_points * 5
class TestInterpolate(TestCase):
def test_interpolate(self):
with open("linestring.json", 'r') as f:
s = shape(json.load(f))
points = interpolate(10, s)
l = LineString(points)
self.assertEqual(distance_in_meters(l), total_distance_in_meters)
And here is linestring that I use for tests.
{
"type": "LineString",
"coordinates": [
[
37.6232398,
55.7615482
],
[
37.6262633,
55.7606701
],
[
37.6232398,
55.7615482
],
[
37.6262633,
55.7606701
],
[
37.6232398,
55.7615482
],
[
37.6262633,
55.7606701
]
]
}
What I've done is I've taken coordinates of two points from google maps and measured distance between them also in google maps. Than I've duplicated it several times creating a path that swings back and forth 5 times.
And the result of the test is
1864.394777794176 != 1248.35
Which is pretty far off from the actual distance of linestring. I suppose that the reason is - I'm not using right CRS for meters projection.