惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
DataBreaches.Net
Microsoft Security Blog
Microsoft Security Blog
大猫的无限游戏
大猫的无限游戏
B
Blog RSS Feed
MyScale Blog
MyScale Blog
博客园_首页
S
SegmentFault 最新的问题
WordPress大学
WordPress大学
小众软件
小众软件
V
Visual Studio Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Vercel News
Vercel News
Hugging Face - Blog
Hugging Face - Blog
The GitHub Blog
The GitHub Blog
D
Docker
宝玉的分享
宝玉的分享
博客园 - 【当耐特】
F
Fortinet All Blogs
V
V2EX
Last Week in AI
Last Week in AI
Blog — PlanetScale
Blog — PlanetScale
Microsoft Azure Blog
Microsoft Azure Blog
IT之家
IT之家
雷峰网
雷峰网
博客园 - 叶小钗
月光博客
月光博客
J
Java Code Geeks
量子位
爱范儿
爱范儿
阮一峰的网络日志
阮一峰的网络日志
Martin Fowler
Martin Fowler
H
Help Net Security
酷 壳 – CoolShell
酷 壳 – CoolShell
腾讯CDC
Latest news
Latest news
Recent Announcements
Recent Announcements
Google DeepMind News
Google DeepMind News
美团技术团队
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
The Blog of Author Tim Ferriss
T
Troy Hunt's Blog
B
Blog
T
Tenable Blog
S
Schneier on Security
L
LangChain Blog
L
LINUX DO - 热门话题
博客园 - 司徒正美
I
InfoQ
P
Privacy International News Feed
P
Privacy & Cybersecurity Law Blog

博客园 - 我才是银古

第16章:常见问题、排错与最佳实践 第15章:扩展生态、MCAD 与外部集成 第12章:实战案例:机械结构与 3D 打印零件 第14章:构建、测试、调试与贡献流程 第13章:OpenSCAD 源码架构与核心执行流程 第11章:预览、渲染、网格精度与性能优化 第09章:列表推导、递归与算法建模 第08章:参数化零件库与复用设计 第10章:导入导出、命令行与自动化 第06章:CSG 布尔建模方法 第07章:二维图形、拉伸、旋转与投影 第05章:基础几何、坐标系与变换 第04章:参数、变量、函数、模块与作用域 OpenSCAD 教程目录 第03章:OpenSCAD 语言基础 第02章:安装、环境配置与开发工作流 第01章:OpenSCAD 项目全景与学习路线 第02章:源码获取、编译与开发环境配置 第01章:OCCT项目全景与学习路线 第18章:二次开发实战与综合案例 第18章:综合实战案例 第17章:数据交换与协同 第16章:源码架构与二次开发 第15章:插件与自定义工作台开发 第14章:Python脚本宏与自动化 第13章:FEM仿真分析 第12章:CAM数控加工 第11章:SurfaceMesh与逆向工程 第10章:Draft二维绘图与BIM建筑 第09章:工程图TechDraw 第07章:参数化表达式与Spreadsheet 第08章:装配设计Assembly 第06章:Part工作台与几何内核 第05章:PartDesign实体特征建模 第04章:草图Sketcher约束建模 第02章:安装版本与工作环境配置 第03章:界面工作台与基础操作 第01章:项目全景与学习路线 第十二章:插件开发、研究功能与最佳实践 第十章:定时任务与自动化(Cron) 第七章:技能、记忆与自学习闭环 第八章:MCP 集成与上下文文件 第六章:工具系统与终端后端 第五章:模型供应商与配置体系 Hermes Agent 教程目录 第十一章:语音、视觉、浏览器与子代理协作 第四章:CLI/TUI 与会话管理 第十二章:学习路线、实战方案与最佳实践 第十一章:源码结构、开发调试与插件开发 第十章:自动化、远程访问、日志与排障 第九章:Control UI、节点、Canvas 与语音能力 第七章:工具、技能、插件与能力扩展 第八章:安全模型、访问控制与沙箱实践 第六章:Agent 工作区、会话与多智能体路由 第五章:多通道消息接入与聊天平台配置 第四章:配置体系、模型接入与认证管理 第三章:Gateway 架构、协议与运行机制 第二章:安装、环境准备与快速上手 第一章:OpenClaw 项目概览与核心定位 oh-my-openagent 教程目录 09-命令模型回退与配置参考 10-实战案例最佳实践与故障排除 05-工作模式-Ultrawork-Prometheus-Atlas 08-Hooks与MCP系统 06-Category与Skill系统 07-核心工具链 04-智能体全景详解 03-安装与环境配置 02-整体架构与多模型编排机制 01-项目简介与核心理念 01-项目概览与学习路线 02-安装部署与工具适配 03-Skill机制与using-superpowers 05-TDD系统化调试与完成前验证 04-需求澄清方案设计与计划编写 07-并行智能体子智能体与Git-Worktree 第六章:代码审查、反馈处理与分支收尾 08-中国特色Skills与本土团队落地 09-MCP构建工作流执行与自定义Skill 第23章:FreeCAD-Python-API Clipper2 C# 源码解读教程 第19章:PolyTree 多边形树结构 第20章:实际应用与最佳实践 第18章:Minkowski 和与差 第17章:RectClip 矩形裁剪优化 第16章:ClipperOffset 偏移类详解 第15章:填充规则详解 第14章:布尔运算执行流程 第13章:ClipperD 浮点裁剪类 第11章:OutRec 与 OutPt 输出结构 第9章:Active 活动边结构 第10章:Vertex 顶点与 LocalMinima 局部极小值 第12章:Clipper64 裁剪类详解 第7章:高精度运算与128位整数 第8章:ClipperBase 基类详解 第5章:枚举类型与常量定义 第6章:InternalClipper 内部工具类 第2章:核心数据结构 - Point64、PointD 第3章:路径与多边形表示 - Path64、PathD、Paths64、PathsD 第4章:矩形边界 - Rect64、RectD
第九章:邻近分析与位置服务
我才是银古 · 2026-06-22 · via 博客园 - 我才是银古

