计算 2 个 MKPolygons 的并集

发布于 2024-11-26 10:38:03 字数 199 浏览 3 评论 0原文

我正在使用多边形 MKOverlays 开发地图应用程序。我需要合并(联合)重叠的多边形。

有一个众所周知的算法可以做到这一点吗?是否有任何免费的现有库/实现可以帮助进行此类几何操作?

我找到了 GEOS 库,但显然它的许可条款不允许在不分发源代码的情况下使用。还有其他人使用这个库吗?如果是,我在哪里可以找到将其包含在我的 Xcode 项目中的方法。

I am working on map applications with polygon MKOverlays. I have a requirement to merge (union) overlapping polygons.

Is there a well known algorithm to do this? Are there any free existing libraries/implementations that help with such geometry operations?

I have found the GEOS library, but apparently its licensing terms disallow use without distributing your source code. Is anyone else using this library. If yes, where can I find the way to include this in my Xcode project.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

执笏见 2024-12-03 10:38:03

尝试gpc。他们拥有多个许可证。他们的页面上还列出了类似的库。

Try gpc. They have several licences. Also there are similar libraries listed on their page.

蓝色星空 2024-12-03 10:38:03

有一个很棒的库 RSClipperWrapper 它基本上是 剪辑器。他们的网站内甚至还有一个很棒的库比较:

在此处输入图像描述

TL;DR,免费库,无错误且快速。

一些注意事项:

  • RSClipperWrapper 采用 CGPoint 但不用担心,你可以通过 lat /长入其中,它将完成工作(经过测试和验证)。
  • 为了方便起见,我编写了一个扩展,这样我们就可以传递一个自定义的Polygon数组并获取合并的多边形 - 如果您使用MKPolygon或其他类型,那么不要忘记调整您的类型

    扩展 Clipper {
      静态函数联合(多边形:[多边形])-> [多边形] {
        让pointPolygons =转换(多边形:多边形)
        让 unionfied = Clipper.unionPolygons(pointsPolygons, withPolygons:pointsPolygons)
        返回转换(pointsPolygons:unionfied)
       }
    
      static func Convert(多边形: [多边形]) -> [[CG点]] {
        var PolygonsPoints: [[CGPoint]] = []
        对于多边形中的多边形 {
            变量点:[CGPoint] = []
            对于 Polygon.locations 中的位置 {
                points.append(CGPoint(x: 位置.坐标.纬度, y: 位置.坐标.经度))
            }
            PolygonsPoints.append(点)
        }
        返回多边形点
       }
    
      static func Convert(pointsPolygons: [[CGPoint]]) ->; [多边形] {
        var 多边形: [多边形] = []
        对于pointsPolygons 中的pointsPolygons {
            变量位置:[CLLocation] = []
            对于pointsPolygon中的点{
                locations.append(CLLocation(纬度:CLLocationDegrees(point.x),经度:CLLocationDegrees(point.y)))
            }
            让多边形 = 多边形(位置:位置)
            多边形.append(多边形)
        }
        返回多边形
     }
    }
    

There is a great library RSClipperWrapper which is basically a wrapper for Clipper. There is even a great library comparison inside their website:

enter image description here

TL;DR, free library, error free and fast.

A few notes:

  • RSClipperWrapper takes CGPoint but fear not, you can pass lat/long into it and it will get the job done (tested and verified).
  • For convinice I've written an extension so we can just pass a custom Polygon array and get the merged polygons - if you're using MKPolygon or other type then don't forget adjust your type:

    extension Clipper {
      static func union(polygons: [Polygon]) -> [Polygon] {
        let pointsPolygons = convert(polygons: polygons)
        let unionfied = Clipper.unionPolygons(pointsPolygons, withPolygons: pointsPolygons)
        return convert(pointsPolygons: unionfied)
       }
    
      static func convert(polygons: [Polygon]) -> [[CGPoint]] {
        var polygonsPoints: [[CGPoint]] = []
        for polygon in polygons {
            var points: [CGPoint] = []
            for location in polygon.locations {
                points.append(CGPoint(x: location.coordinate.latitude, y: location.coordinate.longitude))
            }
            polygonsPoints.append(points)
        }
        return polygonsPoints
       }
    
      static func convert(pointsPolygons: [[CGPoint]]) -> [Polygon] {
        var polygons: [Polygon] = []
        for pointsPolygon in pointsPolygons {
            var locations: [CLLocation] = []
            for point in pointsPolygon {
                locations.append(CLLocation(latitude: CLLocationDegrees(point.x), longitude: CLLocationDegrees(point.y)))
            }
            let polygon = Polygon(locations: locations)
            polygons.append(polygon)
        }
        return polygons
     }
    }
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文