示例#1
0
        protected override void RunCore(Dictionary <string, string> parameters)
        {
            bool parametersValid = TryParseParameters(parameters);

            try
            {
                if (parametersValid)
                {
                    var sourceFeatures = GetSourceFeatures();
                    var resultFeatures = MergeFeatures(sourceFeatures, columns);

                    ShpFileExporter shpFileExporter = new ShpFileExporter();
                    FileExportInfo  fileInfor       = new FileExportInfo(resultFeatures, columns, outputPath, Wkt);
                    shpFileExporter.ExportToFile(fileInfor);
                }
            }
            catch (Exception ex)
            {
                var args = new UpdatingProgressLongRunningTaskPluginEventArgs(LongRunningTaskState.Error);
                args.ExceptionInfo = new LongRunningTaskExceptionInfo(ex.Source, ex.StackTrace);
                OnUpdatingProgress(args);
            }
            finally
            {
                var args = new UpdatingProgressLongRunningTaskPluginEventArgs(LongRunningTaskState.Updating);
                args.Message = "Finished";
                OnUpdatingProgress(args);
            }
        }
示例#2
0
        private Collection <Feature> MergeFeatures(Collection <Feature> sourceFeatures, IEnumerable <FeatureSourceColumn> featureColumns)
        {
            Collection <Feature> features = new Collection <Feature>();
            int id    = 0;
            int count = sourceFeatures.Count;
            var args  = new UpdatingProgressLongRunningTaskPluginEventArgs(LongRunningTaskState.Updating);

            foreach (Feature feature in sourceFeatures)
            {
                args.Current = (id + 1) * 100 / count;
                OnUpdatingProgress(args);

                foreach (FeatureSourceColumn featureColumn in featureColumns)
                {
                    if (!feature.ColumnValues.Keys.Contains(featureColumn.ColumnName))
                    {
                        feature.ColumnValues.Add(featureColumn.ColumnName, "0");
                    }
                }
                try
                {
                    if (!feature.GetShape().Validate(ShapeValidationMode.Simple).IsValid)
                    {
                        throw new Exception("This feature is invalid.");
                    }
                    features.Add(new Feature(feature.GetWellKnownBinary(), id.ToString(), feature.ColumnValues));
                }
                catch (Exception ex)
                {
                    HandleExceptionFromInvalidFeature(feature.Id, ex.Message);
                }
                id++;
            }
            return(features);
        }
        private void ReportProgress(int progress)
        {
            var args = new UpdatingProgressLongRunningTaskPluginEventArgs(LongRunningTaskState.Updating);

            args.Current = progress;

            OnUpdatingProgress(args);
        }
        private void HandleExceptionFromInvalidFeature(string id, string message)
        {
            var args = new UpdatingProgressLongRunningTaskPluginEventArgs(LongRunningTaskState.Error);

            args.ExceptionInfo = new LongRunningTaskExceptionInfo(message, null);
            args.Message       = id;

            OnUpdatingProgress(args);
        }
        protected override void RunCore(Dictionary <string, string> parameters)
        {
            ParseParameters(parameters);
            var clippingFeatures = GetClippingFeatures();

            masterLayerFeatureSource.Open();
            var columns = masterLayerFeatureSource.GetColumns();

            masterLayerFeatureSource.Close();

            var clippedFeatures = Clip(masterLayerFeatureSource, clippingFeatures, clipType);

            ExportToFile(outputPath, clippedFeatures, columns);

            var args = new UpdatingProgressLongRunningTaskPluginEventArgs(LongRunningTaskState.Updating);

            args.Message = "Finished";
            OnUpdatingProgress(args);
        }
        protected override void RunCore(Dictionary <string, string> parameters)
        {
            bool parametersValid = TryParseParameters(parameters);

            try
            {
                if (parametersValid)
                {
                    Split();
                }
            }
            catch (Exception ex)
            {
                var args = new UpdatingProgressLongRunningTaskPluginEventArgs(LongRunningTaskState.Error);
                args.ExceptionInfo = new LongRunningTaskExceptionInfo(ex.Source, ex.StackTrace);
                OnUpdatingProgress(args);
            }
            finally
            {
                var args = new UpdatingProgressLongRunningTaskPluginEventArgs(LongRunningTaskState.Updating);
                args.Message = "Finished";
                OnUpdatingProgress(args);
            }
        }
        private void OutPutResults(IEnumerable <Feature> resultFeatures)
        {
            string exportPath = string.Empty;

            //if (outputToMap)
            //{
            //    string dir = Path.GetDirectoryName(tempShapeFilePath);
            //    string fileName = Path.GetFileNameWithoutExtension(tempShapeFilePath);
            //    string outputToMapTempFilePath = Path.Combine(dir, fileName + "OutPutToMapResult.shp");

            //    exportPath = outputToMapTempFilePath;

            //    var args = new UpdatingProgressLongRunningTaskPluginEventArgs(LongRunningTaskState.Updating);
            //    args.Parameters.Add("OutputToMapTempFilePath", exportPath);
            //    OnUpdatingProgress(args);
            //}
            //else
            if (outputToFile && !string.IsNullOrEmpty(outputFilePath))
            {
                exportPath = outputFilePath;

                var args = new UpdatingProgressLongRunningTaskPluginEventArgs(LongRunningTaskState.Updating);
                args.Message = "Creating File";
                OnUpdatingProgress(args);
            }

            FileExportInfo  info     = new FileExportInfo(resultFeatures, columnsToInclude, exportPath, displayProjectionParameters);
            ShpFileExporter exporter = new ShpFileExporter();

            exporter.ExportToFile(info);

            OnUpdatingProgress(new UpdatingProgressLongRunningTaskPluginEventArgs(LongRunningTaskState.Updating)
            {
                Message = "Finished"
            });
        }
        private void Split()
        {
            try
            {
                var args = new UpdatingProgressLongRunningTaskPluginEventArgs(LongRunningTaskState.Updating);

                featureSource.Open();
                Collection <Feature> allFeatures = featureSource.GetAllFeatures(ReturningColumnsType.AllColumns);
                var columns = featureSource.GetColumns();
                featureSource.Close();

                var featuresGroups = allFeatures.GroupBy(tmpFeature
                                                         => tmpFeature.ColumnValues[splitColumnName]);

                int i     = 0;
                int count = exportConfigs.Count;
                exportConfigs.ForEach(config =>
                {
                    string folderName = outputPath;
                    string fileName   = config.Value;
                    if (String.IsNullOrEmpty(fileName))
                    {
                        fileName = String.Format(CultureInfo.InvariantCulture, "{0}_{1}.shp", layerName, config.Key);
                    }

                    if (!fileName.EndsWith(".shp", StringComparison.OrdinalIgnoreCase))
                    {
                        fileName += ".shp";
                    }

                    string finalShapeFilePath = Path.Combine(folderName, fileName);

                    if (File.Exists(finalShapeFilePath) && overwriteOutputFiles)
                    {
                        //CloseExistingLayersAndCollectOverlaysToRefresh(this, finalShapeFilePath);
                        RemoveShapeFile(finalShapeFilePath);
                    }

                    //this.OutputShapeFileNames.Add(finalShapeFilePath);
                    var featureGroup = featuresGroups.FirstOrDefault(group => group.Key == config.Key);
                    if (featureGroup != null)
                    {
                        ShpFileExporter exporter = new ShpFileExporter();
                        exporter.ExportToFile(new FileExportInfo(featureGroup, columns, finalShapeFilePath, wkt));
                    }

                    args.Message = String.Format(CultureInfo.InvariantCulture, "Building... ({0}/{1})", ++i, count);
                    OnUpdatingProgress(args);
                });
            }
            catch (Exception ex)
            {
                //HandleExceptionFromInvalidFeature(feature.Id, ex.Message);
            }
            finally
            {
                //this.IsBusy = false;
                //this.BusyContent = String.Empty;
                //this.CurrentThread = null;
            }
        }
