コード例 #1
0
        /// <summary>
        /// Buffer the shapefile
        /// </summary>
        /// <param name="shapefilename">
        /// The shapefilename.
        /// </param>
        /// <param name="theForm">
        /// The form.
        /// </param>
        /// <returns>
        /// True on success
        /// </returns>
        private static bool BufferShapefile(string shapefilename, Form1 theForm)
        {
            try
            {
                // Check inputs:
                if (!Helper.CheckShapefileLocation(shapefilename, theForm))
                {
                    return(false);
                }

                // Open the sf:
                var sf = Fileformats.OpenShapefile(shapefilename, theForm);
                if (sf == null)
                {
                    theForm.Error(string.Empty, "Opening input shapefile was unsuccessful");
                    return(false);
                }

                var globalSettings = new GlobalSettings();
                globalSettings.ResetGdalError();
                theForm.Progress(string.Empty, 100, "Start buffering " + Path.GetFileName(shapefilename));

                // Make the distance depending on the projection.
                var distance = 1000.0;
                if (sf.GeoProjection.IsGeographic)
                {
                    distance = 0.1;
                }

                var bufferedSf = sf.BufferByDistance(distance, 16, false, false);

                // Do some checks:
                if (!Helper.CheckShapefile(sf, bufferedSf, globalSettings.GdalLastErrorMsg, theForm))
                {
                    return(false);
                }

                Helper.ColorShapes(ref bufferedSf, 0, tkMapColor.LightBlue, tkMapColor.LightYellow, true);

                // Save result:
                var newFilename = shapefilename.Replace(".shp", "-buffered.shp");
                Helper.DeleteShapefile(newFilename);
                bufferedSf.SaveAs(newFilename, theForm);
                theForm.Progress(string.Empty, 100, "The resulting shapefile has been saved as " + newFilename);

                // Load the files:
                MyAxMap.Clear();
                Fileformats.OpenShapefileAsLayer(shapefilename, theForm, true);
                bufferedSf.DefaultDrawingOptions.FillVisible = false;
                if (MyAxMap.AddLayer(bufferedSf, true) == -1)
                {
                    theForm.Error(string.Empty, "Could not add the buffered shapefile to the map");
                    return(false);
                }

                // Wait to show the map:
                Application.DoEvents();
            }
            catch (Exception exception)
            {
                theForm.Error(string.Empty, "Exception: " + exception.Message);
            }

            return(true);
        }
コード例 #2
0
        /// <summary>
        /// Clip the shapefile
        /// </summary>
        /// <param name="shapefilename">
        /// The shapefile name
        /// </param>
        /// <param name="overlayFilename">
        /// The name of the overlay shapefile
        /// </param>
        /// <param name="theForm">
        /// The form
        /// </param>
        /// <returns>
        /// True on success
        /// </returns>
        private static bool ClipShapefile(string shapefilename, string overlayFilename, Form1 theForm)
        {
            try
            {
                // Check inputs:
                if (!Helper.CheckShapefileLocation(shapefilename, theForm))
                {
                    return(false);
                }

                if (!Helper.CheckShapefileLocation(overlayFilename, theForm))
                {
                    return(false);
                }

                // Open the sf:
                var sf = Fileformats.OpenShapefile(shapefilename, theForm);
                if (sf == null)
                {
                    theForm.Error(string.Empty, "Opening input shapefile was unsuccessful");
                    return(false);
                }

                var overlaySf = Fileformats.OpenShapefile(overlayFilename, theForm);
                if (overlaySf == null)
                {
                    theForm.Error(string.Empty, "Opening overlay shapefile was unsuccessful");
                    return(false);
                }

                theForm.Progress(string.Format(
                                     "Clipping {0} ({1}) with {2} ({3})",
                                     Path.GetFileName(shapefilename),
                                     sf.ShapefileType,
                                     Path.GetFileName(overlayFilename),
                                     overlaySf.ShapefileType));

                var globalSettings = new GlobalSettings();
                globalSettings.ResetGdalError();
                theForm.Progress(string.Empty, 0, "Start clipping " + Path.GetFileName(shapefilename));

                // selecting only part of overlay, then the check about the varying
                // number of shapes in input/output will work
                var max = Math.Max(overlaySf.NumShapes / 2, 1);
                for (var i = 0; i < max; i++)
                {
                    overlaySf.ShapeSelected[i] = true;
                }

                var clippedSf = sf.Clip(false, overlaySf, true);

                // Do some checks:
                if (!Helper.CheckShapefile(sf, clippedSf, globalSettings.GdalLastErrorMsg, theForm))
                {
                    return(false);
                }

                Helper.ColorShapes(ref clippedSf, 0, tkMapColor.DarkRed, tkMapColor.LightSeaGreen, true);

                // Count the resulting shapes it should be higher than the input shapefile.
                var numClippedSf = clippedSf.NumShapes;
                var numInputSf   = sf.NumShapes;

                // Reset map:
                MyAxMap.Clear();

                if (numClippedSf == numInputSf)
                {
                    // Nothing was clipped
                    theForm.Error(
                        string.Empty,
                        "The resulting shapefile has the same number of shapes as the input shapefile. Either the input files are incorrect or the clipping function doesn't behaves as expected.");
                    MyAxMap.AddLayer(sf, true);
                    MyAxMap.AddLayer(overlaySf, true);

                    // Wait to show the map:
                    Application.DoEvents();

                    return(false);
                }

                // Save result:
                if (overlayFilename != null)
                {
                    var newFilename = shapefilename.Replace(
                        ".shp",
                        string.Format("-{0}", Path.GetFileName(overlayFilename).Replace(".shp", "-clipped.shp")));
                    Helper.DeleteShapefile(newFilename);
                    clippedSf.SaveAs(newFilename, theForm);
                    theForm.Progress(string.Empty, 100, "The resulting shapefile has been saved as " + newFilename);

                    // Load the files:
                    Fileformats.OpenShapefileAsLayer(overlayFilename, theForm, false);
                    if (MyAxMap.AddLayer(clippedSf, true) == -1)
                    {
                        theForm.Error(string.Empty, "Could not add the clipped shapefile to the map");
                        return(false);
                    }

                    // Wait to show the map:
                    Application.DoEvents();
                }
                else
                {
                    return(false);
                }
            }
            catch (Exception exception)
            {
                theForm.Error(string.Empty, "Exception: " + exception.Message);
            }

            theForm.Progress(string.Empty, 100, "This clipping has finished.");
            return(true);
        }
