Inheritance: MultiSurface
Beispiel #1
0
        public static FeatureDataRow LocatePolygon2(SharpMap.Geometries.Point punto, SharpMap.Data.FeatureDataTable fdt)
        {
            FeatureDataRow fdr = null;

            if ((fdt as DataTable).Rows.Count == 1)
            {
                fdr = (FeatureDataRow)(fdt as DataTable).Rows[0];
            }
            else
            {
                GisSharpBlog.NetTopologySuite.Geometries.GeometryFactory f = new GeometryFactory(new PrecisionModel());
                foreach (DataRow r in (fdt as DataTable).Rows)
                {
                    if ((r as  FeatureDataRow).Geometry.GetType() == typeof(SharpMap.Geometries.MultiPolygon))
                    {
                        // Doble cast: de Geometria a MultiPolygon, y de DataRow a FeatureDataRow.
                        SharpMap.Geometries.MultiPolygon SharpMultiPol = (SharpMap.Geometries.MultiPolygon)(r as  FeatureDataRow).Geometry;
                        GisSharpBlog.NetTopologySuite.Geometries.Geometry[] NTSGeom = GeometryConverter.ToNTSGeometry(new SharpMap.Geometries.Geometry[1] {
                            SharpMultiPol
                        }, f);
                        GisSharpBlog.NetTopologySuite.Geometries.MultiPolygon NTSMultiPol = (GisSharpBlog.NetTopologySuite.Geometries.MultiPolygon)NTSGeom[0];
                        if (NTSMultiPol.Contains(new GisSharpBlog.NetTopologySuite.Geometries.Point(punto.X, punto.Y)) == true)
                        {
                            fdr = (FeatureDataRow)r;
                            break;
                        }
                    }
                }
            }
            return(fdr);
        }
        /// <summary>
        /// Return a copy of this geometry
        /// </summary>
        /// <returns>Copy of Geometry</returns>
        public new MultiPolygon Clone()
        {
            MultiPolygon geoms = new MultiPolygon();

            for (int i = 0; i < _Polygons.Count; i++)
            {
                geoms.Polygons.Add(_Polygons[i].Clone());
            }
            return(geoms);
        }
Beispiel #3
0
        /// <summary>
        /// Return a copy of this geometry
        /// </summary>
        /// <returns>Copy of Geometry</returns>
        public new MultiPolygon Clone()
        {
            var geoms = new MultiPolygon();

            foreach (Polygon polygon in polygons)
            {
                geoms.Polygons.Add(polygon.Clone());
            }
            return(geoms);
        }
Beispiel #4
0
        /// <summary>
        /// This method produces instances of type <see cref="SharpMap.Geometries.MultiPolygon"/>.
        /// </summary>
        /// <returns>The created geometries</returns>
        internal override Collection<Geometry> createGeometries()
        {
            MultiPolygon multiPolygon = null;

            IPathNode multiPolygonNode = new PathNode(_GMLNS, "MultiPolygon", (NameTable) _XmlReader.NameTable);
            IPathNode multiSurfaceNode = new PathNode(_GMLNS, "MultiSurface", (NameTable) _XmlReader.NameTable);
            IPathNode multiPolygonNodeAlt = new AlternativePathNodesCollection(multiPolygonNode, multiSurfaceNode);
            IPathNode polygonMemberNode = new PathNode(_GMLNS, "polygonMember", (NameTable) _XmlReader.NameTable);
            IPathNode surfaceMemberNode = new PathNode(_GMLNS, "surfaceMember", (NameTable) _XmlReader.NameTable);
            IPathNode polygonMemberNodeAlt = new AlternativePathNodesCollection(polygonMemberNode, surfaceMemberNode);
            IPathNode linearRingNode = new PathNode(_GMLNS, "LinearRing", (NameTable) _XmlReader.NameTable);
            string[] labelValue = new string[1];
            bool geomFound = false;

            try
            {
                // Reading the entire feature's node makes it possible to collect label values that may appear before or after the geometry property
                while ((_FeatureReader = GetSubReaderOf(_XmlReader, null, _FeatureNode)) != null)
                {
                    while (
                        (_GeomReader =
                         GetSubReaderOf(_FeatureReader, labelValue, multiPolygonNodeAlt, polygonMemberNodeAlt)) != null)
                    {
                        multiPolygon = new MultiPolygon();
                        GeometryFactory geomFactory = new PolygonFactory(_GeomReader, _FeatureTypeInfo);
                        Collection<Geometry> polygons = geomFactory.createGeometries();

                        foreach (Polygon polygon in polygons)
                            multiPolygon.Polygons.Add(polygon);

                        _Geoms.Add(multiPolygon);
                        geomFound = true;
                    }
                    if (geomFound) AddLabel(labelValue[0], _Geoms[_Geoms.Count - 1]);
                    geomFound = false;
                }
            }
            catch (Exception ex)
            {
                Trace.TraceError("An exception occured while parsing a multi-polygon geometry: " + ex.Message);
                throw ex;
            }

            return _Geoms;
        }
 /// <summary>
 /// Return a copy of this geometry
 /// </summary>
 /// <returns>Copy of Geometry</returns>
 public new MultiPolygon Clone()
 {
     MultiPolygon geoms = new MultiPolygon();
     for(int i=0;i<_Polygons.Count;i++)
         geoms.Polygons.Add(_Polygons[i].Clone());
     return geoms;
 }