示例#9
0
        protected override void RunCore(Dictionary <string, string> parameters)
        {
            bool allParametersValid = ParseParameters(parameters);

            if (allParametersValid)
            {
                UpdatingProgressLongRunningTaskPluginEventArgs args = null;

                var features = GetFeaturesFromTempFile().ToArray();
                int index    = 1;
                int count    = features.Count();

                SimplificationType simType = SimplificationType.DouglasPeucker;
                if (preserveTopology)
                {
                    simType = SimplificationType.TopologyPreserving;
                }

                Collection <Feature> simplifiedFeatures = new Collection <Feature>();
                foreach (Feature feature in features)
                {
                    try
                    {
                        var       shape           = feature.GetShape();
                        var       areaShape       = shape as AreaBaseShape;
                        var       lineShape       = shape as LineBaseShape;
                        BaseShape simplifiedShape = null;

                        if (areaShape != null)
                        {
                            if (selectedDistanceUnit == "Decimal Degrees")
                            {
                                simplifiedShape = areaShape.Simplify(simplificationTolerance, simType);
                            }
                            else
                            {
                                simplifiedShape = areaShape.Simplify(mapUnit, simplificationTolerance, (DistanceUnit)converter.ConvertBack(selectedDistanceUnit, null, null, null), simType);
                            }
                        }
                        else if (lineShape != null)
                        {
                            if (selectedDistanceUnit == "Decimal Degrees")
                            {
                                simplifiedShape = lineShape.Simplify(simplificationTolerance, simType);
                            }
                            else
                            {
                                simplifiedShape = lineShape.Simplify(mapUnit, simplificationTolerance, (DistanceUnit)converter.ConvertBack(selectedDistanceUnit, null, null, null), simType);
                            }
                        }

                        if (simplifiedShape != null)
                        {
                            Feature newFeature = new Feature(simplifiedShape.GetWellKnownBinary(), feature.Id, feature.ColumnValues);
                            newFeature.Tag = feature.Tag;
                            simplifiedFeatures.Add(newFeature);
                        }
                    }
                    catch (Exception ex)
                    {
                        args               = new UpdatingProgressLongRunningTaskPluginEventArgs(LongRunningTaskState.Error);
                        args.Message       = feature.Id;
                        args.ExceptionInfo = new LongRunningTaskExceptionInfo(ex.Message, ex.StackTrace);

                        OnUpdatingProgress(args);
                    }

                    args         = new UpdatingProgressLongRunningTaskPluginEventArgs(LongRunningTaskState.Updating);
                    args.Current = index * 100 / count;

                    OnUpdatingProgress(args);

                    index++;
                }

                args         = new UpdatingProgressLongRunningTaskPluginEventArgs(LongRunningTaskState.Updating);
                args.Message = "Creating File";
                OnUpdatingProgress(args);

                Output(simplifiedFeatures);

                args         = new UpdatingProgressLongRunningTaskPluginEventArgs(LongRunningTaskState.Updating);
                args.Message = "Finished";
                OnUpdatingProgress(args);
            }
            else
            {
                //report error and return.
            }
        }
        protected override void RunCore(Dictionary <string, string> parameters)
        {
            bool parametersValid = parameterNames.All(name => parameters.ContainsKey(name));

            if (parametersValid)
            {
                double        distance     = 0;
                int           smoothness   = 0;
                BufferCapType capType      = BufferCapType.Butt;
                GeographyUnit mapUnit      = GeographyUnit.Unknown;
                DistanceUnit  distanceUnit = DistanceUnit.Feet;

                var featuresToBuffer = GetFeaturesToBuffer(parameters[parameterNames[0]]);
                double.TryParse(parameters[parameterNames[1]], out distance);
                int.TryParse(parameters[parameterNames[2]], out smoothness);
                Enum.TryParse <BufferCapType>(parameters[parameterNames[3]], out capType);
                Enum.TryParse <GeographyUnit>(parameters[parameterNames[4]], out mapUnit);
                Enum.TryParse <DistanceUnit>(parameters[parameterNames[5]], out distanceUnit);

                int bufferdFeaturesCount = 0;
                Collection <Feature> bufferedFeatures = new Collection <Feature>();
                foreach (Feature feature in featuresToBuffer)
                {
                    try
                    {
                        BaseShape         shape           = feature.GetShape();
                        MultipolygonShape bufferedShape   = shape.Buffer(distance, smoothness, capType, mapUnit, distanceUnit);
                        Feature           bufferedFeature = new Feature(bufferedShape.GetWellKnownBinary(), feature.Id, feature.ColumnValues);
                        bufferedFeature.Tag = feature.Tag;
                        bufferedFeatures.Add(bufferedFeature);
                    }
                    catch (Exception ex)
                    {
                        UpdatingProgressLongRunningTaskPluginEventArgs errorEventArgs = new UpdatingProgressLongRunningTaskPluginEventArgs(LongRunningTaskState.Error);
                        errorEventArgs.ExceptionInfo = new LongRunningTaskExceptionInfo(ex.Message, ex.StackTrace);
                        errorEventArgs.Message       = feature.Id;
                        OnUpdatingProgress(errorEventArgs);

                        continue;
                    }

                    Interlocked.Increment(ref bufferdFeaturesCount);
                    OnUpdatingProgress(new UpdatingProgressLongRunningTaskPluginEventArgs(LongRunningTaskState.Updating)
                    {
                        Current = bufferdFeaturesCount * 100 / featuresToBuffer.Count
                    });
                }

                OnUpdatingProgress(new UpdatingProgressLongRunningTaskPluginEventArgs(LongRunningTaskState.Updating)
                {
                    Message = "Creating File"
                });

                string outputPath = parameters[parameterNames[6]];
                string projection = parameters[parameterNames[7]];
                Output(bufferedFeatures, outputPath, projection);

                OnUpdatingProgress(new UpdatingProgressLongRunningTaskPluginEventArgs(LongRunningTaskState.Updating)
                {
                    Message = "Finished"
                });
            }
        }
        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);
            }
        }