Exemplo n.º 1
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.");
        }
Exemplo n.º 2
0
        // Private methods.
        /// <summary>
        /// Sets the current map.
        /// </summary>
        /// <param name="map">The map.</param>
        private void OnMapChanged(Map map)
        {
            // If the map has not changed, do nothing.
            if (this.map == map) return;

            // Lock the drawing synchronization object.
            lock (this.syncDrawing)
            {
                // Set the current map.
                this.map = map;

                // Get an exclusive reader lock to the regions list.

                this.regions.Lock();
                try
                {
                    // Dispose the existing map regions.
                    foreach (MapRegion region in this.regions)
                    {
                        region.Dispose();
                    }
                }
                finally
                {
                    this.regions.Unlock();
                }

                // Clear the regions list.
                this.regions.Clear();
                // If the current map is not null.
                if (null != this.map)
                {
                    // Create the map shapes.
                    foreach (MapShape shape in map.Shapes)
                    {
                        // Switch according to the shape type.
                        switch (shape.Type)
                        {
                            case MapShapeType.Polygon:
                                // Get the polygon shape.
                                MapShapePolygon shapePolygon = shape as MapShapePolygon;
                                // Create a map region for this shape.
                                MapRegion region = new MapRegion(shapePolygon);
                                // Update the region.
                                region.Update(this.mapBounds, this.mapScale);
                                // Add the map region to the region items.
                                this.regions.Add(region);
                                break;
                            default: continue;
                        }
                    }
                }
            }

            // Refresh the current map.
            this.OnRefreshMap();
        }