งานและการสร้าง Shapefile โดยใช้ไลบรารี C#
Contents
[
Hide
]
สร้าง ShapeFile
Aspose.GIS for .NET ช่วยให้คุณสร้าง ShapeFile ใหม่และเพิ่มข้อมูลลงในนั้นได้ สามารถใส่ข้อมูล Attributes ลงใน ShapeFile และเพิ่ม Features ที่เกี่ยวข้องกับข้อมูลเหล่านั้นได้
This file contains hidden or 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); | |
} |
สร้าง Vector Layer พร้อมระบบอ้างอิงเชิงพื้นที่
This file contains hidden or 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 | |
} |
เพิ่มฟีเจอร์ใหม่ใน ShapeFile
This file contains hidden or 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); | |
} | |
แปลง Shape File Polygon เป็น Shape File Line String
This file contains hidden or 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); | |
} | |
} | |
} |