[postgis-users] extent of properties within a specified distance

Obe, Regina robe.dnd at cityofboston.gov
Wed May 21 03:38:57 PDT 2008

Have you tried using ST_DWithin(...)  ?
- even with Within (neither Distance nor Within use spatial indexes so
you are essentially always doing a table scan.  You would still need an
&& for those to make them use indexes.  So try:
SELECT ST_Extent(geom) as ext, Max(ST_Distance(geom, '<center of
polygon>')) as max_dist
FROM tarea
WHERE  ST_DWithin(geom,'<center of polygon>', 50);
If you are using an older version of Postgis that doesn't have the ST_
(e.g. pre 1.3)
Do the following instead
SELECT Extent(geom) as ext, Max(Distance(geom, '<center of polygon>'))
as max_dist
FROM tarea
WHERE  geom && Expand('<center of polygon>',50)  AND Distance(geom,
'<center of polygon>') < 50;
Hope that helps,



From: postgis-users-bounces at postgis.refractions.net
[mailto:postgis-users-bounces at postgis.refractions.net] On Behalf Of User
Sent: Wednesday, May 21, 2008 5:19 AM
To: PostGIS Users Discussion
Subject: Re: [postgis-users] extent of properties within a specified


thanks, but the problem is still there, the time Distance() query is
taking. the concept behind using within() is only to minimize the number
of time the distance function executes. it works ok without using
within() also, but the time is still much longer. is there any way to
minimize the time it is taking to perform distance(), or any other way
to do the same task without using distance().


----- Original Message ----
From: Andy Anderson <aanderson at amherst.edu>
To: PostGIS Users Discussion <postgis-users at postgis.refractions.net>
Sent: Tuesday, May 20, 2008 10:09:27 PM
Subject: Re: [postgis-users] extent of properties within a specified

On May 19, 2008, at 5:01 AM, User Map wrote:



	thanks for reply. i think i have to clarify my problem a bit
more. i want to get the records from my table within a specified
distance for e.g 50 meters. For this i have to calculate the extent
first to get the image in mapserver. what i have is just a polygon geom,
which area as you know can vary from property to property. i dont want
to take into account the area/distance the property holds, but a center
point for that polygon, so to calculate the distance equally on each
side of that point, and then calculate the extent of those records, for
the image to be generated. what i have done uptil now is:

	SELECT extent(geom),max(Distance('<center of polygon>',geom))as

	from tarea

	where within(geom,'<expand(geom,50)from the given geom>')

	and Distance('<center of polygon>',geom)<=50

	but this query is taking about 25-26 mins which is quite a large
time span. and also when i get the results and on showing through the
map server, the scale bar is not matching, i mean it has to show a 100
meter bar scale, but, it is showing more then 100 meters on the
scalebar, about 192 or so.

A couple of thoughts, upon which the more experienced may correct or

1) It looks to me like you are performing a duplicate calculation. I'm
not sure if Postgres is smart enough to avoid this by itself, but just
in case you should take advantage of a subexpression to perform the
calculation once for each geom:

SELECT extent(geom), distance from (SELECT geom, Distance('<center of
polygon>', geom)) from tarea) as tdistance where distance <= 50.

2) Seems like

within(geom,'<expand(geom,50)from the given geom>')

is unnecessary, it will always be true. Or do you mean something
different than the simpler form:

within(geom, expand(geom,50))

-- Andy

The substance of this message, including any attachments, may be
confidential, legally privileged and/or exempt from disclosure
pursuant to Massachusetts law. It is intended
solely for the addressee. If you received this in error, please
contact the sender and delete the material from any computer.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/postgis-users/attachments/20080521/aae944bb/attachment.html>

More information about the postgis-users mailing list