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