Skip to content

IRI.Maptor.Sta.ShapefileFormat Usage Guide

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

Usage Guide

Basic Concepts

The IRI.Maptor.Sta.ShapefileFormat library provides comprehensive support for reading, writing, and manipulating ESRI Shapefile formats.

Reading Shapefiles

Simple Reading

using IRI.Maptor.Sta.ShapefileFormat;

// Read all shapes from a shapefile
var shapes = await Shapefile.ReadShapesAsync("path/to/your/file.shp");

foreach (var shape in shapes)
{
    Console.WriteLine($"Shape ID: {shape.RecordNumber}");
    Console.WriteLine($"Geometry: {shape.AsSqlServerWkt()}");
}

Reading with Attributes

// Read shapes with attribute data
var shapesWithAttributes = await Shapefile.ReadShapesWithAttributesAsync("path/to/your/file.shp");

foreach (var shape in shapesWithAttributes)
{
    Console.WriteLine($"Shape ID: {shape.RecordNumber}");
    Console.WriteLine($"Geometry: {shape.AsSqlServerWkt()}");
    
    // Access attribute data
    foreach (var attribute in shape.Attributes)
    {
        Console.WriteLine($"{attribute.Key}: {attribute.Value}");
    }
}

Writing Shapefiles

Creating New Shapefiles

// Create a new shapefile
var shapes = new List<ShapefileShape>();

// Add your shapes here...

// Write to file
await Shapefile.WriteShapesAsync(shapes, "output/path.shp");

Working with Different Geometry Types

Point Geometries

// Create a point
var point = new PointShape(recordNumber: 1, x: 10.0, y: 20.0);

// Add to shapefile
shapes.Add(point);

Polyline Geometries

// Create a polyline
var points = new[] { 
    new Point(0, 0), 
    new Point(10, 0), 
    new Point(10, 10) 
};
var polyline = new PolylineShape(recordNumber: 1, points);

shapes.Add(polyline);

Coordinate Systems

Working with Projections

// Read shapefile with coordinate system information
var shapes = await Shapefile.ReadShapesAsync("file.shp", "file.prj");

// Access coordinate system
var coordinateSystem = shapes.CoordinateSystem;
Console.WriteLine($"Projection: {coordinateSystem.Name}");

Performance Considerations

Streaming Large Files

// For large files, use streaming approach
await foreach (var shape in Shapefile.ReadShapesStreamAsync("large_file.shp"))
{
    // Process each shape individually
    ProcessShape(shape);
}

Memory Management

// Dispose resources properly
using var shapefile = new ShapefileReader("file.shp");
var shapes = await shapefile.ReadAllAsync();
// Resources are automatically disposed

Error Handling

try
{
    var shapes = await Shapefile.ReadShapesAsync("file.shp");
}
catch (ShapefileException ex)
{
    Console.WriteLine($"Shapefile error: {ex.Message}");
}
catch (FileNotFoundException ex)
{
    Console.WriteLine($"File not found: {ex.FileName}");
}

For more examples, see the Examples page

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