Repository navigation
IRI.Maptor.Sta.ShapefileFormat Troubleshooting
Hossein Narimani Rad edited this page Sep 28, 2025
·
4 revisions
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!");
}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}");
}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}");
}
}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);
}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}");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();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);
}// Enable debug logging
Shapefile.EnableDebugLogging = true;
// Log all operations
Shapefile.LogOperation = (operation, details) =>
{
Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] {operation}: {details}");
};// 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}");// 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");- Review the API Reference
- See Usage Guide for examples
- Check Examples for common patterns
- Create an issue on GitHub with:
- Error message and stack trace
- Sample data (if possible)
- Steps to reproduce
- Environment details (.NET version, OS, etc.)
- 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
π Home
π IRI.Maptor.Sta.ShapefileFormat
Coming Soon
Coming Soon
Maptor Spatial Library Ecosystem