在 C# 中过滤和索引 GIS 矢量图层
Contents
[
Hide
]
使用 Aspose.GIS for .NET 可以通过属性值或空间边界过滤图层。您还可以使用索引来加速过滤和空间查询。
属性索引
无索引的过滤
以下是如何按属性的值过滤图层:
This file contains hidden or 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 (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(); | |
} |
有索引的过滤
如果图层只被过滤一次,那么上面的代码是可以的。但是,如果图层可能被多次查询,我们可以从属性索引中受益。构建属性索引需要一些时间,但可以多次重用它来加速过滤。
以下示例使用属性索引文件来加速按属性值过滤图层:
This file contains hidden or 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 (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(); | |
} |
保存过滤后的要素
过滤后的要素可以保存到图层中:
This file contains hidden or 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 (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 的要素并将它们添加到地图中:
This file contains hidden or 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 (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); | |
} |
空间索引
空间索引用于加速空间查询。就像属性索引一样,空间索引在创建后会被重用。
查找距离点最近的要素
以下是如何使用空间索引来加速查找距离某个点最近的要素:
This file contains hidden or 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 (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(); | |
} |
选择与几何体相交的要素
以下示例使用空间索引来加速选择与几何体相交的要素:
This file contains hidden or 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 (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(); | |
} |