Get points inside a bounding box
I'm trying to choose places from my postgis db that are inside a certain bounding box. I'm trying to accomplish this with this 开发者_StackOverflow中文版query:
//latlong - latitude, longitude of a place
SELECT * FROM places WHERE St_Contains(St_GeomFromText('Polygon((:top_left_long :top_left_lat, :bottom_right_long :bottom_right_lat))'), latlong);
First of all - I get the following error:
Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: :top_left_lat
What does it mean? And the second issue - am I feeding these parameters in good order? I mean - first longitude, then latitude?
Here is a query I used in an old project:
SELECT param1, param2, ...
FROM messages
WHERE ST_Contains(
ST_SetSRID(
ST_MakeBox2D(
ST_Point(0, 50), ST_Point(50,0)
),
4326
),
the_geom
)
the_geom was my geometry column Note: MakeBox2D take top-left and right-bottom
With those arguments you can't build a Polygon.
A Polygon is a planar Surface defined by 1 exterior boundary and 0 or more interior boundaries. Each interior boundary defines a hole in the Polygon. A Triangle is a polygon with 3 distinct, non-collinear vertices and no interior boundary.
The exterior boundary LinearRing defines the “top” of the surface which is the side of the surface from which the exterior boundary appears to traverse the boundary in a counter clockwise direction. The interior LinearRings will have the opposite orientation, and appear as clockwise when viewed from the “top”.
The assertions for Polygons (the rules that define valid Polygons) are as follows:
a) Polygons are topologically closed;
b) The boundary of a Polygon consists of a set of LinearRings that make up its exterior and interior boundaries;
c) No two Rings in the boundary cross and the Rings in the boundary of a Polygon may intersect at a Point but only as a tangent, e.g.
(taken from OpenGIS Implementation Specification for Geographic information - Simple feature access - Part 1: Common architecture)
With those arguments ou can either create a box2d or create the polygon feeding all the indidual points.
Two quick notes:
- make sure your arguments are strings or
St_GeomFromText
will not work - use
ST_SetSRID
to define your coordinate system so that you don't have unpleasant results
I think it's because :top_left_long
and other params are not replaced by your values.
Can you print the SQL query before the execution?
精彩评论