I have a large geojson data of around 50.000 features. When I try to add them to mapView
"Message from debugger: Terminated due to memory issue" occurred.
I am dividing 50.000 feature around 30-35 layer. Memory using going up to 1.3 GB.
First, I try to add geojson as MGLAnnotation
it is too slow and still the same error. And I add geojson as MGLSymbolStyleLayer
but still the same error.
func drawPoint(geoJson : String , id: String) {
DispatchQueue.global(qos: .background).async(execute: {
do {
let data = geoJson.data(using: .utf8)
let kgmId = "kgm-\(id)"
guard let shapeCollectionFeature = try MGLShape(data: data!, encoding: String.Encoding.utf8.rawValue) as? MGLShapeCollectionFeature else {
fatalError("Could not cast to specified MGLShapeCollectionFeature")
}
self.removeLineLayer(type: kgmId)
let source = MGLShapeSource(identifier: kgmId, shape: shapeCollectionFeature, options: nil)
if self.mapView.style?.source(withIdentifier: kgmId) == nil{
self.mapView.style?.addSource(source)
}
let pointLayer = MGLSymbolStyleLayer(identifier: kgmId, source: source)
let zoomStops = [
13.49: NSExpression(forConstantValue: 0),
13.5: NSExpression(forConstantValue: 1)
]
pointLayer.iconOpacity = NSExpression(format: "mgl_interpolate:withCurveType:parameters:stops:($zoomLevel, 'linear', nil, %@)", zoomStops)
let filteredLayer = layersArr.first(where: {$0.layer == id})
let type = filteredLayer?.style.type
if let type = type{
if type == "basic"{
pointLayer.iconImageName = NSExpression(forConstantValue: kgmId)
}else if type == "coded"{
let style = layersArr.first(where: {$0.layer == id})?.style
let fieldName = style?.data[0].field_name
if let fieldName = fieldName{
pointLayer.iconImageName = NSExpression(format: "FUNCTION(%@, 'valueForKeyPath:', %K)", self.poiIcons[id] as! [String:String],fieldName)
}
}
}
pointLayer.iconAllowsOverlap = NSExpression(forConstantValue: true)
if self.mapView.style?.layer(withIdentifier: id) == nil{
self.mapView.style!.addLayer(pointLayer)
}
} catch {
print("GeoJSON parsing failed")
}
})
}
What is the best way to add large geojson data to the mapView?