Best way to convert degrees to meters?

1,087 views
Skip to first unread message

Preet

unread,
Jun 24, 2011, 4:48:30 PM6/24/11
to SpatiaLite Users
When doing a search for geometry within a certain distance of a given
point, we can use something like the "PtDistanceWithin" function. If
both input arguments are in WGS84, we can specify a distance using
meters. If I now want to use a spatial index with this query to speed
it up I can use "RTreeDistanceWithin()" beforehand. But
RTreeDistanceWithin, and most other spatial functions requires the use
of distance/points within whatever SRID profile is being used... for
WGS84, this is long/lat in degrees. I spent a long time combing
through functions, but I couldn't find anything to convert a distance
in meters to one in degrees. This makes sense, since the conversion
would depend on where on the surface of the Earth you're converting
from. But if I want to define a box from some given point using meters
I'm lost. I could use Geodesic/GreatCircleLength but I don't know the
second point that is some specified distance away (in meters!).

The only thing I could think of was:
* Get my starting point (lat/long)
* Use Transform() to project it to meters
* Use Shift() to move the point to the edge of my bounding box,
specifying the shift distance in meters
* Transform() back to (lat/long)
* Make LineString from both Points
* Use Length() to get the right distance in degrees

Is this the right method? I don't even know if it works, still trying
to get the syntax right and have it run... is my line of thinking okay
though? Is there a better way to do it?

Preet

Preet

unread,
Jun 24, 2011, 7:40:14 PM6/24/11
to SpatiaLite Users
For anyone else wondering, the above steps do work. Here's an example
(RC 2.4-5):

-- Length of 10km expressed in degrees at given location ~= 0.12
ST_Length(MakeLine((MakePoint(-79.3849,43.6529,4326)),
(Transform(ShiftCoords(Transform(MakePoint(-79.3849,43.6529,4326),
2163),10000,0),4326))))

Unfortunately I'm having trouble with the final step of including the
spatial index filter in my query (so close!). Here's my query:

SELECT osm_nodes.node_id
FROM osm_nodes
WHERE PtDistWithin(osm_nodes.Geometry,
MakePoint(-79.3849,43.6529,4326), 10000) == 1
AND osm_nodes.node_id IN
(
SELECT pkid
FROM idx_osm_nodes_Geometry
WHERE pkid MATCH RTreeDistWithin(MakePoint(-79.3849,43.6529,4326),
0.12)
)

I get an "SQL error: SQL logic error or missing database" message. I'm
following this example in the cookbook:
http://www.gaia-gis.it/spatialite-2.4.0-4/spatialite-cookbook/html/neighbours.html,
but I've modified it slightly for my database. Could anyone tell me
where my mistake is?

Regards,

-Preet

a.furieri

unread,
Jun 25, 2011, 8:16:23 AM6/25/11
to SpatiaLite Users
Hi Preet,

just a general order consideration:
Lat/Long coordinates are useful when you
absolutely need to handle some world-wide
coverage (sea/air navigation and so on).

But in many "normal" cases only a by far
smaller portion of planet Earth is actually
required to be accessed (county, region, state ...).

If this one is your case, "projecting" your
OSM datasets into the appropriate planar CRS
(may well be, using the corresponding WGS84
UTM zone) surely will make your "distance"
problems much easier to be resolved.

And performance/timings will greatly benefit
from this for sure.

bye Sandro
Reply all
Reply to author
Forward
0 new messages