Export or Read GIS Features, Points, Geometries from CSV in C#
Contents
[
Hide
]
GIS C# Library Aspose.GIS lets you read features from comma-separated values (CSV) files.
Reading Features from CSV 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 (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(); | |
} | |
} |
Reading Points from CSV 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 (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(); | |
} | |
} |
Reading Geometries using WKT column
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(); | |
} | |
} |
Export to CSV format
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); | |
} |