Skip to content

fix: limit TopoJson bounds to the selected object - #2311

Open
senanurcetin wants to merge 1 commit into
python-visualization:mainfrom
senanurcetin:fix/topojson-selected-object-bounds
Open

senanurcetin wants to merge 1 commit into
python-visualization:mainfrom
senanurcetin:fix/topojson-selected-object-bounds

Conversation

@senanurcetin

Copy link
Copy Markdown

Summary

This PR fixes an issue in TopoJson.get_bounds() where the calculated bounds include unrelated geometries from the same TopoJSON dataset.

The implementation now calculates bounds using only the geometries referenced by the selected object_path.

Problem

Currently, TopoJson.get_bounds() iterates over every arc in the entire topology, even when those arcs belong to different objects.

This can produce incorrect geographic bounds and cause Map.fit_bounds() to zoom out unnecessarily.

For example, a topology containing two geographically distant objects may return bounds covering both objects, even when only one is selected.

Changes

  • Calculate geographic bounds using only the selected TopoJSON object.
  • Support reversed TopoJSON arc indices.
  • Handle nested GeometryCollection objects.
  • Include Point and MultiPoint coordinates.
  • Correctly process negative coordinate transformation scales.
  • Support absolute coordinates when no transformation is provided.
  • Add regression tests for the affected scenarios.

The existing public API remains unchanged.

Reproduction

import folium

topology = {
    "type": "Topology",
    "objects": {
        "near": {"type": "Polygon", "arcs": [[0]]},
        "far": {"type": "Polygon", "arcs": [[1]]},
    },
    "arcs": [
        [[0, 0], [2, 0], [0, 1], [-2, -1]],
        [[100, 60], [2, 0], [0, 1], [-2, -1]],
    ],
    "transform": {
        "scale": [1, 1],
        "translate": [0, 0],
    },
}

layer = folium.TopoJson(topology, "objects.near")

assert layer.get_bounds() == [[0, 0], [1, 2]]

Previously, the returned bounds also included the geographically distant object.

With this change, the bounds are limited to the selected geometry.

Testing

Five focused regression scenarios were verified locally:

  • Selected-object bounds
  • Reversed arc indices
  • Mixed GeometryCollection geometries
  • Negative transformation scales
  • Absolute coordinates without transformation

Local validation results:

  • 5/5 focused regression tests passed.
  • The tests reproduced failures before the fix.
  • Python compilation checks passed.

Validation was performed using an isolated Folium 0.20.0 environment with the proposed implementation applied.

The complete upstream test suite, Ruff, Black, Sphinx documentation build, and browser integration tests have not yet been executed against the submitted branch. These remain subject to project CI verification.

Compatibility

No public API changes are introduced.

The fix preserves existing usage of TopoJson.get_bounds() while correcting the geographic extent calculation.

AI Assistance Disclosure

AI assistance was used to investigate the existing implementation, identify the bounds calculation issue, prepare the code changes, and develop focused regression tests.

The changes remain subject to project CI verification and maintainer review.

@senanurcetin

Copy link
Copy Markdown
Author

Hi Folium maintainers,

I've submitted a fix to ensure TopoJson.get_bounds() calculates bounds using only the selected topology object.

The PR includes regression tests covering reversed arcs, geometry collections, negative scales, and TopoJSON data without a transform.

I noticed that the GitHub Actions workflows are marked as action_required. If maintainer approval is needed to run the checks, could you please authorize them when convenient?

AI assistance was used in preparing this contribution.

Thank you for reviewing!

senanurcetin added a commit to senanurcetin/folium that referenced this pull request Oct 20, 2026
Decode selected arcs (including reversed indices), geometry collections,
point coordinates, and absolute coordinates without transforms.
Handle negative transforms and add regression tests for map bounds.
@senanurcetin
senanurcetin force-pushed the fix/topojson-selected-object-bounds branch from c222a9b to 9d807fc Compare October 20, 2026 22:25

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant