Beispiel #1
0
        /// <summary>
        /// creates a shapefile for grid labels given filename
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="labelProperties"></param>
        public void LoadLabelShapefile(string fileName, Dictionary <string, uint> labelProperties, Shapefile sfLabelPath)
        {
            _labelPropertiesDictionary = labelProperties;

            _shapeFileGrid25Labels = new Shapefile();
            _shapeFileGrid25Labels.Open(fileName, null);
            _shapeFileGrid25Labels.GenerateLabels(_shapeFileGrid25Labels.FieldIndexByName["Label"], tkLabelPositioning.lpCentroid);
            SetupLabelProperties();
        }
        /// <summary>
        /// labels the shapefile using parameters in labelXML
        /// </summary>
        /// <param name="labelXML"></param>
        /// <returns></returns>
        public bool LabelShapefile(string labelXML)
        {
            var lbls = 0;

            //applies label properties to the current shapefile's labels
            _shapeFile.Labels.Deserialize(labelXML);

            //we convert string labelXML to an xml object
            var doc = new XmlDocument();

            doc.LoadXml(labelXML);

            //we use attributes such as SourceField and Positioning in generating labels for the shapefile
            doc.DocumentElement.With(d =>
            {
                if (d.Attributes["Expression"] != null)
                {
                    _mapLayer.Expression         = d.Attributes["Expression"].Value;
                    _shapeFile.Labels.Expression = _mapLayer.Expression;
                    lbls = _shapeFile.Labels.Count;
                    _mapLayer.LabelSource = "Expression";
                }
                else if (d.Attributes["SourceField"] != null)
                {
                    _mapLayer.LabelField = int.Parse(d.Attributes["SourceField"].Value);
                    lbls = _shapeFile.GenerateLabels(_mapLayer.LabelField, (tkLabelPositioning)int.Parse(d.Attributes["Positioning"].Value), true);
                    _mapLayer.LabelSource = "SourceField";
                }

                if (d.Attributes["VisibilityExpression"] != null)
                {
                    _mapLayer.LabelsVisibilityExpression = d.Attributes["VisibilityExpression"].Value;
                }

                //AvoidCollision is not included in labelXML so we put it here
                if (d.Attributes["AvoidCollision"]?.Value == "1")
                {
                    _shapeFile.Labels.AvoidCollisions = true;
                }
                else if (d.Attributes["AvoidCollision"]?.Value == "0")
                {
                    _shapeFile.Labels.AvoidCollisions = false;
                }
            });

            return(lbls >= 0);
        }
Beispiel #3
0
 public int GenerateEmptyLabels(LabelPosition method, bool largestPartOnly = false)
 {
     return(_shapefile.GenerateLabels(-1, (tkLabelPositioning)method, largestPartOnly));
 }
