高效搜索大型 GeoJson 文件以查看它是否包含点(Swift)

问题描述 投票:0回答:0

我有一个 200mb 的 geojson 文件,其中包含一个国家/地区的速度限制。简而言之,该文件包含包含不同区域的多边形,每个多边形都有给定的速度限制值。

我正在创建一个 ios 快速导航应用程序,它需要查询此 geojson 文件以获取用户所处的速度限制。我目前的方法有点像线性搜索,因此在工作时会导致程序滞后和大量性能命中。

我当前的实现:

func findSpeedLimit(location: CLLocationCoordinate2D, geoJSON: GeoJSON) -> Int? {
    // Decode the GeoJSON data into a FeatureCollection object
    guard case let .featureCollection(featureCollection) = geoJSON else {
        print("Error: GeoJSON data is not a FeatureCollection")
        return nil
    }
    
    // Create a Point object from the location coordinates
    let point = Point(x: location.longitude, y: location.latitude)
    //print("Looking for speed limit at location: \(location.latitude), \(location.longitude)")
    
    // Loop through the features in the feature collection
    for (index, feature) in featureCollection.features.enumerated() {
        // Check if the feature has a geometry and properties
        guard let geometry = feature.geometry,
              let properties = feature.properties else {
            print("Feature \(index) has no geometry or properties")
            continue
        }
        
        do {
            // Check if the geometry contains the point
            if try geometry.contains(point) {
                // Check if the properties have a speedLimitZoneValue key
                if let speedLimit = properties["speedLimitZoneValue"] {
                    let speedLimitString = String(describing: speedLimit)
                    if let number = Int(speedLimitString.components(separatedBy: CharacterSet.decimalDigits.inverted).joined()) {
                        //print (number)
                        //print("Speed limit found for feature \(index): \(number) km/h")
                        return number
                    }
                    
                } else {
                    print("Feature \(index) has no 'speedLimitZoneValue' property")
                }
            } else {
                //print("Feature \(index) does not contain the point")
            }
        } catch {
            print("Error checking if geometry contains point: \(error)")
        }
    }
    
    return nil
}

ios json swift mapkit geojson
© www.soinside.com 2019 - 2024. All rights reserved.