第九章:邻近分析与位置服务

9.1 概述

邻近分析是 GIS 中最常用的分析类型之一,用于查找几何对象上最近的点或顶点。geometry-api-net 通过 Proximity2DOperator 提供了强大的邻近分析功能。

9.1.1 应用场景

场景 描述
最近地点查找 查找距离用户最近的商店、ATM、加油站等
路径捕捉 将 GPS 点捕捉到道路网络
编辑辅助 在编辑时捕捉到最近的顶点或边
碰撞检测 查找最近的障碍物
缓冲区分析 查找在特定距离内的所有对象

9.1.2 核心类

// 操作符
public class Proximity2DOperator
{
    public static Proximity2DOperator Instance { get; }
    
    // 查找最近坐标(可以在边上)
    public Proximity2DResult GetNearestCoordinate(
        Geometry geometry, Point inputPoint, bool testPolygonInterior = false);
    
    // 查找最近顶点
    public Proximity2DResult GetNearestVertex(
        Geometry geometry, Point inputPoint);
    
    // 查找指定范围内的多个顶点
    public Proximity2DResult[] GetNearestVertices(
        Geometry geometry, Point inputPoint, 
        double searchRadius, int maxVertexCount = int.MaxValue);
}

// 结果类
public class Proximity2DResult
{
    public Point Coordinate { get; }     // 最近坐标
    public int VertexIndex { get; }      // 顶点索引
    public double Distance { get; }      // 距离
    public bool IsEmpty { get; }         // 是否为空结果
}

9.2 查找最近坐标

9.2.1 概念

GetNearestCoordinate 方法查找几何对象上距离查询点最近的坐标。这个坐标可以在顶点上,也可以在边的中间位置。

9.2.2 基本使用

using Esri.Geometry.Core;
using Esri.Geometry.Core.Geometries;
using Esri.Geometry.Core.Operators;

// 创建一条折线
var polyline = new Polyline();
polyline.AddPath(new List<Point>
{
    new Point(0, 0),
    new Point(10, 0),
    new Point(10, 10)
});

// 查询点
var queryPoint = new Point(5, 5);

// 查找最近坐标
var result = GeometryEngine.GetNearestCoordinate(polyline, queryPoint);

Console.WriteLine($"最近坐标:({result.Coordinate.X}, {result.Coordinate.Y})");
Console.WriteLine($"距离:{result.Distance:F4}");
Console.WriteLine($"顶点索引:{result.VertexIndex}");
// 最近坐标在 (10, 5),是从 (10, 0) 到 (10, 10) 边上的点

9.2.3 点在多边形内测试

// 创建多边形
var polygon = new Polygon();
polygon.AddRing(new List<Point>
{
    new Point(0, 0),
    new Point(100, 0),
    new Point(100, 100),
    new Point(0, 100),
    new Point(0, 0)
});

