Repository navigation
IRI.Maptor.Sta.ShapefileFormat Examples
Hossein Narimani Rad edited this page Sep 28, 2025
·
4 revisions
using IRI.Maptor.Sta.ShapefileFormat;
// Read all shapes from a shapefile
var shapes = await Shapefile.ReadShapesAsync("countries.shp");
Console.WriteLine($"Found {shapes.Count()} shapes");
foreach (var shape in shapes)
{
Console.WriteLine($"Shape {shape.RecordNumber}: {shape.AsWkt()}");
}// Read shapes with attribute data
var shapesWithAttributes = await Shapefile.ReadShapesWithAttributesAsync("cities.shp");
foreach (var shape in shapesWithAttributes)
{
Console.WriteLine($"City: {shape.GetAttribute<string>("NAME")}");
Console.WriteLine($"Population: {shape.GetAttribute<int>("POPULATION")}");
Console.WriteLine($"Geometry: {shape.AsWkt()}");
}// Create points for cities
var cities = new List<ShapefileShapeWithAttributes>
{
new PointShapeWithAttributes(1, 10.0, 20.0)
{
Attributes = new Dictionary<string, object>
{
["NAME"] = "New York",
["POPULATION"] = 8000000
}
},
new PointShapeWithAttributes(2, 15.0, 25.0)
{
Attributes = new Dictionary<string, object>
{
["NAME"] = "London",
["POPULATION"] = 9000000
}
}
};
// Write to shapefile
await Shapefile.WriteShapesWithAttributesAsync(cities, "cities.shp");// Create polygons for countries
var countries = new List<ShapefileShapeWithAttributes>
{
new PolygonShapeWithAttributes(1, new[]
{
new[] { new Point(0, 0), new Point(10, 0), new Point(10, 10), new Point(0, 10), new Point(0, 0) }
})
{
Attributes = new Dictionary<string, object>
{
["COUNTRY"] = "Example Country",
["AREA"] = 100.0
}
}
};
await Shapefile.WriteShapesWithAttributesAsync(countries, "countries.shp");// Read shapefile in WGS84
var wgs84Shapes = await Shapefile.ReadShapesAsync("data.shp", "data.prj");
// Transform to Web Mercator
var webMercatorShapes = wgs84Shapes.TransformTo(CoordinateSystem.FromEpsgCode(3857));
// Write transformed data
await Shapefile.WriteShapesAsync(webMercatorShapes, "web_mercator_data.shp");// Create a shapefile with coordinate system
var shapes = await Shapefile.ReadShapesAsync("input.shp");
// Define coordinate system
var coordSys = CoordinateSystem.FromEpsgCode(4326); // WGS84
// Write with coordinate system
await Shapefile.WriteShapesAsync(shapes, "output.shp");
await coordSys.WritePrjFileAsync("output.prj");// Process large shapefiles efficiently
await foreach (var shape in Shapefile.ReadShapesStreamAsync("large_file.shp"))
{
// Process each shape individually
if (shape.BoundingBox.Area > 1000)
{
Console.WriteLine($"Large shape found: {shape.RecordNumber}");
}
}// Filter shapes by geometry type
var pointShapes = shapes.Where(s => s.ShapeType == ShapeType.Point);
// Filter by attribute values
var largeCities = shapesWithAttributes
.Where(s => s.GetAttribute<int>("POPULATION") > 1000000);
// Transform geometries
var transformedShapes = shapes
.Select(s => s.TransformTo(CoordinateSystem.FromEpsgCode(3857)))
.ToList();try
{
var shapes = await Shapefile.ReadShapesAsync("file.shp");
}
catch (FileNotFoundException ex)
{
Console.WriteLine($"Shapefile not found: {ex.FileName}");
}
catch (InvalidShapefileException ex)
{
Console.WriteLine($"Invalid shapefile: {ex.Message}");
}
catch (EncodingException ex)
{
Console.WriteLine($"Encoding error: {ex.Message}");
Console.WriteLine($"Try encoding: {ex.SuggestedEncoding}");
}// Read shapefile with international characters
var internationalShapes = await Shapefile.ReadShapesWithAttributesAsync("international_data.shp");
foreach (var shape in internationalShapes)
{
// Handle different languages
var name = shape.GetAttribute<string>("NAME");
var nameLocal = shape.GetAttribute<string>("NAME_LOCAL");
Console.WriteLine($"English: {name}");
Console.WriteLine($"Local: {nameLocal}");
}// Create shapefile with international data
var internationalCities = new List<ShapefileShapeWithAttributes>
{
new PointShapeWithAttributes(1, 139.6917, 35.6895) // Tokyo coordinates
{
Attributes = new Dictionary<string, object>
{
["NAME"] = "Tokyo",
["NAME_JP"] = "東京",
["POPULATION"] = 14000000
}
}
};
// Write with UTF-8 encoding
await Shapefile.WriteShapesWithAttributesAsync(
internationalCities,
"international_cities.shp",
"international_cities.dbf",
"international_cities.cpg",
Encoding.UTF8);// Process multiple shapefiles in parallel
var shapefilePaths = new[] { "file1.shp", "file2.shp", "file3.shp" };
var tasks = shapefilePaths.Select(async path =>
{
var shapes = await Shapefile.ReadShapesAsync(path);
return ProcessShapes(shapes);
});
var results = await Task.WhenAll(tasks);// Process large files without loading everything into memory
var totalShapes = 0;
await foreach (var shape in Shapefile.ReadShapesStreamAsync("huge_file.shp"))
{
totalShapes++;
// Process shape
ProcessShape(shape);
// Log progress every 1000 shapes
if (totalShapes % 1000 == 0)
{
Console.WriteLine($"Processed {totalShapes} shapes");
}
}For more detailed information, see the Usage Guide and Troubleshooting