Example #1
0
 /// <summary>
 /// Converts a MultiPolygon to &lt;MultiPolygon Text&gt; format, then Appends to it to the writer.
 /// </summary>
 /// <param name="multiPolygon">The MultiPolygon to process.</param>
 /// <param name="level"></param>
 /// <param name="writer">The output stream to Append to.</param>
 protected void AppendMultiPolygonText(MultiPolygon multiPolygon, int level, StringWriter writer)
 {
     if (multiPolygon.isEmpty())
     {
         writer.Write("EMPTY");
     }
     else
     {
         int  level2   = level;
         bool doIndent = false;
         writer.Write("(");
         for (int i = 0; i < multiPolygon.getNumGeometries(); i++)
         {
             if (i > 0)
             {
                 writer.Write(", ");
                 level2   = level + 1;
                 doIndent = true;
             }
             //AppendPolygonText((Polygon) multiPolygon.GetGeometryN(i), level2, doIndent, writer);
             AppendPolygonText((Polygon)multiPolygon.getGeometryN(i), level2, doIndent, writer);
         }
         writer.Write(")");
     }
 }
Example #2
0
        /// <summary>
        /// Returns the largest <b>Polygon</b> in terms of area contained in the <b>MultiPolygon</b>.
        /// </summary>
        /// <param name="multiPolygon">The <b>MultiPolygon</b>.</param>
        /// <returns>The largest <b>Polygon</b> in terms of area contained in the <b>MultiPolygon</b>.</returns>
        /// <exception cref="ArgumentException"><i>multiPolygon</i> does not contain any <b>Polygon</b> objects.</exception>
        public static Polygon GetLargestPolygon(MultiPolygon multiPolygon)
        {
            if (multiPolygon.getNumGeometries() == 0)
            {
                throw new ArgumentException("MultiPolygon contains no polygons.", "multiPolygon");
            }

            Polygon largest = (Polygon)multiPolygon.getGeometryN(0);

            for (int i = 0; i < multiPolygon.getNumGeometries(); i++)
            {
                if (multiPolygon.getGeometryN(i).getArea() > largest.getArea())
                {
                    largest = (Polygon)multiPolygon.getGeometryN(i);
                }
            }
            return(largest);
        }
Example #3
0
        public void TestMultiPolygon1()
        {
            string          wkt          = "MULTIPOLYGON (((10 10, 10 20, 20 20, 20 15 , 10 10), (50 40, 50 50, 60 50, 60 40, 50 40)))";
            GeometryFactory factory      = new GeometryFactory();
            Geometry        geometry     = new GeometryWKTReader(factory).Create(wkt);
            MultiPolygon    multiPolygon = (MultiPolygon)geometry;

            //Assertion.AssertEquals("Multilinestring 1",2,multiPolygon.NumGeometries);
            Geometry   g     = multiPolygon.getGeometryN(0);
            Polygon    poly1 = (Polygon)multiPolygon.getGeometryN(0);
            LinearRing shell = poly1.shell;
            LinearRing hole  = poly1.holes[0];

            Assertion.AssertEquals("MPS 1", 10.0, shell.getCoordinates()[0].x);
            Assertion.AssertEquals("MPS 2", 10.0, shell.getCoordinates()[0].y);
            Assertion.AssertEquals("MPS 3", 10.0, shell.getCoordinates()[1].x);
            Assertion.AssertEquals("MPS 4", 20.0, shell.getCoordinates()[1].y);
            Assertion.AssertEquals("MPS 5", 20.0, shell.getCoordinates()[2].y);
            Assertion.AssertEquals("MPS 6", 20.0, shell.getCoordinates()[2].y);
            Assertion.AssertEquals("MPS 7", 20.0, shell.getCoordinates()[3].x);
            Assertion.AssertEquals("MPS 8", 15.0, shell.getCoordinates()[3].y);
            Assertion.AssertEquals("MPS 9", 10.0, shell.getCoordinates()[4].x);
            Assertion.AssertEquals("MPS 10", 10.0, shell.getCoordinates()[4].y);

            Assertion.AssertEquals("MPS 11", 50.0, hole.getCoordinates()[0].x);
            Assertion.AssertEquals("MPS 12", 40.0, hole.getCoordinates()[0].y);
            Assertion.AssertEquals("MPS 13", 50.0, hole.getCoordinates()[1].x);
            Assertion.AssertEquals("MPS 14", 50.0, hole.getCoordinates()[1].y);
            Assertion.AssertEquals("MPS 15", 60.0, hole.getCoordinates()[2].x);
            Assertion.AssertEquals("MPS 16", 50.0, hole.getCoordinates()[2].y);
            Assertion.AssertEquals("MPS 17", 60.0, hole.getCoordinates()[3].x);
            Assertion.AssertEquals("MPS 18", 40.0, hole.getCoordinates()[3].y);
            Assertion.AssertEquals("MPS 19", 50.0, hole.getCoordinates()[4].x);
            Assertion.AssertEquals("MPS 20", 40.0, hole.getCoordinates()[4].y);

            string wkt2 = new GeometryWKTWriter().Write(multiPolygon);

            Assertion.AssertEquals("wkt", true, Compare.WktStrings(wkt, wkt2));
        }
