public IEnumerable <ValidationResult> Validate(ValidationContext validationContext)
        {
            var errors = new List <ValidationResult>();

            FileResource.ValidateFileSize(FileResourceData, errors, GeneralUtility.NameOf(() => FileResourceData));

            using (var disposableTempFile = DisposableTempFile.MakeDisposableTempFileEndingIn(".gdb.zip"))
            {
                var gdbFile = disposableTempFile.FileInfo;
                FileResourceData.SaveAs(gdbFile.FullName);

                var ogr2OgrCommandLineRunner = new Ogr2OgrCommandLineRunner(FirmaWebConfiguration.Ogr2OgrExecutable,
                                                                            Ogr2OgrCommandLineRunner.DefaultCoordinateSystemId,
                                                                            FirmaWebConfiguration.HttpRuntimeExecutionTimeout.TotalMilliseconds);

                List <string> featureClassNames = null;
                try
                {
                    featureClassNames = OgrInfoCommandLineRunner.GetFeatureClassNamesFromFileGdb(new FileInfo(FirmaWebConfiguration.OgrInfoExecutable),
                                                                                                 gdbFile,
                                                                                                 Ogr2OgrCommandLineRunner.DefaultTimeOut);
                }
                catch (Exception e)
                {
                    errors.Add(new ValidationResult("There was a problem uploading your file geodatabase. Verify it meets the requirements and is not corrupt."));
                    SitkaLogger.Instance.LogDetailedErrorMessage(e);
                }

                if (featureClassNames != null)
                {
                    var featureClasses = featureClassNames.ToDictionary(x => x,
                                                                        x =>
                    {
                        try
                        {
                            var geoJson = ogr2OgrCommandLineRunner.ImportFileGdbToGeoJson(gdbFile, x, false);
                            return(JsonTools.DeserializeObject <FeatureCollection>(geoJson));
                        }
                        catch (Exception e)
                        {
                            errors.Add(new ValidationResult($"There was a problem processing the Feature Class \"{x}\"."));
                            SitkaLogger.Instance.LogDetailedErrorMessage(e);
                            return(null);
                        }
                    }).Where(x => x.Value != null && FocusAreaLocationStaging.IsUsableFeatureCollectionGeoJson(x.Value));

                    if (!featureClasses.Any())
                    {
                        errors.Add(new ValidationResult("There are no usable Feature Classes in the uploaded file. Feature Classes must contain Polygon and/or Multi-Polygon features."));
                    }
                }
            }

            return(errors);
        }
예제 #2
0
        public IEnumerable <ValidationResult> Validate(ValidationContext validationContext)
        {
            var errors = new List <ValidationResult>();

            FileResource.ValidateFileSize(FileResourceData, errors, GeneralUtility.NameOf(() => FileResourceData));

            using (var disposableTempFile = DisposableTempFile.MakeDisposableTempFileEndingIn(".gdb.zip"))
            {
                var gdbFile = disposableTempFile.FileInfo;
                FileResourceData.SaveAs(gdbFile.FullName);

                try
                {
                    var featureClassNames = OgrInfoCommandLineRunner.GetFeatureClassNamesFromFileGdb(
                        new FileInfo(NeptuneWebConfiguration.OgrInfoExecutable),
                        gdbFile,
                        Ogr2OgrCommandLineRunner.DefaultTimeOut);

                    if (featureClassNames?.Count == 0)
                    {
                        errors.Add(new ValidationResult(
                                       "The file geodatabase contained no feature class. Please upload a file geodatabase containing exactly one feature class."));
                        return(errors);
                    }

                    if (featureClassNames?.Count != 1)
                    {
                        errors.Add(new ValidationResult(
                                       "The file geodatabase contained more than one feature class. Please upload a file geodatabase containing exactly one feature class."));
                        return(errors);
                    }

                    var featureClassName = featureClassNames[0];
                    if (!OgrInfoCommandLineRunner.ConfirmAttributeExistsOnFeatureClass(
                            new FileInfo(NeptuneWebConfiguration.OgrInfoExecutable),
                            gdbFile,
                            Ogr2OgrCommandLineRunner.DefaultTimeOut, featureClassName, TreatmentBMPNameField))
                    {
                        errors.Add(new ValidationResult($"The feature class in the file geodatabase does not have an attribute named {TreatmentBMPNameField}. Please double-check the attribute name you entered and try again."));
                        return(errors);
                    }
                }
                catch (Exception e)
                {
                    SitkaLogger.Instance.LogAbridgedErrorMessage(e);
                    errors.Add(new ValidationResult(
                                   "There was a problem uploading your file geodatabase. Verify it meets the requirements and is not corrupt."));
                }
            }

            return(errors);
        }
        public static List <ProjectLocationStagingUpdate> CreateProjectLocationStagingUpdateListFromGdb(FileInfo gdbFile, ProjectUpdateBatch projectUpdateBatch, Person currentPerson)
        {
            var ogr2OgrCommandLineRunner = new Ogr2OgrCommandLineRunner(FirmaWebConfiguration.Ogr2OgrExecutable,
                                                                        Ogr2OgrCommandLineRunner.DefaultCoordinateSystemId,
                                                                        FirmaWebConfiguration.HttpRuntimeExecutionTimeout.TotalMilliseconds);

            var featureClassNames = OgrInfoCommandLineRunner.GetFeatureClassNamesFromFileGdb(new FileInfo(FirmaWebConfiguration.OgrInfoExecutable), gdbFile, Ogr2OgrCommandLineRunner.DefaultTimeOut);

            var projectLocationStagings =
                featureClassNames.Select(x => new ProjectLocationStagingUpdate(projectUpdateBatch, currentPerson, x, ogr2OgrCommandLineRunner.ImportFileGdbToGeoJson(gdbFile, x, true), true)).ToList();

            return(projectLocationStagings);
        }
