Xuất hoặc Đọc các Đối tượng Địa lý, Điểm, Hình học từ CSV trong C#
Contents
[
Hide
]
Thư viện GIS C# Aspose.GIS cho phép bạn đọc các đối tượng địa lý từ các tệp giá trị phân cách bằng dấu phẩy (CSV).
Đọc các Đối tượng Địa lý từ Tệp CSV
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 (var layer = Drivers.Csv.OpenLayer(dataDir + "sample.csv")) | |
{ | |
// print attributes | |
foreach (var attribute in layer.Attributes) | |
{ | |
Console.Write($"'{attribute.Name}' "); | |
} | |
Console.WriteLine(); | |
// print records | |
foreach (var feature in layer) | |
{ | |
var dump = feature.GetValuesDump(); | |
foreach (var item in dump) | |
{ | |
Console.Write($"'{item}' "); | |
} | |
Console.WriteLine(); | |
} | |
} |
Đọc các Điểm từ Tệp CSV
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 (var layer = Drivers.Csv.OpenLayer(dataDir + "geometries.csv", | |
new CsvOptions() | |
{ | |
ColumnX = "x" , | |
ColumnY = "y", | |
ColumnZ = "z", | |
ColumnM = "m" | |
})) | |
{ | |
// print geometry in wkt format | |
foreach (var feature in layer) | |
{ | |
Console.Write($"'{feature.Geometry.AsText()}: "); | |
Console.WriteLine(); | |
} | |
} |
Đọc Hình học bằng cột WKT
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 (var layer = Drivers.Csv.OpenLayer(dataDir + "geometries.csv", | |
new CsvOptions() | |
{ | |
ColumnWkt = "geom_data" | |
})) | |
{ | |
// print geometry in wkt format | |
foreach (var feature in layer) | |
{ | |
Console.Write($"'{feature.Geometry.AsText()}: "); | |
Console.WriteLine(); | |
} | |
} |
Xuất sang định dạng CSV
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 | |
// apply a custom delimiter and a column for geometry. | |
var options = new CsvOptions() | |
{ | |
ColumnWkt = "geom_data", | |
Delimiter = ';' | |
}; | |
// create a new CSV layer | |
using (var layer = Drivers.Csv.CreateLayer(dataDir + "csv_out.csv", options)) | |
{ | |
layer.Attributes.Add(new FeatureAttribute("string_data", AttributeDataType.String)); | |
layer.Attributes.Add(new FeatureAttribute("int_data", AttributeDataType.Integer)); | |
layer.Attributes.Add(new FeatureAttribute("bool_data", AttributeDataType.Boolean)); | |
layer.Attributes.Add(new FeatureAttribute("float_data", AttributeDataType.Double)); | |
Feature feature = layer.ConstructFeature(); | |
feature.SetValue("string_data", "string value"); | |
feature.SetValue("int_data", 10); | |
feature.SetValue("bool_data", true); | |
feature.SetValue("float_data", 3.14); | |
feature.Geometry = new LineString(new[] { new Point(0, 0), new Point(1, 1) }); | |
layer.Add(feature); | |
Feature feature2 = layer.ConstructFeature(); | |
feature2.SetValue("string_data", "string value2"); | |
feature2.SetValue("int_data", 100); | |
feature2.SetValue("bool_data", false); | |
feature2.SetValue("float_data", 3.1415); | |
feature2.Geometry = Geometry.Null; | |
layer.Add(feature2); | |
} |