Example #4
0
        private static Geometry Simplify(MultiPolygon multipolygon, double tolerance)
        {
            GeometryFactory gf = new GeometryFactory();

            Polygon[] generalizedPolygons = new Polygon[multipolygon.getNumGeometries()];
            for (int i = 0; i < generalizedPolygons.Length; i++)
            {
                Polygon polygon = (Polygon)multipolygon.getGeometryN(i);
                generalizedPolygons[i] = Simplify(polygon, tolerance);
            }
            MultiPolygon multiPolygon = gf.createMultiPolygon(generalizedPolygons);

            return(multiPolygon);
        }
Example #5
0
 /// <summary>
 /// Converts a MultiPolygon to &lt;MultiPolygon Text&gt; format, then Appends to it to the writer.
 /// </summary>
 /// <param name="multiPolygon">The MultiPolygon to process.</param>
 /// <param name="writer">The output stream to Append to.</param>
 public void AppendMultiPolygonText(MultiPolygon multiPolygon, TextWriter writer)
 {
     if (multiPolygon.isEmpty())
     {
         writer.Write("EMPTY");
     }
     else
     {
         //writer.Write("M");
         for (int i = 0; i < multiPolygon.getNumGeometries(); i++)
         {
             /*if (i > 0 && (i<multiPolygon.GetNumGeometries()-1) )
              * {
              *      writer.Write(", ");
              * }*/
             AppendPolygonText((Polygon)multiPolygon.getGeometryN(i), writer);
         }
         //writer.Write("Z");
     }
 }
Example #6
0
        private void WriteMultiPolygon(MultiPolygon mp, WKBByteOrder byteOrder)
        {
            // Write byte order
            _writer.Write((byte)byteOrder);

            // Write type
            this.WriteType(mp, byteOrder);

            // Get the number of polygons in this multipolygon.
            int numpolygons = mp.getNumGeometries();

            // Write the number of polygons.
            this.Write((uint)numpolygons, byteOrder);

            for (int i = 0; i < numpolygons; i++)
            {
                // Write each polygon.
                this.WritePolygon((Polygon)mp.getGeometryN(i), byteOrder);
            }
        }
Example #7
0
        private int GetNumParts(Geometry geometry)
        {
            int numParts = 0;

            if (geometry is MultiPolygon)
            {
                numParts = 0;
                MultiPolygon multiPolygon = (MultiPolygon)geometry;
                for (int i = 0; i < multiPolygon.getNumGeometries(); i++)
                {
                    numParts = numParts + GetNumParts(multiPolygon.getGeometryN(i));
                }
            }
            else if (geometry is Polygon)
            {
                numParts = ((Polygon)geometry).getNumInteriorRing() + 1;
            }
            else
            {
                throw new InvalidOperationException("Should not get here.");
            }

            return(numParts);
        }
Example #8
0
 /// <summary>
 /// Converts a MultiPolygon to &lt;MultiPolygon Text&gt; format, then Appends to it to the writer.
 /// </summary>
 /// <param name="multiPolygon">The MultiPolygon to process.</param>
 /// <param name="level"></param>
 /// <param name="writer">The output stream to Append to.</param>
 protected void AppendMultiPolygonText(MultiPolygon multiPolygon, int level, StringWriter writer)
 {
     if ( multiPolygon.isEmpty() )
     {
         writer.Write("EMPTY");
     }
     else
     {
         int level2 = level;
         bool doIndent = false;
         writer.Write("(");
         for (int i = 0; i < multiPolygon.getNumGeometries(); i++)
         {
             if (i > 0)
             {
                 writer.Write(", ");
                 level2 = level + 1;
                 doIndent = true;
             }
             //AppendPolygonText((Polygon) multiPolygon.GetGeometryN(i), level2, doIndent, writer);
             AppendPolygonText((Polygon) multiPolygon.getGeometryN(i), level2, doIndent, writer);
         }
         writer.Write(")");
     }
 }