コード例 #3
0
        /// <summary>
        /// The simplify shapefile.
        /// </summary>
        /// <param name="shapefilename">
        /// The shapefilename.
        /// </param>
        /// <param name="theForm">
        /// The form.
        /// </param>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        private static bool SimplifyShapefile(string shapefilename, Form1 theForm)
        {
            try
            {
                // Check inputs:
                if (!Helper.CheckShapefileLocation(shapefilename, theForm))
                {
                    return(false);
                }

                // Open the sf:
                var sf = Fileformats.OpenShapefile(shapefilename, theForm);
                if (sf == null)
                {
                    theForm.Error(string.Empty, "Opening input shapefile was unsuccessful");
                    return(false);
                }

                var globalSettings = new GlobalSettings();
                globalSettings.ResetGdalError();
                theForm.Progress(string.Empty, 0, "Start simplifying " + Path.GetFileName(shapefilename));

                // Make the tolerance depending on the projection.
                var tolerance = 10d;
                if (sf.GeoProjection.IsGeographic)
                {
                    tolerance = 0.1;
                }

                var simplifiedSf = sf.SimplifyLines(tolerance, false);

                // Do some checks:
                if (!Helper.CheckShapefile(sf, simplifiedSf, globalSettings.GdalLastErrorMsg, theForm))
                {
                    return(false);
                }

                // give the resulting lines a good width and color:
                var utils = new Utils {
                    GlobalCallback = theForm
                };
                simplifiedSf.DefaultDrawingOptions.LineWidth   = 2;
                simplifiedSf.DefaultDrawingOptions.LineColor   = utils.ColorByName(tkMapColor.OrangeRed);
                simplifiedSf.DefaultDrawingOptions.LineStipple = tkDashStyle.dsSolid;

                // Save result:
                var newFilename = shapefilename.Replace(".shp", "-simplified.shp");
                Helper.DeleteShapefile(newFilename);
                simplifiedSf.SaveAs(newFilename, theForm);
                theForm.Progress(string.Empty, 100, "The resulting shapefile has been saved as " + newFilename);

                // Load the files:
                MyAxMap.Clear();
                if (MyAxMap.AddLayer(simplifiedSf, true) == -1)
                {
                    theForm.Error(string.Empty, "Could not add the simplified shapefile to the map");
                    return(false);
                }

                Fileformats.OpenShapefileAsLayer(shapefilename, theForm, false);

                // Wait to show the map:
                Application.DoEvents();
            }
            catch (Exception exception)
            {
                theForm.Error(string.Empty, "Exception: " + exception.Message);
            }

            return(true);
        }