Filtrage et indexation des calques vectoriels SIG en C#
Avec Aspose.GIS pour .NET, vous pouvez filtrer les calques par valeurs d’attributs ou limites spatiales. Vous pouvez également utiliser des index pour accélérer le filtrage et les requêtes spatiales.
Index d’attribut
Filtrage sans index
Voici comment filtrer un calque par les valeurs d’un attribut :
// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET | |
using (var layer = VectorLayer.Open(citiesPath, Drivers.GeoJson)) | |
{ | |
// Select features based on values of the attribute. This code enumerates all features in the layer | |
// and selects all features that match a condition. | |
var features = layer.WhereGreaterOrEqual("population", 2000).WhereSmaller("population", 5000); | |
// Print results. | |
Console.WriteLine("Cities where population >= 2000 and population < 5000:"); | |
foreach (var feature in features) | |
{ | |
var name = feature.GetValue<string>("name"); | |
var population = feature.GetValue<int>("population"); | |
Console.WriteLine(name + ", " + population); | |
} | |
Console.WriteLine(); | |
} |
Filtrage avec index
Le code ci-dessus est correct tant que le calque n’est filtré qu’une seule fois. Mais, si le calque risque d’être interrogé plusieurs fois, nous pouvons tirer parti des index d’attributs. La création d’un index d’attribut prend un certain temps, mais il peut être réutilisé plusieurs fois pour accélérer le filtrage.
L’exemple suivant utilise un fichier d’index d’attributs pour accélérer le filtrage des calques par les valeurs de l’attribut :
// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET | |
using (var layer = VectorLayer.Open(citiesPath, Drivers.GeoJson)) | |
{ | |
// Use attribute index to speed up search by 'population' attribute. | |
// Aspose.GIS builds a new index if it doesn't exist, otherwise existing index is reused. | |
// Any path can be used. | |
var attributeIndexPath = Path.ChangeExtension(citiesPath, "population_out.ix"); | |
layer.UseAttributesIndex(attributeIndexPath, "population"); | |
// Select features based on values of the attribute. Since we use attribute index it is not necessary to | |
// test all features of the layer and filtering time is reduced significantly. | |
var towns = layer.WhereGreaterOrEqual("population", 2000).WhereSmaller("population", 5000); | |
// Print results. | |
Console.WriteLine("Cities where population >= 2000 and population < 5000:"); | |
foreach (var town in towns) | |
{ | |
var name = town.GetValue<string>("name"); | |
var population = town.GetValue<int>("population"); | |
Console.WriteLine(name + ", " + population); | |
} | |
Console.WriteLine(); | |
} |
Enregistrer les caractéristiques filtrées
Les caractéristiques filtrées peuvent être enregistrées dans des calques :
// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET | |
using (var layer = VectorLayer.Open(citiesPath, Drivers.GeoJson)) | |
{ | |
// Use attribute index to speed up search by 'population' attribute. | |
var attributeIndexPath = Path.ChangeExtension(citiesPath, "population_out.ix"); | |
layer.UseAttributesIndex(attributeIndexPath, "population"); | |
// Save all features with population between 2000 and 5000 to the output file. | |
layer.WhereGreaterOrEqual("population", 2000) | |
.WhereSmaller("population", 5000) | |
.SaveTo(DataDir + "towns_out.shp", Drivers.Shapefile); | |
} |
Rendre les caractéristiques filtrées
Il est également possible de rendre des caractéristiques filtrées. L’exemple suivant utilise un index d’attributs pour sélectionner rapidement toutes les caractéristiques dont la population est supérieure à 2000 et les ajouter à la carte :
// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET | |
using (var map = new Map(600, 400)) | |
using (var cities = VectorLayer.Open(citiesPath, Drivers.GeoJson)) | |
{ | |
map.Padding = 20; | |
// Use attribute index to speed up search by 'population' attribute. | |
var attributeIndexPath = Path.ChangeExtension(citiesPath, "population_out.ix"); | |
cities.UseAttributesIndex(attributeIndexPath, "population"); | |
// Render all cities with population greater than 2000. | |
map.Add(cities.WhereGreater("population", 2000), new SimpleMarker { FillColor = Color.Red }); | |
map.Render(outputPath, Renderers.Svg); | |
} |
Index spatial
Les index spatiaux sont utilisés pour accélérer les requêtes spatiales. Tout comme les index d’attributs, les index spatiaux sont réutilisés après leur création.
Trouver des caractéristiques proches d’un point
Voici comment utiliser un index spatial pour accélérer la recherche de la caractéristique la plus proche d’un certain point :
// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET | |
using (var layer = VectorLayer.Open(path, Drivers.GeoJson)) | |
{ | |
// Use spatial index to speed up spatial queries. | |
// Aspose.GIS builds a new index if it doesn't exist, otherwise existing index is reused. | |
// Any path can be used. | |
var spatialIndexPath = Path.ChangeExtension(path, ".spatial_out.ix"); | |
layer.UseSpatialIndex(spatialIndexPath); | |
var point = new Point(12.30, 50.33); | |
// Since we use spatial index, nearest-to finds the closest feature much faster. | |
var nearest = layer.NearestTo(point); | |
Console.WriteLine("City nearest to (12.30 50.33) is " + nearest.GetValue<string>("name")); | |
Console.WriteLine(); | |
} |
Sélectionner des caractéristiques qui intersectent une géométrie
L’exemple suivant utilise un index spatial pour accélérer la sélection de caractéristiques qui intersectent une géométrie :
// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET | |
using (var layer = VectorLayer.Open(path, Drivers.GeoJson)) | |
{ | |
// Use spatial index to speed up 'WhereIntersects'. | |
var spatialIndexPath = Path.ChangeExtension(path, ".spatial_out.ix"); | |
layer.UseSpatialIndex(spatialIndexPath); | |
var polygon = Geometry.FromText("Polygon((12.30 50.33, 22.49 54.87, 21.92 42.53, 12.30 50.33))"); | |
var intersecting = layer.WhereIntersects(polygon); | |
Console.WriteLine("Cities within " + polygon.AsText() + ":"); | |
foreach (var feature in intersecting) | |
{ | |
var name = feature.GetValue<string>("name"); | |
var location = (IPoint) feature.Geometry; | |
Console.WriteLine($"{name} at ({location.X}, {location.Y})"); | |
} | |
Console.WriteLine(); | |
} |