Reputation: 618
im making a panorama viewer, connected to a postgres db, I need to download building names, street names, interest points within a given range. I really appreciate if you could shed some light on this matter.
Regards,
Upvotes: 1
Views: 489
Reputation: 9167
As you did not give us some table specifications, an artificial example will have to do.
WITH yourpoint AS (
SELECT
st_transform(st_setsrid(st_makepoint(yourlon, yourlat), 4326), yourgeomsrid) AS point
)
SELECT
building.name
FROM
building, yourpoint
WHERE
ST_Dwithin(building.the_geom, yourpoint.point, 400)
Now, let's have a look at the interesting parts. At first we need a geometry to accomodate your given point in the correct coordinate system. I created a substatement (CTE) that generates such a point. Maybe the point is within your database, though.
Important: For calculating distances, the point you specify and the geometries have to be in the same coordinate system. Thus, I encapsulated the st_makepoint
with st_setsrid
, which adds coordinate system information to the point. GPS usually is in WGS84 which has SRID 4326. Now, we have to transform that point to the coordinate system of your point data - the SRID therefore you have to know.
All the magic for finding the buildings close by happens in the WHERE
clause. ST_Dwithin
looks up all geometries that have a distance of less than 400 (measured in the unit of your coordinate system) to each other.
See the postgis manual for details.
If your coordinates are in WGS84, you should use the geography datatype:
WITH yourpoint AS (
SELECT
geography(st_setsrid(st_makepoint(yourlon, yourlat), 4326)) AS point
)
SELECT
building.name
FROM
building, yourpoint
WHERE
ST_Dwithin(Geography(building.the_geom), yourpoint.point, 400)
Upvotes: 3
Reputation: 401
Try
select * FROM
LayerName
WHERE
ST_Dwithin(LayerName.the_geom,
GeomFromText('POINT(x,y)',-1)
, radius)
layerName is your layer, x,y from GPS, -1 is your srid, radius is radius of your buffer(double)
Upvotes: 2