Work and Generate Shapefile using C# library
Contents
[
Hide
]
Create ShapeFile
Aspose.GIS for .NET lets you create new ShapeFile and add information to it. A ShapeFile can be populated with Attributes information and Features can be added against these.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET | |
using (VectorLayer layer = VectorLayer.Create(dataDir + "NewShapeFile_out.shp", Drivers.Shapefile)) | |
{ | |
// add attributes before adding features | |
layer.Attributes.Add(new FeatureAttribute("name", AttributeDataType.String)); | |
layer.Attributes.Add(new FeatureAttribute("age", AttributeDataType.Integer)); | |
layer.Attributes.Add(new FeatureAttribute("dob", AttributeDataType.DateTime)); | |
// case 1: sets values | |
Feature firstFeature = layer.ConstructFeature(); | |
firstFeature.Geometry = new Point(33.97, -118.25); | |
firstFeature.SetValue("name", "John"); | |
firstFeature.SetValue("age", 23); | |
firstFeature.SetValue("dob", new DateTime(1982, 2,5, 16, 30,0)); | |
layer.Add(firstFeature); | |
Feature secondFeature = layer.ConstructFeature(); | |
secondFeature.Geometry = new Point(35.81, -96.28); | |
secondFeature.SetValue("name", "Mary"); | |
secondFeature.SetValue("age", 54); | |
secondFeature.SetValue("dob", new DateTime(1984, 12, 15, 15, 30, 0)); | |
layer.Add(secondFeature); | |
// case 2: sets new values for all of the attributes. | |
Feature thirdFeature = layer.ConstructFeature(); | |
secondFeature.Geometry = new Point(34.81, -92.28); | |
object[] data = new object[3] {"Alex", 25, new DateTime(1989, 4, 15, 15, 30, 0)}; | |
secondFeature.SetValues(data); | |
layer.Add(thirdFeature); | |
} |
Create Vector Layer with Spatial Reference System
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET | |
var parameters = new ProjectedSpatialReferenceSystemParameters | |
{ | |
Name = "WGS 84 / World Mercator", | |
Base = SpatialReferenceSystem.Wgs84, | |
ProjectionMethodName = "Mercator_1SP", | |
LinearUnit = Unit.Meter, | |
XAxis = new Axis("Easting", AxisDirection.East), | |
YAxis = new Axis("Northing", AxisDirection.North), | |
AxisesOrder = ProjectedAxisesOrder.XY, | |
}; | |
parameters.AddProjectionParameter("central_meridian", 0); | |
parameters.AddProjectionParameter("scale_factor", 1); | |
parameters.AddProjectionParameter("false_easting", 0); | |
parameters.AddProjectionParameter("false_northing", 0); | |
var projectedSrs = SpatialReferenceSystem.CreateProjected(parameters, Identifier.Epsg(3395)); | |
using (var layer = Drivers.Shapefile.CreateLayer(dataDir + "filepath_out.shp", new ShapefileOptions(), projectedSrs)) | |
{ | |
var feature = layer.ConstructFeature(); | |
feature.Geometry = new Point(1, 2); | |
layer.Add(feature); | |
feature = layer.ConstructFeature(); | |
feature.Geometry = new Point(1, 2) { SpatialReferenceSystem = SpatialReferenceSystem.Nad83 }; | |
try | |
{ | |
layer.Add(feature); // geometry of feature has different SRS - exception is thrown | |
} | |
catch (GisException e) | |
{ | |
Console.WriteLine(e.Message); | |
} | |
} | |
using (var layer = Drivers.Shapefile.OpenLayer(dataDir + "filepath_out.shp")) | |
{ | |
var srsName = layer.SpatialReferenceSystem.Name; // "WGS 84 / World Mercator" | |
layer.SpatialReferenceSystem.IsEquivalent(projectedSrs); // true | |
} |
Add new features in ShapeFile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET | |
string path = Path.Combine(dataDir, "point_xyz_out", "point_xyz.shp"); | |
using (var layer = Drivers.Shapefile.EditLayer(path)) | |
{ | |
var feature = layer.ConstructFeature(); | |
feature.SetValue<int>("ID", 5); | |
feature.Geometry = new Point(-5, 5) {Z = 2}; | |
layer.Add(feature); | |
} | |
Convert Polygon Shape File Line String Shape File
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET | |
using (VectorLayer source = VectorLayer.Open(dataDir + "PolygonShapeFile.shp", Drivers.Shapefile)) | |
{ | |
using (VectorLayer destination = VectorLayer.Create(dataDir + "PolygonShapeFileToLineShapeFile_out.shp", Drivers.Shapefile)) | |
{ | |
foreach (Feature sourceFeature in source) | |
{ | |
Polygon polygon = (Polygon)sourceFeature.Geometry; | |
LineString line = new LineString(polygon.ExteriorRing); | |
Feature destinationFeature = destination.ConstructFeature(); | |
destinationFeature.Geometry = line; | |
destination.Add(destinationFeature); | |
} | |
} | |
} |