Beispiel #6
0
 public static void DrawMultiPolygon(Graphics g, MultiPolygon pols, Brush brush, Pen pen, bool clip, Map map)
 {
     for (int i = 0; i < pols.Polygons.Count; i++)
         DrawPolygon(g, pols.Polygons[i], brush, pen, clip, map);
 }
Beispiel #7
0
            public void EndFigure()
            {
                GeometryType type = _CurrentType.Peek();

                switch (type)
                {
                case GeometryType.GeometryCollection:
                {
                    var g = new SmGeometries.GeometryCollection();
                    g.Collection = _Figures;
                    _Figures     = new SmGeometries.Geometry[] { g };
                }
                break;

                case GeometryType.LineString:
                    _Figures.Add(new SmGeometries.LineString(_CurrentPoints));
                    break;

                case GeometryType.MultiLineString:
                {
                    var g = new SmGeometries.MultiLineString();
                    g.LineStrings = _Figures.Cast <SmGeometries.LineString>().ToList <SmGeometries.LineString>();
                    _Figures      = new SmGeometries.Geometry[] { g };
                }
                break;

                case GeometryType.MultiPoint:
                {
                    var g = new SmGeometries.MultiPoint();
                    g.Points = _Figures.Cast <SmGeometries.Point>().ToList <SmGeometries.Point>();
                    _Figures = new SmGeometries.Geometry[] { g };
                }
                break;

                case GeometryType.MultiPolygon:
                {
                    var g = new SmGeometries.MultiPolygon();
                    g.Polygons = _Figures.Cast <SmGeometries.Polygon>().ToList <SmGeometries.Polygon>();
                    _Figures   = new SmGeometries.Geometry[] { g };
                }
                break;

                case GeometryType.Point:
                    if (_CurrentPoints.Count > 0)
                    {
                        _Figures.Add(_CurrentPoints[0]);
                    }
                    else
                    {
                        _Figures.Add(new SmGeometries.Point());
                    }
                    break;

                case GeometryType.Polygon:
                    _Figures.Add(new SmGeometries.LinearRing(_CurrentPoints));
                    break;

                default:
                    throw new NotSupportedException(
                              string.Format(
                                  CultureInfo.CurrentCulture,
                                  SR.UnsupportedGeometryTypeException,
                                  type
                                  )
                              );
                }

                _CurrentPoints.Clear();
            }
		/// <summary>
		/// Transforms a <see cref="SharpMap.Geometries.MultiPolygon"/>.
		/// </summary>
		/// <param name="polys">MultiPolygon to transform</param>
		/// <param name="transform">MathTransform</param>
		/// <returns>Transformed MultiPolygon</returns>
		public static MultiPolygon TransformMultiPolygon(MultiPolygon polys, IMathTransform transform)
		{
            MultiPolygon pOut = new MultiPolygon(polys.Polygons.Count);

			foreach(Polygon p in polys)
				pOut.Polygons.Add(TransformPolygon(p, transform));
			
            return pOut;
		}
