示例#1
0
        /// <summary>
        /// Creates a new map region from the specified map shape, given the geographic map bounds and map scale.
        /// </summary>
        /// <param name="shape">The map shape.</param>
        public MapRegion(MapShapePolygon shape)
        {
            // Validate the parameters.
            if (null == shape) throw new ArgumentNullException("shape");

            // Save the map shape.
            this.shape = shape;
            // Create the map points.
            this.points = new PointF[shape.Parts.Count][];
            for (int index = 0; index < shape.Parts.Count; index++)
            {
                this.points[index] = new PointF[shape.Parts[index].Points.Count];
            }
            // Create the graphics path.
            this.path = new GraphicsPath();
            // Get the region metadata.
            this.name = shape.Metadata["admin"];
        }
示例#2
0
        private void OnOpenFile(string fileName)
        {
            // Show message.
            this.textBox.AppendText(string.Format("Opening file \'{0}\'...{1}", fileName, Environment.NewLine));

            try
            {
                // Open a stream to the ZIP file.
                using (FileStream fileInStream = new FileStream(fileName, FileMode.Open))
                {
                    // Open the ZIP archive.
                    using (ZipArchive zipArchive = new ZipArchive(fileInStream, ZipArchiveMode.Read))
                    {
                        // The shape file name.
                        string shapeFileName = null;

                        this.textBox.AppendText(string.Format("Extracting shape ZIP archive...{0}", Environment.NewLine));
                        foreach (ZipArchiveEntry entry in zipArchive.Entries)
                        {
                            // If this is the shape file, save the name.
                            if (Path.GetExtension(entry.Name) == ".shp")
                            {
                                shapeFileName = entry.Name;
                            }
                            this.textBox.AppendText(string.Format("- {0}: {1} bytes {2} bytes compressed{3}", entry.Name, entry.Length, entry.CompressedLength, Environment.NewLine));
                        }

                        // If there are no entries, throw an exception.
                        if (null == shapeFileName) throw new FileNotFoundException("The ZIP archive does not contain a shape file.");

                        // Create the name of a temporary folder.
                        string tempFolder = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());

                        this.textBox.AppendText(string.Format("Shape file name is: \'{0}\'{1}", shapeFileName, Environment.NewLine));

                        // Create the temporary folder.
                        System.IO.Directory.CreateDirectory(tempFolder);

                        this.textBox.AppendText(string.Format("Creating temporary folder \'{0}\'...{1}", tempFolder, Environment.NewLine));

                        try
                        {
                            // Extract the shapefile contents.
                            zipArchive.ExtractToDirectory(tempFolder);

                            // Open the shapefile.
                            using (Shapefile shapefile = new Shapefile(Path.Combine(tempFolder, shapeFileName)))
                            {
                                this.textBox.AppendText(Environment.NewLine);

                                // Write the basic information.
                                this.textBox.AppendText(string.Format("Type: {0}, Shapes: {1:n0}{2}", shapefile.Type, shapefile.Count, Environment.NewLine));

                                this.textBox.AppendText(Environment.NewLine);

                                // Create a map object.
                                Map map = new Map(new MapRectangle(
                                    shapefile.BoundingBox.Left,
                                    shapefile.BoundingBox.Top,
                                    shapefile.BoundingBox.Right,
                                    shapefile.BoundingBox.Bottom));

                                // Write the bounding box of this shape file.
                                this.textBox.AppendText(string.Format("Bounds: {0},{1} -> {2},{3}{4}",
                                    shapefile.BoundingBox.Left,
                                    shapefile.BoundingBox.Top,
                                    shapefile.BoundingBox.Right,
                                    shapefile.BoundingBox.Bottom,
                                    Environment.NewLine));

                                // Enumerate all shapes.
                                foreach (Shape shape in shapefile)
                                {
                                    // Shape basic information.
                                    //this.textBox.AppendText(string.Format("{0} {1} {2} ", shape.RecordNumber, shape.Type, shape.GetMetadata("name")));

                                    // Create a new shape.
                                    MapShape mapShape;
                                    switch (shape.Type)
                                    {
                                        case ShapeType.Point:
                                            ShapePoint shapePoint = shape as ShapePoint;
                                            mapShape = new MapShapePoint(new MapPoint(shapePoint.Point.X, shapePoint.Point.Y));
                                            break;
                                        case ShapeType.Polygon:
                                            ShapePolygon shapePolygon = shape as ShapePolygon;

                                            //this.textBox.AppendText(string.Format(": {0}", shapePolygon.Parts.Count));

                                            MapShapePolygon mapShapePolygon = new MapShapePolygon(new MapRectangle(
                                                shapePolygon.BoundingBox.Left,
                                                shapePolygon.BoundingBox.Top,
                                                shapePolygon.BoundingBox.Right,
                                                shapePolygon.BoundingBox.Bottom));
                                            foreach(PointD[] part in shapePolygon.Parts)
                                            {
                                                MapPart mapPart = new MapPart();
                                                foreach (PointD point in part)
                                                {
                                                    mapPart.Points.Add(point.X, point.Y);
                                                }
                                                mapShapePolygon.Parts.Add(mapPart);
                                            }
                                            mapShape = mapShapePolygon;
                                            break;
                                        default:
                                            throw new NotSupportedException(string.Format("Shape type {0} is not supported.", shape.Type));
                                    }
                                    // Add the shape metadata.
                                    foreach (string name in shape.GetMetadataNames())
                                    {
                                        mapShape.Metadata[name] = shape.GetMetadata(name);
                                    }
                                    // Add the shape to the map.
                                    map.Shapes.Add(mapShape);
                                    //this.textBox.AppendText(Environment.NewLine);
                                }

                                this.textBox.AppendText(Environment.NewLine);

                                // Create a memory stream.
                                using (MemoryStream memoryStream = new MemoryStream())
                                {
                                    // Serialize the map data.
                                    map.Write(memoryStream);
                                    // Display the XML.
                                    this.textBox.AppendText(Encoding.UTF8.GetString(memoryStream.ReadToEnd()));

                                    this.textBox.AppendText(Environment.NewLine);
                                    this.textBox.AppendText(Environment.NewLine);

                                    // Set the stream position to zero.
                                    memoryStream.Position = 0;
                                    // Display a dialog to save the file.
                                    if (this.saveFileDialog.ShowDialog(this) == DialogResult.OK)
                                    {
                                        // Create a file stream.
                                        using (FileStream fileOutStream = System.IO.File.Create(this.saveFileDialog.FileName))
                                        {
                                            // Compress the stream.
                                            //using (GZipStream zipStream = new GZipStream(fileOutStream, CompressionLevel.Optimal))
                                            //{
                                                this.textBox.AppendText("Uncompressed data is {0} bytes.{1}".FormatWith(memoryStream.Length, Environment.NewLine));
                                                memoryStream.CopyTo(fileOutStream);
                                                this.textBox.AppendText("Compressed data is {0} bytes.{1}".FormatWith(fileOutStream.Length, Environment.NewLine));
                                            //}
                                        }
                                    }
                                }
                                //this.textBox.AppendText(map.ToXml().ToString());
                                this.textBox.AppendText(Environment.NewLine);
                            }
                        }
                        finally
                        {
                            // Delete the temporary folder.
                            this.textBox.AppendText(Environment.NewLine);
                            System.IO.Directory.Delete(tempFolder, true);
                            this.textBox.AppendText(string.Format("Temporary folder \'{0}\' deleted.{1}", tempFolder, Environment.NewLine));
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                this.textBox.AppendText(string.Format("An exception occurred. {0}", exception.Message));
            }
            this.textBox.AppendText(Environment.NewLine);
            this.textBox.AppendText("Done.");
        }