private void btnUnion_Click(object sender, EventArgs e)
        {
            InMemoryFeatureLayer inMemoryLayer = (InMemoryFeatureLayer)winformsMap1.FindFeatureLayer("InMemoryFeatureLayer");

            if (inMemoryLayer.InternalFeatures.Count > 1)
            {
                GeoCollection <AreaBaseShape> areaBaseShapes = new GeoCollection <AreaBaseShape>();

                GeoCollection <Feature> features = inMemoryLayer.InternalFeatures;
                foreach (Feature feature in features)
                {
                    AreaBaseShape targetAreaBaseShape = feature.GetShape() as AreaBaseShape;
                    if (targetAreaBaseShape != null)
                    {
                        areaBaseShapes.Add(targetAreaBaseShape);
                    }
                }

                AreaBaseShape unionedAreaBaseShape = AreaBaseShape.Union(areaBaseShapes);
                unionedAreaBaseShape.Id = "UnionedFeature";
                inMemoryLayer.InternalFeatures.Clear();
                inMemoryLayer.InternalFeatures.Add("UnionedFeature", new Feature(unionedAreaBaseShape));
                inMemoryLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle.FillSolidBrush.Color = GeoColor.FromArgb(100, GeoColor.SimpleColors.Green);

                winformsMap1.Refresh(winformsMap1.Overlays["InMemoryOverlay"]);
            }
        }
示例#2
0
        /// <summary>
        /// Unions all the features in the dividedCityLimits layer and displays the results on the map
        /// </summary>
        private void UnionShapes_OnClick(object sender, EventArgs e)
        {
            LayerOverlay layerOverlay = (LayerOverlay)mapView.Overlays["layerOverlay"];

            ShapeFileFeatureLayer dividedCityLimits = (ShapeFileFeatureLayer)layerOverlay.Layers["dividedCityLimits"];
            InMemoryFeatureLayer  unionLayer        = (InMemoryFeatureLayer)layerOverlay.Layers["unionLayer"];

            // Query the dividedCityLimits layer to get all the features
            dividedCityLimits.Open();
            var features = dividedCityLimits.QueryTools.GetAllFeatures(ReturningColumnsType.NoColumns);

            dividedCityLimits.Close();

            // Union all the features into a single Multipolygon Shape
            var union = AreaBaseShape.Union(features);

            // Add the union shape into unionLayer to display the result.
            // If this were to be a permanent change to the dividedCityLimits FeatureSource, you would modify the underlying data using BeginTransaction and CommitTransaction instead.
            unionLayer.InternalFeatures.Clear();
            unionLayer.InternalFeatures.Add(new Feature(union));

            // Hide the dividedCityLimits layer
            dividedCityLimits.IsVisible = false;

            // Redraw the layerOverlay to see the unioned features on the map
            layerOverlay.Refresh();
        }
示例#3
0
        public static Feature UnionAreaFeatures(IEnumerable <Feature> features)
        {
            Collection <Feature> tempFeatuers = new Collection <Feature>();

            foreach (var item in features)
            {
                if (!SqlTypesGeometryHelper.IsValid(item))
                {
                    tempFeatuers.Add(SqlTypesGeometryHelper.MakeValid(item));
                }
                else
                {
                    tempFeatuers.Add(item);
                }
            }

            var unionresult = AreaBaseShape.Union(tempFeatuers);

            return(new Feature(unionresult));
        }