Beispiel #9
0
        /// <summary>
        /// Reads and parses the geometry with ID 'oid' from the ShapeFile
        /// </summary>
        /// <remarks><see cref="FilterDelegate">Filtering</see> is not applied to this method</remarks>
        /// <param name="oid">Object ID</param>
        /// <returns>geometry</returns>
        private Geometry ReadGeometry(uint oid)
        {
            brShapeFile.BaseStream.Seek(GetShapeIndex(oid) + 8, 0); //Skip record number and content length
            ShapeType type = (ShapeType) brShapeFile.ReadInt32(); //Shape type
            if (type == ShapeType.Null)
                return null;
            if (_ShapeType == ShapeType.Point || _ShapeType == ShapeType.PointM || _ShapeType == ShapeType.PointZ)
            {
                Point tempFeature = new Point();
                return new Point(brShapeFile.ReadDouble(), brShapeFile.ReadDouble());
            }
            else if (_ShapeType == ShapeType.Multipoint || _ShapeType == ShapeType.MultiPointM ||
                     _ShapeType == ShapeType.MultiPointZ)
            {
                brShapeFile.BaseStream.Seek(32 + brShapeFile.BaseStream.Position, 0); //skip min/max box
                MultiPoint feature = new MultiPoint();
                int nPoints = brShapeFile.ReadInt32(); // get the number of points
                if (nPoints == 0)
                    return null;
                for (int i = 0; i < nPoints; i++)
                    feature.Points.Add(new Point(brShapeFile.ReadDouble(), brShapeFile.ReadDouble()));

                return feature;
            }
            else if (_ShapeType == ShapeType.PolyLine || _ShapeType == ShapeType.Polygon ||
                     _ShapeType == ShapeType.PolyLineM || _ShapeType == ShapeType.PolygonM ||
                     _ShapeType == ShapeType.PolyLineZ || _ShapeType == ShapeType.PolygonZ)
            {
                brShapeFile.BaseStream.Seek(32 + brShapeFile.BaseStream.Position, 0); //skip min/max box

                int nParts = brShapeFile.ReadInt32(); // get number of parts (segments)
                if (nParts == 0)
                    return null;
                int nPoints = brShapeFile.ReadInt32(); // get number of points

                int[] segments = new int[nParts + 1];
                //Read in the segment indexes
                for (int b = 0; b < nParts; b++)
                    segments[b] = brShapeFile.ReadInt32();
                //add end point
                segments[nParts] = nPoints;

                if ((int) _ShapeType%10 == 3)
                {
                    MultiLineString mline = new MultiLineString();
                    for (int LineID = 0; LineID < nParts; LineID++)
                    {
                        LineString line = new LineString();
                        for (int i = segments[LineID]; i < segments[LineID + 1]; i++)
                            line.Vertices.Add(new Point(brShapeFile.ReadDouble(), brShapeFile.ReadDouble()));
                        mline.LineStrings.Add(line);
                    }
                    if (mline.LineStrings.Count == 1)
                        return mline[0];
                    return mline;
                }
                else //(_ShapeType == ShapeType.Polygon etc...)
                {
                    //First read all the rings
                    List<LinearRing> rings = new List<LinearRing>();
                    for (int RingID = 0; RingID < nParts; RingID++)
                    {
                        LinearRing ring = new LinearRing();
                        for (int i = segments[RingID]; i < segments[RingID + 1]; i++)
                            ring.Vertices.Add(new Point(brShapeFile.ReadDouble(), brShapeFile.ReadDouble()));
                        rings.Add(ring);
                    }
                    bool[] IsCounterClockWise = new bool[rings.Count];
                    int PolygonCount = 0;
                    for (int i = 0; i < rings.Count; i++)
                    {
                        IsCounterClockWise[i] = rings[i].IsCCW();
                        if (!IsCounterClockWise[i])
                            PolygonCount++;
                    }
                    if (PolygonCount == 1) //We only have one polygon
                    {
                        Polygon poly = new Polygon();
                        poly.ExteriorRing = rings[0];
                        if (rings.Count > 1)
                            for (int i = 1; i < rings.Count; i++)
                                poly.InteriorRings.Add(rings[i]);
                        return poly;
                    }
                    else
                    {
                        MultiPolygon mpoly = new MultiPolygon();
                        Polygon poly = new Polygon();
                        poly.ExteriorRing = rings[0];
                        for (int i = 1; i < rings.Count; i++)
                        {
                            if (!IsCounterClockWise[i])
                            {
                                mpoly.Polygons.Add(poly);
                                poly = new Polygon(rings[i]);
                            }
                            else
                                poly.InteriorRings.Add(rings[i]);
                        }
                        mpoly.Polygons.Add(poly);
                        return mpoly;
                    }
                }
            }
            else
                throw (new ApplicationException("Shapefile type " + _ShapeType.ToString() + " not supported"));
        }
