Write or Access GIS Features from TopoJSON format in C#
Contents
[
Hide
]
Working with TopoJSON Format
Access features in TopoJSON
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 sampleTopoJsonPath = RunExamples.GetDataDir() + "sample.topojson"; | |
StringBuilder builder = new StringBuilder(); | |
using (VectorLayer layer = VectorLayer.Open(sampleTopoJsonPath, Drivers.TopoJson)) | |
{ | |
foreach (Feature feature in layer) | |
{ | |
// get id property | |
int id = feature.GetValue<int>("id"); | |
// get name of the object that contains this feature | |
string objectName = feature.GetValue<string>("topojson_object_name"); | |
// get name attribute property, located inside 'properties' object | |
string name = feature.GetValue<string>("name"); | |
// get geometry of the feature. | |
string geometry = feature.Geometry.AsText(); | |
builder.AppendFormat("Feature with ID {0}:\n", id); | |
builder.AppendFormat("Object Name = {0}\n", objectName); | |
builder.AppendFormat("Name = {0}\n", name); | |
builder.AppendFormat("Geometry = {0}\n", geometry); | |
} | |
} | |
Console.WriteLine("Output:"); | |
Console.WriteLine(builder.ToString()); | |
// Output: | |
// Feature with ID 0: | |
// Object Name = named_object_1 | |
// Name = point_feature | |
// Geometry = POINT (102 0.5) | |
// Feature with ID 1: | |
// Object Name = named_object_1 | |
// Name = line_feature | |
// Geometry = LINESTRING (102 0, 103 1, 104 0, 105 1) | |
// Feature with ID 2: | |
// Object Name = named_object_2 | |
// Name = polygon_feature | |
// Geometry = POLYGON ((100 0, 100 1, 101 1, 101 0, 100 0)) |
Write features to TopoJSON
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 outputPath = RunExamples.GetDataDir() + "sample_out.topojson"; | |
using (VectorLayer layer = VectorLayer.Create(outputPath, Drivers.TopoJson)) | |
{ | |
// add attributes that we are going to set | |
layer.Attributes.Add(new FeatureAttribute("name", AttributeDataType.String)); | |
layer.Attributes.Add(new FeatureAttribute("measurement", AttributeDataType.Double)); | |
layer.Attributes.Add(new FeatureAttribute("id", AttributeDataType.Integer)); | |
var feature0 = layer.ConstructFeature(); | |
feature0.SetValue("name", "name_0"); | |
feature0.SetValue("measurement", 1.03); | |
feature0.SetValue("id", 0); | |
feature0.Geometry = new Point(1.3, 2.3); | |
layer.Add(feature0); | |
var feature1 = layer.ConstructFeature(); | |
feature1.SetValue("name", "name_1"); | |
feature1.SetValue("measurement", 10.03); | |
feature1.SetValue("id", 1); | |
feature1.Geometry = new Point(241.32, 23.2); | |
layer.Add(feature1); | |
} |