# [postgis-users] Simple question: How to get projected distance?

Lance Arlaus lance.nospam.1 at codeberet.com
Fri Aug 12 08:44:14 PDT 2005

Thanks for the reply.  I'm trying to solve what I thought would be a fairly
simple set of problems for a GIS system, something I've seen in examples.
However, I've tried several options thus far with no luck.  Any help would
be greatly appreciated.

Here's what I'm trying to accomplish given a set of incoming lat/long points
(WGS 84) and shapes defined using a series of the same:
1. Find the distance between any two points.  Ideally, this would be the
distance along the surface of the reference spheroid, or a reasonable
approximation (0.5% for distances up to 20km would be fine).
2. Create arbitrary polygons (approximate circle around a point or a shape
representing a postal code) and perform Boolean operations on them (union,
intersection, and the like).
3. Efficiently perform basic queries against the points and shapes finding
which points are contained within a corresponding area or within a certain
distance of a specific point.

I realize that the SRID is simply a tag on the data, but I'm assuming the
underlying GIS library would use the tag to intelligently interpret
about the various coordinate systems, and still can't seem to get this to
work.

Here's what I've tried and the problems I've encountered:
1. Tagging the points with an SRID of 4326 (WGS 84) and directly querying
for the distance.  Since it's not a projected coordinate system, it just
gives the Cartesian distance.  Pretty useless.
2. Use distance_sphere() to calculate the distance.  Great, I get distances,
but can't create shapes, etc.
3. Transform the points into a UTM projection and create polygons and query
for distance on the transformed points.  This works, however the system must
work across the US and I'd have to have a table for each UTM zone and add a
lot of logic to accommodate not to mention separate indexes, etc.  Very ugly
option.
4. Transform into UTM, perform the operations, and transform the UTM
coordinates back into WGS 84 lat/long, storing the resulting points in one
table with the appropriate indexes attached for efficient querying.
Ideally, I'd like to end up with a set of WGS 84 defined geometries that I
can query.  Unfortunately, this doesn't work.  Transforming the points back
into WGS 84 yields nonsense results.

I thought this would be very simple.  At this point, I'm quite frustrated.
What am I doing wrong??

Some of my results:
-- Create WGS84 point and transform into UTM zone 18N
ASText(Transform(PointFromText('POINT(40.8117 -74.218)', 4326), 32618));
Result: "POINT(2192975.52043048 -10779305.4033352)"

-- Create WGS84 point, transform into UTM zone 18N, and transform back
ASText(Transform(Transform(PointFromText('POINT(40.8117 -74.218)', 4326),
32618), 4326));
Result: "POINT(-75 -90)"

Thanks,
Lance

Lance Arlaus wrote:
> I know this is a simple question, but I'm just getting started with
> PostGIS and I can't seem to perform a simple calculation.
>
> I just need to calculate the distance between two points.  What am I
> doing wrong?
>
>
>
> Query:
>
>     SELECT distance(PointFromText('POINT(40.8117 -74.218)', 32617),
> PointFromText('POINT(40.8272 -74.1242)', 32617))
>
> Returns:
>
>     0.095.

This is the distance in the input units. Since the values you are using
are decimal degrees the result is 0.095... decimal degrees and probably
assumes that you are computing this in a flat X-Y plane.

> Of course if I do the following, I get a reasonable answer (about 10km),
> but it's not exactly what I want

OK, what is it that you want?

>     SELECT distance_sphere(PointFromText('POINT(40.8117 -74.218)',
> 32617), PointFromText('POINT(40.8272 -74.1242)', 32617))

This is better than you first try because this is computing the arc
distance along the great circles between the two points.

> Even constructing a line and taking the length doesn't work and I've
> tried all SRIDs.
>

SRIDs are an attribute associated with the geometry, like, red, green,
blue. Changing them will not change the answer unless you pass them to a
utility that is actively aware of them. They are there to you don't try
to compare or manipulate two points that were stored in different SRIDs.

The standard use of them is to use them to perform projections from the
objects stored SRID to some common workspace that would be good for
manipulation.

The SRID for WGS84 is I believe 4326

To do things right you need to understand the projection space that you
data is in and how it effects calculations and the postGIS functions.

>
> Sorry to ask such a simple question.
>
> Also, is there a document that describes the well known SRIDs?  I'm not
> sure which one to use.  For example, the data I've got references WGS
> 84, but there's a ton of SRIDs defined.  I've found docs describing the
> format of the specification, but how are the SRIDs created (who serves
> as the clearinghouse for this info)?

HTH,
-Steve
_______________________________________________
postgis-users mailing list
postgis-users at postgis.refractions.net
http://postgis.refractions.net/mailman/listinfo/postgis-users