Best Way to Calculate Distance Between Point and Line

552 views
Skip to first unread message

Chris White

unread,
Oct 15, 2012, 5:33:41 PM10/15/12
to spatiali...@googlegroups.com
Hi All,

Does anyone know the best way (using spatialite), to calculate the distance between a Point (Lat, Long) and a Line (sequence of Lat, Longs). I was playing around directory with GEOS but wasn't sure if that was the best way. I basically have a user's GPS location and want to see how far away they are from a road segment in the road network.

I have something like this (Objective-C):

int setValue = 0;

GEOSCoordSequence *sequence1 = GEOSCoordSeq_create(1, 2);

setValue = GEOSCoordSeq_setX(sequence1, 0, currentPosition.lon);

setValue = GEOSCoordSeq_setY(sequence1, 0, currentPosition.lat);

    

GEOSGeometry *point1 = GEOSGeom_createPoint(sequence1);

    

NSArray *points = [currentSegment positions];

int size = [points count];

GEOSCoordSequence *sequence2 = GEOSCoordSeq_create(size, 2);

for (int i = 0; i < [points count]; i++) {

        Position *position = [points objectAtIndex:i];

        setValue = GEOSCoordSeq_setX(sequence2, i, position.lon);

        setValue = GEOSCoordSeq_setY(sequence2, i, position.lat);

}

GEOSGeometry *line1 = GEOSGeom_createLineString(sequence2);

    

double dist = 0.0f;

int result = GEOSDistance(point1, line1, &dist);

    

GEOSGeom_destroy(line1);

GEOSGeom_destroy(point1);


Any info would be great.


Thanks!

Chris

a.fu...@lqt.it

unread,
Oct 15, 2012, 6:17:58 PM10/15/12
to spatiali...@googlegroups.com
> Does anyone know the best way (using spatialite), to calculate the
> distance between a Point (Lat, Long) and a Line (sequence of Lat,
> Longs).
>

Hi Chris,

using Spatial SQL it's not at all difficult solving a problem
like this one: you simply have to execute the appropriate SQL
function, e.g.

SELECT ST_Distance(geom1, geom2);

please note: geom1 and geom2 could be any possible type:
- POINT / LINESTRING
- POINT / POINT
- LINESTRING / POLYGONG ... and so on ...

it doesn't matter; you'll surely get the minimun distance
between your geometries in any case.

ST_Distance() is still based on GEOS own functions; but Spatial
SQL introduces a further abstraction layers, so there is any need
at all to care about the many GEOS-specific idiosyncrasies.
You simply have to prepare and execute the appropriate SQL Statement,
and you'll immediately get back the corresponding result.

bye Sandro



--
Il messaggio e' stato analizzato alla ricerca di virus o
contenuti pericolosi da MailScanner, ed e'
risultato non infetto.

Chris White

unread,
Oct 15, 2012, 6:25:24 PM10/15/12
to spatiali...@googlegroups.com
OK thanks so much Sandro. What distance units is the result in?

a.fu...@lqt.it

unread,
Oct 15, 2012, 6:42:15 PM10/15/12
to spatiali...@googlegroups.com
> OK thanks so much Sandro. What distance units is the result in?
>

exactly the same of your input geometries.
if you are using Long/Lat coordinates, this means an angle
measured in degrees.
if you are using some planar (aka projected) CRS this usually
means meters.

if your problem is Point/Point there is a rather simple way
allowing to immediately get a metric distance from Long/Lat
coords, i.e.

SELECT GeodesicLenght( MakeLine(point1, point2) );
or
SELECT GreatCircleLength( MakeLine(point1, point2) );

* GreatCircle is less accurate, but faster (being based
on a spherical Earth)
* GeodesicLength is much accurate (being based on the
ellipsoid), but is obvuiously slower

if you problem if of the Point/Linestring or Point/Polygon
classe, you can still follow the same approach, but you
have first to determine the coordinates of the nearest
point on the Linestring (or Ring). e.g.

SELECT GeodesicLenght( MakeLine(point1, ST_ClosestPoint(point1, line2)
) );

Chris White

unread,
Oct 15, 2012, 7:14:30 PM10/15/12
to spatiali...@googlegroups.com
OK thanks so much. I'll have to give all of this a try.

--
You received this message because you are subscribed to the Google Groups "SpatiaLite Users" group.
To post to this group, send email to spatialite-users@googlegroups.com.
To unsubscribe from this group, send email to spatialite-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/spatialite-users?hl=en.


Reply all
Reply to author
Forward
0 new messages