C# での GIS ベクトルレイヤーのフィルタリングとインデックス作成

Aspose.GIS for .NET を使用すると、属性値または空間範囲でレイヤーをフィルタリングできます。また、インデックスを使用して、フィルタリングと空間クエリの速度を向上させることができます。

属性インデックス

インデックスなしでのフィルタリング

ここでは、属性の値でレイヤーをフィルタリングする方法を示します。

// 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();
}

インデックス付きでのフィルタリング

上記のコードは、レイヤーが一度だけフィルタリングされる限り問題ありません。ただし、レイヤーが複数回クエリされる可能性が高い場合は、属性インデックスからメリットを得ることができます。属性インデックスの構築には時間がかかりますが、フィルタリングを高速化するために何度も再利用できます。

次の例では、属性インデックスファイルを使用して、属性の値によるレイヤーのフィルタリングを高速化します。

// 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();
}

フィルタリングされたフィーチャの保存

フィルタリングされたフィーチャはレイヤーに保存できます。

// 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);
}

フィルタリングされたフィーチャのレンダリング

フィルタリングされたフィーチャをレンダリングすることも可能です。次の例では、属性インデックスを使用して、人口が 2000 を超えるすべてのフィーチャをすばやく選択し、マップに追加します。

// 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);
}

空間インデックス

空間インデックスは、空間クエリを高速化するために使用されます。属性インデックスと同様に、空間インデックスは作成後に再利用されます。

ポイントに最も近いフィーチャの検索

ここでは、空間インデックスを使用して、ある点に最も近いフィーチャをすばやく見つける方法を示します。

// 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();
}

ジオメトリと交差するフィーチャの選択

次の例では、空間インデックスを使用して、ジオメトリと交差するフィーチャの選択を高速化します。

// 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();
}