Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Add toggle for viewport filtering in vector layers #1997

Closed
tchahnson opened this issue Dec 11, 2024 · 7 comments
Closed

[FEATURE] Add toggle for viewport filtering in vector layers #1997

tchahnson opened this issue Dec 11, 2024 · 7 comments
Labels
feature This issue requests a new feature invalid This bug could not be reproduced or does not exist, or is very low quality S: core Scoped to the core flutter_map functionality

Comments

@tchahnson
Copy link

What do you want implemented?

I propose adding a boolean toggle (drawToViewport) to PolygonLayerOptions (and possibly other vector layers like PolylineLayerOptions). This would allow developers to enable or disable viewport-based filtering of vector shapes (e.g., polygons, polylines).

This feature would:

  1. Enhance flexibility: Allow applications to toggle viewport filtering as per their specific requirements.
  2. Improve debugging: Developers could easily toggle filtering to troubleshoot rendering or dataset issues.
  3. Support broader use cases: Some applications (e.g., analytical dashboards) may prefer to render all data regardless of performance trade-offs.

Details:

Currently, vector layers such as polygons are filtered based on the viewport to improve performance when displaying large datasets. While this is useful, there are scenarios where developers may want to display all vector shapes regardless of their visibility within the viewport. For example:

  • Showing an overview of all data on the map at a lower resolution.
  • Performing visual debugging or analytical tasks that require all polygons to be rendered.

By introducing a drawToViewport boolean, developers can explicitly control whether to filter vector data by the viewport.

What other alternatives are available?

Currently, the only workaround is to modify the logic in _updateDisplayedPolygons (or similar methods) to disable filtering. However, this approach:

  1. Requires manual changes to application code or library source.
  2. Reduces maintainability and could break when updating the flutter_map library.

Introducing this feature at the library level ensures a standardized, reusable, and maintainable solution.

Can you provide any other information?

Here’s an example of how the drawToViewportoption could be implemented:

  1. Adding the toggle to layer options:

class PolygonLayerOptions {
  final bool drawToViewport;

  PolygonLayerOptions({
    required this.polygons,
    this.drawToViewport = true, // Defaults to true for backward compatibility
    ...
  });
}

  1. Updating filtering logic in _updateDisplayedPolygons:

    if (drawToViewport) {
      // Filter polygons based on viewport
      _displayedPolygons = currentPolygons
          .where((p) => _polygonIntersectsBounds(
                p,
                topLeftLatLng.latitude,
                bottomRightLatLng.latitude,
                topLeftLatLng.longitude,
                bottomRightLatLng.longitude,
              ))
          .toList();
    } else {
      // Skip filtering and display all polygons
      _displayedPolygons = currentPolygons;
    }

This change would be backward-compatible, as drawToViewportwould default to true.

Severity

Annoying: Currently have to use workarounds

@tchahnson tchahnson added the feature This issue requests a new feature label Dec 11, 2024
@JaffaKetchup
Copy link
Member

JaffaKetchup commented Dec 11, 2024

Hi @tchahnson,
Thanks for submitting a feature request. I recommend not using AI in situations such as this, as it may often lack context or apply to an old version, both of which are true here.
This feature already exists for both PolylineLayer (see .cullingMargin) and PolygonLayer (see .polygonCulling). Documentation can be found in your IDE or at https://pub.dev/documentation/flutter_map/latest/flutter_map.
If you need any help, please feel free to join our Discord server!

@JaffaKetchup JaffaKetchup closed this as not planned Won't fix, can't repro, duplicate, stale Dec 11, 2024
@github-project-automation github-project-automation bot moved this from To do to Done in Development Dec 11, 2024
@tchahnson
Copy link
Author

tchahnson commented Dec 11, 2024

Thank you for the clarification @JaffaKetchup !

@JaffaKetchup JaffaKetchup added invalid This bug could not be reproduced or does not exist, or is very low quality S: core Scoped to the core flutter_map functionality labels Dec 11, 2024
@tchahnson
Copy link
Author

tchahnson commented Dec 12, 2024

Hi @JaffaKetchup

I have attempted to test the culling, and I have found that it seems to make no effective improvements in performance.

FlutterMap(
  mapController: _mapController,
  options: const MapOptions(
    initialCenter: LatLng(42.0, -71.0),
    initialZoom: 7.0,
    interactionOptions: InteractionOptions(
      flags: InteractiveFlag.all, // Enable all interactions
    ),
  ),
  children: [
    mapLayer,
    if (_showVectorData &&
        (_lowResPolygons.isNotEmpty ||
            _highResPolygons.isNotEmpty))
      PolygonLayer(
        polygons:
            _useHighRes ? _highResPolygons : _lowResPolygons,
        polygonCulling: true,
      ),
  ],
),
                
                

And for the following comparison of my version (code not included in comment) [left] vs the out the box culling enabled (as per code seen above) [right], I have found that the performance and speed is considerably improved with my method.

I understand that the 'polygons & total edges displayed' may not reflect the actual rendered shapes and that the shapes may be cached instead for performance. As I am unable to optimize the performance, may I ask for assistance with diagnostics to better configure flutter_map for performance?

image

image

@josxha
Copy link
Contributor

josxha commented Dec 13, 2024

Hello @tchahnson, can you elaborate a bit how _highResPolygons and _lowResPolygons get created in your example please? Are these generated at runtime?

@tchahnson
Copy link
Author

The _highResPolygons and _lowResPolygons are initialized as empty lists in the state of the screen and are generated dynamically at runtime. The data is initially loaded as full-resolution shapes (high resolution). These shapes are then reduced in quality using a combination of Radial Distance Simplification and Douglas-Peucker Simplification to create simplified versions for lower resolutions.

In the example provided, as the resolution of vector data is dynamically adjusted based on the zoom level, only the higher resolution polygons are used.

@josxha
Copy link
Contributor

josxha commented Dec 14, 2024

Is is possible that you mix up culling and simplification? flutter_map has a simplification algorithm as well that can be configured with the simplificationTolerance parameter but your feature request is about polygon culling, right?

@tchahnson
Copy link
Author

Yes, the polygons generated in my examples are identical between the two scenarios, and my request is specific to the rendering of polygons dependent on their inclusion in the view window.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature This issue requests a new feature invalid This bug could not be reproduced or does not exist, or is very low quality S: core Scoped to the core flutter_map functionality
Projects
Status: Done
Development

No branches or pull requests

3 participants