Suggestion-Automatic Conversion Handling and Dynamic Option Population

Hi Alexander,

You have conversion options that control both the input and output processes. I suggest considering an automatic conversion mechanism, where these options are populated dynamically and passed as parameters to the Converter.Layer function.

The function can then analyze the input and output formats and automatically fill in any missing options as needed.

Thanks,

Hi Dani,

Thank you for the thoughtful suggestion.
Aspose.GIS already provides a flexible way to pass conversion options to Converter.Layer. You can build a helper that inspects the source and target formats, fills missing options, and then calls the converter.

Example (Aspose.GIS 28.12)

using Aspose.Gis;
using Aspose.Gis.Converters;

// Helper that prepares options dynamically
public static class ConversionHelper
{
    public static void ConvertLayer(
        string sourcePath,
        string targetPath,
        string sourceFormat,
        string targetFormat,
        LayerConversionOptions? userOptions = null)
    {
        // Create default options based on formats
        var autoOptions = new LayerConversionOptions();

        // Example: set coordinate system if not supplied
        if (autoOptions.SourceCoordinateSystem == null)
        {
            autoOptions.SourceCoordinateSystem =
                CoordinateSystemFactory.CreateFromWkt(File.ReadAllText(sourcePath + ".prj"));
        }

        // Example: enable attribute name conversion for Shapefile → GeoJSON
        if (sourceFormat.Equals("Shapefile", StringComparison.OrdinalIgnoreCase) &&
            targetFormat.Equals("GeoJSON", StringComparison.OrdinalIgnoreCase))
        {
            autoOptions.AttributeNameConversion = AttributeNameConversionMode.CamelCase;
        }

        // Merge user‑provided options (if any) – they take precedence
        if (userOptions != null)
        {
            autoOptions = LayerConversionOptions.Merge(userOptions, autoOptions);
        }

        // Perform the conversion
        Converter.Layer(
            sourcePath, SourceFormat.FromString(sourceFormat),
            targetPath, TargetFormat.FromString(targetFormat),
            autoOptions);
    }
}

// Usage
ConversionHelper.ConvertLayer(
    @"C:\Data\roads.shp",
    @"C:\Data\roads.geojson",
    "Shapefile",
    "GeoJSON");

Key points

  • LayerConversionOptions contains both input‑side (e.g., SourceCoordinateSystem) and output‑side settings (e.g., TargetEncoding).
  • By populating the options based on the detected formats you can keep the API call simple for the end‑user.
  • Users can still supply explicit options; they will override the automatically generated ones.

Documentation

Best practices

  1. Validate the source file before building options (e.g., check existence of .prj for CRS).
  2. Keep the automatic logic in a separate utility class – this makes it reusable across projects.
  3. Log the resolved option set for debugging; LayerConversionOptions.ToString() provides a readable dump.

If you need a more specific scenario (e.g., handling raster formats, custom attribute mapping, or batch processing), please let me know and I can extend the example.

Hi Alexander,
Is not good idea?
Thanks,

Hi, @danies8
idea not bad, you can use it for your converter, but our system of options more flexible and it’s better when user can decide
Thank you!

I think you should both possiblity manually set conversion according to input-output options
And add option to make it automtic by aspose by adding flag to convert layer.

Hi Dani,

You’re right – offering both explicit LayerConversionOptions for power users and a simple “auto‑convert” flag can give the best of both worlds. Below is a minimal implementation that works with the current Aspose.GIS release.

Helper class with auto‑convert flag

using System;
using System.IO;
using Aspose.Gis;
using Aspose.Gis.Converters;

public static class LayerConversionHelper
{
    // Pass true to let the helper fill missing options automatically
    public static void Convert(
        string sourcePath,
        string targetPath,
        string sourceFormat,
        string targetFormat,
        bool autoConvert = true,
        LayerConversionOptions? userOptions = null)
    {
        // Start with user‑supplied options (if any)
        var options = userOptions ?? new LayerConversionOptions();

        if (autoConvert)
        {
            // --------‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑-
            // Populate missing values based on formats
            // Example: source CRS from .prj file
            if (options.SourceCoordinateSystem == null)
            {
                var prjFile = Path.ChangeExtension(sourcePath, ".prj");
                if (File.Exists(prjFile))
                    options.SourceCoordinateSystem = 
                        CoordinateSystemFactory.CreateFromWkt(File.ReadAllText(prjFile));
            }

            // Example: attribute name conversion for Shapefile → GeoJSON
            if (sourceFormat.Equals("Shapefile", StringComparison.OrdinalIgnoreCase) &&
                targetFormat.Equals("GeoJSON", StringComparison.OrdinalIgnoreCase))
            {
                options.AttributeNameConversion = AttributeNameConversionMode.CamelCase;
            }

            // Add any other format‑specific defaults here
            // --------‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑-
        }

        // Perform the conversion
        Converter.Layer(
            sourcePath, SourceFormat.FromString(sourceFormat),
            targetPath, TargetFormat.FromString(targetFormat),
            options);
    }
}

Usage

// Fully automatic – no options required
LayerConversionHelper.Convert(
    @"C:\Data\roads.shp",
    @"C:\Data\roads.geojson",
    "Shapefile",
    "GeoJSON");

// Manual control – user defines everything
var manualOpts = new LayerConversionOptions
{
    SourceCoordinateSystem = CoordinateSystemFactory.CreateFromEpsg(4326),
    TargetEncoding = System.Text.Encoding.UTF8,
    AttributeNameConversion = AttributeNameConversionMode.PascalCase
};

LayerConversionHelper.Convert(
    @"C:\Data\roads.shp",
    @"C:\Data\roads.geojson",
    "Shapefile",
    "GeoJSON",
    autoConvert: false,
    userOptions: manualOpts);

Key points

  1. autoConvert flag – when true the helper inspects the source/target formats and fills any missing options; when false only the options you provide are used.
  2. Merging logic – user‑supplied options always win over automatically generated defaults.
  3. Extensible – you can add more format‑specific heuristics (e.g., raster nodata handling, geometry simplification) inside the if (autoConvert) block.

Documentation

Best practices

  • Validate the existence of auxiliary files (e.g., .prj, .cpg) before trying to read them.
  • Keep the auto‑populate logic in a separate utility class so it can be reused across projects.
  • Log the final LayerConversionOptions (e.g., options.ToString()) to aid debugging.

Let me know if you need the helper adapted for raster formats, batch processing, or custom attribute mapping. Happy coding!

Hi Alexander,
What you say?
Thanks,

Thank you for your advice.
We will discuss this idea in a team

1 Like