Lọc và Tạo Chỉ Mục cho Các Lớp Vector GIS trong C#
Với Aspose.GIS for .NET, bạn có thể lọc các lớp theo giá trị thuộc tính hoặc giới hạn không gian. Bạn cũng có thể sử dụng chỉ mục để tăng tốc độ lọc và truy vấn không gian.
Chỉ mục Thuộc tính
Lọc Không Có Chỉ Mục
Đây là cách lọc một lớp theo giá trị của một thuộc tính:
// 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(); | |
} |
Lọc Với Chỉ Mục
Mã trên hoạt động tốt miễn là lớp chỉ được lọc một lần. Nhưng, nếu lớp có khả năng được truy vấn nhiều lần, chúng ta có thể hưởng lợi từ các chỉ mục thuộc tính. Việc xây dựng chỉ mục thuộc tính mất một chút thời gian, nhưng nó có thể được sử dụng nhiều lần để tăng tốc độ lọc.
Ví dụ sau sử dụng tệp chỉ mục thuộc tính để tăng tốc độ lọc lớp theo giá trị của thuộc tính:
// 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(); | |
} |
Lưu Các Tính Năng Đã Lọc
Các tính năng đã lọc có thể được lưu vào các lớp:
// 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); | |
} |
Kết Xuất Các Tính Năng Đã Lọc
Bạn cũng có thể kết xuất các tính năng đã lọc. Ví dụ sau sử dụng chỉ mục thuộc tính để nhanh chóng chọn tất cả các tính năng có dân số lớn hơn 2000 và thêm chúng vào bản đồ:
// 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); | |
} |
Chỉ Mục Không Gian
Các chỉ mục không gian được sử dụng để tăng tốc độ truy vấn không gian. Giống như các chỉ mục thuộc tính, các chỉ mục không gian được tái sử dụng sau khi tạo.
Tìm Các Tính Năng Gần Nhất với Điểm
Đây là cách sử dụng chỉ mục không gian để tăng tốc độ tìm kiếm tính năng gần nhất với một điểm:
// 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(); | |
} |
Chọn Các Tính Năng Giao Với Hình Học
Ví dụ sau sử dụng chỉ mục không gian để tăng tốc độ chọn các tính năng giao với hình học:
// 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(); | |
} |