예제 #4
0
        public static List <ProjectLocationStagingUpdate> CreateProjectLocationStagingUpdateListFromKmz(FileInfo disposableTempFileFileInfo, string fileName, ProjectUpdateBatch projectUpdateBatch, FirmaSession currentFirmaSession)
        {
            var ogr2OgrCommandLineRunner = new Ogr2OgrCommandLineRunner(FirmaWebConfiguration.Ogr2OgrExecutable,
                                                                        LtInfoGeometryConfiguration.DefaultCoordinateSystemId,
                                                                        FirmaWebConfiguration.HttpRuntimeExecutionTimeout.TotalMilliseconds);

            var featureClassNames = OgrInfoCommandLineRunner.GetFeatureClassNamesFromFileKmz(new FileInfo(FirmaWebConfiguration.OgrInfoExecutable), disposableTempFileFileInfo, fileName, Ogr2OgrCommandLineRunner.DefaultTimeOut);

            var projectLocationStagings =
                featureClassNames.Select(x => new ProjectLocationStagingUpdate(projectUpdateBatch, currentFirmaSession.Person, "", ogr2OgrCommandLineRunner.ImportFileKmzToGeoJson(disposableTempFileFileInfo, true), true)).ToList();

            Check.Require(!projectLocationStagings.All(x => x.ToGeoJsonFeatureCollection().Features.All(y => y.Geometry == null)), new SitkaGeometryDisplayErrorException($"Cannot create stagings for a location when all features don't have a geometry"));
            return(projectLocationStagings);
        }
예제 #5
0
        public static List <string> ImportShapefileIntoSqlTempTable(string shapeFilePath)
        {
            var connectionString = FirmaWebConfiguration.DatabaseConnectionString;

            connectionString = connectionString.Replace("Trusted_Connection=True", "trusted_connection=yes");
            var ogr2OgrCommandLineRunner = new Ogr2OgrCommandLineRunner(FirmaWebConfiguration.Ogr2OgrExecutable,
                                                                        Ogr2OgrCommandLineRunner.DefaultCoordinateSystemId,
                                                                        FirmaWebConfiguration.HttpRuntimeExecutionTimeout.TotalMilliseconds);
            var featureClassNames = OgrInfoCommandLineRunner.GetFeatureClassNamesFromShapefile(new FileInfo(FirmaWebConfiguration.OgrInfoExecutable), shapeFilePath, Ogr2OgrCommandLineRunner.DefaultTimeOut);

            ogr2OgrCommandLineRunner.ImportPolyFromShapefile(shapeFilePath, false,
                                                             GISImportTableName, GeomName, FIDName, connectionString);
            return(featureClassNames);
        }