// 测试点
var pointInside = new Point(50, 50);
var pointOutside = new Point(150, 50);

// testPolygonInterior = true 时,如果点在多边形内部,返回该点本身,距离为 0
var resultInside = GeometryEngine.GetNearestCoordinate(polygon, pointInside, testPolygonInterior: true);
Console.WriteLine($"内部点距离:{resultInside.Distance}");  // 0

var resultOutside = GeometryEngine.GetNearestCoordinate(polygon, pointOutside, testPolygonInterior: true);
Console.WriteLine($"外部点距离:{resultOutside.Distance}");  // 50

// testPolygonInterior = false(默认)时,返回边界上的最近点
var resultBoundary = GeometryEngine.GetNearestCoordinate(polygon, pointInside, testPolygonInterior: false);
Console.WriteLine($"到边界的距离:{resultBoundary.Distance}");  // 50

9.2.4 不同几何类型

// Point - 返回该点
var point = new Point(10, 10);
var result1 = GeometryEngine.GetNearestCoordinate(point, new Point(5, 5));
Console.WriteLine($"点距离:{result1.Distance:F2}");  // ~7.07

// MultiPoint - 返回最近的点
var multiPoint = new MultiPoint();
multiPoint.Add(new Point(0, 0));
multiPoint.Add(new Point(10, 10));
multiPoint.Add(new Point(20, 20));
var result2 = GeometryEngine.GetNearestCoordinate(multiPoint, new Point(12, 12));
Console.WriteLine($"最近点:({result2.Coordinate.X}, {result2.Coordinate.Y})");  // (10, 10)

// Envelope - 返回边界上的最近点
var envelope = new Envelope(0, 0, 100, 100);
var result3 = GeometryEngine.GetNearestCoordinate(envelope, new Point(50, 150));
Console.WriteLine($"最近坐标:({result3.Coordinate.X}, {result3.Coordinate.Y})");  // (50, 100)

9.3 查找最近顶点

9.3.1 概念

GetNearestVertex 方法只在几何对象的顶点中查找,不考虑边上的点。

9.3.2 使用示例

// 创建折线
var polyline = new Polyline();
polyline.AddPath(new List<Point>
{
    new Point(0, 0),
    new Point(10, 0),
    new Point(10, 10),
    new Point(0, 10)
});

var queryPoint = new Point(8, 8);

// 查找最近顶点
var result = GeometryEngine.GetNearestVertex(polyline, queryPoint);

Console.WriteLine($"最近顶点:({result.Coordinate.X}, {result.Coordinate.Y})");  // (10, 10)
Console.WriteLine($"顶点索引:{result.VertexIndex}");  // 2
Console.WriteLine($"距离:{result.Distance:F4}");

9.3.3 与 GetNearestCoordinate 比较

var polyline = new Polyline();
polyline.AddPath(new List<Point>
{
    new Point(0, 0),
    new Point(10, 0)
});

var queryPoint = new Point(5, 3);

// GetNearestCoordinate - 返回边上的点 (5, 0)
var coordResult = GeometryEngine.GetNearestCoordinate(polyline, queryPoint);
Console.WriteLine($"最近坐标:({coordResult.Coordinate.X}, {coordResult.Coordinate.Y})");
// 输出:(5, 0)

// GetNearestVertex - 返回顶点 (0, 0) 或 (10, 0)
var vertexResult = GeometryEngine.GetNearestVertex(polyline, queryPoint);
Console.WriteLine($"最近顶点:({vertexResult.Coordinate.X}, {vertexResult.Coordinate.Y})");
// 输出:(0, 0) 或 (10, 0)(取决于哪个更近)

9.4 范围内查找多个顶点

9.4.1 概念

GetNearestVertices 方法查找指定搜索半径内的所有顶点,并按距离排序返回。

9.4.2 使用示例

// 创建多点
var multiPoint = new MultiPoint();
multiPoint.Add(new Point(0, 0));
multiPoint.Add(new Point(10, 10));
multiPoint.Add(new Point(20, 20));
multiPoint.Add(new Point(30, 30));
multiPoint.Add(new Point(100, 100));

var queryPoint = new Point(15, 15);
double searchRadius = 20;

// 查找范围内的所有顶点
var results = GeometryEngine.GetNearestVertices(multiPoint, queryPoint, searchRadius);

