[postgis-users] Select polygons within polygon with PostGIS 2.0

Sandro Santilli strk at keybit.net
Wed Oct 10 03:16:29 PDT 2012


On Wed, Oct 10, 2012 at 11:44:54AM +0200, Michal Zimmermann wrote:
> Thanks for the answers guys! I guess the data is ok, I was given them
> at the university which had bought it from the GIS company, that's why
> I trust this data source. Also when I try selecting the disctricts
> with ArcMap, it works (not with QGIS, though!).
> Sandro, would you be so kind and share some thoughts on PostGIS
> Topology? As I understood, it is a completely new feature of v2,
> right? What are the benefits?

One benefit is making the spatial relationships explicit, so that 
you could encode your counties as being composed by the districts,
and the districts would be composed by faces, which would be defined
by (shared) edges.
At that point querying for "all districts in a county" would just be
a matter of listing the components of a "county" TopoGeometry.

But I guess you're still in the data preparation phase as you still
don't know which district record belong to which county, right ?
In that case you'll need ST_Covers (assuming the input data is
_really_ correct and doesn't contain cases in which the shape
of a district is slightly outside the shape of its county.

Note that storing the data in a topology would be an excellent way to
have better control on those kind of data problems (often not easily
detectable as invisible to the eye).

--strk;

  ,------o-. 
  |   __/  |    Delivering high quality PostGIS 2.0 !
  |  / 2.0 |    http://strk.keybit.net
  `-o------'


> On 10/10/12, Sandro Santilli <strk at keybit.net> wrote:
> > On Wed, Oct 10, 2012 at 10:27:42AM +0200, Michal Zimmermann wrote:
> >> Hi to all,
> >> I have a relation representing all the counties in my country and the
> >> other one containing all the districts - e. g. each and every county
> >> is composed of several districts. How do I select them with spatial
> >> query? I tried with
> >> select nazorp from kraje as k, orp_wgs as o WHERE ST_Within(o.geom,k.geom)
> >> AND
> >> k.nazev = 'Liberecký' // name of the county
> >>
> >> but had wrong results returned (well, they are probably not wrong,
> >> they are just not what I expected them to be). ST_Within only returns
> >> districts that don't share a boundary with the county, but I need to
> >> get all the districts within the county. Is that possible? I haven't
> >> found any built-in function suitable for my needs yet.
> >
> > Try ST_Covers, which includes the boundaries.
> >
> > PS: yours is the perfect use case for PostGIS Topology !
> >
> > --strk;



More information about the postgis-users mailing list