예제 #6
0
        public async Task <IActionResult> UploadGDBAndParseFeatureClasses([FromForm] IFormFile inputFile)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            byte[] inputFileContents;
            await using (var ms = new MemoryStream(4096))
            {
                await inputFile.CopyToAsync(ms);

                inputFileContents = ms.ToArray();
            }
            // save the gdb file contents to UploadedGdb so user doesn't have to wait for upload of file again
            var uploadedGdbID = UploadedGdb.CreateNew(_dbContext, inputFileContents);

            using var disposableTempFile = DisposableTempFile.MakeDisposableTempFileEndingIn(".gdb.zip");
            var gdbFile = disposableTempFile.FileInfo;

            System.IO.File.WriteAllBytes(gdbFile.FullName, inputFileContents);

            try
            {
                var featureClassInfos = OgrInfoCommandLineRunner.GetFeatureClassInfoFromFileGdb(
                    _rioConfiguration.OgrInfoExecutable,
                    gdbFile.FullName,
                    250000000, _logger, 1);
                var uploadParcelLayerInfoDto = new UploadParcelLayerInfoDto()
                {
                    UploadedGdbID  = uploadedGdbID,
                    FeatureClasses = featureClassInfos
                };

                return(Ok(uploadParcelLayerInfoDto));
            }
            catch (System.ComponentModel.DataAnnotations.ValidationException e)
            {
                _logger.LogError(e, e.Message);
                UploadedGdb.Delete(_dbContext, uploadedGdbID);
                return(BadRequest(e.Message));
            }
            catch (Exception e)
            {
                _logger.LogError(e, e.Message);
                UploadedGdb.Delete(_dbContext, uploadedGdbID);
                return(BadRequest("Error reading GDB file!"));
            }
        }
