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

Added geoJSON source getBounds method #5403

Open
wants to merge 43 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
427b8b8
added geoJSON source getBounds method
AbelVM Jan 24, 2025
4447079
Bump dependabot/fetch-metadata from 2.2.0 to 2.3.0 (#141)
dependabot[bot] Jan 27, 2025
424ab5c
Bump stylelint from 16.13.2 to 16.14.0 (#142)
dependabot[bot] Jan 27, 2025
4790824
Bump @vitest/ui from 3.0.3 to 3.0.4 (#143)
dependabot[bot] Jan 27, 2025
ad8cf0a
geoJSON source getBounds method, tweaks 1
AbelVM Jan 27, 2025
15c6d00
Merge branch 'maplibre:main' into main
AbelVM Jan 27, 2025
e53ead9
Merge remote-tracking branch 'refs/remotes/origin/main'
AbelVM Jan 27, 2025
7a6e82f
geoJSON source getBounds method, change from promise to raw bounds
AbelVM Jan 27, 2025
3016268
Merge branch 'main' into main
HarelM Jan 27, 2025
a9640a4
geoJSON source getBounds method, fixed vitest version
AbelVM Jan 27, 2025
038fc08
geoJSON source getBounds method, overwrite packaje.json files from so…
AbelVM Jan 27, 2025
0a161f1
geoJSON source getBounds method, change back function type to promise
AbelVM Jan 27, 2025
6ce70fb
geoJSON source getBounds method, typing enforcement
AbelVM Jan 27, 2025
d8ac930
geoJSON source getBounds method, support single features
AbelVM Jan 27, 2025
f805489
geoJSON source getBounds method, cover any GeoJSON case
AbelVM Jan 27, 2025
ac7afa1
geoJSON source getBounds method, async test
AbelVM Jan 27, 2025
c112544
geoJSON source getBounds method, fixed tests and removed async stuff
AbelVM Jan 27, 2025
6c3e0cd
Bump vitest from 3.0.3 to 3.0.4 (#144)
dependabot[bot] Jan 28, 2025
7455aff
Bump pbf from 3.3.0 to 4.0.1 (#146)
dependabot[bot] Jan 28, 2025
91a8193
Bump @vitest/coverage-v8 from 3.0.3 to 3.0.4 (#147)
dependabot[bot] Jan 28, 2025
944c5d9
Bump @typescript-eslint/eslint-plugin from 8.21.0 to 8.22.0 (#148)
dependabot[bot] Jan 28, 2025
873b344
Bump @types/node from 22.10.10 to 22.12.0 (#149)
dependabot[bot] Jan 28, 2025
a471b2c
Bump @typescript-eslint/parser from 8.21.0 to 8.22.0 (#145)
dependabot[bot] Jan 28, 2025
2c493a1
geoJSON source getBounds method, back to async, still errors
AbelVM Jan 28, 2025
3a2e5c7
geoJSON source getBounds method, added missing unit tests
AbelVM Jan 28, 2025
8c60411
geoJSON source getBounds method, move tests from unit to integration
AbelVM Jan 28, 2025
0c49eda
geoJSON source getBounds method, move tests from unit to integration,…
AbelVM Jan 28, 2025
fffd20d
geoJSON source getBounds method, move tests from unit to integration,…
AbelVM Jan 28, 2025
c629dee
Merge branch 'main' into main
AbelVM Jan 28, 2025
059b744
geoJSON source getBounds method, move tests from unit to integration,…
AbelVM Jan 28, 2025
f33c64a
geoJSON source getBounds method, move tests from unit to integration,…
AbelVM Jan 28, 2025
6fd45ca
geoJSON source getBounds method, scoping tests
AbelVM Jan 28, 2025
d2642b7
geoJSON source getBounds method, scoping tests
AbelVM Jan 28, 2025
3cce7b8
geoJSON source getBounds method, added all tests as browser tests
AbelVM Jan 28, 2025
bbe2976
geoJSON source getBounds method, added all tests as browser tests
AbelVM Jan 28, 2025
cde489c
geoJSON source getBounds method, expectedBytes += 4
AbelVM Jan 28, 2025
9b5274c
geoJSON source getBounds method, trunked coordinates and other minor…
AbelVM Jan 28, 2025
6db2340
geoJSON source getBounds method, once => on
AbelVM Jan 28, 2025
97ed09c
geoJSON source getBounds method, once => on
AbelVM Jan 28, 2025
7f41246
geoJSON source getBounds method, back to old load event managing
AbelVM Jan 28, 2025
47c0856
Merge branch 'main' into main
AbelVM Jan 30, 2025
d323876
geoJSON source getBounds method, remove commented lines
AbelVM Jan 30, 2025
99a7b9e
Merge remote-tracking branch 'refs/remotes/origin/main'
AbelVM Jan 30, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### ✨ Features and improvements

- Added `getBounds()` method to `GeoJSONSource` ([#5403](https://github.com/maplibre/maplibre-gl-js/pull/5403))
- _...Add new stuff here..._

### 🐞 Bug fixes
Expand Down
32 changes: 32 additions & 0 deletions src/source/geojson_source.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {SubdivisionGranularitySetting} from '../render/subdivision_granularity_s
import {type ActorMessage, MessageType} from '../util/actor_messages';
import {type Actor} from '../util/actor';
import {MercatorTransform} from '../geo/projection/mercator_transform';
/* import {LngLatBounds} from '../geo/lng_lat_bounds';
AbelVM marked this conversation as resolved.
Show resolved Hide resolved
import type {FeatureCollection} from 'geojson'; */

const wrapDispatcher = (dispatcher) => {
return {
Expand Down Expand Up @@ -506,3 +508,33 @@ describe('GeoJSONSource#serialize', () => {
});
});
});
/*
describe('GeoJSONSource#getBounds', () => {
const probe = hawkHill as FeatureCollection;
test('FeatureCollection', async () => {
const source = new GeoJSONSource('id', {data: probe} as GeoJSONSourceOptions, mockDispatcher, undefined);
const testbounds = new LngLatBounds([-122.49378204345702, 37.82880236636284, -122.48339653015138, 37.83381888486939]);
const bounds = await source.getBounds();
expect(bounds).toEqual(testbounds);
});
test('Feature', async () => {
const source = new GeoJSONSource('id', {data: probe.features[0]} as GeoJSONSourceOptions, mockDispatcher, undefined);
const testbounds = new LngLatBounds([-122.49378204345702, 37.82880236636284, -122.48339653015138, 37.83381888486939]);
const bounds = await source.getBounds();
expect(bounds).toEqual(testbounds);
});
test('GeometryCollection', async () => {
const geometrycollection = {'type': 'GeometryCollection', 'geometries': [probe.features[0].geometry, probe.features[0].geometry]};
const source = new GeoJSONSource('id', {data: geometrycollection} as GeoJSONSourceOptions, mockDispatcher, undefined);
const testbounds = new LngLatBounds([-122.49378204345702, 37.82880236636284, -122.48339653015138, 37.83381888486939]);
const bounds = await source.getBounds();
expect(bounds).toEqual(testbounds);
});
test('Geometry', async () => {
const source = new GeoJSONSource('id', {data: probe.features[0].geometry} as GeoJSONSourceOptions, mockDispatcher, undefined);
const testbounds = new LngLatBounds([-122.49378204345702, 37.82880236636284, -122.48339653015138, 37.83381888486939]);
const bounds = await source.getBounds();
expect(bounds).toEqual(testbounds);
});
});
*/
44 changes: 44 additions & 0 deletions src/source/geojson_source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import type {GeoJSONWorkerOptions, LoadGeoJSONParameters} from './geojson_worker_source';
import {type WorkerTileParameters} from './worker_source';
import {MessageType} from '../util/actor_messages';
import {LngLatBounds} from '../geo/lng_lat_bounds';
import type {Point, MultiPoint, LineString, MultiLineString, Polygon, MultiPolygon} from 'geojson';

/**
* Options object for GeoJSONSource.
Expand Down Expand Up @@ -249,6 +251,48 @@
return this.actor.sendAsync({type: MessageType.getData, data: options});
}

/**
* Allows to get the source's actual boundaries.
*
* @returns a promise which resolves to the source's actual boundaries
*/

async getBounds(): Promise<LngLatBounds> {
type justGeometry = Point | MultiPoint | LineString | MultiLineString | Polygon | MultiPolygon;
const bounds: LngLatBounds = new LngLatBounds();
const data: GeoJSON.GeoJSON = await this.getData();
let coordinates: number[];
if (!!data) return bounds;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure this is what I would expect as a return value...

Copy link
Author

@AbelVM AbelVM Jan 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO, if the source has no data, its bounding box should be empty

switch(data.type){
case 'Feature':
if (data.geometry.type === 'GeometryCollection') {
coordinates = data.geometry.geometries.map((g: justGeometry) => g.coordinates).flat(Infinity) as number[];
}else{
coordinates = data.geometry.coordinates.flat(Infinity) as number[];
}
break;
case 'FeatureCollection':
coordinates = data.features.map(f => {
if (f.geometry.type === 'GeometryCollection') {
return f.geometry.geometries.map((g: justGeometry) => g.coordinates);
}else{
return f.geometry.coordinates;
}
}).flat(Infinity) as number[];
break;
case 'GeometryCollection':
coordinates = data.geometries.map((g: justGeometry) => g.coordinates).flat(Infinity) as number[];
break;
default:
coordinates = data.coordinates.flat(Infinity) as number[];
}
if(coordinates.length == 0) return bounds;
for(let i = 0; i < coordinates.length - 1; i += 2){
bounds.extend([coordinates[i], coordinates[i+1]]);
}
return bounds;
}

Check warning on line 294 in src/source/geojson_source.ts

View check run for this annotation

Codecov / codecov/patch

src/source/geojson_source.ts#L261-L294

Added lines #L261 - L294 were not covered by tests

/**
* To disable/enable clustering on the source options
* @param options - The options to set
Expand Down
81 changes: 81 additions & 0 deletions test/integration/browser/browser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,58 @@
import type {default as MapLibreGL, Map} from '../../../dist/maplibre-gl';
import {sleep} from '../../../src/util/test/util';

import {type Actor} from '../../../src/util/actor';
AbelVM marked this conversation as resolved.
Show resolved Hide resolved
import {type Dispatcher} from '../../../src/util/dispatcher';
import {GeoJSONSource, type GeoJSONSourceOptions} from '../../../src/source/geojson_source';
import {LngLatBounds} from '../../../src/geo/lng_lat_bounds';
import type {FeatureCollection} from 'geojson';

const wrapDispatcher = (dispatcher) => {
return {
getActor() {
return dispatcher as Actor;
}
} as Dispatcher;
};

const mockDispatcher = wrapDispatcher({
sendAsync() { return Promise.resolve({}); }
});

const hawkHill = {
'type': 'FeatureCollection',
'features': [{
'type': 'Feature',
'properties': {},
'geometry': {
'type': 'LineString',
'coordinates': [
[-122.48369693756104, 37.83381888486939],
[-122.48348236083984, 37.83317489144141],
[-122.48339653015138, 37.83270036637107],
[-122.48356819152832, 37.832056363179625],
[-122.48404026031496, 37.83114119107971],
[-122.48404026031496, 37.83049717427869],
[-122.48348236083984, 37.829920943955045],
[-122.48356819152832, 37.82954808664175],
[-122.48507022857666, 37.82944639795659],
[-122.48610019683838, 37.82880236636284],
[-122.48695850372314, 37.82931081282506],
[-122.48700141906738, 37.83080223556934],
[-122.48751640319824, 37.83168351665737],
[-122.48803138732912, 37.832158048267786],
[-122.48888969421387, 37.83297152392784],
[-122.48987674713133, 37.83263257682617],
[-122.49043464660643, 37.832937629287755],
[-122.49125003814696, 37.832429207817725],
[-122.49163627624512, 37.832564787218985],
[-122.49223709106445, 37.83337825839438],
[-122.49378204345702, 37.83368330777276]
]
}
}]
} as GeoJSON.GeoJSON;

const testWidth = 800;
const testHeight = 600;
const deviceScaleFactor = 2;
Expand Down Expand Up @@ -449,3 +501,32 @@
expect(center.lat).toBeCloseTo(47.29960);
});
});

describe('GeoJSONSource#getBounds', () => {
const probe = hawkHill as FeatureCollection;
test('FeatureCollection', async () => {
const source = new GeoJSONSource('id', {data: probe} as GeoJSONSourceOptions, mockDispatcher, undefined);
const testbounds = new LngLatBounds([-122.49378204345702, 37.82880236636284, -122.48339653015138, 37.83381888486939]);
const bounds = await source.getBounds();
expect(bounds).toEqual(testbounds);

Check failure on line 511 in test/integration/browser/browser.test.ts

View workflow job for this annotation

GitHub Actions / Integration tests (ubuntu-latest)

test/integration/browser/browser.test.ts > GeoJSONSource#getBounds > FeatureCollection

AssertionError: expected LngLatBounds{} to deeply equal LngLatBounds{ …(2) } - Expected + Received - LngLatBounds { - "_ne": LngLat { - "lat": 37.83381888486939, - "lng": -122.48339653015138, - }, - "_sw": LngLat { - "lat": 37.82880236636284, - "lng": -122.49378204345702, - }, - } + LngLatBounds {} ❯ test/integration/browser/browser.test.ts:511:24
});
test('Feature', async () => {
const source = new GeoJSONSource('id', {data: probe.features[0]} as GeoJSONSourceOptions, mockDispatcher, undefined);
const testbounds = new LngLatBounds([-122.49378204345702, 37.82880236636284, -122.48339653015138, 37.83381888486939]);
const bounds = await source.getBounds();
expect(bounds).toEqual(testbounds);

Check failure on line 517 in test/integration/browser/browser.test.ts

View workflow job for this annotation

GitHub Actions / Integration tests (ubuntu-latest)

test/integration/browser/browser.test.ts > GeoJSONSource#getBounds > Feature

AssertionError: expected LngLatBounds{} to deeply equal LngLatBounds{ …(2) } - Expected + Received - LngLatBounds { - "_ne": LngLat { - "lat": 37.83381888486939, - "lng": -122.48339653015138, - }, - "_sw": LngLat { - "lat": 37.82880236636284, - "lng": -122.49378204345702, - }, - } + LngLatBounds {} ❯ test/integration/browser/browser.test.ts:517:24
});
test('GeometryCollection', async () => {
const geometrycollection = {'type': 'GeometryCollection', 'geometries': [probe.features[0].geometry, probe.features[0].geometry]};
const source = new GeoJSONSource('id', {data: geometrycollection} as GeoJSONSourceOptions, mockDispatcher, undefined);
const testbounds = new LngLatBounds([-122.49378204345702, 37.82880236636284, -122.48339653015138, 37.83381888486939]);
const bounds = await source.getBounds();
expect(bounds).toEqual(testbounds);

Check failure on line 524 in test/integration/browser/browser.test.ts

View workflow job for this annotation

GitHub Actions / Integration tests (ubuntu-latest)

test/integration/browser/browser.test.ts > GeoJSONSource#getBounds > GeometryCollection

AssertionError: expected LngLatBounds{} to deeply equal LngLatBounds{ …(2) } - Expected + Received - LngLatBounds { - "_ne": LngLat { - "lat": 37.83381888486939, - "lng": -122.48339653015138, - }, - "_sw": LngLat { - "lat": 37.82880236636284, - "lng": -122.49378204345702, - }, - } + LngLatBounds {} ❯ test/integration/browser/browser.test.ts:524:24
});
test('Geometry', async () => {
const source = new GeoJSONSource('id', {data: probe.features[0].geometry} as GeoJSONSourceOptions, mockDispatcher, undefined);
const testbounds = new LngLatBounds([-122.49378204345702, 37.82880236636284, -122.48339653015138, 37.83381888486939]);
const bounds = await source.getBounds();
expect(bounds).toEqual(testbounds);

Check failure on line 530 in test/integration/browser/browser.test.ts

View workflow job for this annotation

GitHub Actions / Integration tests (ubuntu-latest)

test/integration/browser/browser.test.ts > GeoJSONSource#getBounds > Geometry

AssertionError: expected LngLatBounds{} to deeply equal LngLatBounds{ …(2) } - Expected + Received - LngLatBounds { - "_ne": LngLat { - "lat": 37.83381888486939, - "lng": -122.48339653015138, - }, - "_sw": LngLat { - "lat": 37.82880236636284, - "lng": -122.49378204345702, - }, - } + LngLatBounds {} ❯ test/integration/browser/browser.test.ts:530:24
});
});
Loading