Skip to content

IRI.Maptor.Sta.ShapefileFormat Troubleshooting

Hossein Narimani Rad edited this page Sep 28, 2025 · 4 revisions

Troubleshooting

Common Issues

File Not Found Errors

Problem: FileNotFoundException when reading shapefile

System.IO.FileNotFoundException: Could not find file 'path/to/file.shp'

Solutions:

  • Verify the file path is correct
  • Check if the file exists using File.Exists()
  • Ensure you have read permissions for the file
  • Check for typos in the filename
// Verify file exists before reading
if (File.Exists("file.shp"))
{
    var shapes = await Shapefile.ReadShapesAsync("file.shp");
}
else
{
    Console.WriteLine("Shapefile not found!");
}

Invalid Shapefile Format

Problem: InvalidShapefileException when reading shapefile

IRI.Maptor.Sta.ShapefileFormat.InvalidShapefileException: Invalid shapefile format

Solutions:

  • Verify the file is a valid ESRI shapefile
  • Check if the file is corrupted
  • Ensure all required files (.shp, .shx, .dbf) are present
  • Try opening the file in a GIS application to verify integrity
try
{
    var shapes = await Shapefile.ReadShapesAsync("file.shp");
}
catch (InvalidShapefileException ex)
{
    Console.WriteLine($"Invalid shapefile: {ex.Message}");
    Console.WriteLine($"Validation error: {ex.ValidationMessage}");
}

Encoding Issues

Problem: Garbled text in attribute values

Attribute values appear as: "??????" or "ééé"

Solutions:

  • Check if CPG file exists and contains correct encoding
  • Verify the source data was saved with the expected encoding
  • Try different encodings manually
// Try different encodings
var encodings = new[] { Encoding.UTF8, Encoding.GetEncoding("windows-1252"), Encoding.GetEncoding("iso-8859-1") };

foreach (var encoding in encodings)
{
    try
    {
        var shapes = await Shapefile.ReadShapesWithAttributesAsync("file.shp", "file.dbf", encoding: encoding);
        Console.WriteLine($"Success with encoding: {encoding.EncodingName}");
        break;
    }
    catch (EncodingException)
    {
        Console.WriteLine($"Failed with encoding: {encoding.EncodingName}");
    }
}

Memory Issues

Problem: OutOfMemoryException when processing large files

System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown

Solutions:

  • Use streaming APIs for large files
  • Process files in smaller chunks
  • Increase available memory
  • Use 64-bit application
// Use streaming for large files
await foreach (var shape in Shapefile.ReadShapesStreamAsync("large_file.shp"))
{
    ProcessShape(shape);
}

Coordinate System Issues

Problem: Incorrect coordinate transformations

Coordinates appear in wrong location after transformation

Solutions:

  • Verify source and target coordinate systems
  • Check PRJ file content
  • Ensure coordinate system definitions are correct
  • Test with known coordinate pairs
// Validate coordinate system
var coordSys = CoordinateSystem.FromPrjFile("file.prj");
Console.WriteLine($"Coordinate system: {coordSys.Name}");
Console.WriteLine($"EPSG code: {coordSys.EpsgCode}");

// Test transformation with known points
var testPoint = new PointShape(1, 0, 0); // Greenwich
var transformed = testPoint.TransformTo(CoordinateSystem.FromEpsgCode(3857));
Console.WriteLine($"Transformed coordinates: {transformed.X}, {transformed.Y}");

Performance Issues

Slow Reading Performance

Problem: Shapefile reading is very slow

Reading a 100MB shapefile takes several minutes

Solutions:

  • Use streaming APIs
  • Process in parallel
  • Optimize I/O operations
  • Check disk performance
// Use parallel processing
var shapes = await Shapefile.ReadShapesAsync("file.shp");
var processedShapes = shapes.AsParallel()
    .Select(ProcessShape)
    .ToList();

High Memory Usage

Problem: Application uses too much memory

Memory usage grows rapidly when processing shapefiles

Solutions:

  • Use streaming APIs
  • Dispose resources properly
  • Process files in batches
  • Monitor memory usage
// Process in batches
const int batchSize = 1000;
var batch = new List<ShapefileShape>();

await foreach (var shape in Shapefile.ReadShapesStreamAsync("file.shp"))
{
    batch.Add(shape);
    
    if (batch.Count >= batchSize)
    {
        ProcessBatch(batch);
        batch.Clear();
    }
}

// Process remaining shapes
if (batch.Any())
{
    ProcessBatch(batch);
}

Debugging Tips

Enable Detailed Logging

// Enable debug logging
Shapefile.EnableDebugLogging = true;

// Log all operations
Shapefile.LogOperation = (operation, details) => 
{
    Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] {operation}: {details}");
};

Validate Shapefile Structure

// Get shapefile information
var info = await Shapefile.GetShapefileInfoAsync("file.shp");
Console.WriteLine($"Shape count: {info.ShapeCount}");
Console.WriteLine($"Shape type: {info.ShapeType}");
Console.WriteLine($"Bounding box: {info.BoundingBox}");
Console.WriteLine($"Encoding: {info.Encoding.EncodingName}");

Test with Sample Data

// Create test shapefile
var testShapes = new List<ShapefileShape>
{
    new PointShape(1, 0, 0),
    new PointShape(2, 1, 1)
};

await Shapefile.WriteShapesAsync(testShapes, "test.shp");

// Read it back to verify
var readShapes = await Shapefile.ReadShapesAsync("test.shp");
Console.WriteLine($"Read back {readShapes.Count()} shapes");

Getting Help

Check Documentation

Report Issues

  • Create an issue on GitHub with:
    • Error message and stack trace
    • Sample data (if possible)
    • Steps to reproduce
    • Environment details (.NET version, OS, etc.)

Community Support

  • Check existing GitHub issues
  • Ask questions in discussions
  • Review sample code in the repository

For more help, see the Usage Guide or create an issue on GitHub

Maptor Wiki

🏠 Home

πŸ“š Library Groups

πŸ“¦ IRI.Maptor.Sta

πŸ“ IRI.Maptor.Sta.ShapefileFormat

πŸ”§ IRI.Maptor.Ket

Coming Soon

πŸ–₯️ IRI.Maptor.Jab

Coming Soon

πŸš€ Getting Started

πŸ”— External Resources


Maptor Spatial Library Ecosystem

Clone this wiki locally