Spatialite API: ST_DWithin

717 views
Skip to first unread message

Genscher

unread,
Oct 20, 2010, 7:34:34 PM10/20/10
to SpatiaLite Users
Hello!

I am almost finished with having a functional drop-in replacement for
PGSQL / osm2pgsql with Mapnik.

Maybe someone can tell me an easy way how to solve my last problem:
The missing of "ST_DWithin".
See here for the definition: http://postgis.refractions.net/documentation/manual-svn/ST_DWithin.html

Is there an (easy) way to have a Spatialite drop-in replacement?


Thanks for your help,
Daniel

a.furieri

unread,
Nov 5, 2010, 8:14:27 AM11/5/10
to SpatiaLite Users
Hi Daniel,

supporting the PostGIS-like ST_DWithin()
function in SpatiaLite is quite impossible:
- we have no Geography data-type: we simply
have Geometry
- in SpatiaLite directly accessing a Spatial
Index from a SQL function isn't possible
at all.
- computing geodesic distances for complex
Linestrings or Polygons seems to require
an intolerable amount of time.

Anyway, something like ST_DWithin() is
surely useful and absolutely required.

---------------

Boolean PtDistWithin (
geom1 Geometry,
geom2 Geometry,
dist Double precision
[, use_spheroid Boolean] )

a] if *geom1* and/or *geom2* are invalid,
return value is NULL

b] if both *geom1* and *geom2* are simple
POINTs into the 4326 SRID (WGS84),
then the GreatCircle (or Geodesic)
distance is evaluated (measured in
Meters)

c] in any other case the 'plain' distance
will be evaluated as usual, and the measure
unit will be the 'natural' one for the
corresponding SRID

d] if the computed distance (following the
above rule) is <= *dist*, the function
will return TRUE
otherwise will return FALSE

the optional arg *use_spheroid* is used
only in the b] case:
- if *use_spheroid=TRUE* the geodesic
distance will be computed (slowest,
but most accurate)
- if *use_spheroid=FALSE* the great
circle distance will be computed
(fastest, bet less accurate)
Default: FALSE (great circle)



code snippet #1:
=====================================

CREATE TABLE MyPoints (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL);

SELECT AddGeometryColumn ('MyPoints',
'Geometry', 4326, 'POINT', 'XY');

INSERT INTO MyPoints ....;

SELECT * FROM MyPoints
WHERE PtDistWithin(Geometry,
MakePoint(11.87691, 43.46139, 4326),
10000.0) = 1;

SELECT * FROM MyPoints
WHERE PtDistWithin(Geometry,
MakePoint(11.87691, 43.46139, 4326),
2500.0, 1) = 1;

the first query will identify
any point laying within a radius
of 10 km (using the fastest great
circle distance)

the second query will identify
any point laying within a radius
of 2.5 km (using the most accurate
geodesic distance)



code snippet #2:
===========================
SELECT CreateSpatialIndex('MyPoints',
'Geometry');

SELECT * FROM MyPoints
WHERE PtDistWithin(Geometry,
MakePoint(11.87691, 43.46139, 4326),
2500.0, 1) = 1 AND ROWID IN (
SELECT pkid FROM idx_MyPoints_Geometry
WHERE pkid MATCH RTreeIntersects(
11.87691 - 0.5, 43.46139 - 0.5,
11.87691 + 0.5, 43.46139 + 0.5)
);

and when the dataset to be inspected does
actually contains an huge number of rows,
using a Spatial Index is a really good
idea :-)

=====================================

to be released ASAP (really ...)

bye Sandro

Genscher

unread,
Nov 6, 2010, 4:58:28 PM11/6/10
to SpatiaLite Users
Hello Sandro,

That's great news - awesome work!

Thanks,

Daniel
Reply all
Reply to author
Forward
0 new messages