Skip to content

Commit

Permalink
catch exceptions while parsing and fix poliline on mobile
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianBissekkou committed Nov 26, 2023
1 parent 199734c commit ec61e1b
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.esri.arcgisruntime.mapping.Basemap
import com.esri.arcgisruntime.mapping.BasemapStyle
import com.esri.arcgisruntime.mapping.Viewpoint
import com.esri.arcgisruntime.mapping.view.AnimationCurve
import com.esri.arcgisruntime.mapping.view.Graphic
import com.esri.arcgisruntime.mapping.view.GraphicsOverlay
import com.esri.arcgisruntime.mapping.view.MapView
import com.google.gson.reflect.TypeToken
Expand Down Expand Up @@ -50,7 +51,8 @@ internal class ArcgisMapView(
private lateinit var zoomStreamHandler: ZoomStreamHandler
private lateinit var centerPositionStreamHandler: CenterPositionStreamHandler

private val methodChannel = MethodChannel(binaryMessenger, "dev.fluttercommunity.arcgis_map_sdk/$viewId")
private val methodChannel =
MethodChannel(binaryMessenger, "dev.fluttercommunity.arcgis_map_sdk/$viewId")

override fun getView(): View = view

Expand Down Expand Up @@ -193,7 +195,13 @@ internal class ArcgisMapView(

private fun onAddGraphic(call: MethodCall, result: MethodChannel.Result) {
val graphicArguments = call.arguments as Map<String, Any>
val newGraphic = GraphicsParser.parse(graphicArguments)
lateinit var newGraphic: List<Graphic>
try {
newGraphic = GraphicsParser.parse(graphicArguments)
} catch (e: Throwable) {
result.error("unknown_error", "Error while adding graphic. $e)", null)
return
}

val existingIds =
defaultGraphicsOverlay.graphics.mapNotNull { it.attributes["id"] as? String }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package dev.fluttercommunity.arcgis_map_sdk_android.util

import com.esri.arcgisruntime.geometry.Point
import com.esri.arcgisruntime.geometry.PointCollection
import com.esri.arcgisruntime.geometry.Polygon
import com.esri.arcgisruntime.geometry.Polyline
import com.esri.arcgisruntime.geometry.SpatialReferences
import com.esri.arcgisruntime.mapping.view.Graphic
import com.esri.arcgisruntime.symbology.*
import com.esri.arcgisruntime.symbology.PictureMarkerSymbol
import com.esri.arcgisruntime.symbology.SimpleFillSymbol
import com.esri.arcgisruntime.symbology.SimpleLineSymbol
import com.esri.arcgisruntime.symbology.SimpleMarkerSymbol
import com.esri.arcgisruntime.symbology.Symbol
import dev.fluttercommunity.arcgis_map_sdk_android.model.LatLng
import dev.fluttercommunity.arcgis_map_sdk_android.model.symbol.PictureMarkerSymbolPayload
import dev.fluttercommunity.arcgis_map_sdk_android.model.symbol.SimpleFillSymbolPayload
Expand Down Expand Up @@ -51,11 +57,21 @@ class GraphicsParser {
}

private fun parsePolyline(map: Map<String, Any>): List<Graphic> {
val points = parseToClass<List<List<LatLng>>>(map["paths"]!!)
val points = parseToClass<List<List<List<Double>>>>(map["paths"]!!)

return points.map { subPoints ->
Graphic().apply {
geometry = Polyline(PointCollection(subPoints.map { it.toAGSPoint() }))
geometry = Polyline(PointCollection(subPoints.map { coordinateArray ->
val x = coordinateArray.elementAtOrNull(0)
val y = coordinateArray.elementAtOrNull(1)
val z = coordinateArray.elementAtOrNull(2)
if (x == null || y == null) {
throw Exception("Coordinate array needs at least 2 doubles. Got $coordinateArray")
}

if (z != null) Point(x, y, z, SpatialReferences.getWgs84())
else Point(x, y, SpatialReferences.getWgs84())
}))
symbol = parseSymbol(map)
}
}
Expand Down
8 changes: 7 additions & 1 deletion arcgis_map_sdk_ios/ios/Classes/ArcgisMapView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,13 @@ class ArcgisMapView: NSObject, FlutterPlatformView {

private func onAddGraphic(_ call: FlutterMethodCall, _ result: @escaping FlutterResult) {
let parser = GraphicsParser()
let newGraphics = parser.parse(dictionary: call.arguments as! Dictionary<String, Any>)
var newGraphics = [AGSGraphic]()
do {
newGraphics.append(contentsOf: try parser.parse(dictionary: call.arguments as! Dictionary<String, Any>))
} catch {
result(FlutterError(code: "unknown_error", message: "Error while adding graphic. \(error)", details: nil))
}


let existingIds = defaultGraphicsOverlay.graphics.compactMap { object in
let graphic = object as! AGSGraphic
Expand Down
43 changes: 25 additions & 18 deletions arcgis_map_sdk_ios/ios/Classes/GraphicsParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ import Foundation
import ArcGIS

class GraphicsParser {
func parse(dictionary: Dictionary<String, Any>) -> [AGSGraphic] {
func parse(dictionary: Dictionary<String, Any>) throws -> [AGSGraphic] {
let type = dictionary["type"] as! String

let newGraphics: [AGSGraphic]
switch (type) {
case "point":
newGraphics = parsePoint(dictionary)
newGraphics = try! parsePoint(dictionary)
case "polygon":
newGraphics = parsePolygon(dictionary)
newGraphics = try! parsePolygon(dictionary)
case "polyline":
newGraphics = parsePolyline(dictionary)
newGraphics = try! parsePolyline(dictionary)
default:
fatalError("Unknown type: \(type)")
throw ParseException(message: "Unknown graphic type: \(type)")
}

let attributes = dictionary["attributes"] as? Dictionary<String, Any>
Expand All @@ -33,32 +33,39 @@ class GraphicsParser {
return newGraphics
}

private func parsePoint(_ dictionary: [String: Any]) -> [AGSGraphic] {
private func parsePoint(_ dictionary: [String: Any]) throws -> [AGSGraphic] {
let point: LatLng = try! JsonUtil.objectOfJson(dictionary["point"] as! Dictionary<String, Any>)

let graphic = AGSGraphic()
graphic.geometry = point.toAGSPoint()
graphic.symbol = parseSymbol(dictionary["symbol"] as! Dictionary<String, Any>)
graphic.symbol = try! parseSymbol(dictionary["symbol"] as! Dictionary<String, Any>)

return [graphic]
}

private func parsePolyline(_ dictionary: [String: Any]) -> [AGSGraphic] {
private func parsePolyline(_ dictionary: [String: Any]) throws -> [AGSGraphic] {
let payload: PathPayload = try! JsonUtil.objectOfJson(dictionary)

return payload.paths.map { coordinates in
let points = coordinates.map {
$0.toAGSPoint()
return try payload.paths.map { coordinates in
let points = try coordinates.map { array in
if(array.count < 2) {
throw ParseException(message: "Size of coordinates need to be at least 2. Got \(array)")
}
if(array.count > 2) {
return AGSPoint(x: array[0], y: array[1], z: array[2], spatialReference: .wgs84())
}
return AGSPoint(x: array[0], y: array[1], spatialReference: .wgs84())
}

let graphic = AGSGraphic()

graphic.geometry = AGSPolyline(points: points)
graphic.symbol = parseSymbol(dictionary["symbol"] as! Dictionary<String, Any>)
graphic.symbol = try! parseSymbol(dictionary["symbol"] as! Dictionary<String, Any>)

return graphic
}
}

private func parsePolygon(_ dictionary: [String: Any]) -> [AGSGraphic] {
private func parsePolygon(_ dictionary: [String: Any]) throws -> [AGSGraphic] {
let payload: PolygonPayload = try! JsonUtil.objectOfJson(dictionary)

return payload.rings.map { ring in
Expand All @@ -67,15 +74,15 @@ class GraphicsParser {
AGSPoint(x: coordinate[0], y: coordinate[1], spatialReference: .wgs84())
}
graphic.geometry = AGSPolygon(points: points)
graphic.symbol = parseSymbol(dictionary["symbol"] as! Dictionary<String, Any>)
graphic.symbol = try! parseSymbol(dictionary["symbol"] as! Dictionary<String, Any>)

return graphic
}
}

// region symbol parsing

private func parseSymbol(_ dictionary: [String: Any]) -> AGSSymbol {
private func parseSymbol(_ dictionary: [String: Any]) throws -> AGSSymbol {
let type = dictionary["type"] as! String;
switch (type) {
case "simple-marker":
Expand All @@ -87,7 +94,7 @@ class GraphicsParser {
case "simple-line":
return parseSimpleLineSymbol(dictionary)
default:
fatalError("Unknown type: \(type)")
throw ParseException(message: "Unknown symbol type: \(type)")
}
}

Expand Down Expand Up @@ -153,7 +160,7 @@ class GraphicsParser {
}

private struct PathPayload: Codable {
let paths: [[LatLng]]
let paths: [[[Double]]]
}

private struct PolygonPayload: Codable {
Expand Down
13 changes: 13 additions & 0 deletions arcgis_map_sdk_ios/ios/Classes/ParseException.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// ParseException.swift
// arcgis_map_sdk_ios
//
// Created by Julian Bissekkou on 26.11.23.
//

import Foundation


struct ParseException: Error {
let message: String
}
2 changes: 1 addition & 1 deletion example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: cc1f88378b4bfcf93a6ce00d2c587857c6008d3b

COCOAPODS: 1.13.0
COCOAPODS: 1.12.1
13 changes: 7 additions & 6 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,6 @@ class _ExampleMapState extends State<ExampleMap> {
layerId: _lineLayerId,
elevationMode: ElevationMode.onTheGround,
);
_connectTwoPinsWithPolyline(
id: 'connecting-polyline-01',
name: 'Connecting polyline',
start: _firstPinCoordinates,
end: _secondPinCoordinates,
);

// Create GraphicsLayer with 3D Pins
await _createGraphicLayer(layerId: _pinLayerId);
Expand All @@ -136,6 +130,13 @@ class _ExampleMapState extends State<ExampleMap> {
});
}

_connectTwoPinsWithPolyline(
id: 'connecting-polyline-01',
name: 'Connecting polyline',
start: _firstPinCoordinates,
end: _secondPinCoordinates,
);

// Add Polygons to the PolyLayer
_addPolygon(
layerId: _polyLayerId,
Expand Down
26 changes: 13 additions & 13 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ packages:
dependency: transitive
description:
name: collection
sha256: ee67cb0715911d28db6bf4af1026078bd6f0128b07a5f66fb2ed94ec6783c09a
sha256: f092b211a4319e98e5ff58223576de6c2803db36221657b46c82574721240687
url: "https://pub.dev"
source: hosted
version: "1.18.0"
version: "1.17.2"
cupertino_icons:
dependency: "direct main"
description:
Expand Down Expand Up @@ -166,10 +166,10 @@ packages:
dependency: transitive
description:
name: meta
sha256: a6e590c838b18133bb482a2745ad77c5bb7715fb0451209e1a7567d416678b8e
sha256: "3c74dbf8763d36539f114c799d8a2d87343b5067e9d796ca22b5eb8437090ee3"
url: "https://pub.dev"
source: hosted
version: "1.10.0"
version: "1.9.1"
path:
dependency: transitive
description:
Expand Down Expand Up @@ -203,18 +203,18 @@ packages:
dependency: transitive
description:
name: stack_trace
sha256: "73713990125a6d93122541237550ee3352a2d84baad52d375a4cad2eb9b7ce0b"
sha256: c3c7d8edb15bee7f0f74debd4b9c5f3c2ea86766fe4178eb2a18eb30a0bdaed5
url: "https://pub.dev"
source: hosted
version: "1.11.1"
version: "1.11.0"
stream_channel:
dependency: transitive
description:
name: stream_channel
sha256: ba2aa5d8cc609d96bbb2899c28934f9e1af5cddbd60a827822ea467161eb54e7
sha256: "83615bee9045c1d322bbbd1ba209b7a749c2cbcdcb3fdd1df8eb488b3279c1c8"
url: "https://pub.dev"
source: hosted
version: "2.1.2"
version: "2.1.1"
string_scanner:
dependency: transitive
description:
Expand All @@ -235,10 +235,10 @@ packages:
dependency: transitive
description:
name: test_api
sha256: "5c2f730018264d276c20e4f1503fd1308dfbbae39ec8ee63c5236311ac06954b"
sha256: "75760ffd7786fffdfb9597c35c5b27eaeec82be8edfb6d71d32651128ed7aab8"
url: "https://pub.dev"
source: hosted
version: "0.6.1"
version: "0.6.0"
vector_math:
dependency: transitive
description:
Expand All @@ -251,10 +251,10 @@ packages:
dependency: transitive
description:
name: web
sha256: afe077240a270dcfd2aafe77602b4113645af95d0ad31128cc02bce5ac5d5152
sha256: dc8ccd225a2005c1be616fe02951e2e342092edf968cf0844220383757ef8f10
url: "https://pub.dev"
source: hosted
version: "0.3.0"
version: "0.1.4-beta"
sdks:
dart: ">=3.2.0-194.0.dev <4.0.0"
dart: ">=3.1.0 <4.0.0"
flutter: ">=3.10.0"

0 comments on commit ec61e1b

Please sign in to comment.