示例#4
0
        private IEnumerable <Feature> InverseClip(FeatureLayer featureLayer, IEnumerable <Feature> clippingFeatures)
        {
            lock (featureLayer)
            {
                if (!featureLayer.IsOpen)
                {
                    featureLayer.Open();
                }
                Collection <Feature> results         = featureLayer.FeatureSource.GetFeaturesOutsideBoundingBox(ExtentHelper.GetBoundingBoxOfItems(clippingFeatures), featureLayer.GetDistinctColumnNames());
                Collection <Feature> sourceFeatures  = new Collection <Feature>();
                SimpleShapeType      simpleShapeType = GisEditor.LayerManager.GetFeatureSimpleShapeType(featureLayer);
                int index = 1;
                if (simpleShapeType == SimpleShapeType.Point)
                {
                    featureLayer.Open();
                    Collection <Feature> allFeatures = featureLayer.FeatureSource.GetAllFeatures(featureLayer.GetDistinctColumnNames());
                    featureLayer.Close();
                    foreach (Feature f in results)
                    {
                        allFeatures.Remove(f);
                    }
                    foreach (var f in InverseClipPoints(allFeatures, clippingFeatures, simpleShapeType))
                    {
                        results.Add(f);
                    }
                }
                else if (simpleShapeType == SimpleShapeType.Line)
                {
                    bool isOpen = false;
                    Proj4ProjectionInfo projectionInfo = featureLayer.GetProj4ProjectionInfo();
                    //MultipolygonShape areaBaseShape = AreaBaseShape.Union(GetValidFeatures(clippingFeatures));
                    List <AreaBaseShape> clippingAreaShapes = GetValidFeatures(clippingFeatures)
                                                              .Select(f => f.GetShape())
                                                              .OfType <AreaBaseShape>()
                                                              .ToList();
                    MultipolygonShape areaBaseShape = AreaBaseShape.Union(clippingAreaShapes);

                    if (projectionInfo != null && projectionInfo.CanProject)
                    {
                        if (featureLayer.IsOpen)
                        {
                            featureLayer.Close();
                            projectionInfo.Close();
                            isOpen = true;
                        }
                        featureLayer.FeatureSource.Projection = null;
                    }
                    featureLayer.Open();
                    featureLayer.FeatureSource.GetFeaturesInsideBoundingBox(areaBaseShape.GetBoundingBox(), featureLayer.GetDistinctColumnNames()).ForEach(f => { if (!areaBaseShape.Contains(f))
                                                                                                                                                                  {
                                                                                                                                                                      sourceFeatures.Add(f);
                                                                                                                                                                  }
                                                                                                                                                           });
                    int count = sourceFeatures.Count;
                    if (projectionInfo != null)
                    {
                        featureLayer.FeatureSource.Projection = projectionInfo.Projection;
                        if (isOpen)
                        {
                            featureLayer.Open();
                        }
                    }
                    if (featureLayer.IsOpen)
                    {
                        featureLayer.Close();
                    }
                    foreach (var feature in sourceFeatures)
                    {
                        isCanceled = ReportProgress(index, count);
                        if (isCanceled)
                        {
                            break;
                        }

                        index++;
                        try
                        {
                            //if (areaBaseShape.IsDisjointed(feature))
                            if (SqlTypesGeometryHelper.IsDisjointed(areaBaseShape, feature))
                            {
                                results.Add(feature);
                            }
                            else
                            {
                                MultilineShape multiLine   = (MultilineShape)feature.GetShape();
                                MultilineShape resultShape = new MultilineShape();
                                foreach (LineShape lineShape in multiLine.Lines)
                                {
                                    //if (areaBaseShape.IsDisjointed(lineShape))
                                    if (SqlTypesGeometryHelper.IsDisjointed(areaBaseShape, lineShape))
                                    {
                                        resultShape.Lines.Add(lineShape);
                                    }
                                    else
                                    {
                                        var resultLine = lineShape.GetDifference(areaBaseShape);
                                        foreach (var line in resultLine.Lines)
                                        {
                                            resultShape.Lines.Add(line);
                                        }
                                    }
                                }
                                if (resultShape != null && resultShape.Lines.Count > 0)
                                {
                                    results.Add(new Feature(resultShape.GetWellKnownBinary(), feature.Id, feature.ColumnValues));
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            GisEditor.LoggerManager.Log(LoggerLevel.Debug, ex.Message, new ExceptionInfo(ex));
                            HandleExceptionFromInvalidFeature(feature.Id, ex.Message);
                        }
                    }
                }
                else if (simpleShapeType == SimpleShapeType.Area)
                {
                    //MultipolygonShape areaBaseShape = AreaBaseShape.Union(GetValidFeatures(clippingFeatures));
                    List <AreaBaseShape> clippingAreaShapes = GetValidFeatures(clippingFeatures)
                                                              .Select(f => f.GetShape())
                                                              .OfType <AreaBaseShape>()
                                                              .ToList();

                    BaseShape         unionResultShape = SqlTypesGeometryHelper.Union(clippingAreaShapes);
                    MultipolygonShape areaBaseShape    = ConvertSqlQueryResultToMultiPolygonShape(unionResultShape);

                    bool isOpen = false;
                    Proj4ProjectionInfo projectionInfo = featureLayer.GetProj4ProjectionInfo();
                    if (projectionInfo != null && projectionInfo.CanProject)
                    {
                        if (featureLayer.IsOpen)
                        {
                            featureLayer.Close();
                            if (projectionInfo != null)
                            {
                                projectionInfo.Close();
                            }
                            isOpen = true;
                        }
                        featureLayer.FeatureSource.Projection = null;
                    }
                    if (!featureLayer.IsOpen)
                    {
                        featureLayer.Open();
                    }
                    featureLayer.FeatureSource.GetFeaturesInsideBoundingBox(areaBaseShape.GetBoundingBox(), featureLayer.GetDistinctColumnNames()).ForEach(f => sourceFeatures.Add(f));
                    if (featureLayer.IsOpen)
                    {
                        featureLayer.Close();
                    }
                    if (projectionInfo != null)
                    {
                        featureLayer.FeatureSource.Projection = projectionInfo.Projection;
                        if (isOpen)
                        {
                            featureLayer.Open();
                        }
                    }

                    int count = sourceFeatures.Count;
                    foreach (var feature in sourceFeatures)
                    {
                        isCanceled = ReportProgress(index, count);
                        if (isCanceled)
                        {
                            break;
                        }

                        index++;
                        try
                        {
                            //if (areaBaseShape.IsDisjointed(feature))
                            if (SqlTypesGeometryHelper.IsDisjointed(areaBaseShape, feature))
                            {
                                results.Add(feature);
                            }
                            else
                            {
                                //var clippedShape = ((AreaBaseShape)feature.GetShape()).GetDifference(areaBaseShape);
                                BaseShape         differenceResultShape = SqlTypesGeometryHelper.GetDifference((AreaBaseShape)feature.GetShape(), areaBaseShape);
                                MultipolygonShape clippedShape          = ConvertSqlQueryResultToMultiPolygonShape(differenceResultShape);
                                if (clippedShape != null && clippedShape.Polygons.Count > 0)
                                {
                                    results.Add(new Feature(clippedShape.GetWellKnownBinary(), feature.Id, feature.ColumnValues));
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            GisEditor.LoggerManager.Log(LoggerLevel.Debug, ex.Message, new ExceptionInfo(ex));
                            HandleExceptionFromInvalidFeature(feature.Id, ex.Message);
                        }
                    }
                }
                else
                {
                    throw new NotSupportedException("The ShapeFileType is not supported.");
                }
                return(results);
            }
        }
        private IEnumerable <Feature> StandardClip(FeatureSource featureSource, IEnumerable <Feature> features)
        {
            lock (featureSource)
            {
                Collection <Feature> results = new Collection <Feature>();

                //There is a bug about projection boundingbox, here is a workaround for it.
                bool       isOpen        = false;
                Projection tmpProjection = null;
                if (featureSource.Projection != null &&
                    featureSource.Projection is GISEditorManagedProj4Projection &&
                    ((GISEditorManagedProj4Projection)featureSource.Projection).IsProjectionParametersEqual)
                {
                    tmpProjection = featureSource.Projection;
                    if (featureSource.IsOpen)
                    {
                        featureSource.Close();
                        featureSource.Projection.Close();
                        isOpen = true;
                    }
                    featureSource.Projection = null;
                }

                if (!featureSource.IsOpen)
                {
                    featureSource.Open();
                }
                Collection <Feature> sourceFeatures = featureSource.GetFeaturesInsideBoundingBox(ExtentHelper.GetBoundingBoxOfItems(features), ReturningColumnsType.AllColumns);

                if (tmpProjection != null)
                {
                    featureSource.Projection = tmpProjection;
                    if (isOpen)
                    {
                        featureSource.Open();
                    }
                }

                ShapeFileType shapeFileType = ((ShapeFileFeatureSource)featureSource).GetShapeFileType();
                if (featureSource.IsOpen)
                {
                    featureSource.Close();
                }

                int index = 1;
                int count = sourceFeatures.Count;
                if (shapeFileType == ShapeFileType.Point || shapeFileType == ShapeFileType.Multipoint)
                {
                    return(StandardClipPoints(sourceFeatures, features, shapeFileType));
                }
                else if (shapeFileType == ShapeFileType.Polyline)
                {
                    MultipolygonShape areaBaseShape = AreaBaseShape.Union(GetValidFeatures(features));
                    foreach (var feature in sourceFeatures)
                    {
                        ReportProgress(index * 100 / count);
                        index++;
                        try
                        {
                            if (areaBaseShape.Contains(feature))
                            {
                                results.Add(feature);
                            }
                            else
                            {
                                var clippedShape = ((LineBaseShape)feature.GetShape()).GetIntersection(areaBaseShape);
                                if (clippedShape != null && clippedShape.Lines.Count > 0)
                                {
                                    results.Add(new Feature(clippedShape.GetWellKnownBinary(), feature.Id, feature.ColumnValues));
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            HandleExceptionFromInvalidFeature(feature.Id, ex.Message);
                        }
                    }
                }
                else if (shapeFileType == ShapeFileType.Polygon)
                {
                    MultipolygonShape areaBaseShape = AreaBaseShape.Union(GetValidFeatures(features));
                    foreach (var feature in sourceFeatures)
                    {
                        ReportProgress(index * 100 / count);
                        try
                        {
                            index++;
                            if (areaBaseShape.Contains(feature))
                            {
                                results.Add(feature);
                            }
                            else
                            {
                                var clippedShape = areaBaseShape.GetIntersection(feature);
                                if (clippedShape != null && clippedShape.Polygons.Count > 0)
                                {
                                    results.Add(new Feature(clippedShape.GetWellKnownBinary(), feature.Id, feature.ColumnValues));
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            HandleExceptionFromInvalidFeature(feature.Id, ex.Message);
                        }
                    }
                }
                else
                {
                    throw new NotSupportedException("The ShapeFileType is not supported.");
                }
                return(results);
            }
        }
        private IEnumerable <Feature> InverseClip(FeatureSource featureSource, IEnumerable <Feature> clippingFeatures)
        {
            lock (featureSource)
            {
                if (!featureSource.IsOpen)
                {
                    featureSource.Open();
                }
                Collection <Feature> results        = featureSource.GetFeaturesOutsideBoundingBox(ExtentHelper.GetBoundingBoxOfItems(clippingFeatures), ReturningColumnsType.AllColumns);
                Collection <Feature> sourceFeatures = new Collection <Feature>();
                ShapeFileType        shapeFileType  = ((ShapeFileFeatureSource)featureSource).GetShapeFileType();
                int index = 1;
                if (shapeFileType == ShapeFileType.Point || shapeFileType == ShapeFileType.Multipoint)
                {
                    featureSource.Open();
                    Collection <Feature> allFeatures = featureSource.GetAllFeatures(ReturningColumnsType.AllColumns);
                    featureSource.Close();
                    foreach (Feature f in results)
                    {
                        allFeatures.Remove(f);
                    }
                    foreach (var f in InverseClipPoints(allFeatures, clippingFeatures, shapeFileType))
                    {
                        results.Add(f);
                    }
                }
                else if (shapeFileType == ShapeFileType.Polyline)
                {
                    bool              isOpen        = false;
                    Projection        tmpProjection = null;
                    MultipolygonShape areaBaseShape = AreaBaseShape.Union(GetValidFeatures(clippingFeatures));
                    if (featureSource.Projection != null &&
                        featureSource.Projection is GISEditorManagedProj4Projection &&
                        ((GISEditorManagedProj4Projection)featureSource.Projection).IsProjectionParametersEqual)
                    {
                        tmpProjection = featureSource.Projection;
                        if (featureSource.IsOpen)
                        {
                            featureSource.Close();
                            featureSource.Projection.Close();
                            isOpen = true;
                        }
                        featureSource.Projection = null;
                    }
                    featureSource.Open();
                    featureSource.GetFeaturesInsideBoundingBox(areaBaseShape.GetBoundingBox(), ReturningColumnsType.AllColumns).ForEach(f => { if (!areaBaseShape.Contains(f))
                                                                                                                                               {
                                                                                                                                                   sourceFeatures.Add(f);
                                                                                                                                               }
                                                                                                                                        });
                    int count = sourceFeatures.Count;
                    if (tmpProjection != null)
                    {
                        featureSource.Projection = tmpProjection;
                        if (isOpen)
                        {
                            featureSource.Open();
                        }
                    }
                    if (featureSource.IsOpen)
                    {
                        featureSource.Close();
                    }
                    foreach (var feature in sourceFeatures)
                    {
                        ReportProgress(index * 100 / count);
                        index++;
                        try
                        {
                            if (areaBaseShape.IsDisjointed(feature))
                            {
                                results.Add(feature);
                            }
                            else
                            {
                                MultilineShape multiLine   = (MultilineShape)feature.GetShape();
                                MultilineShape resultShape = new MultilineShape();
                                foreach (LineShape lineShape in multiLine.Lines)
                                {
                                    if (areaBaseShape.IsDisjointed(lineShape))
                                    {
                                        resultShape.Lines.Add(lineShape);
                                    }
                                    else
                                    {
                                        Collection <PointShape> points = new Collection <PointShape>();
                                        points.Add(new PointShape(lineShape.Vertices[0]));
                                        lineShape.GetIntersection(areaBaseShape).Lines.ForEach(l =>
                                        {
                                            PointShape p1 = new PointShape(l.Vertices[0]);
                                            if (points.Count(p => p.X == p1.X && p.Y == p1.Y && p.Z == p1.Z) <= 0)
                                            {
                                                points.Add(p1);
                                            }
                                            PointShape p2 = new PointShape(l.Vertices[l.Vertices.Count - 1]);
                                            if (points.Count(p => p.X == p2.X && p.Y == p2.Y && p.Z == p2.Z) <= 0)
                                            {
                                                points.Add(p2);
                                            }
                                        });
                                        PointShape endPoint = new PointShape(lineShape.Vertices[lineShape.Vertices.Count - 1]);
                                        if (points.Count(p => p.X == endPoint.X && p.Y == endPoint.Y && p.Z == endPoint.Z) <= 0)
                                        {
                                            points.Add(endPoint);
                                        }

                                        for (int i = 0; i < points.Count; i++)
                                        {
                                            if (i != points.Count - 1)
                                            {
                                                LineBaseShape lineBaseShape = lineShape.GetLineOnALine(points[i], points[i + 1]);

                                                if (!areaBaseShape.Intersects(lineBaseShape.GetCenterPoint()))
                                                {
                                                    resultShape.Lines.Add((LineShape)lineBaseShape);
                                                }
                                            }
                                        }
                                    }
                                }
                                if (resultShape != null && resultShape.Lines.Count > 0)
                                {
                                    results.Add(new Feature(resultShape.GetWellKnownBinary(), feature.Id, feature.ColumnValues));
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            HandleExceptionFromInvalidFeature(feature.Id, ex.Message);
                        }
                    }
                }
                else if (shapeFileType == ShapeFileType.Polygon)
                {
                    MultipolygonShape areaBaseShape = AreaBaseShape.Union(GetValidFeatures(clippingFeatures));

                    bool       isOpen        = false;
                    Projection tmpProjection = null;
                    if (featureSource.Projection != null &&
                        featureSource.Projection is GISEditorManagedProj4Projection &&
                        ((GISEditorManagedProj4Projection)featureSource.Projection).IsProjectionParametersEqual)
                    {
                        tmpProjection = featureSource.Projection;
                        if (featureSource.IsOpen)
                        {
                            featureSource.Close();
                            featureSource.Projection.Close();
                            isOpen = true;
                        }
                        featureSource.Projection = null;
                    }
                    if (!featureSource.IsOpen)
                    {
                        featureSource.Open();
                    }
                    featureSource.GetFeaturesInsideBoundingBox(areaBaseShape.GetBoundingBox(), ReturningColumnsType.AllColumns).ForEach(f => { if (!areaBaseShape.IsDisjointed(f))
                                                                                                                                               {
                                                                                                                                                   sourceFeatures.Add(f);
                                                                                                                                               }
                                                                                                                                        });
                    if (featureSource.IsOpen)
                    {
                        featureSource.Close();
                    }
                    if (tmpProjection != null)
                    {
                        featureSource.Projection = tmpProjection;
                        if (isOpen)
                        {
                            featureSource.Open();
                        }
                    }

                    int count = sourceFeatures.Count;
                    foreach (var feature in sourceFeatures)
                    {
                        ReportProgress(index * 100 / count);
                        index++;
                        try
                        {
                            if (areaBaseShape.IsDisjointed(feature))
                            {
                                results.Add(feature);
                            }
                            else
                            {
                                var clippedShape = ((AreaBaseShape)feature.GetShape()).GetDifference(areaBaseShape);
                                if (clippedShape != null && clippedShape.Polygons.Count > 0)
                                {
                                    results.Add(new Feature(clippedShape.GetWellKnownBinary(), feature.Id, feature.ColumnValues));
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            HandleExceptionFromInvalidFeature(feature.Id, ex.Message);
                        }
                    }
                }
                else
                {
                    throw new NotSupportedException("The ShapeFileType is not supported.");
                }
                return(results);
            }
        }
        // Tuple
        // item1: columnName,
        // item2: columnType,
        // item3: OperatorMode.
        public static Collection <Feature> Dissolve(IEnumerable <string> dissolveColumnNames
                                                    , IEnumerable <Tuple <string, string, DissolveOperatorMode> > pairedColumnStrategies
                                                    , IEnumerable <Feature> featuresToDissovle)
        {
            var featureGroupByShapeType = featuresToDissovle.GroupBy(f =>
            {
                return(f.GetShape().GetType());
            });

            Collection <Feature> dissolveFeatures = new Collection <Feature>();
            Action <IGrouping <Type, Feature>, Action <IEnumerable <Feature>, Dictionary <string, string> > > dissolveAction
                = new Action <IGrouping <Type, Feature>, Action <IEnumerable <Feature>, Dictionary <string, string> > >
                      ((groupByType, finalProcessAction) =>
            {
                var groupByColumns = GroupByColumns(groupByType, dissolveColumnNames);
                groupByColumns.ForEach(groupFeature =>
                {
                    Dictionary <string, string> newColumnValues = new Dictionary <string, string>();
                    foreach (var tmpMatchColumn in dissolveColumnNames)
                    {
                        newColumnValues.Add(tmpMatchColumn, groupFeature.First().ColumnValues[tmpMatchColumn]);
                    }

                    foreach (var operatorPair in pairedColumnStrategies)
                    {
                        CollectNewColumnValues(groupFeature, newColumnValues, operatorPair);
                    }

                    newColumnValues.Add("Count", groupFeature.Count().ToString());

                    try
                    {
                        if (finalProcessAction != null)
                        {
                            finalProcessAction(groupFeature, newColumnValues);
                        }
                    }
                    catch (Exception ex)
                    {
                        GisEditor.LoggerManager.Log(LoggerLevel.Debug, ex.Message, new ExceptionInfo(ex));
                    }
                });
            });

            featureGroupByShapeType.ForEach(groupByType =>
            {
                if (groupByType.Key.IsSubclassOf(typeof(AreaBaseShape)))
                {
                    dissolveAction(groupByType, (tmpFeatures, tmpColumnValues) =>
                    {
                        MultipolygonShape dissolveShape = AreaBaseShape.Union(GetValidFeatures(tmpFeatures));
                        if (dissolveShape != null)
                        {
                            Feature dissolveFeature = new Feature(dissolveShape, tmpColumnValues);
                            dissolveFeatures.Add(dissolveFeature);
                        }
                    });
                }
                else if (groupByType.Key.IsSubclassOf(typeof(LineBaseShape)))
                {
                    dissolveAction(groupByType, (tmpFeatures, tmpColumnValues) =>
                    {
                        //MultilineShape dissolveShape = LineBaseShape.Union(tmpFeatures);
                        //Feature dissolveFeature = new Feature(dissolveShape, tmpColumnValues);
                        //dissolveFeatures.Add(dissolveFeature);
                        MultilineShape dissolveShape = new MultilineShape();
                        tmpFeatures.ForEach(tmpFeature =>
                        {
                            BaseShape tmpShape      = tmpFeature.GetShape();
                            LineShape tmpLine       = tmpShape as LineShape;
                            MultilineShape tmpMLine = tmpShape as MultilineShape;
                            if (tmpLine != null)
                            {
                                dissolveShape.Lines.Add(tmpLine);
                            }
                            else if (tmpMLine != null)
                            {
                                tmpMLine.Lines.ForEach(tmpLineInMLine => dissolveShape.Lines.Add(tmpLineInMLine));
                            }
                        });

                        if (dissolveShape.Lines.Count > 0)
                        {
                            dissolveFeatures.Add(new Feature(dissolveShape, tmpColumnValues));
                        }
                    });
                }
                else if (groupByType.Key.IsSubclassOf(typeof(PointBaseShape)))
                {
                    dissolveAction(groupByType, (tmpFeatures, tmpColumnValues) =>
                    {
                        MultipointShape multipointShape = new MultipointShape();
                        tmpFeatures.ForEach(tmpFeature =>
                        {
                            BaseShape tmpShape        = tmpFeature.GetShape();
                            PointShape tmpPoint       = tmpShape as PointShape;
                            MultipointShape tmpMPoint = tmpShape as MultipointShape;
                            if (tmpPoint != null)
                            {
                                multipointShape.Points.Add(tmpPoint);
                            }
                            else if (tmpMPoint != null)
                            {
                                tmpMPoint.Points.ForEach(tmpPointInMPointShape => multipointShape.Points.Add(tmpPointInMPointShape));
                            }
                        });
                        dissolveFeatures.Add(new Feature(multipointShape, tmpColumnValues));
                    });
                }
            });

            return(dissolveFeatures);
        }
示例#8
0
        private void InternalFeatures_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.Action == NotifyCollectionChangedAction.Reset)
            {
                SetAreaText(0);
                SetSelectedText(0);
                FeatureInfoWindow.Instance.Refresh(new Dictionary <FeatureLayer, Collection <Feature> >());
            }
            else
            {
                if (setAreaTextTimer == null)
                {
                    setAreaTextTimer          = new DispatcherTimer();
                    setAreaTextTimer.Interval = TimeSpan.FromMilliseconds(500);
                    setAreaTextTimer.Tick    += (s, args) =>
                    {
                        Task.Factory.StartNew(() =>
                        {
                            double area = 0;
                            int count   = 0;
                            Proj4Projection managedProj4Projection = GetProjection();

                            try
                            {
                                Collection <AreaBaseShape> areaShapes = new Collection <AreaBaseShape>();
                                count = GisEditor.ActiveMap.SelectionOverlay.HighlightFeatureLayer.InternalFeatures.Count;
                                foreach (var feature in GisEditor.ActiveMap.SelectionOverlay.HighlightFeatureLayer.InternalFeatures)
                                {
                                    AreaBaseShape areaBaseShape = feature.GetShape() as AreaBaseShape;
                                    if (areaBaseShape != null)
                                    {
                                        AreaBaseShape projectedAreaShape = managedProj4Projection.ConvertToExternalProjection(areaBaseShape) as AreaBaseShape;
                                        if (projectedAreaShape != null)
                                        {
                                            areaShapes.Add(projectedAreaShape);
                                        }
                                    }
                                }
                                if (areaShapes.Count > 0)
                                {
                                    var unionShape = AreaBaseShape.Union(areaShapes);
                                    area           = unionShape.GetArea(GeographyUnit.DecimalDegree, MeasureTrackInteractiveOverlay.AreaUnit);
                                }
                            }
                            catch (Exception ex)
                            {
                                GisEditor.LoggerManager.Log(LoggerLevel.Debug, ex.Message, new ExceptionInfo(ex));
                            }

                            Application.Current.Dispatcher.BeginInvoke(() =>
                            {
                                SetAreaText(Math.Round(area, 2));
                                SetSelectedText(count);
                            }, DispatcherPriority.Background);
                        });

                        setAreaTextTimer.Stop();
                    };
                }

                if (setAreaTextTimer.IsEnabled)
                {
                    setAreaTextTimer.Stop();
                }

                setAreaTextTimer.Start();
            }
        }
        private void Dissolve()
        {
            try
            {
                Collection <Feature> dissolvedFeatures = new Collection <Feature>();
                // get features to dissolve.
                Collection <Feature> featuresToDissolve = GetFeaturesToDissolve();
                // group features.
                var featureGroupByShapeType = featuresToDissolve.GroupBy(f =>
                {
                    return(f.GetShape().GetType());
                });

                Action <IGrouping <Type, Feature>, Action <IEnumerable <Feature>, Dictionary <string, string> > > dissolveAction
                    = new Action <IGrouping <Type, Feature>, Action <IEnumerable <Feature>, Dictionary <string, string> > >
                          ((groupByType, finalProcessAction) =>
                {
                    //CancelTask(this.CancellationSource.Token, content, parameter);
                    var groupByColumns = GroupByColumns(groupByType, this.matchColumns);
                    groupByColumns.ForEach(groupFeature =>
                    {
                        //CancelTask(this.CancellationSource.Token, content, parameter);
                        Dictionary <string, string> newColumnValues = new Dictionary <string, string>();
                        foreach (var tmpMatchColumn in this.matchColumns)
                        {
                            newColumnValues.Add(tmpMatchColumn, groupFeature.First().ColumnValues[tmpMatchColumn]);
                        }

                        foreach (var operatorPair in this.operatorPairs)
                        {
                            //CancelTask(this.CancellationSource.Token, content, parameter);
                            CollectNewColumnValues(groupFeature, newColumnValues, operatorPair);
                        }

                        newColumnValues.Add("Count", groupFeature.Count().ToString());

                        try
                        {
                            if (finalProcessAction != null)
                            {
                                finalProcessAction(groupFeature, newColumnValues);
                            }
                        }
                        catch { }
                    });
                });

                featureGroupByShapeType.ForEach(groupByType =>
                {
                    //CancelTask(this.CancellationSource.Token, content, parameter);
                    if (groupByType.Key.IsSubclassOf(typeof(AreaBaseShape)))
                    {
                        dissolveAction(groupByType, (tmpFeatures, tmpColumnValues) =>
                        {
                            MultipolygonShape dissolveShape = AreaBaseShape.Union(GetValidFeatures(tmpFeatures));
                            if (dissolveShape != null)
                            {
                                Feature dissolveFeature = new Feature(dissolveShape, tmpColumnValues);
                                dissolvedFeatures.Add(dissolveFeature);
                            }
                        });
                    }
                    else if (groupByType.Key.IsSubclassOf(typeof(LineBaseShape)))
                    {
                        dissolveAction(groupByType, (tmpFeatures, tmpColumnValues) =>
                        {
                            //MultilineShape dissolveShape = LineBaseShape.Union(tmpFeatures);
                            //Feature dissolveFeature = new Feature(dissolveShape, tmpColumnValues);
                            //dissolveFeatures.Add(dissolveFeature);
                            MultilineShape dissolveShape = new MultilineShape();
                            tmpFeatures.ForEach(tmpFeature =>
                            {
                                BaseShape tmpShape      = tmpFeature.GetShape();
                                LineShape tmpLine       = tmpShape as LineShape;
                                MultilineShape tmpMLine = tmpShape as MultilineShape;
                                if (tmpLine != null)
                                {
                                    dissolveShape.Lines.Add(tmpLine);
                                }
                                else if (tmpMLine != null)
                                {
                                    tmpMLine.Lines.ForEach(tmpLineInMLine => dissolveShape.Lines.Add(tmpLineInMLine));
                                }
                            });

                            if (dissolveShape.Lines.Count > 0)
                            {
                                dissolvedFeatures.Add(new Feature(dissolveShape, tmpColumnValues));
                            }
                        });
                    }
                    else if (groupByType.Key.IsSubclassOf(typeof(PointBaseShape)))
                    {
                        dissolveAction(groupByType, (tmpFeatures, tmpColumnValues) =>
                        {
                            MultipointShape multipointShape = new MultipointShape();
                            tmpFeatures.ForEach(tmpFeature =>
                            {
                                BaseShape tmpShape        = tmpFeature.GetShape();
                                PointShape tmpPoint       = tmpShape as PointShape;
                                MultipointShape tmpMPoint = tmpShape as MultipointShape;
                                if (tmpPoint != null)
                                {
                                    multipointShape.Points.Add(tmpPoint);
                                }
                                else if (tmpMPoint != null)
                                {
                                    tmpMPoint.Points.ForEach(tmpPointInMPointShape => multipointShape.Points.Add(tmpPointInMPointShape));
                                }
                            });
                            dissolvedFeatures.Add(new Feature(multipointShape, tmpColumnValues));
                        });
                    }
                });

                // collect export columns.
                var filteredFeatureColumns = new Collection <FeatureSourceColumn>();
                foreach (var matchColumn in this.matchColumns)
                {
                    var tmpColumn = featureColumns.FirstOrDefault(f => f.ColumnName.Equals(matchColumn, StringComparison.Ordinal));
                    if (tmpColumn != null)
                    {
                        filteredFeatureColumns.Add(tmpColumn);
                    }
                }

                foreach (var extraColumn in this.operatorPairs)
                {
                    var tmpColumn = featureColumns.FirstOrDefault(f => f.ColumnName.Equals(extraColumn.ColumnName, StringComparison.Ordinal));
                    if (tmpColumn != null)
                    {
                        filteredFeatureColumns.Add(tmpColumn);
                    }
                }

                filteredFeatureColumns.Add(new FeatureSourceColumn("Count", "Integer", 8));

                string saveFolder = Path.GetDirectoryName(outputPath);
                if (!Directory.Exists(saveFolder))
                {
                    Directory.CreateDirectory(saveFolder);
                }

                ShpFileExporter exporter = new ShpFileExporter();
                exporter.ExportToFile(new FileExportInfo(dissolvedFeatures, filteredFeatureColumns, outputPath, Wkt));
            }
            catch (Exception e)
            {
            }
            finally
            {
                var args = new UpdatingProgressLongRunningTaskPluginEventArgs(LongRunningTaskState.Updating);
                args.Message = "Finished";

                OnUpdatingProgress(args);
            }
        }