public string GetFile()
        {
            if (!Directory.Exists(Constans.Constans.PathToSaveFile))
            {
                throw new Exception(Constans.Constans.AppDataFolderDosentExists);
            }
            string       resultGeoJson = "";
            var          stream        = HttpContext.Current.Request.GetBufferedInputStream();
            StreamReader reader        = new StreamReader(stream);

            reader.ReadToEnd();

            var postedFile = HttpContext.Current.Request.Files[Constans.Constans.FormObjectName];
            var ignore     = HttpContext.Current.Request.InputStream;

            if (postedFile != null)
            {
                ClearDataDirectory(Constans.Constans.PathToSaveFile);
                var pathToZipFile = Path.Combine(Constans.Constans.PathToSaveFile, postedFile.FileName);
                postedFile.SaveAs(pathToZipFile);

                string fullPathToShpFile = ZipFileHelper.ExtractZipFile(pathToZipFile);

                IGeometryServices geometryServices = new NtsGeometryServices();

                ShapeFileHelper shapeFileHelper = new ShapeFileHelper(fullPathToShpFile);
                resultGeoJson = shapeFileHelper.ConvertShapeFileToGeoJson();
                ClearDataDirectory(Constans.Constans.PathToSaveFile);
            }
            return(resultGeoJson);
        }
