Ejemplo n.º 1
0
        /// <summary>
        /// Read a shapefile MultiPoint record.
        /// </summary>
        /// <param name="stream">Input stream.</param>
        /// <param name="record">Shapefile record to be updated.</param>
        private static void ReadMultipoint(Stream stream, ShapeFileRecord record)
        {
            // Bounding Box.
            record.XMin = ShapeFile.ReadDouble64_LE(stream);
            record.YMin = ShapeFile.ReadDouble64_LE(stream);
            record.XMax = ShapeFile.ReadDouble64_LE(stream);
            record.YMax = ShapeFile.ReadDouble64_LE(stream);

            // Num Points.
            int numPoints = ShapeFile.ReadInt32_LE(stream);

            // Points.
            for (int i = 0; i < numPoints; i++)
            {
                Point p = new Point();
                p.X = ShapeFile.ReadDouble64_LE(stream);
                p.Y = ShapeFile.ReadDouble64_LE(stream);
                record.Points.Add(p);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Read a shapefile Polygon record.
        /// </summary>
        /// <param name="stream">Input stream.</param>
        /// <param name="record">Shapefile record to be updated.</param>
        private static void ReadPolygon( Stream stream, ShapeFileRecord record )
        {
            // Bounding Box.
            record.XMin = ShapeFile.ReadDouble64_LE( stream );
            record.YMin = ShapeFile.ReadDouble64_LE( stream );
            record.XMax = ShapeFile.ReadDouble64_LE( stream );
            record.YMax = ShapeFile.ReadDouble64_LE( stream );

            // Num Parts and Points.
            int numParts = ShapeFile.ReadInt32_LE( stream );
            int numPoints = ShapeFile.ReadInt32_LE( stream );

            // Parts.
            for( int i = 0; i < numParts; i++ )
            {
                record.Parts.Add( ShapeFile.ReadInt32_LE( stream ) );
            }

            // Points.
            for( int i = 0; i < numPoints; i++ )
            {
                Point p = new Point();
                p.X = ShapeFile.ReadDouble64_LE( stream );
                p.Y = ShapeFile.ReadDouble64_LE( stream );
                record.Points.Add( p );
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Read a shapefile record.
        /// </summary>
        /// <param name="stream">Input stream.</param>
        public ShapeFileRecord ReadShapeFileRecord( Stream stream )
        {
            ShapeFileRecord record = new ShapeFileRecord();

            // Record Header.
            record.RecordNumber = ShapeFile.ReadInt32_BE( stream );
            record.ContentLength = ShapeFile.ReadInt32_BE( stream );

            // Shape Type.
            record.ShapeType = ShapeFile.ReadInt32_LE( stream );

            // Read the shape geometry, depending on its type.
            switch( record.ShapeType )
            {
                case ( int ) ShapeType.NullShape:
                    // Do nothing.
                    break;
                case ( int ) ShapeType.Point:
                    ShapeFile.ReadPoint( stream, record );
                    break;
                case ( int ) ShapeType.PolyLine:
                    // PolyLine has exact same structure as Polygon in shapefile.
                    ShapeFile.ReadPolygon( stream, record );
                    break;
                case ( int ) ShapeType.Polygon:
                    ShapeFile.ReadPolygon( stream, record );
                    break;
                case ( int ) ShapeType.Multipoint:
                    ShapeFile.ReadMultipoint( stream, record );
                    break;
                default:
                    {
                        string msg = String.Format( System.Globalization.CultureInfo.InvariantCulture, "ShapeType {0} is not supported.", ( int ) record.ShapeType );
                        throw new NotSupportedException( msg );
                    }
            }

            // Add the record to our internal list.
            this.records.Add( record );

            return record;
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Read a shapefile Point record.
        /// </summary>
        /// <param name="stream">Input stream.</param>
        /// <param name="record">Shapefile record to be updated.</param>
        private static void ReadPoint( Stream stream, ShapeFileRecord record )
        {
            // Points - add a single point.
            Point p = new System.Windows.Point();
            p.X = ShapeFile.ReadDouble64_LE( stream );
            p.Y = ShapeFile.ReadDouble64_LE( stream );
            record.Points.Add( p );

            // Bounding Box.
            record.XMin = p.X;
            record.YMin = p.Y;
            record.XMax = record.XMin;
            record.YMax = record.YMin;
        }
Ejemplo n.º 5
0
        private static ESRI.ArcGIS.Client.Geometry.Geometry GetPolygon(ShapeFileRecord record)
        {
            Random           rnd          = new Random();
            Polygon          polygon      = new Polygon();
            SpatialReference geoReference = new SpatialReference(4326);

            try
            {
                for (int i = 0; i < record.NumberOfParts; i++)
                {
                    // Determine the starting index and the end index
                    // into the points array that defines the figure.
                    int start = record.Parts[i];
                    int end;
                    if (record.NumberOfParts > 1 && i != (record.NumberOfParts - 1))
                    {
                        end = record.Parts[i + 1];
                    }
                    else
                    {
                        end = record.NumberOfPoints;
                    }

                    ESRI.ArcGIS.Client.Geometry.PointCollection points = new ESRI.ArcGIS.Client.Geometry.PointCollection();
                    // Add line segments to the polyline
                    bool isWebMercator = false;
                    if (record.Points.Count > 0)
                    {
                        if (record.Points[0].Y < -90 || record.Points[0].Y > 90)
                        {
                            isWebMercator = true;
                        }
                        else
                        {
                            polygon.SpatialReference = geoReference;
                        }
                    }
                    for (int j = start; j < end; j++)
                    {
                        if (record.NumberOfPoints < 5000 || rnd.Next(0, 5) == 1)
                        {
                            System.Windows.Point point = record.Points[j];
                            if (isWebMercator)
                            {
                                points.Add(new MapPoint(point.X, point.Y));
                            }
                            else
                            {
                                points.Add(new MapPoint(point.X, point.Y, geoReference));
                            }
                            if (leftMostPoint == 0)
                            {
                                leftMostPoint   = point.X;
                                rightMostPoint  = point.X;
                                topMostPoint    = point.Y;
                                bottomMostPoint = point.Y;
                            }
                            else
                            {
                                if (point.X < leftMostPoint)
                                {
                                    leftMostPoint = point.X;
                                }
                                if (point.X > rightMostPoint)
                                {
                                    rightMostPoint = point.X;
                                }
                                if (point.Y < topMostPoint)
                                {
                                    topMostPoint = point.Y;
                                }
                                if (point.Y > bottomMostPoint)
                                {
                                    bottomMostPoint = point.Y;
                                }
                            }
                        }
                    }

                    polygon.Rings.Add(points);
                }
            }
            catch (Exception ex)
            {
                //
            }
            return(polygon);
        }