Computing the union 2 MKPolygons

巧了我就是萌 提交于 2019-12-12 17:14:25

问题


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.


回答1:


The only free libraries I'm aware of are -

Clipper: http://angusj.com/delphi/clipper.php

Boost Polygon: http://www.boost.org/doc/libs/1_47_0/libs/polygon/doc/index.htm

Boost Geometry: http://trac.osgeo.org/ggl/




回答2:


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




回答3:


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

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
     }
    }
    


来源:https://stackoverflow.com/questions/6796458/computing-the-union-2-mkpolygons

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!