예제 #2
0
        private void ProcessWithShapeFilesOnly()
        {
            int count = featureSources.Sum(f =>
            {
                if (!f.IsOpen)
                {
                    f.Open();
                }
                return(f.GetCount());
            });

            var args = new UpdatingTaskProgressEventArgs(TaskState.Updating);
            var shapeFileFeatureSources = featureSources.OfType <ShapeFileFeatureSource>().ToList();
            var shapeFileType           = shapeFileFeatureSources.First().GetShapeFileType();

            var currentProgress = 0;
            var shapeFileHelper = new ShapeFileHelper(shapeFileType, OutputPathFileName, columns, wkt);

            foreach (var featureSource in shapeFileFeatureSources)
            {
                shapeFileHelper.ForEachFeatures(featureSource, f =>
                {
                    if (f.GetWellKnownBinary() != null)
                    {
                        foreach (var featureColumn in columns)
                        {
                            if (!f.ColumnValues.Keys.Contains(featureColumn.ColumnName))
                            {
                                f.ColumnValues.Add(featureColumn.ColumnName, "0");
                            }
                        }

                        shapeFileHelper.Add(new Feature(f.GetWellKnownBinary(), Guid.NewGuid().ToString(), f.ColumnValues));
                    }

                    currentProgress++;
                    var progressPercentage = currentProgress * 100 / count;
                    args            = new UpdatingTaskProgressEventArgs(TaskState.Updating, progressPercentage);
                    args.Current    = currentProgress;
                    args.UpperBound = count;
                    OnUpdatingProgress(args);
                    isCanceled = args.TaskState == TaskState.Canceled;
                    return(isCanceled);
                });
            }

            shapeFileHelper.Commit();
        }
        private void BufferShapeFile()
        {
            var args          = new UpdatingTaskProgressEventArgs(TaskState.Updating);
            var currentSource = (ShapeFileFeatureSource)FeatureSource;

            if (!currentSource.IsOpen)
            {
                currentSource.Open();
            }

            var canceled      = false;
            var projectionWkt = Proj4Projection.ConvertProj4ToPrj(DisplayProjectionParameters);
            var helper        = new ShapeFileHelper(ShapeFileType.Polygon, outputPathFileName, currentSource.GetColumns(), projectionWkt);

            helper.CapabilityToFlush = 1000;
            helper.ForEachFeatures(currentSource, (f, currentProgress, upperBound, percentage) =>
            {
                try
                {
                    if (f.GetWellKnownBinary() != null)
                    {
                        //Feature bufferedFeature = f.Buffer(Distance, Smoothness, Capstyle, MapUnit, DistanceUnit);
                        //Feature bufferedFeature = SqlTypesGeometryHelper.Buffer(f, Distance, Smoothness, Capstyle, MapUnit, DistanceUnit);
                        Feature bufferedFeature = BufferFeature(f);
                        helper.Add(bufferedFeature);
                    }

                    args            = new UpdatingTaskProgressEventArgs(TaskState.Updating, percentage);
                    args.Current    = currentProgress;
                    args.UpperBound = upperBound;
                    OnUpdatingProgress(args);

                    canceled = args.TaskState == TaskState.Canceled;
                    return(canceled);
                }
                catch (Exception ex)
                {
                    GisEditor.LoggerManager.Log(LoggerLevel.Debug, ex.Message, new ExceptionInfo(ex));
                    return(false);
                }
            });

            helper.Commit();
        }
        private void DataJoinShapeFile()
        {
            var args = new UpdatingTaskProgressEventArgs(TaskState.Updating);
            ShapeFileFeatureSource currentSource = ShapeFileFeatureSource;

            if (!currentSource.IsOpen)
            {
                currentSource.Open();
            }
            var index = 0;
            var count = currentSource.GetAllFeatures(ReturningColumnsType.AllColumns).Count;

            Collection <DbfColumn> includeColumns = new Collection <DbfColumn>();

            RemoveUnduplicateColumn(IncludedColumnsList);

            foreach (var column in IncludedColumnsList)
            {
                DbfColumnType tmpDbfColumnType = DbfColumnType.Character;
                if (Enum.TryParse(column.TypeName, out tmpDbfColumnType))
                {
                    DbfColumn dbfColumn = new DbfColumn(column.ColumnName, tmpDbfColumnType, column.MaxLength, 0);
                    includeColumns.Add(dbfColumn);
                }
            }

            ShapeFileType shapeFileType = GetShapeFileType(currentSource.GetAllFeatures(ReturningColumnsType.AllColumns).FirstOrDefault());
            var           projectionWkt = Proj4Projection.ConvertProj4ToPrj(DisplayProjectionParameters);
            var           dataTable     = DataJoinAdapter.ReadDataToDataGrid(CsvFilePath, Delimiter);
            var           featureRows   = dataTable.Rows;

            var helper = new ShapeFileHelper(shapeFileType, OutputPathFileName, includeColumns, projectionWkt);

            helper.ForEachFeatures(currentSource, (f, currentProgress, upperBound, percentage) =>
            {
                try
                {
                    bool canceled = false;
                    if (f.GetWellKnownBinary() != null)
                    {
                        index++;
                        try
                        {
                            var matchedDataRow = featureRows.Cast <DataRow>().FirstOrDefault(r => MatchConditions.All(tmpCondition => f.ColumnValues[tmpCondition.SelectedLayerColumn.ColumnName]
                                                                                                                      == r[tmpCondition.SelectedDelimitedColumn.ColumnName].ToString()));

                            if (matchedDataRow != null)
                            {
                                SetFeatureColumnValues(f, matchedDataRow, IncludedColumnsList, InvalidColumns);
                                helper.Add(f);
                            }
                            else if (IsIncludeAllFeatures)
                            {
                                helper.Add(f);
                            }

                            if (UpdateProgress(OnUpdatingProgress, index, count))
                            {
                                canceled = true;
                            }
                        }
                        catch (Exception ex)
                        {
                            var errorEventArgs   = new UpdatingTaskProgressEventArgs(TaskState.Error);
                            errorEventArgs.Error = new ExceptionInfo(string.Format(CultureInfo.InvariantCulture, "Feature id: {0}, {1}"
                                                                                   , f.Id, ex.Message)
                                                                     , ex.StackTrace
                                                                     , ex.Source);
                            GisEditor.LoggerManager.Log(LoggerLevel.Debug, ex.Message, new ExceptionInfo(ex));
                            errorEventArgs.Message = f.Id;
                            OnUpdatingProgress(errorEventArgs);
                        }
                    }

                    args            = new UpdatingTaskProgressEventArgs(TaskState.Updating, percentage);
                    args.Current    = currentProgress;
                    args.UpperBound = upperBound;
                    OnUpdatingProgress(args);

                    canceled = args.TaskState == TaskState.Canceled;
                    return(canceled);
                }
                catch
                {
                    return(false);
                }
            });

            helper.Commit();

            SavePrjFile(OutputPathFileName, DisplayProjectionParameters);
        }
        private void SimplifyShapeFile()
        {
            var args          = new UpdatingTaskProgressEventArgs(TaskState.Updating);
            var currentSource = (ShapeFileFeatureSource)FeatureSource;

            if (!currentSource.IsOpen)
            {
                currentSource.Open();
            }

            var canceled      = false;
            var shapeFileType = currentSource.GetShapeFileType();
            var projectionWkt = Proj4Projection.ConvertProj4ToPrj(DisplayProjectionParameters);
            var helper        = new ShapeFileHelper(shapeFileType, outputPathFileName, currentSource.GetColumns(), projectionWkt);

            try
            {
                helper.ForEachFeatures(currentSource, (f, currentProgress, upperBound, percentage) =>
                {
                    try
                    {
                        if (f.GetWellKnownBinary() != null)
                        {
                            SimplificationType simType = SimplificationType.DouglasPeucker;
                            if (preserveTopology)
                            {
                                simType = SimplificationType.TopologyPreserving;
                            }

                            var shape                 = f.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 feature = new Feature(simplifiedShape);
                                foreach (var item in f.ColumnValues)
                                {
                                    feature.ColumnValues[item.Key] = item.Value;
                                }
                                helper.Add(feature);
                            }
                        }

                        args            = new UpdatingTaskProgressEventArgs(TaskState.Updating, percentage);
                        args.Current    = currentProgress;
                        args.UpperBound = upperBound;
                        OnUpdatingProgress(args);

                        canceled = args.TaskState == TaskState.Canceled;
                        return(canceled);
                    }
                    catch (Exception e)
                    {
                        GisEditor.LoggerManager.Log(LoggerLevel.Debug, e.Message, new ExceptionInfo(e));
                        return(false);
                    }
                });
            }
            finally
            {
                helper.Commit();
            }
        }
