Skip to content

Select methods for adding/removing features #16923

Merged
ahocevar merged 6 commits into
openlayers:mainfrom
laundmo:select-interaction-methods
Jul 4, 2025
Merged

Select methods for adding/removing features #16923
ahocevar merged 6 commits into
openlayers:mainfrom
laundmo:select-interaction-methods

Conversation

@laundmo

@laundmo laundmo commented Jun 20, 2025

Copy link
Copy Markdown
Contributor

This PR fixes #16918 and fixes #16597

by adding the following (public) methods to the Select interaction:

  • selectFeature(feature) - selects a feature as if the user clicked it, only if it passes the filters (layer and filter function). sends a SelectEvent if its selected
  • deselectFeature(feature) - deselects a feature if its currently selected, sending a SelectEvent if it got deselected
  • toggleFeature(feature) - toggles a feature on and off, sends a SelectEvent depending on what happened
  • clearSelection() - remove all features from selection, sends a SelectEvent with all of them in .deselected

To accomplish this, i've added some private methods and migrated the mapBrowserEvent handling code to using them.

Examples are updated, i've utilized more features of Select for some of them.

Tests aren't run yet, due to issues (see below)

@laundmo

laundmo commented Jun 20, 2025

Copy link
Copy Markdown
Contributor Author

I was not able to run (and therefore write) tests, every single browser test was timing out after 2500ms with Error: Timeout of 2500ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.. I have no idea why this is happening and would appreciate any help.

Heres a longer excerpt of running the tests

Chrome Headless 137.0.0.0 (Windows 10) ol/source/GeoTIFF loading manages load states FAILED
        Error: Timeout of 2500ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
