Get Distance in Meters instead of degrees in Spatialite

tmanthey picture tmanthey · Aug 30, 2012 · Viewed 16.5k times · Source

I have the following query:

select distance(GeomFromText('POINT(8 49)',4326),GeomFromText('LINESTRING(8.329969 49.919323,8.330181 49.919468)',4326))

this gives me 0.97 degrees. But I need it in meters and do not know which SRID to transform to.

Can somebody give me an example how to get the result in meters for spatialite?

The positions are all in Europe.

Answer

Incidently picture Incidently · Aug 30, 2012

Just multiply the value in degrees by 111195 - this value is (Earth mean radius)*PI/180 - that is 'mean length of one great circle degree in meters on Earth's surface'.

The result obtained using this method is within 1% of the geodesic distance for the WGS84 ellipsoid.


EDIT

OK, my answer above still stands for the question: "how to convert arcs in degrees into lengths in meters", however, it's not the question you asked (should have asked).

I haven't used Spatialite professionally, so I assumed that your sample query indeed returns the 'length in degrees'. That's not true.

Unfortunately, it appears that Spatialite fails to calculate the distance in 'geographic sense'. Despite your geometries are defined with SRID 4326, it treats them as if they were on a plane.

Here's a simple proof:

select Distance(GeomFromText('POINT(0 0)',4326),GeomFromText('POINT(3 4)',4326));

returns 5.0.

It's a shame ...

Lets have a look at your original query:

select Distance(
  GeomFromText('POINT(8 49)',4326),
  GeomFromText('LINESTRING(8.329969 49.919323,8.330181 49.919468)',4326)
)

An equivalent query in MS SQL Server:

SELECT (geography::STGeomFromText('POINT(8 49)', 4326)).STDistance(geography::STGeomFromText('LINESTRING(8.329969 49.919323,8.330181 49.919468)', 4326));

gets you the correct result immediately: 105006.59673084648, in meters, and without any extra brouhaha.

So what are your options with Spatialite?

Indeed, as you said in comments, one option is to project your geometries, and calculate on those. Using SRID 3035 for Europe makes sense, too (if your locations are mostly in Germany, I'd consider SRID 25832).

select Distance(
  Transform(GeomFromText('POINT(8 49)',4326),25832),
  Transform(GeomFromText('LINESTRING(8.329969 49.919323,8.330181 49.919468)',4326),25832)
)

returns 104969.401605453.

As to your other sample (in comments):

select distance(
  Transform(GeomFromText('POINT(8.328957 49.920900)',4326),3035),
  Transform(GeomFromText('POINT(8.339665 49.918000)',4326),3035)
)

There's a simpler way to do it (if you have two POINTs, not a POINT and a LINESTRING): create a LINESTRING with your POINTs and use GeodesicLength function, like this:

select GeodesicLength(GeomFromText('LINESTRING(8.328957 49.920900, 8.339665 49.918000)',4326))

It returns 833.910006698673, as expected.