Skip to content

armgit / Filee

Public Imported from GitHub
0

Free, open-source file converter. Convert 263 formats offline by drag & drop — images, PDF, Word, Excel, PowerPoint, HWP/HWPX, video, audio, e-books and archives. No upload, no Office needed.

Geospatial: review follow-ups

KnifeLemon6289b4fefbf2
diff --git a/src/Filee.App/ViewModels/PresetEditorViewModel.cs b/src/Filee.App/ViewModels/PresetEditorViewModel.cs
index 43343414..a05bb020 100644
--- a/src/Filee.App/ViewModels/PresetEditorViewModel.cs
+++ b/src/Filee.App/ViewModels/PresetEditorViewModel.cs
@@ -547,7 +547,8 @@ public sealed partial class PresetEditorViewModel : ObservableObject
output.FileNamePattern = string.IsNullOrWhiteSpace(Pattern) ? "{name}" : Pattern.Trim();
output.Conflict = Conflict.Value;
output.KeepDates = KeepDates;
- output.ZipResult = ZipResult;
+ // Only for geospatial targets, where it is offered: a box ticked for Shapefile must not zip a later PNG.
+ output.ZipResult = ShowZipResult && ZipResult;
return true;
}
diff --git a/src/Filee.Engines/Geo/KmlFormat.cs b/src/Filee.Engines/Geo/KmlFormat.cs
index 812f336d..6eaf51d1 100644
--- a/src/Filee.Engines/Geo/KmlFormat.cs
+++ b/src/Filee.Engines/Geo/KmlFormat.cs
@@ -180,23 +180,23 @@ internal static class KmlFormat
if (!text.Contains(',', StringComparison.Ordinal))
return text;
var builder = new System.Text.StringBuilder(text.Length);
- var space = ''; // white space seen since the last character: kept unless a comma is next to it
+ var space = '\0'; // white space seen since the last character: kept unless a comma is next to it
var afterComma = false;
foreach (var ch in text)
{
if (char.IsWhiteSpace(ch))
{
- if (!afterComma && space == '')
+ if (!afterComma && space == '\0')
space = ch;
continue;
}
- if (ch != ',' && space != '')
+ if (ch != ',' && space != '\0')
builder.Append(space);
- space = '';
+ space = '\0';
builder.Append(ch);
afterComma = ch == ',';
}
- if (space != '')
+ if (space != '\0')
builder.Append(space);
return builder.ToString();
}
diff --git a/src/Filee.Engines/Geo/ShapefileFormat.cs b/src/Filee.Engines/Geo/ShapefileFormat.cs
index c85816c7..2b83126e 100644
--- a/src/Filee.Engines/Geo/ShapefileFormat.cs
+++ b/src/Filee.Engines/Geo/ShapefileFormat.cs
@@ -182,6 +182,8 @@ internal static class ShapefileFormat
var lines = new List<List<Coord>>(parts);
for (var p = 0; p < parts; p++)
{
+ if (p % 1024 == 1023)
+ cancellationToken.ThrowIfCancellationRequested();
var line = new List<Coord>(starts[p + 1] - starts[p]);
for (var i = starts[p]; i < starts[p + 1]; i++)
{
@@ -215,9 +217,11 @@ internal static class ShapefileFormat
internal static GeoGeometry? Polygons(List<List<Coord>> lines, CancellationToken cancellationToken)
{
var rings = new List<(IReadOnlyList<Coord> Ring, double Area)>();
- foreach (var line in lines)
+ for (var l = 0; l < lines.Count; l++)
{
- if (GeoRings.Close(line) is { } ring && GeoRings.SignedArea(ring) is var area && area != 0)
+ if (l % 1024 == 1023)
+ cancellationToken.ThrowIfCancellationRequested();
+ if (GeoRings.Close(lines[l]) is { } ring && GeoRings.SignedArea(ring) is var area && area != 0)
rings.Add((ring, area));
}
if (rings.Count == 0)
@@ -230,7 +234,14 @@ internal static class ShapefileFormat
outers = holes;
holes = [];
}
- var bounds = outers.Select(o => Bounds(o.Ring)).ToArray();
+ var bounds = new (double MinX, double MinY, double MaxX, double MaxY)[outers.Count];
+ for (var i = 0; i < outers.Count; i++)
+ {
+ if (i % 1024 == 1023)
+ cancellationToken.ThrowIfCancellationRequested();
+ bounds[i] = Bounds(outers[i].Ring);
+ }
+ cancellationToken.ThrowIfCancellationRequested();
var smallestFirst = Enumerable.Range(0, outers.Count).OrderBy(i => Math.Abs(outers[i].Area)).ToArray();
var assigned = outers.Select(o => new List<IReadOnlyList<Coord>> { o.Ring }).ToList();
var holeCount = 0;
@@ -258,8 +269,18 @@ internal static class ShapefileFormat
return polygons.Count == 1 ? polygons[0] : new GeoMultiPolygon(polygons);
}
- private static (double MinX, double MinY, double MaxX, double MaxY) Bounds(IReadOnlyList<Coord> ring) =>
- (ring.Min(p => p.X), ring.Min(p => p.Y), ring.Max(p => p.X), ring.Max(p => p.Y));
+ private static (double MinX, double MinY, double MaxX, double MaxY) Bounds(IReadOnlyList<Coord> ring)
+ {
+ var (minX, minY, maxX, maxY) = (double.MaxValue, double.MaxValue, double.MinValue, double.MinValue);
+ foreach (var p in ring)
+ {
+ minX = Math.Min(minX, p.X);
+ minY = Math.Min(minY, p.Y);
+ maxX = Math.Max(maxX, p.X);
+ maxY = Math.Max(maxY, p.Y);
+ }
+ return (minX, minY, maxX, maxY);
+ }
private static double Double(ReadOnlySpan<byte> c, int at) => BinaryPrimitives.ReadDoubleLittleEndian(c[at..]);
diff --git a/tests/Filee.App.Tests/RenderTests.cs b/tests/Filee.App.Tests/RenderTests.cs
index d70816f7..d90d9fea 100644
--- a/tests/Filee.App.Tests/RenderTests.cs
+++ b/tests/Filee.App.Tests/RenderTests.cs
@@ -575,6 +575,42 @@ public class RenderTests
}
}
+ /// <summary>The geospatial donut opens for Shapefiles with its six slices, rounded by the softness like the others.</summary>
+ [AvaloniaFact]
+ public void Geospatial_donut_opens_for_shapefiles_with_rounded_slices()
+ {
+ TestServices.EnsureInitialized("en");
+ var theme = AppHost.Get<ThemeService>();
+ try
+ {
+ theme.Apply(new ThemeSettings { Mode = ThemeMode.Light, CornerRadius = 28 }, "en");
+ var vm = AppHost.Get<RadialViewModel>();
+ vm.Load([@"C:\gis\roads.shp", @"C:\gis\parcels.shp"], clickMode: true);
+ vm.FitToThisDonut();
+ Assert.Equal(["to-geojson", "to-shp", "to-kml", "to-csv", "to-gpx", "to-kmz"], vm.Presets.Select(p => p.Id));
+ Assert.All(vm.Items, item => Assert.True(item.IsEnabled));
+ var window = new RadialWindow { DataContext = vm };
+ window.Show();
+ var donut = window.DonutControl;
+ donut.SetExternalHighlight(1);
+ Pump();
+ Assert.Equal(14, donut.SliceCornerRadius);
+
+ // A corner the rounding cut away still picks its slice.
+ var n = vm.Items.Count;
+ var center = new Avalonia.Point(donut.Bounds.Width / 2, donut.Bounds.Height / 2);
+ var inner = vm.OuterRadius * vm.HoleRatio;
+ var corner = DonutGeometry.Wedge(center, inner, vm.OuterRadius, DonutGeometry.MidAngle(4, n), DonutGeometry.Span(n), 3).Start;
+ Assert.Equal(new DonutHit(DonutHitKind.Segment, 4), donut.HitTest(center + (corner - center) * 0.995));
+ Save(window, "donut-geospatial-soft.png");
+ window.Close();
+ }
+ finally
+ {
+ theme.Apply(new ThemeSettings(), "en");
+ }
+ }
+
/// <summary>The middle of slot <paramref name="slot"/> of a donut with <paramref name="slots"/> slices.</summary>
private static Avalonia.Point DonutPoint(DonutMenu donut, int slot, int slots)
{
@@ -1313,7 +1349,9 @@ public class RenderTests
Assert.True(vm.ShowZipResult);
vm.Target = vm.Targets.Single(t => t.Value == "png");
Assert.False(vm.ShowZipResult);
- Assert.False(vm.ShowPdf && vm.Target.Value == "shp");
+ // The box ticked for Shapefile, hidden for PNG, doesn't zip the PNG.
+ Assert.True(vm.Apply());
+ Assert.False(preset.Output.ZipResult);
}
[AvaloniaFact]
diff --git a/tests/Filee.Core.Tests/ProfileResetTests.cs b/tests/Filee.Core.Tests/ProfileResetTests.cs
index 248c7ac5..6d69e2ec 100644
--- a/tests/Filee.Core.Tests/ProfileResetTests.cs
+++ b/tests/Filee.Core.Tests/ProfileResetTests.cs
@@ -115,6 +115,32 @@ public class ProfileResetTests
Assert.Contains("to-tiff", NewStore(dir).Profiles.Single(p => p.Id == "images").PresetIds);
}
+ [Fact]
+ public void The_geospatial_donut_resets_like_the_other_built_in_ones()
+ {
+ using var dir = new TempDir();
+ var store = NewStore(dir);
+ var geo = Profile(store, "geo");
+ geo.Name = "Maps";
+ geo.PresetIds = ["to-kml"];
+ geo.Extensions.Remove("kml");
+ store.Profiles.Insert(0, new ToolbarProfile { Id = "earth", Name = "Earth", Extensions = ["kml"], PresetIds = ["to-geojson"] });
+ store.Presets.RemoveAll(p => p.Id == "to-kmz");
+ store.SaveLibrary();
+
+ Assert.True(store.CanResetProfile(geo));
+ Assert.True(store.ResetProfile(geo));
+
+ var builtIn = BuiltIn("geo");
+ Assert.Equal("", geo.Name);
+ Assert.Equal("profile.geo", geo.NameKey);
+ Assert.Equal(builtIn.PresetIds, geo.PresetIds);
+ Assert.Equal(builtIn.Extensions.Where(e => e != "kml"), geo.Extensions); // .kml stays on the user's donut
+ Assert.Equal(["kml"], Profile(store, "earth").Extensions);
+ Assert.Equal("kmz", store.FindPreset("to-kmz")!.TargetFormat); // the deleted slice's preset is back
+ Assert.All(geo.PresetIds, id => Assert.NotNull(store.FindPreset(id)));
+ }
+
[Fact]
public void A_profile_the_user_made_has_no_default_to_go_back_to()
{