GisConverter-BuildOutputPath keeps original name

Dani_S 5,581 Reputation points
2026-01-07T09:09:29.7033333+00:00

1.Current code:

The BuildOutputPath give baseName="output" as file name

for all converted files in output folder.

For example of converting archive file to output folder:

Output.cpg

Output.dbf

Output.shp

Output.shx

3.Keep original names in archive and single files input..

   internal static string? BuildOutputPath(string outputFolderPath, string gisTargetFormatOption)
   {

       var maybeOutFileExt = FileExtensionHelpers.FromOption(gisTargetFormatOption);
       if (maybeOutFileExt == null)
       {
           Log.Error($"BuildOutputPath: unknown target format '{gisTargetFormatOption}'. Cannot determine output file extension.");
           return null;
       }

       var extDot = FileExtensionHelpers.ToDotExtension(maybeOutFileExt.Value);

       var baseName = $"output";
       var candidate = Path.Combine(outputFolderPath, baseName + extDot);

       Log.Info($"BuildOutputPath: target output file will be '{candidate}'.");
       return candidate;

4.Can you please fix it, add all needed effected files, and effected tests.

Thanks in advance,

Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
{count} votes

Answer accepted by question author
  1. Michael Le (WICLOUD CORPORATION) 10,555 Reputation points Microsoft External Staff Moderator
    2026-01-07T10:06:30.66+00:00

    Hello @Dani_S ,

    You could modify your BuildOutputPath() method to include the original input file path, extract the file name and use it to create the output path.

    ConverterUtils.cs:

    internal static string? BuildOutputPath(string outputFolderPath, string originalInputFilePath, string gisTargetFormatOption)
    {
        var maybeOutFileExt = FileExtensionHelpers.FromOption(gisTargetFormatOption);
        if (maybeOutFileExt == null)
        {
            Log.Error($"BuildOutputPath: unknown target format '{gisTargetFormatOption}'. Cannot determine output file extension.");
            return null;
        }
    
        var extDot = FileExtensionHelpers.ToDotExtension(maybeOutFileExt.Value);
    
        // Extract base name from original input file path (without extension)
        var baseName = Path.GetFileNameWithoutExtension(originalInputFilePath);
    
        // Fallback to "output" if base name is empty or contains only whitespace
        if (string.IsNullOrWhiteSpace(baseName))
        {
            Log.Warn("BuildOutputPath: original input filename is empty or whitespace, using 'output' as fallback.");
            baseName = "output";
        }
    
        var candidate = Path.Combine(outputFolderPath, baseName + extDot);
    
        Log.Info($"BuildOutputPath: target output file will be '{candidate}'.");
        return candidate;
    
    }
    

    Find and replace the existing call to BuildOutputPath() in UniversalGisConverter.cs with this updated signature:

    var outputPath = ConverterUtils.BuildOutputPath(outputFolderPath, gisInputFilePath, gisTargetFormatOption);
    

    And add the tests accordingly in ConverterUtilsTests.cs:

    [Theory(DisplayName = "BuildOutputPath_PreservesOriginalFilename")]
    [InlineData("Cities.geojson", "Shapefile", "Cities.shp")]
    [InlineData("MyData.zip", "GeoJson", "MyData.json")]
    [InlineData("Roads.shp", "Kml", "Roads.kml")]
    [InlineData("Points.csv", "GeoPackage", "Points.gpkg")]
    [InlineData("/path/to/Districts.gml", "EsriJson", "Districts.json")]
    [InlineData("C:\\temp\\Boundaries.gpx", "Shapefile", "Boundaries.shp")]
    public void BuildOutputPath_PreservesOriginalFilename(string inputPath, string targetFormat, string expectedFilename)
    {
        var outputFolder = Path.Combine(_root, "output");
        Directory.CreateDirectory(outputFolder);
    
        var result = ConverterUtils.BuildOutputPath(outputFolder, inputPath, targetFormat);
    
        Assert.NotNull(result);
        Assert.Equal(Path.Combine(outputFolder, expectedFilename), result);
    }
    
    [Theory(DisplayName = "BuildOutputPath_FallsBackToOutput_WhenFilenameEmpty")]
    [InlineData("", "GeoJson")]
    [InlineData("   ", "Shapefile")]
    [InlineData("\t", "Kml")]
    public void BuildOutputPath_FallsBackToOutput_WhenFilenameEmpty(string inputPath, string targetFormat)
    {
        var outputFolder = Path.Combine(_root, "output");
        Directory.CreateDirectory(outputFolder);
    
        var result = ConverterUtils.BuildOutputPath(outputFolder, inputPath, targetFormat);
    
        Assert.NotNull(result);
        Assert.Contains("output", Path.GetFileName(result));
    }
    
    [Fact(DisplayName = "BuildOutputPath_ReturnsNull_ForUnknownFormat")]
    public void BuildOutputPath_ReturnsNull_ForUnknownFormat()
    {
        var outputFolder = Path.Combine(_root, "output");
        Directory.CreateDirectory(outputFolder);
    
        var result = ConverterUtils.BuildOutputPath(outputFolder, "test.geojson", "UnknownFormat");
    
        Assert.Null(result);
    }
    
    [Theory(DisplayName = "BuildOutputPath_HandlesMultipleExtensions")]
    [InlineData("archive.tar.gz", "GeoJson", "archive.tar.json")]
    [InlineData("data.backup.shp", "Kml", "data.backup.kml")]
    public void BuildOutputPath_HandlesMultipleExtensions(string inputPath, string targetFormat, string expectedFilename)
    {
        var outputFolder = Path.Combine(_root, "output");
        Directory.CreateDirectory(outputFolder);
    
        var result = ConverterUtils.BuildOutputPath(outputFolder, inputPath, targetFormat);
    
        Assert.NotNull(result);
        Assert.Equal(Path.Combine(outputFolder, expectedFilename), result);
    }
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.