Select to view content in your preferred language

Polygons constructed in a counter-clockwise fashion are not simple?

2305
4
06-18-2010 09:14 PM
GeorgeSilva
Emerging Contributor
hello guys

I'm having trouble with some unit testing here. Check this out (all are snippets, not everything is in the same class).

This is my constructor:

        public Memorial(PolygonClass perimeter)
        {
            if (!perimeter.IsClosed)
                throw new ArgumentException(Messages.PolygonOpen);

            if (perimeter.IsEmpty)
                throw new ArgumentException(Messages.PolygonEmpty);

            if (!perimeter.IsSimple)
                throw new ArgumentException(Messages.GeometrtyMustBeSimple); // usually getting an error here

            if (!OnlyLineSegments(perimeter))
                throw new ArgumentException(Messages.PolygonWithCurves);

            this._Perimeter = perimeter;
            this._NorthPoint = FindNorthenPoint();
            this._DistancesAndAzimuths = new List<IMeasure>();
        }

Here is the unit test:

        [TestMethod]
        public void TestFindNorthernPoint_SquareCC()
        {
            this.memo1 = new Memorial(BuildSquareCC());

            IPoint p = new PointClass();
            p.PutCoords(1, 1);

            Assert.AreEqual(p.X, memo1.NorthPoint.X);
            Assert.AreEqual(p.Y, memo1.NorthPoint.Y);
        }

        private PolygonClass BuildSquareCC()
        {
            object _missing = Type.Missing;

            IPoint p1 = new PointClass();
            p1.PutCoords(0, 0);

            IPoint p2 = new PointClass();
            p2.PutCoords(0, 1);

            IPoint p3 = new PointClass();
            p3.PutCoords(1, 1);

            IPoint p4 = new PointClass();
            p4.PutCoords(1, 0);

            IPoint p5 = new PointClass();
            p5.PutCoords(0, 0);

            PolygonClass polygon = new PolygonClass();

            polygon.AddPoint(p5, ref _missing, ref _missing);
            polygon.AddPoint(p4, ref _missing, ref _missing);
            polygon.AddPoint(p3, ref _missing, ref _missing);
            polygon.AddPoint(p2, ref _missing, ref _missing);
            polygon.AddPoint(p1, ref _missing, ref _missing);

            for (int i = 0; i <= polygon.PointCount - 1; i++)
            {
                Console.WriteLine("X: {0}; Y: {1}", polygon.get_Point(i).X.ToString(), polygon.get_Point(i).Y.ToString());
            }

            return polygon;
        }

So, are CC polygons NOT simple? Am I doing something wrong??

Thanks
0 Kudos
4 Replies
KimOllivier
Honored Contributor

I understood that a polygon must be defined in a Counter-clockwise direction to be valid. (I got this reversed in my original answer)
A clockwise ring will define a hole in a polygon. But it must be enclosed by the first ring.
So the outside ring must start counter-clockwise with alternate clock/counter-wise nested rings if you had the (real) case of say a lake on an island in a lake.

The check/repair geometry tool will soon take note if you actually manage to build an invalid topology at  a low level. Somet loaders check on loading and reject invalid topology. But if you copy a featureclass using featureclassTofeatureclass you will copy the invalid geometry, storing up problems for any later analysis and drawing.

OGC well known text does not define the direction except to note that the righthand rule will imply the polygon 'faces up". If you hod up your right hand with thumb, first and second fingers stretched, first finger along the line, then the second finger points to the inside and the thumb points up.

KML definitely gets it 'wrong' from an Esri point of view, so always repair geometry when loading KML polygons or they won't draw properly and you will have trouble editing them.

I am having trouble with loading data via spatialite too. The WKT loader seems to bypass any geometry validation and even just gets it wrong.

0 Kudos
GeorgeSilva
Emerging Contributor
Thanks Kimo.

So, I can assume that all outer rings will be clockwise? I don't care how they are built, by connecting points or converting a dxf, ArcGIS will always have outer rings in a clockwise fashion?

Thanks
0 Kudos
KimOllivier
Honored Contributor
Thanks Kimo.

So, I can assume that all outer rings will be clockwise? I don't care how they are built, by connecting points or converting a dxf, ArcGIS will always have outer rings in a clockwise fashion?

Thanks


Here is the shapefile specification
http://www.esri.com/library/whitepapers/pdfs/shapefile.pdf
This explains the rules for shapes, and even more stringent rules apply to OGC polygons.
http://en.wikipedia.org/wiki/Simple_Features
0 Kudos
GeorgeSilva
Emerging Contributor
Big help Kimo! Thanks a million!
0 Kudos