Beispiel #10
0
 /// <summary>
 /// Transforms a <see cref="SharpMap.Geometries.MultiPolygon"/>.
 /// </summary>
 /// <param name="polys">MultiPolygon to transform</param>
 /// <param name="transform">MathTransform</param>
 /// <returns>Transformed MultiPolygon</returns>
 public static MultiPolygon TransformMultiPolygon(MultiPolygon polys, IMathTransform transform)
 {
     MultiPolygon pOut = new MultiPolygon();
     //pOut.Polygons = new Collection<Polygon>(polys.Polygons.Count); //Pre-inialize array size for better performance
     pOut.Polygons = new Collection<Polygon>();
     for (int i = 0; i < polys.NumGeometries; i++)
         pOut.Polygons.Add(TransformPolygon(polys[i], transform));
     return pOut;
 }
        /// <summary>
        /// Creates a <see cref="MultiPolygon"/> using the next token in the stream.
        /// </summary>
        /// <param name="tokenizer">tokenizer over a stream of text in Well-known Text
        /// format. The next tokens must form a MultiPolygon.</param>
        /// <returns>a <code>MultiPolygon</code> specified by the next token in the 
        /// stream, or if if the coordinates used to create the <see cref="Polygon"/>
        /// shells and holes do not form closed linestrings.</returns>
        private static MultiPolygon ReadMultiPolygonText(WktStreamTokenizer tokenizer)
        {
            MultiPolygon polygons = new MultiPolygon();
            string nextToken = GetNextEmptyOrOpener(tokenizer);
            if (nextToken == "EMPTY")
                return polygons;

            Polygon polygon = ReadPolygonText(tokenizer);
            polygons.Polygons.Add(polygon);
            nextToken = GetNextCloserOrComma(tokenizer);
            while (nextToken == ",")
            {
                polygon = ReadPolygonText(tokenizer);
                polygons.Polygons.Add(polygon);
                nextToken = GetNextCloserOrComma(tokenizer);
            }
            return polygons;
        }
Beispiel #12
0
 /// <summary>
 /// Converts a MultiPolygon to &lt;MultiPolygon Tagged
 /// Text&gt; format, then Appends it to the writer.
 /// </summary>
 /// <param name="multiPolygon">The MultiPolygon to process</param>
 /// <param name="writer">The output stream writer to Append to.</param>
 private static void AppendMultiPolygonTaggedText(MultiPolygon multiPolygon, StringWriter writer)
 {
     writer.Write("MULTIPOLYGON ");
     AppendMultiPolygonText(multiPolygon, writer);
 }
 /// <summary>
 /// Transforms a <see cref="SharpMap.Geometries.MultiPolygon"/>.
 /// </summary>
 /// <param name="polys">MultiPolygon to transform</param>
 /// <param name="from">Source Projection</param>
 /// <param name="to">Target Projection</param>
 /// <returns>Transformed MultiPolygon</returns>
 public static MultiPolygon TransformMultiPolygon(MultiPolygon polys, ProjectionInfo from, ProjectionInfo to)
 {
     var pOut = new MultiPolygon {Polygons = new Collection<Polygon>()};
     for (var i = 0; i < polys.NumGeometries; i++)
         pOut.Polygons.Add(TransformPolygon(polys[i], from, to));
     return pOut;
 }