Console.WriteLine($"找到 {results.Length} 个顶点在 {searchRadius} 范围内:");
foreach (var result in results)
{
    Console.WriteLine($"  ({result.Coordinate.X}, {result.Coordinate.Y}) - 距离:{result.Distance:F2}");
}
// 输出按距离排序

9.4.3 限制返回数量

// 只返回最近的 3 个顶点
var results = GeometryEngine.GetNearestVertices(
    multiPoint, 
    queryPoint, 
    searchRadius: 100, 
    maxVertexCount: 3);

Console.WriteLine($"最近的 {results.Length} 个顶点:");

9.4.4 折线和多边形

// 折线的顶点搜索
var polyline = new Polyline();
polyline.AddPath(new List<Point>
{
    new Point(0, 0),
    new Point(5, 0),
    new Point(10, 0),
    new Point(10, 5),
    new Point(10, 10)
});

var queryPoint = new Point(6, 3);
var results = GeometryEngine.GetNearestVertices(polyline, queryPoint, 10);

foreach (var result in results)
{
    Console.WriteLine($"顶点 {result.VertexIndex}: ({result.Coordinate.X}, {result.Coordinate.Y}) - {result.Distance:F2}");
}

9.5 实际应用案例

9.5.1 案例一:查找最近的商店

public class Store
{
    public int Id { get; set; }
    public string Name { get; set; }
    public Point Location { get; set; }
}

public class LocationService
{
    private List<Store> _stores;
    
    public Store FindNearestStore(Point userLocation)
    {
        Store nearest = null;
        double minDistance = double.MaxValue;
        
        foreach (var store in _stores)
        {
            double distance = GeometryEngine.Distance(userLocation, store.Location);
            if (distance < minDistance)
            {
                minDistance = distance;
                nearest = store;
            }
        }
        
        return nearest;
    }
    
    public List<Store> FindStoresWithinRadius(Point userLocation, double radiusDegrees)
    {
        // 将商店位置转换为 MultiPoint
        var storePoints = new MultiPoint();
        foreach (var store in _stores)
        {
            storePoints.Add(store.Location);
        }
        
        // 查找范围内的所有顶点
        var results = GeometryEngine.GetNearestVertices(
            storePoints, userLocation, radiusDegrees);
        
        // 根据顶点索引获取商店
        return results.Select(r => _stores[r.VertexIndex]).ToList();
    }
}

9.5.2 案例二:GPS 捕捉到道路

public class RoadSnapService
{
    private Polyline _roadNetwork;
    
    public Point SnapToRoad(Point gpsPoint)
    {
        // 查找道路上最近的坐标
        var result = GeometryEngine.GetNearestCoordinate(_roadNetwork, gpsPoint);
        
        if (result.Distance > 50)  // 如果距离太远,可能不在道路上
        {
            Console.WriteLine($"警告:距离道路 {result.Distance:F2} 米");
        }
        
        return result.Coordinate;
    }
    
    public Polyline SnapTrack(List<Point> gpsTrack)
    {
        var snappedPoints = new List<Point>();
        
        foreach (var gpsPoint in gpsTrack)
        {
            var result = GeometryEngine.GetNearestCoordinate(_roadNetwork, gpsPoint);
            snappedPoints.Add(result.Coordinate);
        }
        
        var snappedTrack = new Polyline();
        snappedTrack.AddPath(snappedPoints);
        return snappedTrack;
    }
}

9.5.3 案例三:编辑器顶点捕捉

public class GeometryEditor
{
    private List<Geometry> _features = new();
    private double _snapTolerance = 5.0;  // 像素或单位
    
    public Point? GetSnapPoint(Point mouseLocation)
    {
        Point? bestSnapPoint = null;
        double bestDistance = _snapTolerance;
        
        foreach (var feature in _features)
        {
            // 首先尝试捕捉到顶点
            var vertexResult = GeometryEngine.GetNearestVertex(feature, mouseLocation);
            if (vertexResult.Distance < bestDistance)
            {
                bestDistance = vertexResult.Distance;
                bestSnapPoint = vertexResult.Coordinate;
            }
            
            // 如果没有找到顶点,尝试捕捉到边
            if (bestSnapPoint == null)
            {
                var coordResult = GeometryEngine.GetNearestCoordinate(feature, mouseLocation);
                if (coordResult.Distance < bestDistance)
                {
                    bestDistance = coordResult.Distance;
                    bestSnapPoint = coordResult.Coordinate;
                }
            }
        }
        
        return bestSnapPoint;
    }
}

9.5.4 案例四:配送区域分析

