flutter_map icon indicating copy to clipboard operation
flutter_map copied to clipboard

[BUG] Error when using PolygonLayer with parameter simplificationTolerance not equal to 0>

Open longch opened this issue 1 year ago • 2 comments

What is the bug?

When using PolygonLayer with simplificationTolerance, the program reports an error when sliding or zooming the PolygonLayer.:

I/flutter (26644): zone _handleError 'package:flutter_map/src/misc/point_in_polygon.dart': Failed assertion: line 11 pos 10: 'len >= 3': not a polygon stack #0      _AssertionError._doThrowNew (dart:core-patch/errors_patch.dart:51:61)
I/flutter (26644): #1      _AssertionError._throwNew (dart:core-patch/errors_patch.dart:40:5)
I/flutter (26644): #2      isPointInPolygon (package:flutter_map/src/misc/point_in_polygon.dart:11:10)
I/flutter (26644): #3      _PolygonPainter.elementHitTest.<anonymous closure> (package:flutter_map/src/layer/polygon_layer/painter.dart:86:17)
I/flutter (26644): #4      _PolygonPainter.elementHitTest (package:flutter_map/src/layer/polygon_layer/painter.dart:91:10)
I/flutter (26644): #5      HitDetectablePainter.hitTest (package:flutter_map/src/layer/shared/layer_interactivity/internal_hit_detectable.dart:75:11)
I/flutter (26644): #6      HitTestRequiresCameraOrigin.hitTest (package:flutter_map/src/layer/shared/layer_interactivity/internal_hit_detectable.dart:112:18)
I/flutter (26644): #7      RenderCustomPaint.hitTestSelf (package:flutter/src/rendering/custom_paint.dart:564:43)

Disabling simplificationTolerance will not report an error.

flutter_map: ^7.0.2 flutter_map_geojson: ^1.0.8 maps_toolkit2: ^1.0.1+1

How can we reproduce it?

import 'package:flutter_map/flutter_map.dart';
import 'package:maps_toolkit2/maps_toolkit2.dart';
import 'package:flutter_map_geojson/flutter_map_geojson.dart';

class MapPage extends StatefulWidget {
  const MapPage({super.key});

  @override
  State<MapPage> createState() => MapPageState();
}