Beispiel #14
0
    /// <summary>  
    /// Converts a MultiPolygon to &lt;MultiPolygon Tagged  
    /// Text&gt; format, then Appends it to the writer.  
    /// </summary>  
    /// <param name="multiPolygon">The MultiPolygon to process</param>  
    /// <param name="writer">The output stream writer to Append to.</param>  
    private static void AppendMultiPolygonTaggedText(MultiPolygon multiPolygon, StringWriter writer)
    {
      //{  
      //   "type": "MultiPolygon",  
      //   "coordinates": [  
      //       [  
      //           [ [102.0, 2.0], [103.0, 2.0], [103.0, 3.0], [102.0, 3.0], [102.0, 2.0] ]  
      //       ],  
      //       [  
      //           [ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ],  
      //           [ [100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2] ]  
      //       ]  
      //   ]  
      //}  
      writer.WriteLine("{");
      writer.WriteLine("\"type\": \"MultiPolygon\",");
      writer.WriteLine("\"coordinates\": ");
      AppendMultiPolygonText(multiPolygon, writer);
      writer.WriteLine("}");

    }
Beispiel #15
0
		/// <summary>
		/// Writes a multipolygon.
		/// </summary>
		/// <param name="mp">The mulitpolygon to be written.</param>
		/// <param name="bWriter">Stream to write to.</param>
		/// <param name="byteorder">Byte order</param>
		private static void WriteMultiPolygon(MultiPolygon mp, BinaryWriter bWriter, WkbByteOrder byteorder)
		{
			//Write the number of polygons.
			WriteUInt32((uint)mp.Polygons.Count, bWriter, byteorder);

			//Loop on the number of polygons.
			foreach (Polygon poly in mp.Polygons)
			{
				//Write polygon header
				bWriter.Write((byte)byteorder);
				WriteUInt32((uint)WKBGeometryType.wkbPolygon, bWriter, byteorder);
				//Write each polygon.
				WritePolygon(poly, bWriter, byteorder);
			}
		}
            public void EndFigure()
            {
                GeometryType type=_CurrentType.Peek();
                switch (type)
                {
                case GeometryType.GeometryCollection:
                    {
                        var g=new SmGeometries.GeometryCollection();
                        g.Collection=_Figures;
                        _Figures=new SmGeometries.Geometry[] { g };
                    }
                    break;
                case GeometryType.LineString:
                    _Figures.Add(new SmGeometries.LineString(_CurrentPoints));
                    break;
                case GeometryType.MultiLineString:
                    {
                        var g=new SmGeometries.MultiLineString();
                        g.LineStrings=_Figures.Cast<SmGeometries.LineString>().ToList<SmGeometries.LineString>();
                        _Figures=new SmGeometries.Geometry[] { g };
                    }
                    break;
                case GeometryType.MultiPoint:
                    {
                        var g=new SmGeometries.MultiPoint();
                        g.Points=_Figures.Cast<SmGeometries.Point>().ToList<SmGeometries.Point>();
                        _Figures=new SmGeometries.Geometry[] { g };
                    }
                    break;
                case GeometryType.MultiPolygon:
                    {
                        var g=new SmGeometries.MultiPolygon();
                        g.Polygons=_Figures.Cast<SmGeometries.Polygon>().ToList<SmGeometries.Polygon>();
                        _Figures=new SmGeometries.Geometry[] { g };
                    }
                    break;
                case GeometryType.Point:
                    if (_CurrentPoints.Count>0)
                        _Figures.Add(_CurrentPoints[0]);
                    else
                        _Figures.Add(new SmGeometries.Point());
                    break;
                case GeometryType.Polygon:
                    _Figures.Add(new SmGeometries.LinearRing(_CurrentPoints));
                    break;
                default:
                    throw new NotSupportedException(
                        string.Format(
                            CultureInfo.CurrentCulture,
                            SR.UnsupportedGeometryTypeException,
                            type
                        )
                    );
                }

                _CurrentPoints.Clear();
            }
