Skip to content

fix: LatLngBounds overlap calculation#2190

Open
stani-m wants to merge 1 commit intofleaflet:masterfrom
stani-m:fix/latlngbounds-overlap
Open

fix: LatLngBounds overlap calculation#2190
stani-m wants to merge 1 commit intofleaflet:masterfrom
stani-m:fix/latlngbounds-overlap

Conversation

@stani-m
Copy link
Copy Markdown

@stani-m stani-m commented Mar 27, 2026

Original calculation was incorrect and could claim that two non-overlapping bounding boxes overlapped, such as in case of the bounding boxes defined in this geojson

Boxes
{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              18.286332,
              49.622376
            ],
            [
              18.286332,
              48.721399
            ],
            [
              20.122905,
              48.721399
            ],
            [
              20.122905,
              49.622376
            ],
            [
              18.286332,
              49.622376
            ]
          ]
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              20.58826958952466,
              49.33295874620594
            ],
            [
              20.58826958952466,
              49.22512969150049
            ],
            [
              20.676160214524646,
              49.22512969150049
            ],
            [
              20.676160214524646,
              49.33295874620594
            ],
            [
              20.58826958952466,
              49.33295874620594
            ]
          ]
        ]
      }
    }
  ]
}
Loading

This PR fixes the calculation using a version of the formula for determining whether two circles overlap (which the original code seemed to be attempting to do) by

  1. Calculating distance between centers of the circles, or in this case centers of intervals along the longitudinal axis delta = this.longitudeCenter - other.longitudeCenter
  2. Adding together radii of the two circles or in this case adding halves of longitudinal width of the bounding boxes (this.longitudeWidth + other.longitudeWidth) / 2
  3. When distance between centers is greater than sum of their radii they don't overlap, when it's smaller they overlap

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