[geos-devel] strange behaviour of LinearRing::within()

Marco Hugentobler marco.hugentobler at karto.baug.ethz.ch
Fri May 11 12:41:18 EDT 2007


Dear geos developers,

I'm quite new to geos development, so I hope this is the right place for my 
(probably stupid beginer) question:

I have two linearRings and like to use the function within() with them. 
However, the results are strange. And if I create two polygons with the 
rings, the result of Polygon::within() is correct. This happens for me with 
both geos2.2.3 and geos-3.0.0rc4.tar.bz2. Probably I'm doing something wrong. 
Find below a simple example program.

Thanks for your help,
Marco

#include <geos.h>
#include <iostream>

/**Little program to test the within() function of geos*/
int main(void)
{
  geos::geom::GeometryFactory factory;

  std::vector<geos::geom::Geometry*> innerHoles; //no holes
  std::vector<geos::geom::Geometry*> outerHoles;

  //inner ring
  geos::geom::DefaultCoordinateSequence* innerSequence=new 
geos::geom::DefaultCoordinateSequence();
  innerSequence->add(geos::geom::Coordinate(1, 1));
  innerSequence->add(geos::geom::Coordinate(2, 1));
  innerSequence->add(geos::geom::Coordinate(2, 2));
  innerSequence->add(geos::geom::Coordinate(1, 1));
  geos::geom::LinearRing* innerRing = factory.createLinearRing(innerSequence);
  geos::geom::Polygon* innerPolygon = factory.createPolygon(innerRing, 
&innerHoles);

  //outer ring
  geos::geom::DefaultCoordinateSequence* outerSequence=new 
geos::geom::DefaultCoordinateSequence();
  outerSequence->add(geos::geom::Coordinate(0, 0));
  outerSequence->add(geos::geom::Coordinate(3, 0));
  outerSequence->add(geos::geom::Coordinate(3, 3));
  outerSequence->add(geos::geom::Coordinate(0, 3));
  outerSequence->add(geos::geom::Coordinate(0, 0));
  geos::geom::LinearRing* outerRing = factory.createLinearRing(outerSequence);
  geos::geom::Polygon* outerPolygon = factory.createPolygon(outerRing, 
&outerHoles);

  //if(innerPolygon->within(outerPolygon))
  if(innerRing->within(outerRing))
    {
      std::cout << "inner feature is within outer feature" << std::endl;
    }
  else
    {
      std::cout << "outer feature is within inner feature, something must be 
wrong" << std::endl;
    }

  return 0;
}

-- 
Dr. Marco Hugentobler
Institute of Cartography
ETH Zurich
marco.hugentobler at karto.baug.ethz.ch



More information about the geos-devel mailing list