private static async Task <List <CoordinateZ> > OpenTopoElevation(HttpClient client, string openTopoDataSet,
                                                                          List <CoordinateZ> coordinates, IProgress <string>?progress)
        {
            if (!coordinates.Any())
            {
                return(coordinates);
            }

            var partitionedCoordinates = coordinates.Partition(100).ToList();

            var resultList = new List <ElevationResult>();

            progress?.Report(
                $"{coordinates.Count} Coordinates for Elevation - querying in {partitionedCoordinates.Count} groups...");

            foreach (var loopCoordinateGroups in partitionedCoordinates)
            {
                var requestUri =
                    $"https://api.opentopodata.org/v1/{openTopoDataSet}?locations={string.Join("|", loopCoordinateGroups.Select(x => $"{x.Y},{x.X}"))}";

                progress?.Report($"Sending request to {requestUri}");

                var elevationReturn = await client.GetStringAsync(requestUri);

                progress?.Report($"Parsing Return from {requestUri}");

                var elevationParsed = JsonSerializer.Deserialize <ElevationResponse>(elevationReturn);

                if (elevationParsed == null)
                {
                    throw await EventLogContext.TryWriteExceptionToLog(
                              new Exception("Could not parse information from the Elevation Service"), "Elevation Service",
                              "requestUri: {requestUri}");
                }

                resultList.AddRange(elevationParsed.Elevations);
            }

            progress?.Report("Assigning results to Coordinates");

            foreach (var loopResults in resultList)
            {
                coordinates.Where(x => x.X == loopResults.Location.Longitude && x.Y == loopResults.Location.Latitude)
                .ToList().ForEach(x => x.Z = loopResults.Elevation ?? 0);
            }

            return(coordinates);
        }
Пример #2
0
        public static async Task RenameSelectedFile(FileInfo selectedFile, StatusControlContext statusContext,
                                                    Action <FileInfo> setSelectedFile)
        {
            if (selectedFile == null || !selectedFile.Exists)
            {
                statusContext.ToastWarning("No file to rename?");
                return;
            }

            var newName = await statusContext.ShowStringEntry("Rename File",
                                                              $"Rename {Path.GetFileNameWithoutExtension(selectedFile.Name)} - " +
                                                              "File Names must be limited to A-Z a-z 0-9 - . _  :",
                                                              Path.GetFileNameWithoutExtension(selectedFile.Name));

            if (!newName.Item1)
            {
                return;
            }

            var cleanedName = newName.Item2.TrimNullToEmpty();

            if (string.IsNullOrWhiteSpace(cleanedName))
            {
                statusContext.ToastError("Can't rename the file to an empty string...");
                return;
            }

            var noExtensionCleaned = Path.GetFileNameWithoutExtension(cleanedName);

            if (string.IsNullOrWhiteSpace(noExtensionCleaned))
            {
                statusContext.ToastError("Not a valid filename...");
                return;
            }

            if (!FolderFileUtility.IsNoUrlEncodingNeeded(noExtensionCleaned))
            {
                statusContext.ToastError("File Names must be limited to A - Z a - z 0 - 9 - . _");
                return;
            }

            var moveToName = Path.Combine(selectedFile.Directory?.FullName ?? string.Empty,
                                          $"{noExtensionCleaned}{Path.GetExtension(selectedFile.Name)}");

            try
            {
                File.Copy(selectedFile.FullName, moveToName);
            }
            catch (Exception e)
            {
                await EventLogContext.TryWriteExceptionToLog(e, statusContext.StatusControlContextId.ToString(),
                                                             "Exception while trying to rename file.");

                statusContext.ToastError($"Error Copying File: {e.Message}");
                return;
            }

            var finalFile = new FileInfo(moveToName);

            if (!finalFile.Exists)
            {
                statusContext.ToastError("Unknown error renaming file - original file still selected.");
                return;
            }

            try
            {
                setSelectedFile(finalFile);
            }
            catch (Exception e)
            {
                statusContext.ToastError($"Error setting selected file - {e.Message}");
                return;
            }

            statusContext.ToastSuccess($"Selected file now {selectedFile.FullName}");
        }