예제 #7
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="gisFile"></param>
        /// <param name="originalFilename">This is the original name of the file as it appeared on the users file system. It is provided just for error messaging purposes.</param>
        /// <param name="organization"></param>
        /// <returns></returns>
        public static List <OrganizationBoundaryStaging> CreateOrganizationBoundaryStagingStagingListFromGdb(FileInfo gisFile, string originalFilename, Organization organization)
        {
            var ogr2OgrCommandLineRunner = new Ogr2OgrCommandLineRunner(FirmaWebConfiguration.Ogr2OgrExecutable,
                                                                        LtInfoGeometryConfiguration.DefaultCoordinateSystemId,
                                                                        FirmaWebConfiguration.HttpRuntimeExecutionTimeout.TotalMilliseconds);

            var geoJsons =
                OgrInfoCommandLineRunner.GetFeatureClassNamesFromFileGdb(new FileInfo(FirmaWebConfiguration.OgrInfoExecutable), gisFile, originalFilename, Ogr2OgrCommandLineRunner.DefaultTimeOut)
                .ToDictionary(x => x, x => ogr2OgrCommandLineRunner.ImportFileGdbToGeoJson(gisFile, x, false))
                .Where(x => OrganizationBoundaryStaging.IsUsableFeatureCollectionGeoJson(JsonTools.DeserializeObject <FeatureCollection>(x.Value)))
                .ToDictionary(x => x.Key, x => new FeatureCollection(JsonTools.DeserializeObject <FeatureCollection>(x.Value).Features.Where(OrganizationBoundaryStaging.IsUsableFeatureGeoJson).ToList()).ToGeoJsonString());

            Check.Assert(geoJsons.Count != 0, "Number of usable Feature Classes in uploaded file must be greater than 0.");

            return(geoJsons.Select(x => new OrganizationBoundaryStaging(organization, x.Key, x.Value)).ToList());
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="gdbFile"></param>
        /// <param name="originalFilename">This is the original name of the file as it appeared on the users file system. It is provided just for error messaging purposes.</param>
        /// <param name="project"></param>
        /// <param name="currentFirmaSession"></param>
        /// <returns></returns>
        public static List <ProjectLocationStaging> CreateProjectLocationStagingListFromGdb(FileInfo gdbFile,
                                                                                            string originalFilename,
                                                                                            Project project,
                                                                                            FirmaSession currentFirmaSession)
        {
            var ogr2OgrCommandLineRunner = new Ogr2OgrCommandLineRunner(FirmaWebConfiguration.Ogr2OgrExecutable,
                                                                        LtInfoGeometryConfiguration.DefaultCoordinateSystemId,
                                                                        FirmaWebConfiguration.HttpRuntimeExecutionTimeout.TotalMilliseconds);

            var featureClassNames = OgrInfoCommandLineRunner.GetFeatureClassNamesFromFileGdb(new FileInfo(FirmaWebConfiguration.OgrInfoExecutable), gdbFile, originalFilename, Ogr2OgrCommandLineRunner.DefaultTimeOut);

            var projectLocationStagings =
                featureClassNames.Select(x => new ProjectLocationStaging(project, currentFirmaSession.Person, x, ogr2OgrCommandLineRunner.ImportFileGdbToGeoJson(gdbFile, x, true), true)).ToList();

            Check.Require(!projectLocationStagings.All(x => x.ToGeoJsonFeatureCollection().Features.All(y => y.Geometry == null)), new SitkaGeometryDisplayErrorException($"Cannot create stagings for a location when all features don't have a geometry."));
            return(projectLocationStagings);
        }
예제 #9
0
        public static BoundingBox MakeBoundingBoxFromGeoJson(string geoJson)
        {
            var result = OgrInfoCommandLineRunner.GetExtentFromGeoJson(new FileInfo(NeptuneWebConfiguration.OgrInfoExecutable),
                                                                       geoJson,
                                                                       NeptuneWebConfiguration.HttpRuntimeExecutionTimeout.TotalMilliseconds);

            if (result == null)
            {
                return(MakeNewDefaultBoundingBox());
            }

            var p1 = new Point(result.Item2, result.Item1);
            var p2 = new Point(result.Item4, result.Item3);

            var boundingBox = new BoundingBox(p1, p2);

            return(boundingBox);
        }
예제 #10
0
        public static List <string> ImportGdbIntoSqlTempTable(FileInfo gdbFile)
        {
            var connectionString = FirmaWebConfiguration.DatabaseConnectionString;

            connectionString = connectionString.Replace("Trusted_Connection=True", "trusted_connection=yes");
            var ogr2OgrCommandLineRunner = new Ogr2OgrCommandLineRunner(FirmaWebConfiguration.Ogr2OgrExecutable,
                                                                        Ogr2OgrCommandLineRunner.DefaultCoordinateSystemId,
                                                                        FirmaWebConfiguration.HttpRuntimeExecutionTimeout.TotalMilliseconds);
            var featureClassNames        = OgrInfoCommandLineRunner.GetFeatureClassNamesFromFileGdb(new FileInfo(FirmaWebConfiguration.OgrInfoExecutable), gdbFile, Ogr2OgrCommandLineRunner.DefaultTimeOut, false);
            var featureClassNameToImport = string.Empty;

            if (featureClassNames.Count > 1)
            {
                featureClassNameToImport = featureClassNames
                                           .Where(x => !x.Contains("ATTACH", StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();
            }
            ogr2OgrCommandLineRunner.ImportFileGdbToSql(gdbFile, false,
                                                        GISImportTableName, GeomName, FIDName, connectionString, featureClassNameToImport);
            return(featureClassNames);
        }
예제 #11
0
        public bool UpdateModel(Person currentPerson)
        {
            HttpRequestStorage.DatabaseEntities.DelineationStagings.DeleteDelineationStaging(currentPerson.DelineationStagingsWhereYouAreTheUploadedByPerson);
            HttpRequestStorage.DatabaseEntities.SaveChanges();

            using (var disposableTempFile = DisposableTempFile.MakeDisposableTempFileEndingIn(".gdb.zip"))
            {
                var gdbFile = disposableTempFile.FileInfo;
                FileResourceData.SaveAs(gdbFile.FullName);

                var ogr2OgrCommandLineRunner = new Ogr2OgrCommandLineRunner(NeptuneWebConfiguration.Ogr2OgrExecutable,
                                                                            Ogr2OgrCommandLineRunner.DefaultCoordinateSystemId,
                                                                            NeptuneWebConfiguration.HttpRuntimeExecutionTimeout.TotalMilliseconds * 10);

                try
                {
                    var featureClassNames = OgrInfoCommandLineRunner.GetFeatureClassNamesFromFileGdb(new FileInfo(NeptuneWebConfiguration.OgrInfoExecutable),
                                                                                                     gdbFile,
                                                                                                     Ogr2OgrCommandLineRunner.DefaultTimeOut);
                    if (featureClassNames != null)
                    {
                        var columns = new List <string>
                        {
                            $"{currentPerson.PersonID} as UploadedByPersonID",
                            $"{TreatmentBMPNameField} as TreatmentBMPName",
                            $"{StormwaterJurisdictionID} as StormwaterJurisdictionID"
                        };
                        ogr2OgrCommandLineRunner.ImportFileGdbToMsSql(gdbFile, featureClassNames[0], "DelineationStaging", columns,
                                                                      NeptuneWebConfiguration.DatabaseConnectionString, true, Ogr2OgrCommandLineRunner.GEOMETRY_TYPE_POLYGON);
                    }
                }
                catch (Exception e)
                {
                    SitkaLogger.Instance.LogAbridgedErrorMessage(e);
                    return(false);
                }
            }

            return(true);
        }
        public IEnumerable <ValidationResult> Validate(ValidationContext validationContext)
        {
            var currentPerson = HttpRequestStorage.DatabaseEntities.People.Find(PersonID);

            HttpRequestStorage.DatabaseEntities.pLandUseBlockStagingDeleteByPersonID(currentPerson.PersonID);
            var errors = new List <ValidationResult>();

            FileResource.ValidateFileSize(FileResourceData, errors, GeneralUtility.NameOf(() => FileResourceData));

            using (var disposableTempFile = DisposableTempFile.MakeDisposableTempFileEndingIn(".gdb.zip"))
            {
                var gdbFile = disposableTempFile.FileInfo;
                FileResourceData.SaveAs(gdbFile.FullName);

                var ogr2OgrCommandLineRunner = new Ogr2OgrCommandLineRunner(NeptuneWebConfiguration.Ogr2OgrExecutable,
                                                                            Ogr2OgrCommandLineRunner.DefaultCoordinateSystemId,
                                                                            NeptuneWebConfiguration.HttpRuntimeExecutionTimeout.TotalMilliseconds * 10);

                List <string> featureClassNames = null;
                try
                {
                    featureClassNames = OgrInfoCommandLineRunner.GetFeatureClassNamesFromFileGdb(new FileInfo(NeptuneWebConfiguration.OgrInfoExecutable),
                                                                                                 gdbFile,
                                                                                                 Ogr2OgrCommandLineRunner.DefaultTimeOut);
                }
                catch (Exception e)
                {
                    errors.Add(new ValidationResult("There was a problem uploading your file geodatabase. Verify it meets the requirements and is not corrupt."));
                    SitkaLogger.Instance.LogDetailedErrorMessage(e);
                }

                if (featureClassNames != null)
                {
                    if (featureClassNames.Count == 0)
                    {
                        errors.Add(new ValidationResult("The file geodatabase contained no feature class. Please upload a file geodatabase containing exactly one feature class."));
                        return(errors);
                    }

                    if (featureClassNames.Count != 1)
                    {
                        errors.Add(new ValidationResult("The file geodatabase contained more than one feature class. Please upload a file geodatabase containing exactly one feature class."));
                        return(errors);
                    }

                    try
                    {
                        var columns = new List <string>
                        {
                            "PLU_Cat as PriorityLandUseType",
                            "LU_Descr as LandUseDescription",
                            "TGR as TrashGenerationRate",
                            "LU_for_TGR as LandUseForTGR",
                            "MHI as MedianHouseHoldIncome",
                            "Jurisdic as StormwaterJurisdiction",
                            "Permit as PermitType",
                            $"{PersonID} as UploadedByPersonID"
                        };
                        ogr2OgrCommandLineRunner.ImportFileGdbToMsSql(gdbFile, featureClassNames[0],
                                                                      "LandUseBlockStaging", columns,
                                                                      NeptuneWebConfiguration.DatabaseConnectionString, true,
                                                                      Ogr2OgrCommandLineRunner.GEOMETRY_TYPE_POLYGON);
                    }
                    catch (Ogr2OgrCommandLineException e)
                    {
                        if (e.Message.Contains("column does not allow nulls",
                                               StringComparison.InvariantCultureIgnoreCase))
                        {
                            errors.Add(new ValidationResult("The upload contained features with null values. All fields are required."));
                        }
                        else if (e.Message.Contains("Unrecognised field name",
                                                    StringComparison.InvariantCultureIgnoreCase))
                        {
                            errors.Add(new ValidationResult("The columns in the uploaded file did not match the Land Use Block schema. The file is invalid and cannot be uploaded."));
                        }
                        else
                        {
                            errors.Add(new ValidationResult($"There was a problem processing the Feature Class \"{featureClassNames[0]}\". The file may be corrupted or invalid."));
                            SitkaLogger.Instance.LogDetailedErrorMessage(e);
                        }
                    }
                    catch (Exception e)
                    {
                        errors.Add(new ValidationResult($"There was a problem processing the Feature Class \"{featureClassNames[0]}\". Feature Classes must contain Polygon and/or Multi-Polygon features."));
                        SitkaLogger.Instance.LogDetailedErrorMessage(e);
                    }
                }
            }

            return(errors);
        }