[Gdal-dev] Best way to test if one OGRLinearRing is inside of another?

Dan Homerick danhomerick at gmail.com
Sun Sep 23 13:38:20 EDT 2007


Yes, GEOS support is built in. OGRGeometryFactory::haveGEOS() returns true,
and the within/contains functions will return true when testing a point vs.
some (non-LinearRing) geometry.

Test code demonstrating that within/contains never returns true with two
nested, non-identical, closed LineStrings:

    /* a clockwise square starting at (1,2) (closed) */
    double x1[] = {1, 2, 2, 1, 1};
    double y1[] = {2, 2, 1, 1, 2};

    /* a clockwise square starting at (0,3) (closed) */
    double x2[] = {0, 3, 3, 0, 0};
    double y2[] = {3, 3, 0, 0, 3};

    OGRLineString inner;
    inner.setPoints(5, x1, y1);

    OGRLineString outer;
    outer.setPoints(5, x2, y2);

    std::cout << "**********************************************" <<
std::endl;
    std::cout << "inner.Contains(&outer): " << inner.Contains(&outer) <<
std::endl;
    std::cout << "inner.Within(&outer): " << inner.Within(&outer) <<
std::endl;
    std::cout << "outer.Contains(&inner): " << outer.Contains(&inner) <<
std::endl;
    std::cout << "outer.Within(&inner): " << outer.Within(&inner) <<
std::endl;

Results:
**********************************************
inner.Contains(&outer): 0
inner.Within(&outer): 0
outer.Contains(&inner): 0
outer.Within(&inner): 0


On 9/23/07, Mateusz Loskot <mateusz at loskot.net> wrote:
>
> Dan Homerick wrote:
> > Hello List-Readers,
> >
> > I notice that the within/contains functions don't work for LinearRing's
> > (as is indicated in the documentation for rings), but they also don't
> work
> > with two LineStrings (not mentioned, that I see).
>
> Dan,
>
> You should know that spatial predicates are implemented using GEOS,
> so you have to built GDAL/OGR with GEOS support in order to use them.
>
> If this is not the case here and you already have GEOS support in OGR,
> could you provide us with examples of geometries you're testing and
> perhaps some code example?
>
> > Is the best/only way to test if one ring is nested in another to convert
> > the LinearRings to LineStrings, then test each point of 'inner' for
> > containment in 'outer'?
>
> Yes, I believe OGC predicates (within, contains, etc.) are the best tools
> to solve this problem.
>
> Cheers
> --
> Mateusz Loskot
> http://mateusz.loskot.net
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/gdal-dev/attachments/20070923/250a33ec/attachment.html


More information about the Gdal-dev mailing list