public static Tuple <double, double, double, double> GetExtentFromGeoJson(FileInfo ogrInfoFileInfo, string geoJson, double totalMilliseconds) { using (var geoJsonFile = DisposableTempFile.MakeDisposableTempFileEndingIn(".json")) { File.WriteAllText(geoJsonFile.FileInfo.FullName, geoJson); var gdalDataDirectory = new DirectoryInfo(Path.Combine(ogrInfoFileInfo.DirectoryName, "gdal-data")); var commandLineArguments = BuildOgrInfoCommandLineArgumentsGetExtent(geoJsonFile.FileInfo, gdalDataDirectory); var processUtilityResult = ProcessUtility.ShellAndWaitImpl(ogrInfoFileInfo.DirectoryName, ogrInfoFileInfo.FullName, commandLineArguments, true, Convert.ToInt32(totalMilliseconds)); var lines = processUtilityResult.StdOut.Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).ToList(); // I believe this is happening irregularly - I see crashes below I can't readily reproduce. This is an attempt to narrow down the problem. // (Is the problem that a simple GET request is pounding on the Ogr2Ogr command line .EXE, and that the .EXE then gets too busy/overwhelmed to process multiple requests? // Just my first guess. -- SLG 9/30/2019) Check.Ensure(lines.Any(), $"No lines found returning from exec of \"{ogrInfoFileInfo.Name}\" in processed GeoJson string \"{geoJson}\". Raw StdOut: \"{processUtilityResult.StdOut}\""); if (lines.Any(x => x.Contains("Feature Count: 0"))) { return(null); } // We see crash here: "Sequence contains no matching element" on lines.First(..) var extentTokens = lines.First(x => x.StartsWith("Extent:")).Split(new[] { ' ', '(', ')', ',' }, StringSplitOptions.RemoveEmptyEntries).ToList(); return(new Tuple <double, double, double, double>(double.Parse(extentTokens[1]), double.Parse(extentTokens[2]), double.Parse(extentTokens[4]), double.Parse(extentTokens[5]))); } }
public static bool ConfirmAttributeExistsOnFeatureClass(FileInfo ogrInfoFileInfo, FileInfo gdbFileInfo, double totalMilliseconds, string featureClassName, string attributeName) { // ReSharper disable once AssignNullToNotNullAttribute var gdalDataDirectory = new DirectoryInfo(Path.Combine(ogrInfoFileInfo.DirectoryName, "gdal-data")); var commandLineArguments = BuildOgrInfoCommandLineArgumentsToConfirmAttributeExistsOnFeatureClass(gdbFileInfo, gdalDataDirectory, featureClassName); var processUtilityResult = ProcessUtility.ShellAndWaitImpl(ogrInfoFileInfo.DirectoryName, ogrInfoFileInfo.FullName, commandLineArguments, true, Convert.ToInt32(totalMilliseconds)); return(processUtilityResult.StdOut.Contains($"{attributeName}:", StringComparison.InvariantCultureIgnoreCase)); }
public static List <string> GetFeatureClassNamesFromFileGdb(FileInfo ogrInfoFileInfo, FileInfo gdbFileInfo, double totalMilliseconds) { // ReSharper disable once AssignNullToNotNullAttribute var gdalDataDirectory = new DirectoryInfo(Path.Combine(ogrInfoFileInfo.DirectoryName, "gdal-data")); var commandLineArguments = BuildOgrInfoCommandLineArgumentsToListFeatureClasses(gdbFileInfo, gdalDataDirectory); var processUtilityResult = ProcessUtility.ShellAndWaitImpl(ogrInfoFileInfo.DirectoryName, ogrInfoFileInfo.FullName, commandLineArguments, true, Convert.ToInt32(totalMilliseconds)); var featureClassesFromFileGdb = processUtilityResult.StdOut.Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).ToList(); return(featureClassesFromFileGdb.Select(x => x.Split(' ').Skip(1).First()).ToList()); }
// The FileGDB driver for Ogr2Ogr prints an empty line to standard error and returns a code even when successful, so we have to trap that case explicitly // ReSharper disable once UnusedMethodReturnValue.Local private ProcessUtilityResult ExecuteOgr2OgrCommandForFileGdbWrite(List <string> commandLineArguments) { var processUtilityResult = ProcessUtility.ShellAndWaitImpl(_ogr2OgrExecutable.DirectoryName, _ogr2OgrExecutable.FullName, commandLineArguments, true, Convert.ToInt32(_totalMilliseconds)); if (processUtilityResult.ReturnCode != 0 && !(processUtilityResult.StdOutAndStdErr.Equals("[stdout] \r\n[stderr] \r\n") || processUtilityResult.StdOutAndStdErr.Equals("[stderr] \r\n[stdout] \r\n"))) { var argumentsAsString = String.Join(" ", commandLineArguments.Select(ProcessUtility.EncodeArgumentForCommandLine).ToList()); var fullProcessAndArguments = $"{ProcessUtility.EncodeArgumentForCommandLine(_ogr2OgrExecutable.FullName)} {argumentsAsString}"; var errorMessage = $"Process \"{_ogr2OgrExecutable.Name}\" returned with exit code {processUtilityResult.ReturnCode}, expected exit code 0.\r\n\r\nStdErr and StdOut:\r\n{processUtilityResult.StdOutAndStdErr}\r\n\r\nProcess Command Line:\r\n{fullProcessAndArguments}\r\n\r\nProcess Working Directory: {_ogr2OgrExecutable.DirectoryName}"; throw new Ogr2OgrCommandLineException(errorMessage); } return(processUtilityResult); }
public static ProcessUtilityResult ExecutePyqgisScript(string pathToPyqgisScript, string workingDirectory) { var commandLineArguments = new List <string> { "/q", "/c", NeptuneWebConfiguration.PathToPyqgisLauncher, pathToPyqgisScript, NeptuneWebConfiguration.DatabaseConnectionString }; var processUtilityResult = ProcessUtility.ShellAndWaitImpl(workingDirectory, "cmd.exe", commandLineArguments, true, null); return(processUtilityResult); }
/// <summary> /// /// </summary> /// <param name="ogrInfoExecutableFileInfo">Path to the ogrinfo.exe executable</param> /// <param name="kmlFileInfo"></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="totalMilliseconds"></param> /// <returns></returns> public static List <string> GetFeatureClassNamesFromFileKml(FileInfo ogrInfoExecutableFileInfo, FileInfo kmlFileInfo, string originalFilename, double totalMilliseconds) { // ReSharper disable once AssignNullToNotNullAttribute var gdalDataDirectory = new DirectoryInfo(Path.Combine(ogrInfoExecutableFileInfo.DirectoryName, "gdal-data")); var commandLineArguments = BuildOgrInfoCommandLineArgumentsToListFeatureClassesKml(kmlFileInfo, gdalDataDirectory); var processUtilityResult = ProcessUtility.ShellAndWaitImpl(ogrInfoExecutableFileInfo.DirectoryName, ogrInfoExecutableFileInfo.FullName, commandLineArguments, true, Convert.ToInt32(totalMilliseconds)); if (processUtilityResult.ReturnCode != 0) { throw new SitkaGeometryDisplayErrorException($"{ogrInfoExecutableFileInfo.FullName} unable to open KML file {kmlFileInfo.FullName} - original filename {originalFilename}."); } var featureClassesFromFileKml = processUtilityResult.StdOut.Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).ToList(); return(featureClassesFromFileKml.Select(x => x.Split(new[] { ' ' }, 2).Skip(1).First()).ToList()); }
public static Tuple <double, double, double, double> GetExtentFromGeoJson(FileInfo ogrInfoFileInfo, string geoJson, double totalMilliseconds) { using (var geoJsonFile = DisposableTempFile.MakeDisposableTempFileEndingIn(".json")) { File.WriteAllText(geoJsonFile.FileInfo.FullName, geoJson); var gdalDataDirectory = new DirectoryInfo(Path.Combine(ogrInfoFileInfo.DirectoryName, "gdal-data")); var commandLineArguments = BuildOgrInfoCommandLineArgumentsGetExtent(geoJsonFile.FileInfo, gdalDataDirectory); var processUtilityResult = ProcessUtility.ShellAndWaitImpl(ogrInfoFileInfo.DirectoryName, ogrInfoFileInfo.FullName, commandLineArguments, true, Convert.ToInt32(totalMilliseconds)); var lines = processUtilityResult.StdOut.Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).ToList(); if (lines.Any(x => x.Contains("Feature Count: 0"))) { return(null); } var extentTokens = lines.First(x => x.StartsWith("Extent:")).Split(new[] { ' ', '(', ')', ',' }, StringSplitOptions.RemoveEmptyEntries).ToList(); return(new Tuple <double, double, double, double>(double.Parse(extentTokens[1]), double.Parse(extentTokens[2]), double.Parse(extentTokens[4]), double.Parse(extentTokens[5]))); } }