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 41 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
2 changes: 2 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
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
2 changes: 1 addition & 1 deletion test/build/min.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('test min build', () => {
const decreaseQuota = 4096;

// feel free to update this value after you've checked that it has changed on purpose :-)
const expectedBytes = 909795;
const expectedBytes = 909799;

expect(actualBytes).toBeLessThan(expectedBytes + increaseQuota);
expect(actualBytes).toBeGreaterThan(expectedBytes - decreaseQuota);
Expand Down
212 changes: 211 additions & 1 deletion test/integration/browser/browser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import puppeteer, {type Page, type Browser} from 'puppeteer';
import st from 'st';
import http, {type Server} from 'http';
import type {AddressInfo} from 'net';
import type {default as MapLibreGL, Map} from '../../../dist/maplibre-gl';
import type {default as MapLibreGL, Map, LngLatBounds} from '../../../dist/maplibre-gl';
import {sleep} from '../../../src/util/test/util';

const testWidth = 800;
Expand Down Expand Up @@ -448,4 +448,214 @@ describe('Browser tests', () => {
expect(center.lng).toBeCloseTo(11.39770);
expect(center.lat).toBeCloseTo(47.29960);
});

test('GeoJSONSource#getBounds: FeatureCollection', async () => {
let testbounds: LngLatBounds;
const bounds = await page.evaluate(async () => {
testbounds = new maplibregl.LngLatBounds([-122.493782, 37.828802, -122.483396, 37.833818]);
const map = new maplibregl.Map({
container: 'map',
style: 'https://demotiles.maplibre.org/style.json',
center: [10, 10],
zoom: 10
});
map.on('load', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Use await once(...) to make less indentation and make sure the test waits from the map to load.

Copy link
Author

Choose a reason for hiding this comment

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

done

Copy link
Author

Choose a reason for hiding this comment

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

It looks like await map.once('load'); doesn't work as expected.

I need to move back to map.on('load' =>{...})

(and we might need to change the behavior of once() to reflect the one of on())

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not sure what you are talking about, the unit tests are filled with await once('load');, here's one for example:

await map.once('load');

Copy link
Author

@AbelVM AbelVM Jan 28, 2025

Choose a reason for hiding this comment

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

I'm just posting the errors the tests gave me (commit 6db23408202feea1a9967c083fb07a06b0a9b8db) in a human-readable way.

I've gone to check that weirdness in the documentation, and those in my message above are the links 🤷

Copy link
Collaborator

Choose a reason for hiding this comment

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

on amd once are different...
Using await on(...) shouldn't work as it's not designed to work this way.
Regarding the tests failures after the once change, the error message indicate that the expected object is "{}" which I find odd, regardless of the test failure. I would advise to try and run this code is a jsbin and see how it works, and after it works as expected, copy it to this file...

Copy link
Collaborator

Choose a reason for hiding this comment

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

getData is using messages to communicate with workers, so it you want to use that, you'll need to mock the workers communication since it's running in node environment and not in the browser.
When running things in the browser I expect it to just work, like it does in the examples we have in the docs, which uses this kind of await once code...

Copy link
Author

Choose a reason for hiding this comment

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

Both bounds and testbounds are being used with their default values because await map.once() is not working as expected and... just doesn't wait 😅 So the flow reaches the assessment but the code to get the bounds has not run

The problem I found with the tests is that running them locally or even building sample code, behaves differently than within the contexts that Jenkins/GH Actions/Whatever uses. I get different errors, messages, and so on. That's why I'm sending too many commits, to getting actionable information from the online tests that I don't get locally

Copy link
Collaborator

Choose a reason for hiding this comment

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

This is not resolved.
Please try and use codespaces if you can't reproduce the issue locally...

Copy link
Author

Choose a reason for hiding this comment

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

Any help is welcome, I wasn't able to make the integration tests work using await map.once().

In that very same file, the pattern is map.once('load', callback) (even when load event is not supported, as per the docs, by map.once)

map.once('load', () => resolve());

map.addSource('testid', {
type: 'geojson',
data: {
'type': 'FeatureCollection',
'features': [{
'type': 'Feature',
'properties': {},
'geometry': {
'type': 'LineString',
'coordinates': [
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can the geometry be simplified (2-3 points)?

Copy link
Author

Choose a reason for hiding this comment

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

Sure, but I've just reused the data in the unit tests

Copy link
Collaborator

Choose a reason for hiding this comment

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

Then the data there probably needs simplification as well...

Copy link
Author

Choose a reason for hiding this comment

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

Done

Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't see it... Did you forget to push?

Copy link
Author

Choose a reason for hiding this comment

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

[-122.483696, 37.833818],
[-122.483482, 37.833174],
[-122.483396, 37.832700],
[-122.483568, 37.832056],
[-122.484040, 37.831141],
[-122.484040, 37.830497],
[-122.483482, 37.829920],
[-122.483568, 37.829548],
[-122.485070, 37.829446],
[-122.486100, 37.828802],
[-122.486958, 37.829310],
[-122.487001, 37.830802],
[-122.487516, 37.831683],
[-122.488031, 37.832158],
[-122.488889, 37.832971],
[-122.489876, 37.832632],
[-122.490434, 37.832937],
[-122.491250, 37.832429],
[-122.491636, 37.832564],
[-122.492237, 37.833378],
[-122.493782, 37.833683]
]
}
}]
}
});
const testsource: maplibregl.GeoJSONSource = map.getSource('testid');
return testsource.getBounds();
});
});
expect(bounds).toEqual(testbounds);
});

test('GeoJSONSource#getBounds: Feature', async () => {
let testbounds: LngLatBounds;
const bounds = await page.evaluate(async () => {
testbounds = new maplibregl.LngLatBounds([-122.493782, 37.828802, -122.483396, 37.833818]);
const map = new maplibregl.Map({
container: 'map',
style: 'https://demotiles.maplibre.org/style.json',
center: [10, 10],
zoom: 10
});
map.on('load', () => {
map.addSource('testid', {
type: 'geojson',
data: {
'type': 'Feature',
'properties': {},
'geometry': {
'type': 'LineString',
'coordinates': [
[-122.483696, 37.833818],
[-122.483482, 37.833174],
[-122.483396, 37.832700],
[-122.483568, 37.832056],
[-122.484040, 37.831141],
[-122.484040, 37.830497],
[-122.483482, 37.829920],
[-122.483568, 37.829548],
[-122.485070, 37.829446],
[-122.486100, 37.828802],
[-122.486958, 37.829310],
[-122.487001, 37.830802],
[-122.487516, 37.831683],
[-122.488031, 37.832158],
[-122.488889, 37.832971],
[-122.489876, 37.832632],
[-122.490434, 37.832937],
[-122.491250, 37.832429],
[-122.491636, 37.832564],
[-122.492237, 37.833378],
[-122.493782, 37.833683]
]
}
}
});
const testsource: maplibregl.GeoJSONSource = map.getSource('testid');
return testsource.getBounds();
});
});
expect(bounds).toEqual(testbounds);
});

test('GeoJSONSource#getBounds: Geometry', async () => {
let testbounds: LngLatBounds;
const bounds = await page.evaluate(async () => {
testbounds = new maplibregl.LngLatBounds([-122.493782, 37.828802, -122.483396, 37.833818]);
const map = new maplibregl.Map({
container: 'map',
style: 'https://demotiles.maplibre.org/style.json',
center: [10, 10],
zoom: 10
});
map.on('load', () => {
map.addSource('testid', {
type: 'geojson',
data: {
'type': 'LineString',
'coordinates': [
[-122.483696, 37.833818],
[-122.483482, 37.833174],
[-122.483396, 37.832700],
[-122.483568, 37.832056],
[-122.484040, 37.831141],
[-122.484040, 37.830497],
[-122.483482, 37.829920],
[-122.483568, 37.829548],
[-122.485070, 37.829446],
[-122.486100, 37.828802],
[-122.486958, 37.829310],
[-122.487001, 37.830802],
[-122.487516, 37.831683],
[-122.488031, 37.832158],
[-122.488889, 37.832971],
[-122.489876, 37.832632],
[-122.490434, 37.832937],
[-122.491250, 37.832429],
[-122.491636, 37.832564],
[-122.492237, 37.833378],
[-122.493782, 37.833683]
]
}
});
const testsource: maplibregl.GeoJSONSource = map.getSource('testid');
return testsource.getBounds();
});
});
expect(bounds).toEqual(testbounds);
});

test('GeoJSONSource#getBounds: GeometryCollection', async () => {
let testbounds: LngLatBounds;
const bounds = await page.evaluate(async () => {
testbounds = new maplibregl.LngLatBounds([-122.493782, 37.828802, -122.483396, 37.833818]);
const map = new maplibregl.Map({
container: 'map',
style: 'https://demotiles.maplibre.org/style.json',
center: [10, 10],
zoom: 10
});
map.on('load', () => {
map.addSource('testid', {
type: 'geojson',
data: {
'type': 'GeometryCollection',
'geometries': [
{
'type': 'LineString',
'coordinates': [
[-122.483696, 37.833818],
[-122.483482, 37.833174],
[-122.483396, 37.832700],
[-122.483568, 37.832056],
[-122.484040, 37.831141],
[-122.484040, 37.830497],
[-122.483482, 37.829920],
[-122.483568, 37.829548],
[-122.485070, 37.829446],
[-122.486100, 37.828802]

]
},
{
'type': 'LineString',
'coordinates': [
[-122.486958, 37.829310],
[-122.487001, 37.830802],
[-122.487516, 37.831683],
[-122.488031, 37.832158],
[-122.488889, 37.832971],
[-122.489876, 37.832632],
[-122.490434, 37.832937],
[-122.491250, 37.832429],
[-122.491636, 37.832564],
[-122.492237, 37.833378],
[-122.493782, 37.833683]
]
}
]}
});
const testsource: maplibregl.GeoJSONSource = map.getSource('testid');
return testsource.getBounds();
});
});
expect(bounds).toEqual(testbounds);
});

});
Loading