public class DeliveryService
{
    private Point _warehouseLocation;
    private Polygon _deliveryZone;
    
    public (bool canDeliver, double distance) CheckDelivery(Point customerLocation)
    {
        // 检查是否在配送区域内
        bool inZone = GeometryEngine.Contains(_deliveryZone, customerLocation);
        
        // 计算距离
        var result = GeometryEngine.GetNearestCoordinate(
            new Point(_warehouseLocation.X, _warehouseLocation.Y), 
            customerLocation);
        
        // 使用大地测量距离获取实际距离
        double distance = GeometryEngine.GeodesicDistance(_warehouseLocation, customerLocation);
        
        return (inZone, distance);
    }
    
    public double CalculateDistanceToZoneBorder(Point customerLocation)
    {
        if (GeometryEngine.Contains(_deliveryZone, customerLocation))
        {
            // 客户在区域内,计算到边界的距离
            var result = GeometryEngine.GetNearestCoordinate(
                _deliveryZone, customerLocation, testPolygonInterior: false);
            return result.Distance;
        }
        else
        {
            // 客户在区域外,计算到区域的距离
            var result = GeometryEngine.GetNearestCoordinate(
                _deliveryZone, customerLocation);
            return result.Distance;
        }
    }
}

9.6 性能优化

9.6.1 空间索引建议

对于大量几何对象,应使用空间索引提高性能:

// 简单的网格索引示例
public class SpatialIndex
{
    private Dictionary<(int, int), List<int>> _grid = new();
    private List<Point> _points;
    private double _cellSize;
    
    public SpatialIndex(List<Point> points, double cellSize)
    {
        _points = points;
        _cellSize = cellSize;
        
        for (int i = 0; i < points.Count; i++)
        {
            var cell = GetCell(points[i]);
            if (!_grid.ContainsKey(cell))
                _grid[cell] = new List<int>();
            _grid[cell].Add(i);
        }
    }
    
    private (int, int) GetCell(Point p)
    {
        return ((int)(p.X / _cellSize), (int)(p.Y / _cellSize));
    }
    
    public List<Point> GetNearbyPoints(Point queryPoint, double radius)
    {
        var results = new List<Point>();
        var queryCell = GetCell(queryPoint);
        int cellRadius = (int)Math.Ceiling(radius / _cellSize);
        
        for (int dx = -cellRadius; dx <= cellRadius; dx++)
        {
            for (int dy = -cellRadius; dy <= cellRadius; dy++)
            {
                var cell = (queryCell.Item1 + dx, queryCell.Item2 + dy);
                if (_grid.TryGetValue(cell, out var indices))
                {
                    foreach (var i in indices)
                    {
                        if (GeometryEngine.Distance(_points[i], queryPoint) <= radius)
                            results.Add(_points[i]);
                    }
                }
            }
        }
        
        return results;
    }
}

9.6.2 批量处理

// 预先计算包络矩形进行快速过滤
public List<(Geometry, Proximity2DResult)> BatchNearestSearch(
    List<Geometry> geometries, Point queryPoint, double maxDistance)
{
    var queryEnvelope = new Envelope(
        queryPoint.X - maxDistance,
        queryPoint.Y - maxDistance,
        queryPoint.X + maxDistance,
        queryPoint.Y + maxDistance);
    
    var results = new List<(Geometry, Proximity2DResult)>();
    
    foreach (var geom in geometries)
    {
        // 快速过滤
        if (!geom.GetEnvelope().Intersects(queryEnvelope))
            continue;
        
        // 精确计算
        var result = GeometryEngine.GetNearestCoordinate(geom, queryPoint);
        if (result.Distance <= maxDistance)
        {
            results.Add((geom, result));
        }
    }
    
    return results.OrderBy(r => r.Item2.Distance).ToList();
}

9.7 小结

本章详细介绍了 geometry-api-net 的邻近分析功能:

  1. GetNearestCoordinate:查找几何对象上最近的坐标(包括边上的点)
  2. GetNearestVertex:只在顶点中查找最近点
  3. GetNearestVertices:查找指定范围内的多个顶点

关键应用场景:

  • 最近地点查找
  • GPS 轨迹捕捉
  • 编辑器顶点捕捉
  • 配送区域分析

性能优化建议:

  • 使用包络矩形快速过滤
  • 对大数据集使用空间索引
  • 批量处理时先过滤再计算

在下一章中,我们将学习高级应用与性能优化,探讨如何在实际项目中高效使用 geometry-api-net。