예제 #6
0
 public void ReadShapeFile()
 {
     var shapes = new ShapeFileHelper().Read("..\\..\\..\\data\\KOMMUNE", "KOMNAVN").ToList();
 }
        protected override void RunCore()
        {
            var existingShapePathFileNames = ShapePathFileNames.Where(s => CheckShapeFileExists(s.Key));
            var upperBounds = GetUpperBounds(existingShapePathFileNames);

            CreateOutputPath(OutputPathFileName);
            var currentIndex = 0;

            foreach (var currentShapePathFileName in existingShapePathFileNames)
            {
                try
                {
                    string currentShapeFileName      = Path.GetFileName(currentShapePathFileName.Key);
                    string outputShapePathFileName   = Path.Combine(OutputPathFileName, currentShapeFileName);
                    string outputTempIdxPathFileName = Path.Combine(OutputPathFileName, "TMP" + currentShapeFileName);
                    outputTempIdxPathFileName = Path.ChangeExtension(outputTempIdxPathFileName, ".idx");

                    DeleteRelatedFiles(outputShapePathFileName);

                    Proj4Projection projection = new Proj4Projection();
                    projection.InternalProjectionParametersString = currentShapePathFileName.Value;
                    projection.ExternalProjectionParametersString = TargetProjectionParameter;
                    projection.Open();

                    //CreateShapeFileWithIndex(currentShapePathFileName.Key, outputShapePathFileName, outputTempIdxPathFileName, projection, upperBounds, ref current);

                    var currentFeatureSource = new ShapeFileFeatureSource(currentShapePathFileName.Key);
                    currentFeatureSource.Open();
                    string          projectionWkt = Proj4Projection.ConvertProj4ToPrj(TargetProjectionParameter);
                    ShapeFileHelper helper        = new ShapeFileHelper(currentFeatureSource.GetShapeFileType(), outputShapePathFileName, currentFeatureSource.GetColumns(), projectionWkt);

                    helper.ForEachFeatures(currentFeatureSource, f =>
                    {
                        if (f.GetWellKnownBinary() != null)
                        {
                            var newFeature = projection.ConvertToExternalProjection(f);
                            if (newFeature.CanMakeValid)
                            {
                                newFeature = newFeature.MakeValid();
                            }

                            if (newFeature.GetWellKnownType() != WellKnownType.GeometryCollection)
                            {
                                helper.Add(newFeature);
                            }
                        }

                        currentIndex++;
                        UpdatingTaskProgressEventArgs args = new UpdatingTaskProgressEventArgs(TaskState.Updating, currentIndex * 100 / upperBounds);
                        args.Current    = currentIndex;
                        args.UpperBound = upperBounds;
                        OnUpdatingProgress(args);
                        return(args.TaskState == TaskState.Canceled);
                    });

                    helper.Commit();
                    CreateDbfFile(currentShapePathFileName.Key, outputShapePathFileName);
                    //CreatePrjFile(outputShapePathFileName, TargetProjectionParameter);
                }
                catch (Exception ex)
                {
                    UpdatingTaskProgressEventArgs errorArgs = new UpdatingTaskProgressEventArgs(TaskState.Error);
                    errorArgs.Error = new ExceptionInfo(ex.Message, ex.StackTrace, ex.Source);
                    GisEditor.LoggerManager.Log(LoggerLevel.Debug, ex.Message, new ExceptionInfo(ex));
                    OnUpdatingProgress(errorArgs);
                    continue;
                }
            }
        }