Exemple #1
0
        private IEnumerable <Feature> ShatterTwoFeatures(ref Feature feature1, Feature feature2)
        {
            Collection <Feature> resultFeatures = new Collection <Feature>();
            var columns = GetColumnValues(feature1, feature2);

            // var diff1 = feature1.GetDifference(feature2);
            feature1 = SqlTypesGeometryHelper.MakeValid(feature1);
            feature2 = SqlTypesGeometryHelper.MakeValid(feature2);
            var diff1 = SqlTypesGeometryHelper.GetDifference(feature1, feature2);
            //var diff2 = feature2.GetDifference(feature1);
            var diff2 = SqlTypesGeometryHelper.GetDifference(feature2, feature1);
            //var inter = feature1.GetIntersection(feature2);
            var inter = SqlTypesGeometryHelper.GetIntersection(feature1, feature2);

            feature1 = diff1;
            if (inter != null)
            {
                inter = new Feature(inter.GetWellKnownBinary(), inter.Id, columns);
                AddValidFeature(resultFeatures, inter);
            }

            if (diff2 != null)
            {
                AddValidFeature(resultFeatures, diff2);
            }

            return(resultFeatures);
        }
Exemple #2
0
        public IEnumerable <Feature> IntersectFeatures(IEnumerable <Feature> features)
        {
            List <Feature> featuresList = features.ToList();

            var results       = new ConcurrentStack <Feature>();
            var featureGroups = featuresList.GroupBy(feature => feature.Tag).ToList();
            int index         = 1;

            for (int i = 0; i < featureGroups.Count; i++)
            {
                var group = featureGroups[i];
                foreach (var feature in group)
                {
                    int progress = index * 100 / featuresList.Count;
                    isCanceled = ReportProgress(progress, index, featuresList.Count);
                    if (isCanceled)
                    {
                        break;
                    }

                    var otherFeatures = featureGroups.Where(g => featureGroups.IndexOf(g) > i).SelectMany(f => f);
                    Parallel.ForEach(otherFeatures, otherFeature =>
                    {
                        try
                        {
                            AreaBaseShape originalShape = (AreaBaseShape)feature.GetShape();
                            AreaBaseShape matchShape    = (AreaBaseShape)otherFeature.GetShape();

                            //if (originalShape.Intersects(matchShape))
                            if (SqlTypesGeometryHelper.Intersects(originalShape, matchShape))
                            {
                                //AreaBaseShape resultShape = originalShape.GetIntersection(matchShape);
                                AreaBaseShape resultShape = (AreaBaseShape)SqlTypesGeometryHelper.GetIntersection(originalShape, matchShape);
                                if (resultShape != null)
                                {
                                    var columnValues      = GetColumnValues(feature, otherFeature);
                                    Feature resultFeature = new Feature(resultShape, columnValues);
                                    results.Push(resultFeature);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            GisEditor.LoggerManager.Log(LoggerLevel.Debug, ex.Message, new ExceptionInfo(ex));
                            HandleExceptionFromInvalidFeature(feature.Id, ex.Message);
                        }
                    });

                    index++;
                }
            }

            return(results);
        }
Exemple #3
0
        private int StandardClipLines(IEnumerable <Feature> features, Collection <Feature> results, Collection <Feature> sourceFeatures, int index, int count)
        {
            //MultipolygonShape areaBaseShape = AreaBaseShape.Union(GetValidFeatures(features));
            List <AreaBaseShape> clippingAreaShapes = GetValidFeatures(features).Select(f => f.GetShape()).OfType <AreaBaseShape>().ToList();
            MultipolygonShape    areaBaseShape      = (MultipolygonShape)SqlTypesGeometryHelper.Union(clippingAreaShapes);

            ConcurrentQueue <Feature> concurrentResult = new ConcurrentQueue <Feature>();

            Parallel.ForEach(sourceFeatures, feature =>
            {
                try
                {
                    //if (areaBaseShape.Contains(feature))
                    if (SqlTypesGeometryHelper.Contains(areaBaseShape, feature))
                    {
                        concurrentResult.Enqueue(feature);
                    }
                    else
                    {
                        //var clippedShape = ((LineBaseShape)feature.GetShape()).GetIntersection(areaBaseShape);
                        var clippedShape = SqlTypesGeometryHelper.GetIntersection(feature.GetShape(), areaBaseShape) as MultilineShape;
                        if (clippedShape != null && clippedShape.Lines.Count > 0)
                        {
                            concurrentResult.Enqueue(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);
                }
                finally
                {
                    isCanceled = ReportProgress(index++, count);
                }
            });

            foreach (var feature in concurrentResult)
            {
                results.Add(feature);
            }
            return(index);
        }
Exemple #4
0
        private IEnumerable <Feature> StandardClip(FeatureLayer featureLayer, IEnumerable <Feature> features)
        {
            lock (featureLayer)
            {
                Collection <Feature> results = new Collection <Feature>();

                #region replace project to null

                bool isOpen = false;
                Proj4ProjectionInfo projectionInfo = featureLayer.GetProj4ProjectionInfo();
                if (projectionInfo != null && projectionInfo.CanProject)
                {
                    if (featureLayer.IsOpen)
                    {
                        featureLayer.Close();
                        projectionInfo.Close();
                        isOpen = true;
                    }
                    featureLayer.FeatureSource.Projection = null;
                }

                #endregion replace project to null

                Collection <Feature> tmpFeatures = new Collection <Feature>();
                if (projectionInfo != null && projectionInfo.CanProject)
                {
                    projectionInfo.Open();
                    foreach (var item in features)
                    {
                        tmpFeatures.Add(projectionInfo.ConvertToInternalProjection(item));
                    }
                    projectionInfo.Close();
                }
                else
                {
                    tmpFeatures = new Collection <Feature>(features.ToList());
                }

                if (!featureLayer.IsOpen)
                {
                    featureLayer.Open();
                }
                List <Feature> tmpSourceFeatures = featureLayer.FeatureSource.GetFeaturesInsideBoundingBox(ExtentHelper.GetBoundingBoxOfItems(tmpFeatures), featureLayer.GetDistinctColumnNames()).Select(f => f.MakeValidIfCan()).ToList();

                Collection <Feature> sourceFeatures = new Collection <Feature>(tmpSourceFeatures);
                if (projectionInfo != null)
                {
                    featureLayer.FeatureSource.Projection = projectionInfo.Projection;
                    if (isOpen)
                    {
                        featureLayer.Open();
                    }
                }

                SimpleShapeType simpleShapeType = SimpleShapeType.Unknown;
                var             shapeAdapter    = GisEditor.LayerManager.GetLayerPlugins(featureLayer.GetType()).FirstOrDefault() as FeatureLayerPlugin;
                if (shapeAdapter != null)
                {
                    simpleShapeType = shapeAdapter.GetFeatureSimpleShapeType(featureLayer);
                }
                if (featureLayer.IsOpen)
                {
                    featureLayer.Close();
                }

                int index = 1;
                int count = sourceFeatures.Count;
                if (simpleShapeType == SimpleShapeType.Point)
                {
                    return(StandardClipPoints(sourceFeatures, tmpFeatures));
                }
                else if (simpleShapeType == SimpleShapeType.Line)
                {
                    StandardClipLines(tmpFeatures, results, sourceFeatures, index, count);
                }
                else if (simpleShapeType == SimpleShapeType.Area)
                {
                    //MultipolygonShape areaBaseShape = AreaBaseShape.Union(GetValidFeatures(tmpFeatures));
                    List <AreaBaseShape> clippingAreaShapes = GetValidFeatures(tmpFeatures)
                                                              .Select(f => f.GetShape())
                                                              .OfType <AreaBaseShape>()
                                                              .ToList();

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

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

                        try
                        {
                            index++;
                            //if (areaBaseShape.Contains(feature))
                            if (SqlTypesGeometryHelper.Contains(areaBaseShape, feature))
                            {
                                results.Add(feature);
                            }
                            else
                            {
                                //var clippedShape = areaBaseShape.GetIntersection(feature);
                                AreaBaseShape targetAreaShape = feature.GetShape() as AreaBaseShape;
                                if (targetAreaShape != null)
                                {
                                    var clippedShape = SqlTypesGeometryHelper.GetIntersection(areaBaseShape, targetAreaShape) as MultipolygonShape;
                                    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.");
                }

                Collection <Feature> convertedFeatures = new Collection <Feature>();
                if (projectionInfo != null && projectionInfo.CanProject)
                {
                    projectionInfo.Open();
                    foreach (var item in results)
                    {
                        convertedFeatures.Add(projectionInfo.ConvertToExternalProjection(item));
                    }
                    projectionInfo.Close();
                }
                else
                {
                    convertedFeatures = new Collection <Feature>(results.ToList());
                }

                return(convertedFeatures);
            }
        }