Beispiel #4
0
        /// <summary>
        /// Get the closest point
        /// </summary>
        /// <param name="inputShapefile">
        /// The input shapefile.
        /// </param>
        /// <param name="searchShapefile">
        /// The search shapefile.
        /// </param>
        /// <param name="theForm">
        /// The form.
        /// </param>
        /// <returns>
        /// True on success
        /// </returns>
        private static bool ClosestPoint(string inputShapefile, string searchShapefile, Form1 theForm)
        {
            try
            {
                // Check inputs:
                if (!Helper.CheckShapefileLocation(inputShapefile, theForm))
                {
                    return(false);
                }

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

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

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

                // Create resulting shapefile:
                var linksSf = new Shapefile {
                    GlobalCallback = theForm
                };
                linksSf.CreateNew(string.Empty, ShpfileType.SHP_POLYLINE);
                var fieldIndex = linksSf.EditAddField("FoundId", FieldType.INTEGER_FIELD, 0, 0);

                // Get a random shape
                var index            = new Random().Next(inputSf.NumShapes - 1);
                var randomInputShape = inputSf.Shape[index];

                // Select the shape:
                var utils = new Utils {
                    GlobalCallback = theForm
                };
                inputSf.SelectionColor       = utils.ColorByName(tkMapColor.Yellow);
                inputSf.ShapeSelected[index] = true;

                // Load the files:
                MyAxMap.RemoveAllLayers();
                MyAxMap.AddLayer(searchSf, true);
                MyAxMap.AddLayer(inputSf, true);

                // To store the lenght and index:
                var minLength    = double.MaxValue;
                var closestIndex = -1;

                // Search around the location until at least on shape is found.
                // To optimize searching make sure a spatial index is used:
                if (!searchSf.HasSpatialIndex)
                {
                    searchSf.CreateSpatialIndex(searchSf.Filename);
                }

                searchSf.UseSpatialIndex = true;

                // create start search box:
                var searchExtent = randomInputShape.Extents;

                // Make the start tolerance depending on the projection:
                var tolerance = 2D;
                if (searchSf.GeoProjection.IsGeographic)
                {
                    tolerance = 0.01;
                }

                var    foundShapeId = -1;
                object results      = null;

                // Search in increasing circles:
                var notFound = true;

                // TODO: Add continious loop prevention
                while (notFound)
                {
                    if (searchSf.SelectShapes(searchExtent, tolerance, SelectMode.INTERSECTION, ref results))
                    {
                        var shapeIds = results as int[];

                        // Use the first one:
                        if (shapeIds != null)
                        {
                            foreach (var shapeId in shapeIds)
                            {
                                foundShapeId = shapeId;

                                // When the shapes intersect you get wrong values:
                                var intersects = randomInputShape.Intersects(searchSf.Shape[foundShapeId]);

                                if (intersects)
                                {
                                    continue;
                                }

                                // stop searching:
                                notFound = false;
                                break;
                            }
                        }
                    }

                    // increase tolerance:
                    tolerance = tolerance + tolerance;
                }

                if (foundShapeId == -1)
                {
                    theForm.Error(string.Empty, "Error! Could not find any shapes");
                    return(false);
                }

                // Select the found shape:
                searchSf.SelectionColor = utils.ColorByName(tkMapColor.Red);
                searchSf.ShapeSelected[foundShapeId] = true;

                var link = randomInputShape.ClosestPoints(searchSf.Shape[foundShapeId]);
                if (link != null && link.numPoints > 1)
                {
                    if (link.IsValid)
                    {
                        var shapeId = linksSf.EditAddShape(link);
                        linksSf.EditCellValue(fieldIndex, shapeId, foundShapeId);
                        if (minLength > link.Length)
                        {
                            minLength    = link.Length;
                            closestIndex = foundShapeId;
                        }
                    }
                    else
                    {
                        theForm.Error(string.Empty, "Found link line is invalid: " + link.IsValidReason);
                        return(false);
                    }
                }
                else
                {
                    theForm.Error(string.Empty, "pointShp.ClosestPoints() could not find anything");
                    return(false);
                }

                linksSf.DefaultDrawingOptions.LineColor = utils.ColorByName(tkMapColor.Black);
                linksSf.DefaultDrawingOptions.LineWidth = 2;
                MyAxMap.AddLayer(linksSf, true);
                linksSf.GenerateLabels(fieldIndex, tkLabelPositioning.lpMiddleSegment);
                linksSf.Labels.OffsetX = 10;
                linksSf.Labels.OffsetY = 10;

                MyAxMap.ZoomToMaxExtents();

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

                theForm.Progress(
                    string.Format(
                        "The closest shape is {0}, has a value of {1} and a length of {2}",
                        closestIndex,
                        searchSf.CellValue[0, closestIndex],
                        minLength));

                // Save result:
                var newFilename = inputSf.Filename.Replace(".shp", "-Closest.shp");
                Helper.DeleteShapefile(newFilename);
                if (!linksSf.SaveAs(newFilename, theForm))
                {
                    return(false);
                }

                theForm.Progress(string.Empty, 100, "The resulting shapefile has been saved as " + newFilename);
            }
            catch (Exception exception)
            {
                theForm.Error(string.Empty, "Exception: " + exception.Message);
                return(false);
            }

            return(true);
        }