Example #9
0
        private void WriteMultiPolygon(MultiPolygon mp, WKBByteOrder byteOrder)
        {
            // Write byte order
            _writer.Write((byte)byteOrder);

            // Write type
            this.WriteType(mp, byteOrder);

            // Get the number of polygons in this multipolygon.
            int numpolygons = mp.getNumGeometries();

            // Write the number of polygons.
            this.Write((uint)numpolygons, byteOrder);

            for (int i = 0; i < numpolygons; i++)
            {
                // Write each polygon.
                this.WritePolygon((Polygon)mp.getGeometryN(i), byteOrder);
            }
        }
Example #10
0
        /// <summary>
        /// Convert a JTS Geometry to a WayDataBlock list.
        /// </summary>
        /// <param name="geometry">
        ///            a geometry object which should be converted </param>
        /// <returns> a list of WayBlocks which you can use to save the way. </returns>
        public static IList <WayDataBlock> toWayDataBlockList(Geometry geometry)
        {
            IList <WayDataBlock> res = new List <WayDataBlock>();

            if (geometry is MultiPolygon)
            {
                MultiPolygon mp = (MultiPolygon)geometry;
                for (int i = 0; i < mp.NumGeometries; i++)
                {
                    Polygon      p     = (Polygon)mp.getGeometryN(i);
                    IList <int?> outer = toCoordinateList(p.ExteriorRing);
                    if (outer.Count / 2 > 0)
                    {
                        IList <IList <int?> > inner = new List <IList <int?> >();
                        for (int j = 0; j < p.NumInteriorRing; j++)
                        {
                            IList <int?> innr = toCoordinateList(p.getInteriorRingN(j));
                            if (innr.Count / 2 > 0)
                            {
                                inner.Add(innr);
                            }
                        }
                        res.Add(new WayDataBlock(outer, inner));
                    }
                }
            }
            else if (geometry is Polygon)
            {
                Polygon      p     = (Polygon)geometry;
                IList <int?> outer = toCoordinateList(p.ExteriorRing);
                if (outer.Count / 2 > 0)
                {
                    IList <IList <int?> > inner = new List <IList <int?> >();
                    for (int i = 0; i < p.NumInteriorRing; i++)
                    {
                        IList <int?> innr = toCoordinateList(p.getInteriorRingN(i));
                        if (innr.Count / 2 > 0)
                        {
                            inner.Add(innr);
                        }
                    }
                    res.Add(new WayDataBlock(outer, inner));
                }
            }
            else if (geometry is MultiLineString)
            {
                MultiLineString ml = (MultiLineString)geometry;
                for (int i = 0; i < ml.NumGeometries; i++)
                {
                    LineString   l     = (LineString)ml.getGeometryN(i);
                    IList <int?> outer = toCoordinateList(l);
                    if (outer.Count / 2 > 0)
                    {
                        res.Add(new WayDataBlock(outer, null));
                    }
                }
            }
            else if (geometry is LinearRing || geometry is LineString)
            {
                IList <int?> outer = toCoordinateList(geometry);
                if (outer.Count / 2 > 0)
                {
                    res.Add(new WayDataBlock(outer, null));
                }
            }
            else if (geometry is GeometryCollection)
            {
                GeometryCollection gc = (GeometryCollection)geometry;
                for (int i = 0; i < gc.NumGeometries; i++)
                {
                    IList <WayDataBlock> recursiveResult = toWayDataBlockList(gc.getGeometryN(i));
                    foreach (WayDataBlock wayDataBlock in recursiveResult)
                    {
                        IList <int?> outer = wayDataBlock.OuterWay;
                        if (outer.Count / 2 > 0)
                        {
                            res.Add(wayDataBlock);
                        }
                    }
                }
            }

            return(res);
        }
Example #11
0
 /// <summary>
 /// Converts a MultiPolygon to &lt;MultiPolygon Text&gt; format, then Appends to it to the writer.
 /// </summary>
 /// <param name="multiPolygon">The MultiPolygon to process.</param>
 /// <param name="writer">The output stream to Append to.</param>
 public void AppendMultiPolygonText(MultiPolygon multiPolygon, TextWriter writer)
 {
     if (multiPolygon.isEmpty())
     {
         writer.Write("EMPTY");
     }
     else
     {
         //writer.Write("M");
         for (int i = 0; i < multiPolygon.getNumGeometries(); i++)
         {
             /*if (i > 0 && (i<multiPolygon.GetNumGeometries()-1) )
             {
                 writer.Write(", ");
             }*/
             AppendPolygonText((Polygon) multiPolygon.getGeometryN(i), writer);
         }
         //writer.Write("Z");
     }
 }