class MapPageState extends State<MapPage> {
  final testGeoJson = {
    "type": "FeatureCollection",
    "features": [
      {
        "type": "Feature",
        "geometry": {
          "type": "Polygon",
          "coordinates": [
            [
              [107.07002, 26.32992],
              [107.06982, 26.33182],
              [107.06947, 26.3304],
              [107.06702, 26.32933],
              [107.06563, 26.32643],
              [107.0624, 26.3229],
              [107.06164, 26.3213],
              [107.0618, 26.31898],
              [107.0633, 26.31767],
              [107.06547, 26.31722],
              [107.0666, 26.31653],
              [107.06706, 26.31563],
              [107.06647, 26.31482],
              [107.06523, 26.31429],
              [107.06423, 26.31193],
              [107.06532, 26.30849],
              [107.06511, 26.30724],
              [107.0656, 26.30666],
              [107.06529, 26.30583],
              [107.06446, 26.30503],
              [107.06086, 26.30416],
              [107.06021, 26.30239],
              [107.06102, 26.29904],
              [107.05945, 26.29727],
              [107.06048, 26.29591],
              [107.06063, 26.29444],
              [107.057, 26.29265],
              [107.05471, 26.29256],
              [107.05053, 26.2932],
              [107.05021, 26.29247],
              [107.05675, 26.29185],
              [107.05894, 26.29249],
              [107.0605, 26.29355],
              [107.06115, 26.29453],
              [107.0614, 26.29608],
              [107.06035, 26.29731],
              [107.06149, 26.2991],
              [107.06065, 26.30218],
              [107.06171, 26.30391],
              [107.0648, 26.30464],
              [107.06598, 26.30603],
              [107.06632, 26.30756],
              [107.06507, 26.31064],
              [107.06499, 26.31259],
              [107.06545, 26.31375],
              [107.06693, 26.31449],
              [107.06789, 26.31547],
              [107.06765, 26.31672],
              [107.06733, 26.31643],
              [107.06685, 26.31696],
              [107.06748, 26.3169],
              [107.06606, 26.31776],
              [107.06346, 26.3182],
              [107.06245, 26.31904],
              [107.06207, 26.32069],
              [107.0623, 26.32175],
              [107.06612, 26.32622],
              [107.0672, 26.32867],
              [107.0693, 26.32993],
              [107.07012, 26.32833],
              [107.07104, 26.32759],
              [107.07558, 26.32532],
              [107.07847, 26.32214],
              [107.08065, 26.32067],
              [107.08213, 26.31839],
              [107.08254, 26.31877],
              [107.08478, 26.3188],
              [107.08506, 26.31843],
              [107.08466, 26.31739],
              [107.08516, 26.31691],
              [107.08599, 26.31824],
              [107.08564, 26.31939],
              [107.08612, 26.31995],
              [107.08686, 26.32033],
              [107.08823, 26.32033],
              [107.08814, 26.32083],
              [107.08637, 26.32088],
              [107.0869, 26.32181],
              [107.08795, 26.32302],
              [107.08913, 26.32358],
              [107.09306, 26.32844],
              [107.09367, 26.32863],
              [107.09609, 26.32747],
              [107.09735, 26.3261],
              [107.09752, 26.32507],
              [107.09994, 26.32338],
              [107.1002, 26.32275],
              [107.09983, 26.3212],
              [107.09999, 26.31921],
              [107.09727, 26.31819],
              [107.09706, 26.31695],
              [107.10095, 26.31556],
              [107.10382, 26.31691],
              [107.10645, 26.31878],
              [107.10795, 26.31753],
              [107.10994, 26.31697],
              [107.11128, 26.31591],
              [107.11041, 26.31397],
              [107.10993, 26.31372],
              [107.11054, 26.31227],
              [107.1101, 26.31189],
              [107.1097, 26.30985],
              [107.1102, 26.30861],
              [107.112, 26.30687],
              [107.11201, 26.30467],
              [107.11385, 26.30305],
              [107.11511, 26.30246],
              [107.11566, 26.30123],
              [107.11587, 26.30056],
              [107.11493, 26.29924],
              [107.11526, 26.29823],
              [107.11491, 26.29741],
              [107.11612, 26.29782],
              [107.1167, 26.29855],
              [107.11753, 26.29822],
              [107.11778, 26.29731],
              [107.11879, 26.29742],
              [107.11899, 26.29681],
              [107.12014, 26.29815],
              [107.12048, 26.29783],
              [107.12118, 26.29809],
              [107.12066, 26.2967],
              [107.12192, 26.29661],
              [107.12258, 26.29728],
              [107.12406, 26.29782],
              [107.12326, 26.29834],
              [107.12448, 26.2985],
              [107.12589, 26.29804],
              [107.12725, 26.29841],
              [107.12735, 26.29878],
              [107.1278, 26.29817],
              [107.12876, 26.29815],
              [107.12886, 26.29783],
              [107.13063, 26.29905],
              [107.13197, 26.29915],
              [107.1332, 26.30046],
              [107.13421, 26.3037],
              [107.1329, 26.30395],
              [107.13091, 26.30294],
              [107.12941, 26.30349],
              [107.12901, 26.30274],
              [107.12933, 26.30244],
              [107.12894, 26.30258],
              [107.12966, 26.302],
              [107.12877, 26.30197],
              [107.12829, 26.30165],
              [107.12852, 26.30102],
              [107.12626, 26.30001],
              [107.12546, 26.2999],
              [107.12526, 26.30082],
              [107.12459, 26.30122],
              [107.12233, 26.3006],
              [107.11989, 26.29929],
              [107.12101, 26.29815],
              [107.12051, 26.29787],
              [107.12016, 26.29823],
              [107.11897, 26.29719],
              [107.11884, 26.29776],
              [107.11783, 26.29742],
              [107.11767, 26.2983],
              [107.1168, 26.29869],
              [107.11558, 26.29788],
              [107.11566, 26.29975],
              [107.11637, 26.30081],
              [107.11558, 26.30238],
              [107.11636, 26.3029],
              [107.11621, 26.30343],
              [107.11725, 26.30552],
              [107.11851, 26.30573],
              [107.12018, 26.30508],
              [107.12051, 26.30431],
              [107.12205, 26.30443],
              [107.12238, 26.30415],
              [107.12053, 26.30428],
              [107.12278, 26.30252],
              [107.12491, 26.30438],
              [107.12477, 26.30622],
              [107.12462, 26.30663],
              [107.12351, 26.30638],
              [107.1227, 26.30513],
              [107.12073, 26.30572],
              [107.11949, 26.30661],
              [107.11622, 26.30604],
              [107.11667, 26.3064],
              [107.11511, 26.30568],
              [107.11573, 26.30586],
              [107.11495, 26.30524],
              [107.11559, 26.30401],
              [107.11486, 26.30383],
              [107.11376, 26.30548],
              [107.1131, 26.30788],
              [107.11205, 26.30941],
              [107.11154, 26.30905],
              [107.11108, 26.30943],
              [107.11105, 26.31427],
              [107.11271, 26.31594],
              [107.11155, 26.31797],
              [107.11095, 26.31827],
              [107.11126, 26.3186],
              [107.10964, 26.31925],
              [107.1092, 26.31919],
              [107.10959, 26.31889],
              [107.10813, 26.31954],
              [107.1071, 26.31947],
              [107.10691, 26.31987],
              [107.10599, 26.31923],
              [107.10607, 26.31967],
              [107.10506, 26.32022],
              [107.10426, 26.31975],
              [107.10251, 26.31993],
              [107.10062, 26.31921],
              [107.10062, 26.32106],
              [107.10119, 26.322],
              [107.10062, 26.32138],
              [107.10083, 26.32293],
              [107.10023, 26.32389],
              [107.09874, 26.32486],
              [107.09687, 26.32745],
              [107.0954, 26.32849],
              [107.09319, 26.32926],
              [107.09272, 26.32984],
              [107.09235, 26.32952],
              [107.09305, 26.3291],
              [107.09227, 26.32858],
              [107.09191, 26.32748],
              [107.08924, 26.32433],
              [107.08722, 26.32325],
              [107.08517, 26.32006],
              [107.08351, 26.31931],
              [107.08237, 26.31943],
              [107.08108, 26.32105],
              [107.07863, 26.3226],
              [107.07699, 26.32495],
              [107.0711, 26.32807],
              [107.07002, 26.32992]
            ],
            [
              [107.10453, 26.31875],
              [107.10502, 26.31843],
              [107.1024, 26.31666],
              [107.10038, 26.3163],
              [107.09762, 26.31718],
              [107.09773, 26.31776],
              [107.0985, 26.3181],
              [107.09922, 26.31704],
              [107.10116, 26.31752],
              [107.10331, 26.31734],
              [107.10453, 26.31875]
            ],
            [
              [107.10529, 26.3199],
              [107.10553, 26.31942],
              [107.10516, 26.31928],
              [107.10474, 26.31991],
              [107.10517, 26.31965],
              [107.10529, 26.3199]
            ]
          ]
        },
      }
    ]
  };

