C# 라이브러리를 사용하여 작업하고 Shapefile 생성하기
Contents
[
Hide
]
ShapeFile 만들기
Aspose.GIS for .NET을 사용하면 새 ShapeFile을 만들고 정보를 추가할 수 있습니다. 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 | |
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); | |
} |
공간 참조 시스템으로 벡터 레이어 만들기
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); | |
} | |
Polygon Shape File을 Line String Shape File로 변환
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); | |
} | |
} | |
} |