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.
C# audio-converteravaloniabatch-conversionconvertercsharpdocument-converterdocx-to-pdfdotnetdrag-and-dropfile-conversionfile-converterheic-converterhwphwpximage-converterlinuxmacospdf-convertervideo-converterwindows
Pack a Shapefile with its companion files; count a dropped set as one file
KAPPI19253fc26a1f10b3
diff --git a/src/Filee.App/ViewModels/RadialViewModel.cs b/src/Filee.App/ViewModels/RadialViewModel.csindex 9e6705cc..5ea0f6b4 100644--- a/src/Filee.App/ViewModels/RadialViewModel.cs+++ b/src/Filee.App/ViewModels/RadialViewModel.cs@@ -151,8 +151,11 @@ public sealed partial class RadialViewModel(UserDataStore store, ILocalizer loc,HitBackground = "Transparent";ApplyAppearance();- var profile = ProfileSelector.Select(store.Profiles, files);- var formats = ProfileSelector.DetectFormats(files);+ // A Shapefile selected with its .dbf, .shx, ... picks the donut of the .shp, not the one for unknown files, and+ // counts as one file.+ var counted = CompanionFiles.WithoutCompanions(files);+ var profile = ProfileSelector.Select(store.Profiles, counted);+ var formats = ProfileSelector.DetectFormats(counted);// Both faces follow the same rule: a slice that can't convert these files is greyed out and says why._front = BuildFace(profile?.PresetIds ?? [], formats);(_back, _backIds, _formats) = (null, [.. profile?.BackPresetIds ?? []], formats);@@ -174,8 +177,8 @@ public sealed partial class RadialViewModel(UserDataStore store, ILocalizer loc,}CenterTitle = formats.Count == 1- ? loc.Format("donut.files_same", files.Count, formats[0].DisplayName)- : loc.Format("donut.files_mixed", files.Count);+ ? loc.Format("donut.files_same", counted.Count, formats[0].DisplayName)+ : loc.Format("donut.files_mixed", counted.Count);CenterSubtitle = clickMode ? loc["donut.close_hint"] : null;}diff --git a/src/Filee.Core/Conversion/ConversionJob.cs b/src/Filee.Core/Conversion/ConversionJob.csindex df78b629..fda025c4 100644--- a/src/Filee.Core/Conversion/ConversionJob.cs+++ b/src/Filee.Core/Conversion/ConversionJob.cs@@ -66,6 +66,8 @@ public sealed class ConversionJob/// <param name="fetcher">Saves sources that aren't files yet before they are converted (files from a browser).</param>public ConversionJob(IReadOnlyList<string> sources, Preset preset, string presetDisplayName, ISourceFetcher? fetcher = null){+ // A Shapefile's .dbf, .shx, .prj, ... dropped with its .shp are part of it, not files to convert on their own.+ sources = Formats.CompanionFiles.WithoutCompanions(sources);Preset = preset.Clone(); // later edits of the preset must not affect a running jobPresetDisplayName = presetDisplayName;Fetcher = fetcher;diff --git a/src/Filee.Core/Conversion/JobQueue.cs b/src/Filee.Core/Conversion/JobQueue.csindex 9cf06228..829d08d4 100644--- a/src/Filee.Core/Conversion/JobQueue.cs+++ b/src/Filee.Core/Conversion/JobQueue.cs@@ -391,8 +391,18 @@ public sealed class JobQueue : IAsyncDisposabletry{- await _combiner!.CombineAsync(inputs.Select(f => f.SourcePath).ToList(), output, format.Id, job.Preset,- workDir, progress, job.CancellationToken);+ // A Shapefile goes into the archive with its companion files (.shx, .dbf, .prj, ...): the .shp alone is no map.+ var files = new List<string>();+ var packed = new HashSet<string>(StringComparer.OrdinalIgnoreCase);+ foreach (var source in inputs.Select(f => f.SourcePath))+ {+ foreach (var file in new[] { source }.Concat(CompanionFiles.Of(source)))+ {+ if (packed.Add(Path.GetFullPath(file)))+ files.Add(file);+ }+ }+ await _combiner!.CombineAsync(files, output, format.Id, job.Preset, workDir, progress, job.CancellationToken);inputs[0].Outputs.Add(output);foreach (var f in inputs){diff --git a/src/Filee.Core/Formats/CompanionFiles.cs b/src/Filee.Core/Formats/CompanionFiles.csnew file mode 100644index 00000000..48db1bdf--- /dev/null+++ b/src/Filee.Core/Formats/CompanionFiles.cs@@ -0,0 +1,87 @@+// Files that only work next to another file: a Shapefile is a .shp with its .shx, .dbf, .prj, .cpg and index and+// metadata files of the same name. They travel with the .shp (packed into an archive with it) and, when they are dropped+// together with it, they are part of it rather than files of their own (no donut for unknown files, no failures).++namespace Filee.Core.Formats;++/// <summary>Companion files of formats that are several files on disk.</summary>+public static class CompanionFiles+{+ /// <summary>+ /// What may follow a Shapefile's name ("roads" + "." + part), in the order they are listed: the parts the format+ /// needs, the code page, ESRI's and QGIS's spatial indexes and metadata.+ /// </summary>+ private static readonly string[] ShapefileParts =+ ["shx", "dbf", "prj", "cpg", "sbn", "sbx", "fbn", "fbx", "ain", "aih", "atx", "ixs", "mxs", "qix", "qmd", "qpj", "shp.xml"];++ /// <summary>The companion files of <paramref name="path"/> that exist next to it; none for a file of another format.</summary>+ public static IReadOnlyList<string> Of(string path)+ {+ if (!IsLocal(path) || !IsShapefile(path))+ return [];+ try+ {+ var full = Path.GetFullPath(path);+ var directory = Path.GetDirectoryName(full);+ var name = Path.GetFileNameWithoutExtension(full);+ if (directory is null || !Directory.Exists(directory))+ return [];+ // Any letter case: "ROADS.DBF" next to "roads.shp" on a case-sensitive file system.+ return Directory.EnumerateFiles(directory, name + ".*")+ .Select(file => (File: file, Part: PartOf(Path.GetFileName(file), name)))+ .Where(f => f.Part >= 0)+ .OrderBy(f => f.Part)+ .Select(f => f.File)+ .ToList();+ }+ catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or ArgumentException)+ {+ return [];+ }+ }++ /// <summary>+ /// <paramref name="paths"/> without the files that are companions of a Shapefile also in the list (a .dbf selected+ /// together with its .shp). A companion on its own stays: it is a file like any other.+ /// </summary>+ public static IReadOnlyList<string> WithoutCompanions(IReadOnlyList<string> paths)+ {+ // Only files on disk: a source that is still a web address (a file dragged from a browser) is left as it is.+ var shapefiles = paths.Where(p => IsLocal(p) && IsShapefile(p))+ .Select(p => Key(Path.GetDirectoryName(Path.GetFullPath(p)) ?? "", Path.GetFileNameWithoutExtension(p)))+ .ToHashSet(StringComparer.OrdinalIgnoreCase);+ if (shapefiles.Count == 0)+ return paths;+ return paths.Where(p => !IsCompanionOfOneOf(p, shapefiles)).ToList();+ }++ private static bool IsLocal(string path) => !path.Contains("://", StringComparison.Ordinal) && Path.IsPathFullyQualified(path);++ private static bool IsCompanionOfOneOf(string path, HashSet<string> shapefiles)+ {+ if (!IsLocal(path) || IsShapefile(path))+ return false;+ var full = Path.GetFullPath(path);+ var directory = Path.GetDirectoryName(full) ?? "";+ var fileName = Path.GetFileName(full);+ // "roads.dbf" belongs to "roads"; "roads.shp.xml" also to "roads".+ foreach (var part in ShapefileParts)+ {+ if (fileName.Length > part.Length + 1 && fileName.EndsWith("." + part, StringComparison.OrdinalIgnoreCase)+ && shapefiles.Contains(Key(directory, fileName[..^(part.Length + 1)])))+ return true;+ }+ return false;+ }++ private static int PartOf(string fileName, string name)+ {+ if (fileName.Length <= name.Length + 1 || !fileName.StartsWith(name + ".", StringComparison.OrdinalIgnoreCase))+ return -1;+ return Array.FindIndex(ShapefileParts, part => part.Equals(fileName[(name.Length + 1)..], StringComparison.OrdinalIgnoreCase));+ }++ private static bool IsShapefile(string path) => Path.GetExtension(path).Equals(".shp", StringComparison.OrdinalIgnoreCase);++ private static string Key(string directory, string name) => directory + Path.DirectorySeparatorChar + name;+}diff --git a/tests/Filee.Core.Tests/CompanionFilesTests.cs b/tests/Filee.Core.Tests/CompanionFilesTests.csnew file mode 100644index 00000000..7035673e--- /dev/null+++ b/tests/Filee.Core.Tests/CompanionFilesTests.cs@@ -0,0 +1,72 @@+// A Shapefile's companion files: found next to the .shp in any letter case and in a fixed order, ignored for other+// formats, and taken out of a selection that also holds their .shp (so they don't count as files of their own).++using Filee.Core.Conversion;+using Filee.Core.Formats;+using Filee.Core.Presets;+using Filee.Core.Profiles;++namespace Filee.Core.Tests;++public class CompanionFilesTests+{+ [Fact]+ public void A_shapefile_has_the_files_of_its_name_that_belong_to_it_in_a_fixed_order()+ {+ using var dir = new TempDir();+ var shp = dir.File("roads.shp");+ foreach (var name in new[] { "roads.prj", "roads.DBF", "roads.shx", "roads.cpg", "roads.sbn", "roads.sbx", "roads.shp.xml", "roads.qix" })+ dir.File(name);+ // Not companions: another dataset, a name that only starts the same, an unrelated extension, a file of the name.+ foreach (var name in new[] { "roads2.dbf", "roads.old.dbf", "roads.txt", "roads.zip", "roadsdbf" })+ dir.File(name);++ var companions = CompanionFiles.Of(shp).Select(f => Path.GetFileName(f)!).ToArray();++ Assert.Equal(["roads.shx", "roads.DBF", "roads.prj", "roads.cpg", "roads.sbn", "roads.sbx", "roads.qix", "roads.shp.xml"], companions);+ }++ [Fact]+ public void Other_formats_and_missing_folders_have_no_companions()+ {+ using var dir = new TempDir();+ dir.File("roads.dbf");+ Assert.Empty(CompanionFiles.Of(dir.File("roads.geojson")));+ Assert.Empty(CompanionFiles.Of(Path.Combine(dir.Path, "missing", "roads.shp")));+ Assert.Empty(CompanionFiles.Of(dir.File("lonely.shp")));+ }++ [Fact]+ public void Companions_selected_with_their_shapefile_are_taken_out_and_alone_they_stay()+ {+ var folder = Path.Combine(Path.GetTempPath(), "x");+ string F(string name) => Path.Combine(folder, name);+ string[] selection = [F("roads.shp"), F("ROADS.DBF"), F("roads.shx"), F("roads.prj"), F("roads.shp.xml"), F("rivers.dbf"), F("photo.jpg")];++ Assert.Equal([F("roads.shp"), F("rivers.dbf"), F("photo.jpg")], CompanionFiles.WithoutCompanions(selection));+ Assert.Equal([F("roads.dbf")], CompanionFiles.WithoutCompanions([F("roads.dbf")])); // no .shp: a file like any other+ // Web addresses (files still in a browser) are not files on disk: left as they are.+ string[] web = [F("roads.shp"), "https://example.com/data/roads.dbf", "https://example.com/data/roads.shp"];+ Assert.Equal(web, CompanionFiles.WithoutCompanions(web));+ Assert.Empty(CompanionFiles.Of("https://example.com/data/roads.shp"));+ // The same name in another folder is another dataset.+ string[] two = [F("roads.shp"), Path.Combine(folder, "sub", "roads.dbf")];+ Assert.Equal(two, CompanionFiles.WithoutCompanions(two));+ }++ [Fact]+ public void A_job_and_the_donut_count_a_dropped_shapefile_set_as_one_file()+ {+ using var dir = new TempDir();+ string[] dropped = [dir.File("roads.shp"), dir.File("roads.dbf"), dir.File("roads.shx"), dir.File("roads.prj")];++ var job = new ConversionJob(dropped, new Preset { TargetFormat = "geojson" }, "GeoJSON");+ Assert.Equal([dropped[0]], job.Sources);+ Assert.Single(job.Files);++ // Without the companions the selection is a .shp, so the geospatial donut opens, not the one for mixed files.+ var profiles = BuiltInData.CreateProfiles();+ Assert.Equal("mixed", ProfileSelector.Select(profiles, dropped)!.Id);+ Assert.Equal("geo", ProfileSelector.Select(profiles, CompanionFiles.WithoutCompanions(dropped))!.Id);+ }+}diff --git a/tests/Filee.Engines.Tests/GeoCompanionTests.cs b/tests/Filee.Engines.Tests/GeoCompanionTests.csnew file mode 100644index 00000000..b41cb7e1--- /dev/null+++ b/tests/Filee.Engines.Tests/GeoCompanionTests.cs@@ -0,0 +1,101 @@+// A Shapefile dragged on its own or with its companion files: "Compress into one archive" packs the whole set, a+// conversion of the set converts the .shp once without failing on the .dbf, .shx, ..., and the geospatial donut has the+// archive slice.++using System.IO.Compression;+using Filee.Core.Conversion;+using Filee.Core.Presets;++namespace Filee.Engines.Tests;++public class GeoCompanionTests(EngineFixture fx) : IClassFixture<EngineFixture>+{+ private static Preset ZipAll => BuiltInData.CreatePresets().Single(p => p.Id == "zip-all");++ /// <summary>The GDAL reference Shapefile with ESRI's index and metadata files beside it, as ArcGIS leaves them.</summary>+ private string ShapefileSet()+ {+ var shp = GeoFixtures.Copy(fx, "gdal_points", "shp");+ var folder = Path.GetDirectoryName(shp)!;+ File.WriteAllText(Path.Combine(folder, "gdal_points.sbn"), "index");+ File.WriteAllText(Path.Combine(folder, "gdal_points.sbx"), "index");+ File.WriteAllText(Path.Combine(folder, "gdal_points.shp.xml"), "<metadata/>");+ File.WriteAllText(Path.Combine(folder, "other.dbf"), "not part of it");+ return shp;+ }++ private static string[] Entries(string zip)+ {+ using var archive = ZipFile.OpenRead(zip);+ return [.. archive.Entries.Select(e => e.FullName).Order(StringComparer.Ordinal)];+ }++ private static readonly string[] Expected =+ [+ "gdal_points.cpg", "gdal_points.dbf", "gdal_points.prj", "gdal_points.sbn", "gdal_points.sbx", "gdal_points.shp",+ "gdal_points.shp.xml", "gdal_points.shx",+ ];++ [Fact]+ public async Task Compressing_a_dragged_shp_packs_its_companion_files_with_it()+ {+ var shp = ShapefileSet();+ var job = await fx.ConvertAsync([shp], ZipAll);++ Assert.Equal(JobState.Completed, job.State);+ Assert.Equal(Expected, Entries(job.Outputs.Single()));+ }++ [Fact]+ public async Task Compressing_a_shp_selected_with_its_companions_packs_each_file_once()+ {+ var shp = ShapefileSet();+ var folder = Path.GetDirectoryName(shp)!;+ string[] selection = [shp, .. new[] { "dbf", "shx", "prj", "cpg" }.Select(e => Path.Combine(folder, "gdal_points." + e))];++ var job = await fx.ConvertAsync(selection, ZipAll);++ Assert.Equal(JobState.Completed, job.State);+ Assert.Single(job.Files); // the companions are part of the .shp+ Assert.Equal(Expected, Entries(job.Outputs.Single()));+ }++ [Fact]+ public async Task Two_shapefiles_and_another_file_go_into_one_archive_with_all_their_parts()+ {+ var a = ShapefileSet();+ var b = GeoFixtures.Copy(fx, "gdal_polygons", "shp");+ var note = GeoFixtures.WriteText(fx, "readme.txt", "two layers");++ var job = await fx.ConvertAsync([a, b, note], ZipAll);++ Assert.Equal(JobState.Completed, job.State);+ var entries = Entries(job.Outputs.Single());+ Assert.Equal(Expected.Length + 5 + 1, entries.Length);+ Assert.Contains("gdal_polygons.dbf", entries);+ Assert.Contains("readme.txt", entries);+ Assert.DoesNotContain("other.dbf", entries);+ }++ [Fact]+ public async Task Converting_a_shp_selected_with_its_companions_converts_it_once_without_failures()+ {+ var shp = ShapefileSet();+ var folder = Path.GetDirectoryName(shp)!;+ string[] selection = [shp, .. new[] { "dbf", "shx", "prj", "cpg", "sbn", "shp.xml" }.Select(e => Path.Combine(folder, "gdal_points." + e))];++ var job = await fx.ConvertAsync(selection, new Preset { TargetFormat = "geojson" });++ Assert.Equal(JobState.Completed, job.State);+ var file = Assert.Single(job.Files);+ Assert.Equal(FileState.Done, file.State);+ using var doc = GeoFixtures.ReadJson(file.Outputs.Single());+ Assert.Equal(3, doc.RootElement.GetProperty("features").GetArrayLength());+ }++ [Fact]+ public void The_geospatial_donut_offers_compress_into_one_archive()+ {+ Assert.Contains("zip-all", BuiltInData.GeoProfile().BackPresetIds);+ }+}