ERROR: AggregateError: Request failed
AggregateError: Request failed
    at BlockedSource.fetch (http://localhost:9876/absoluteC:/Users/laundmo/AppData/Local/Temp/_karma_webpack_773544/webpack:/ol/node_modules/geotiff/dist-module/source/blockedsource.js:153:1)
    at GeoTIFF.fromSource (http://localhost:9876/absoluteC:/Users/laundmo/AppData/Local/Temp/_karma_webpack_773544/webpack:/ol/node_modules/geotiff/dist-module/geotiff.js:548:1)
    at async Promise.all (index 0)
Chrome Headless 137.0.0.0 (Windows 10) ol/source/GeoTIFF loading configures itself from source metadata FAILED
        Error: Timeout of 2500ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
ERROR: AggregateError: Request failed
AggregateError: Request failed
    at BlockedSource.fetch (http://localhost:9876/absoluteC:/Users/laundmo/AppData/Local/Temp/_karma_webpack_773544/webpack:/ol/node_modules/geotiff/dist-module/source/blockedsource.js:153:1)
    at GeoTIFF.fromSource (http://localhost:9876/absoluteC:/Users/laundmo/AppData/Local/Temp/_karma_webpack_773544/webpack:/ol/node_modules/geotiff/dist-module/geotiff.js:548:1)
    at async Promise.all (index 0)
Chrome Headless 137.0.0.0 (Windows 10) ol/source/GeoTIFF loading resolves view properties FAILED
        Error: Timeout of 2500ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
ERROR: AggregateError: Request failed
AggregateError: Request failed
    at BlockedSource.fetch (http://localhost:9876/absoluteC:/Users/laundmo/AppData/Local/Temp/_karma_webpack_773544/webpack:/ol/node_modules/geotiff/dist-module/source/blockedsource.js:153:1)
    at GeoTIFF.fromSource (http://localhost:9876/absoluteC:/Users/laundmo/AppData/Local/Temp/_karma_webpack_773544/webpack:/ol/node_modules/geotiff/dist-module/geotiff.js:548:1)
    at async Promise.all (index 0)
Chrome Headless 137.0.0.0 (Windows 10) ol/source/GeoTIFF loading loads tiles FAILED
        Error: Timeout of 2500ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
................................................................................
..
ERROR: DOMException{}
.
ERROR: DOMException{}
Chrome Headless 137.0.0.0 (Windows 10) ol/layer/Image getData() should detect pixels in the layer extent FAILED
        Error: expected null to be an instance of Uint8ClampedArray
            at Assertion.assert (D:/projects/my/working/dir/openlayers/node_modules/expect.js/index.js:96:13)
            at Assertion.a.Assertion.an (D:/projects/my/working/dir/openlayers/node_modules/expect.js/index.js:279:12)
            at Assertion.<computed>.a (D:/projects/my/working/dir/openlayers/node_modules/expect.js/index.js:499:17)
            at Context.<anonymous> (C:/Users/laundmo/AppData/Local/Temp/_karma_webpack_773544/webpack:/ol/test/browser/spec/ol/layer/Image.test.js:66:1)
.........
ERROR: DOMException{}
.
ERROR: DOMException{}
ERROR: DOMException{}
ERROR: DOMException{}
ERROR: DOMException{}
.......
ERROR: DOMException{}
ERROR: DOMException{}
ERROR: DOMException{}
Chrome Headless 137.0.0.0 (Windows 10) ol/renderer/canvas/ImageLayer #getData "before each" hook for "properly detects pixels" FAILED
        Error: Timeout of 2500ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
...
ERROR: DOMException{}
Chrome Headless 137.0.0.0 (Windows 10) ol/renderer/canvas/ImageLayer Image rendering "before each" hook for "dispatches prerender and postrender events on the image layer" FAILED
        Error: Timeout of 2500ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
.
ERROR: DOMException{}
Chrome Headless 137.0.0.0 (Windows 10) ol/renderer/canvas/ImageLayer renderFrame does not render if layer extent does not intersect view extent FAILED
        Error: Timeout of 2500ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
ERROR: DOMException{}
Chrome Headless 137.0.0.0 (Windows 10) ol/renderer/canvas/ImageLayer renderFrame renders if layer extent partially intersects view extent FAILED
        Error: Timeout of 2500ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
ERROR: DOMException{}
Chrome Headless 137.0.0.0 (Windows 10) ol/renderer/canvas/ImageLayer renderFrame renders without clipping when layer extent covers view FAILED
        Error: Timeout of 2500ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
ERROR: DOMException{}
Chrome Headless 137.0.0.0 (Windows 10) ol/renderer/canvas/ImageLayer renderFrame resets image when empty FAILED
        Error: Timeout of 2500ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
.......
ERROR: DOMException{}
Chrome Headless 137.0.0.0 (Windows 10) ol/source/ImageStatic #getImage scales image height to fit imageExtent FAILED
        Error: Timeout of 2500ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
ERROR: DOMException{}
Chrome Headless 137.0.0.0 (Windows 10) ol/source/ImageStatic #getImage scales image width to fit imageExtent FAILED
        Error: Timeout of 2500ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
ERROR: DOMException{}
Chrome Headless 137.0.0.0 (Windows 10) ol/source/ImageStatic #getImage Had an imageSize option which changed the imageExtent when used with incorrect image sizes FAILED
        Error: Timeout of 2500ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
ERROR: DOMException{}
Chrome Headless 137.0.0.0 (Windows 10) ol/source/ImageStatic #getImage triggers image load events FAILED
        Error: Timeout of 2500ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
        

@laundmo laundmo closed this Jun 20, 2025
@laundmo laundmo reopened this Jun 20, 2025
@ahocevar ahocevar changed the title Implement #16918 - Select methods for adding/removing features Select methods for adding/removing features Jun 20, 2025
@github-actions

Copy link
Copy Markdown

📦 Preview the website for this branch here: https://deploy-preview-16923--ol-site.netlify.app/.

@ahocevar ahocevar left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for your contribution, @laundmo. I triggered the CI tests, so you can see the test results - several tests are failing.

I don't know why the tests don't run for you. Did npm install run without any issues? Does npm run karma work? Maybe your Windows installation is locked down so some browser features are blocked?

Simplifying examples is ok, but changing their behavior and thus making them more complex is not so good. A dedicated new example that shows the new features of the Select interaction would be better.

Comment thread .gitattributes
Comment on lines +1 to +4
*.js text eol=lf
*.cjs text eol=lf
*.ts text eol=lf
*.mjs text eol=lf No newline at end of file

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please add these settings to your local git config, not to the repository.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

well, its the repos prettier config which causes it to error on every single line with the default git settings on windows (convert LF to CRLF on pull, and back to LF on push)

i can definitely remove it, but i'd urge you to consider changing something so the default behaviour on windows when using your recommended development setup isn't errors on every single line

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

A note in DEVELOPING.md, describing how to configure git locally, would be the preferred alternative. Because that's missing in our recommended development setup.

@laundmo laundmo Jun 22, 2025

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

aight, if thats what you want, i'll remove the commit. its not ideal though as this is not something which should be configured globally. And you can't configure it locally before cloning, so you'll have to:

  • clone
  • configure
  • git rm everything
  • git reset --hard

to fix the line endings. which is quite an annoyingly convoluted process for something like line endings which usually just works (as most projects don't care about having CRLF during development)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I spent quite some time in the past trying to make Prettier accept that, but failed. If you find a way, that's ok for me too.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You may also want to weigh in on #16597. As I said there, I'm not opposed to that change, so maybe it's time to merge that and you can rebase this pull request and get what you envisioned.

@laundmo

laundmo commented Jun 21, 2025

Copy link
Copy Markdown
Contributor Author

I don't know why the tests don't run for you. Did npm install run without any issues?

Yup, no issues there

Does npm run karma work?

haven't tried that, what i've tried is the code-only tests, which worked fine.

Maybe your Windows installation is locked down so some browser features are blocked?

Not to my knowledge. Its not joined to a Domain, theres no group policies, its a relatively standard windows install.

but changing their behavior and thus making them more complex is not so good.

do you just mean the fact i added a filter to the box select example, the fact i swapped it over to using the new methods at all, or something else? please be specific, that way i know exactly what you have an issue with.

@ahocevar ahocevar left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Regarding the examples, you convinced me. Now please focus on making the tests pass. The hints in the CI output should help, even if you're not able to run the tests locally, for whatever reason.

Comment thread .gitattributes
Comment on lines +1 to +4
*.js text eol=lf
*.cjs text eol=lf
*.ts text eol=lf
*.mjs text eol=lf No newline at end of file

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

A note in DEVELOPING.md, describing how to configure git locally, would be the preferred alternative. Because that's missing in our recommended development setup.

@laundmo

laundmo commented Jun 24, 2025

Copy link
Copy Markdown
Contributor Author

I think i found why browser test are failing: karma-runner/karma#251

seems Karma doesn't handle absolute paths on Windows at all, since 2012? I am seeing the same kind of URL error from that issue in some stacktraces: http://localhost:9876/absoluteC:/Users/laundmo/AppData/Local/Temp/_karma_webpack_367715/webpack:/ol/node_modules/geotiff/dist-module/geotiff.js:548:1

edit: nope, doesn't seem likely, as the js modules seem to load fine, its the assets/data (images etc) which are failing

@laundmo

laundmo commented Jun 24, 2025

Copy link
Copy Markdown
Contributor Author

Okay i finally found out the main issue with tests: It seems that by default, Windows resolves localhost to IPv6 by default, while Karma currently runs in IPv4-only by default.

This means only the proxied paths, where the karma server was making a request to itself after rewriting the URL, it tried to request for example http://localhost:9876/base/spec/ol/data/dot.png which got resolved to http://[::1]:9876/base/spec/ol/data/dot.png, and since Karma doesn't listen to ipv6 by default that caused a ECONNREFUSED error.

Turns out, one fix for this is explicitly setting the hostname to the IPv4 127.0.0.1 instead of the localhost hostname.

@laundmo

laundmo commented Jun 24, 2025

Copy link
Copy Markdown
Contributor Author

tests should be fixed now, and i added tests for the new methods

@ahocevar ahocevar marked this pull request as ready for review July 4, 2025 14:01

@ahocevar ahocevar left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks, @laundmo

@ahocevar ahocevar merged commit ebaa191 into openlayers:main Jul 4, 2025
8 checks passed
@laundmo

laundmo commented Jul 4, 2025

Copy link
Copy Markdown
Contributor Author

Oh, you merged the .gitattributes stuff too, nice. i was wondering when i'd have time to remove that commit...

thanks for merging!

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.

Add method to ol.interaction.Select to add a feature as if it was clicked, including filters, duplicate checks, events, etc.

2 participants