Geography Reference
In-Depth Information
Figure 57: Number of Towns in Cumbria, Durham, and Tyne and Wear Counties
The SQL for this introduces the spatial function ST_Within , which tests to see if one
geometry is fully within another.
There are two important concepts to remember from this example:
For one object to be within another, the inner object must be fully inside the outer
object's bounding line. In the county example, if your geometry is smaller than the
thickness of any of the county boundary lines and lies directly on one, ST_Within
would not have picked it up. Instead, it would have been identified as intersecting
with the geometry rather than being within it.
The order of parameters on some spatial SQL functions is important. In the county
example, if you switch the order of the county and town parameters, you'll find that
you receive no results because a point cannot fully contain a polygon that is much
larger than itself.
Because spatial SQL accounts for boundary polygons when performing relationship-based
measurements, there are a number of different functions that perform very similar tasks with
slight differences.
In the case of ST_Within , we have the following similar functions:
ST_Contains
ST_Covers
ST_CoveredBy
ST_Intersects
The Postgres and OGC specifications document the differences in fine detail way better than
I can describe here, but essentially one only works on the interior of the polygon, and the
others work on combinations of the interior enclosing ring and various levels of intersection.
As a developer, you'll most likely never use anything other than ST_Within , and in rare
cases ST_Contains , for most of the GIS work you'll do.
In our counties example, you can also see that we've had to use ST_Transform to transform
our counties into the correct SRID again. If you keep all your geometry in the same SRID
when loading your database, you begin to see how much simpler your SQL can be.
Scenario 3: How close is this to that?
Knowing how far away something is always has a place in GIS. Whether you need to know
how close the nearest McDonald's is, or how close a friend's house is, this is one of the most
Search WWH ::




Custom Search