Beispiel #17
0
        public static FeatureDataRow LocatePolygon(SharpMap.Geometries.Point punto, SharpMap.Data.FeatureDataTable fdt)
        {
            FeatureDataRow fdr = null;

            if ((fdt as DataTable).Rows.Count == 1)
            {
                fdr = (FeatureDataRow)(fdt as DataTable).Rows[0];
            }
            else
            {
                GisSharpBlog.NetTopologySuite.Geometries.GeometryFactory f = new GeometryFactory(new PrecisionModel());
                foreach (DataRow r in (fdt as DataTable).Rows)
                {
                    if ((r as  FeatureDataRow).Geometry.GetType() == typeof(SharpMap.Geometries.MultiPolygon))
                    {
                        // Doble cast: de Geometria a MultiPolygon, y de DataRow a FeatureDataRow.
                        SharpMap.Geometries.MultiPolygon SharpMultiPol = (SharpMap.Geometries.MultiPolygon)(r as  FeatureDataRow).Geometry;

                        foreach (SharpMap.Geometries.Polygon SharpPol in SharpMultiPol.Polygons)
                        {
                            //Contorno
                            int countVExt = SharpPol.ExteriorRing.Vertices.Count;
                            int i         = 0;
                            GisSharpBlog.NetTopologySuite.Geometries.Coordinate[] ListaCoordsExt = new GisSharpBlog.NetTopologySuite.Geometries.Coordinate[countVExt];
                            foreach (SharpMap.Geometries.Point p in SharpPol.ExteriorRing.Vertices)
                            {
                                ListaCoordsExt[i++] = new GisSharpBlog.NetTopologySuite.Geometries.Coordinate(p.X, p.Y);
                            }

                            //Huecos
                            int countPolInt = SharpPol.InteriorRings.Count;
                            int j           = 0;
                            GisSharpBlog.NetTopologySuite.Geometries.LinearRing[] ListaPolInt = new GisSharpBlog.NetTopologySuite.Geometries.LinearRing[countPolInt];
                            foreach (SharpMap.Geometries.LinearRing ring in SharpPol.InteriorRings)
                            {
                                int countVInt = ring.Vertices.Count;
                                int k         = 0;
                                GisSharpBlog.NetTopologySuite.Geometries.Coordinate[] ListaCoordsInt = new GisSharpBlog.NetTopologySuite.Geometries.Coordinate[countVInt];
                                foreach (SharpMap.Geometries.Point p in ring.Vertices)
                                {
                                    ListaCoordsInt[k++] = new GisSharpBlog.NetTopologySuite.Geometries.Coordinate(p.X, p.Y);
                                }
                                ListaPolInt[j++] = f.CreateLinearRing(ListaCoordsInt);
                            }

                            GisSharpBlog.NetTopologySuite.Geometries.Polygon NTSPol = f.CreatePolygon(f.CreateLinearRing(ListaCoordsExt), ListaPolInt);

                            if (NTSPol.Contains(new GisSharpBlog.NetTopologySuite.Geometries.Point(punto.X, punto.Y)) == true)
                            {
                                fdr = (FeatureDataRow)r;
                                break;
                            }
                        }
                        if (fdr != null)
                        {
                            break;
                        }
                    }
                }
            }
            return(fdr);
        }