  final mapController = MapController();
  GeoJsonParser? myGeoJson;

  @override
  void initState() {
    super.initState();
    init();
  }

  @override
  void dispose() {
    mapController.dispose();
    super.dispose();
  }

  void init() {
    myGeoJson = GeoJsonParser(
      polygonCreationCallback: (points, holePointsList, properties) {
        return Polygon(
          points: points,
          holePointsList: holePointsList,
          borderColor: Colors.green,
          color: Colors.blue.withAlpha(50),
          borderStrokeWidth: 2,
        );
      },
    );
    myGeoJson!.parseGeoJson(testGeoJson);
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: FlutterMap(
        mapController: mapController,
        options: const MapOptions(
          initialCenter: LatLng(26.32992, 107.07002),
          initialZoom: 8,
          minZoom: 3,
          maxZoom: 20,
          keepAlive: true,
          backgroundColor: Colors.transparent,
          interactionOptions: InteractionOptions(
            rotationWinGestures: MultiFingerGesture.none,
            flags: InteractiveFlag.drag | InteractiveFlag.pinchZoom | InteractiveFlag.doubleTapZoom,
          ),
        ),
        children: [
          Stack(
            children: [
              TileLayer(
                urlTemplate: 'https://wprd{s}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&style=7&x={x}&y={y}&z={z}',
                keepBuffer: 5,
                subdomains: const ['01', '02', '03', '04'],
                evictErrorTileStrategy: EvictErrorTileStrategy.notVisible,
              )
            ],
          ),
          if (myGeoJson != null)
            PolygonLayer(
              polygonCulling: true,
              polygons: myGeoJson!.polygons,
              simplificationTolerance: 0.3,
            ),
        ],
      ),
    );
  }
}

Do you have a potential solution?

No response

Platforms

Android 11 HonorX50

Severity

Minimum: Allows normal functioning

longch avatar Jul 17 '24 05:07 longch

Thanks for the report!

This occurs due to a recent addition of an assertion into the method used to test whether a point is within a polygon, which actually checks whether the outline points form a polygon (there are at least 3). I believe that the simplification may result in some lines, or some other artefacts which now trigger this assertion, whereas before they would've failed gracefully and just returned false.

We'll look into it :)

JaffaKetchup avatar Aug 02 '24 21:08 JaffaKetchup

@JaffaKetchup potentially an easy solution would be to remove polygons with less than 3 lines from hit testing (considering it "too thin"). Or using a line-distance function instead

mootw avatar Aug 15 '24 19:08 mootw