Attributes and Features - C# GIS Library
Contents
[
Hide
]
Get Features Count In Layer
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 | |
using (VectorLayer layer = VectorLayer.Open(dataDir + "InputShapeFile.shp", Drivers.Shapefile)) | |
{ | |
Console.WriteLine("Total Features in this file: " + layer.Count); | |
} |
Get Information about Layer Attributes
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 | |
using (VectorLayer layer = VectorLayer.Open(dataDir + "InputShapeFile.shp", Drivers.Shapefile)) | |
{ | |
Console.WriteLine("The layer has {0} attributes defined.\n", layer.Attributes.Count); | |
foreach (FeatureAttribute attribute in layer.Attributes) | |
{ | |
Console.WriteLine("Name: {0}", attribute.Name); | |
Console.WriteLine("Data type: {0}", attribute.DataType); | |
Console.WriteLine("Can be null: {0}", attribute.CanBeNull); | |
} | |
} |
Iterate Over Features In Layer
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 | |
using (VectorLayer layer = VectorLayer.Open(dataDir + "InputShapeFile.shp", Drivers.Shapefile)) | |
{ | |
foreach (Feature feature in layer) | |
{ | |
Console.WriteLine(feature.Geometry.AsText()); | |
} | |
} |
Get Value of a Feature Attribute
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 | |
using (VectorLayer layer = VectorLayer.Open(dataDir + "InputShapeFile.shp", Drivers.Shapefile)) | |
{ | |
for (int i = 0; i < layer.Count; i++) | |
{ | |
Feature feature = layer[i]; | |
Console.WriteLine("Entry {0} information\n ========================", i); | |
// case 1 | |
string nameValue = feature.GetValue<string>("name"); // attribute name is case-sensitive | |
int ageValue = feature.GetValue<int>("age"); | |
string dobValue = feature.GetValue<DateTime>("dob").ToString(); | |
Console.WriteLine("Attribute value for feature #{0} is: {1}, {2}", nameValue, ageValue, dobValue); | |
// case 2 | |
var objName = feature.GetValue("name"); // attribute name is case-sensitive | |
var objAge = feature.GetValue("age"); | |
var objDob = feature.GetValue("dob"); | |
Console.WriteLine("Attribute object for feature #{0} is: {1}, {2}", objName, objAge, objDob); | |
} | |
} |
Get Values of a Feature Attribute
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 | |
using (VectorLayer layer = VectorLayer.Open(dataDir + "InputShapeFile.shp", Drivers.Shapefile)) | |
{ | |
foreach (var feature in layer) | |
{ | |
// reads all the attributes into an array. | |
object[] all = new object[3]; | |
feature.GetValues(all); | |
Console.WriteLine("all : {0}, {1}, {2}", all); | |
// reads several the attributes into an array. | |
object[] several = new object[2]; | |
feature.GetValues(several); | |
Console.WriteLine("several: {0}, {1}", several); | |
// reads the attributes as dump of objects. | |
var dump = feature.GetValuesDump(); | |
Console.WriteLine("dump : {0}, {1}, {2}", dump); | |
Console.WriteLine(); | |
} | |
} |
Filter Features by Attribute Value
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 | |
using (VectorLayer layer = VectorLayer.Open(dataDir + "InputShapeFile.shp", Drivers.Shapefile)) | |
{ | |
// all features with the date value in the attribute "dob" later than 1982-01-01. | |
foreach (Feature feature in layer.WhereGreater("dob", new DateTime(1982, 1, 1, 0, 0, 0))) | |
{ | |
Console.WriteLine(feature.GetValue<DateTime>("dob").ToShortDateString()); | |
} | |
} |
Extract Features from ShapeFile to GeoJSON
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 | |
using (VectorLayer inputLayer = VectorLayer.Open(dataDir + "InputShapeFile.shp", Drivers.Shapefile)) | |
{ | |
using (VectorLayer outputLayer = VectorLayer.Create(dataDir + "ExtractFeaturesFromShapeFileToGeoJSON_out.json", Drivers.GeoJson)) | |
{ | |
outputLayer.CopyAttributes(inputLayer); | |
foreach (Feature inputFeature in inputLayer) | |
{ | |
DateTime? date = inputFeature.GetValue<DateTime?>("dob"); | |
if (date == null || date < new DateTime(1982, 1, 1)) | |
{ | |
continue; | |
} | |
//Construct a new feature | |
Feature outputFeature = outputLayer.ConstructFeature(); | |
outputFeature.Geometry = inputFeature.Geometry; | |
outputFeature.CopyValues(inputFeature); | |
outputLayer.Add(outputFeature); | |
} | |
} | |
} |
Specify Attribute Value Length
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 | |
using (VectorLayer layer = VectorLayer.Create(dataDir + "SpecifyAttributeValueLength_out.shp", Drivers.Shapefile)) | |
{ | |
// add attributes before adding features | |
FeatureAttribute attribute = new FeatureAttribute("wide", AttributeDataType.String); | |
attribute.Width = 120; | |
layer.Attributes.Add(attribute); | |
Feature feature = layer.ConstructFeature(); | |
feature.SetValue("wide", "this string can be up to 120 characters long now."); | |
layer.Add(feature); | |
} |
Get Value of Null Value Attribute
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 | |
using (VectorLayer layer = VectorLayer.Open(dataDir + "InputShapeFile.shp", Drivers.Shapefile)) | |
{ | |
int index = 0; | |
foreach (Feature feature in layer) | |
{ | |
DateTime? date = feature.GetValue<DateTime?>("dob"); | |
Console.WriteLine("Feature #{0}", index); | |
Console.WriteLine("\tAttribute value present: {0}", date.HasValue); | |
if (date.HasValue) | |
{ | |
Console.WriteLine("\tAttribute value: {0}", date.Value); | |
} | |
index++; | |
} | |
} |
Get Value or Default Value of Feature
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 | |
//You can set default value for a feature of attribute in a layer | |
using (var layer = Drivers.GeoJson.CreateLayer(dataDir + "data1_out.json")) | |
{ | |
var attribute = new FeatureAttribute("attribute", AttributeDataType.Integer); | |
attribute.CanBeNull = true; | |
attribute.CanBeUnset = true; | |
layer.Attributes.Add(attribute); | |
Feature feature = layer.ConstructFeature(); | |
int? nullValue = feature.GetValueOrDefault<int?>("attribute"); // value == null | |
var defValue1 = feature.GetValueOrDefault<int?>("attribute", 10); // value == 10 | |
var defValue2 = feature.GetValueOrDefault("attribute", 25); // value == 10 | |
Console.WriteLine($"'{nullValue}' vs '{defValue1}' vs '{defValue2}'"); | |
} | |
//Another example where we set the default value to 100 | |
using (var layer = Drivers.GeoJson.CreateLayer(dataDir + "data2_out.json")) | |
{ | |
var attribute = new FeatureAttribute("attribute", AttributeDataType.Double); | |
attribute.CanBeNull = false; | |
attribute.CanBeUnset = false; | |
attribute.DefaultValue = 100; | |
layer.Attributes.Add(attribute); | |
Feature feature = layer.ConstructFeature(); | |
double defValue1 = feature.GetValueOrDefault<double>("attribute"); // value == 100 | |
var defValue2 = feature.GetValueOrDefault("attribute"); // value == 100 | |
feature.SetValue("attribute", 50); | |
var newValue = feature.GetValueOrDefault<double>("attribute"); // value == 50 | |
Console.WriteLine($"'{defValue1}' vs '{defValue2}' vs '{newValue}'"); | |
} |