Beispiel #18
0
        private static MultiPolygon CreateWKBMultiPolygon(BinaryReader reader, WkbByteOrder byteOrder)
        {
            // Get the number of Polygons.
            int numPolygons = (int) ReadUInt32(reader, byteOrder);

            // Create a new array for the Polygons.
            MultiPolygon polygons = new MultiPolygon();

            // Loop on the number of polygons.
            for (int i = 0; i < numPolygons; i++)
            {
                // read polygon header
                reader.ReadByte();
                ReadUInt32(reader, byteOrder);

                // TODO: Validate type

                // Create the next polygon and add it to the array.
                polygons.Polygons.Add(CreateWKBPolygon(reader, byteOrder));
            }

            //Create and return the MultiPolygon.
            return polygons;
        }
        private static Geometry ToSharpMapMultiPolygon(EsriShapeBuffer shapeBuffer)
        {
            if (shapeBuffer == null)
                return null;

            var multiPartShapeBuffer = shapeBuffer as EsriMultiPartShapeBuffer;
            if (multiPartShapeBuffer == null)
            {
                BoundingBox box;
                return FromShapeFilePolygon(shapeBuffer, out box);
            }

            var hasZ = EsriShapeBuffer.HasZs(shapeBuffer.shapeType);
            var res = new MultiPolygon();
            Polygon poly = null;
            var offset = 0;
            for (var i = 0; i < multiPartShapeBuffer.NumParts; i++)
            {
                var vertices = new List<Point>(multiPartShapeBuffer.Parts[i]);
                for (var j = 0; j < multiPartShapeBuffer.Parts[i]; j++)
                {
                    var index = offset + j;
                    var point = multiPartShapeBuffer.Points[index];
                    vertices.Add(hasZ 
                        ? new Point3D(point.x, point.y, multiPartShapeBuffer.Zs[index])
                        : new Point(point.x, point.y));
                }

                var ring = new LinearRing(vertices);
                if (poly == null || !ring.IsCCW())
                {
                    poly = new Polygon(ring);
                    res.Polygons.Add(poly);
                } 
                else
                {
                    poly.InteriorRings.Add(ring);
                }

                offset += multiPartShapeBuffer.Parts[i];
            }

            if (res.NumGeometries == 1)
                return res.Polygons[0];
            
            return res;
        }
Beispiel #20
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>
 private static void AppendMultiPolygonText(MultiPolygon multiPolygon, StringWriter writer)
 {
     if (multiPolygon == null || multiPolygon.IsEmpty())
         writer.Write("EMPTY");
     else
     {
         writer.Write("(");
         for (int i = 0; i < multiPolygon.Polygons.Count; i++)
         {
             if (i > 0)
                 writer.Write(", ");
             AppendPolygonText(multiPolygon[i], writer);
         }
         writer.Write(")");
     }
 }
        private static Geometry FromShapeFilePolygon(EsriShapeBuffer shapeBuffer, out BoundingBox box)
        {
            box = null;
            if (shapeBuffer == null)
                return null;

            var hasZ = EsriShapeBuffer.HasZs(shapeBuffer.shapeType);
            var hasM = EsriShapeBuffer.HasMs(shapeBuffer.shapeType);
            using (var reader = new BinaryReader(new MemoryStream(shapeBuffer.shapeBuffer)))
            {
                var type = reader.ReadInt32();
                if (!(type == 5 || type == 15 || type == 25))
                    throw new InvalidOperationException();

                box = new BoundingBox(reader.ReadDouble(), reader.ReadDouble(), reader.ReadDouble(), reader.ReadDouble());

                var numParts = reader.ReadInt32();
                var numPoints = reader.ReadInt32();
                var allVertices = new List<Point>(numPoints);

                var parts = new int[numParts+1];
                for (var i = 0; i < numParts; i++)
                    parts[i] = reader.ReadInt32();
                parts[numParts] = numPoints;

                var res = new MultiPolygon();
                Polygon poly = null;
                for (var i = 0; i < numParts; i++)
                {
                    var count = parts[i + 1] - parts[i];
                    var vertices = new List<Point>(count);
                    for (var j = 0; j < count; j++)
                    {
                        var vertex = hasZ
                                         ? new Point3D(reader.ReadDouble(), reader.ReadDouble(), double.NaN)
                                         : new Point(reader.ReadDouble(), reader.ReadDouble());
                        vertices.Add(vertex);
                        allVertices.Add(vertex);
                    }

                    var ring = new LinearRing(vertices);
                    if (poly == null || !ring.IsCCW())
                    {
                        poly = new Polygon(ring);
                        res.Polygons.Add(poly);
                    }
                    else
                    {
                        poly.InteriorRings.Add(ring);
                    }
                }

                if (hasZ)
                {
                    var minZ = reader.ReadDouble();
                    var maxZ = reader.ReadDouble();
                    for (var i = 0; i < numPoints; i++)
                        ((Point3D) allVertices[i]).Z = reader.ReadDouble();
                }

                if (res.NumGeometries == 1)
                    return res.Polygons[0];

                return res;

            }
        }