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

feat: support Mapbox satellite styles #475

Merged
merged 1 commit into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 .env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
VITE_APP_OS_VECTOR_TILES_API_KEY=👻
VITE_APP_OS_FEATURES_API_KEY=👻
VITE_APP_OS_PLACES_API_KEY=👻
VITE_APP_MAPBOX_ACCESS_TOKEN=👻
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,24 @@ These web components can be used independently or together following GOV.UK's [A

Find these components in the wild, including what we're learning through public beta user-testing, at [https://www.ripa.digital/](https://www.ripa.digital/).

## Bring your own API keys

Different features rely on different APIs - namely from Ordnance Survey and Mapbox.

Address autocomplete utilises OS Places API.

For the map:
- We'll attempt to render the map using the OS Vector Tiles API
- You can pass `disableVectorTiles` to render a raster basemap instead which uses the default OS Maps API
- If you don't have an OS API key at all, it defaults to OpenStreetMap
- `clickFeatures` requires the OS Features API
- `applySatelliteStyle` requires a Mapbox Access Token with the scope `style:read`

When using Ordnance Survey APIs:
- Update the `osCopyright` attribution with your license number
- Configure `osProxyEndpoint` to avoid exposing your keys
- ** We are not currently supporting a similar proxy for Mapbox because access tokens can be restricted to specific URLs via your account

## Running locally

- Rename `.env.example` to `.env.local` and replace the values - or simply provide your API keys as props
Expand Down
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
<h1 style="color:red;font-size:16px;">*** This is a testing sandbox - these components are unaware of each other!
***</h1>
<div style="margin-bottom:1em">
<my-map zoom="20" maxZoom="23" id="example-map" drawMode drawMany drawType="Polygon" useScaleBarStyle showScale showNorthArrow showPrint
<my-map zoom="20" maxZoom="23" id="example-map" drawMode drawMany drawType="Polygon" applySatelliteStyle useScaleBarStyle showScale showNorthArrow showPrint
osProxyEndpoint="https://api.editor.planx.dev/proxy/ordnance-survey" ariaLabelOlFixedOverlay="Interactive example map" />
</div>
<div style="margin-bottom:1em">
Expand Down
24 changes: 24 additions & 0 deletions src/components/my-map/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { html, LitElement, unsafeCSS } from "lit";
import { customElement, property } from "lit/decorators.js";
import apply from "ol-mapbox-style";
import { defaults as defaultControls, ScaleLine } from "ol/control";
import { GeoJSON } from "ol/format";
import { GeoJSONFeature } from "ol/format/GeoJSON";
Expand Down Expand Up @@ -191,6 +192,12 @@ export class MyMap extends LitElement {
@property({ type: String })
osProxyEndpoint = "";

@property({ type: Boolean })
applySatelliteStyle = false;
Copy link
Contributor

Choose a reason for hiding this comment

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

We might want to think more widely about the API for this component. I think that a consumer may expect a "basemap" or "layer" prop where they can select OS Vector, OS Raster, Mapbox Satellite, or OSM (the fallback).

Right now you can pass multiple contradictory props (or incomplete props - missing keys for example) in and it's just our internal logic dictating the precedence of these internally. I think it would be better to expose this to consumers up front.

Likewise, some of the styling/drawing props could get joined up as an options object?

Worth some discussion / prototyping - seems a worthwhile consideration before v1?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep fully agree - was writing the README section and remembering how round-about this is currently because of staggered implementations. Now that we have a much clearer list of supported layers, agree there's good room for reducing complexity & offering a single enum prop ✔️ I'll pick this one up shortly separately

Will maybe eventually try to start a similar v1 Github Project here for capturing all our goals - also very interested in how to get to smaller / clearer API before then and there's lots of areas for improvement.


@property({ type: String })
mapboxAccessToken = import.meta.env.VITE_APP_MAPBOX_ACCESS_TOKEN || "";

@property({ type: Boolean })
hideResetControl = false;

Expand Down Expand Up @@ -308,6 +315,23 @@ export class MyMap extends LitElement {

this.map = map;

if (this.mapboxAccessToken && this.applySatelliteStyle) {
if (osVectorTileBaseMap) map.removeLayer(osVectorTileBaseMap);
if (rasterBaseMap) map.removeLayer(rasterBaseMap);

apply(
map,
`https://api.mapbox.com/styles/v1/mapbox/satellite-v9?access_token=${this.mapboxAccessToken}`,
);

const satelliteAttribution =
'<a href="https://www.mapbox.com/about/maps/" target="_blank" rel="noopener noreferrer">© Mapbox</a> <a href="http://www.openstreetmap.org/about/" target="_blank" rel="noopener noreferrer">© OpenStreetMap</a> <a href="https://labs.mapbox.com/contribute/#/-74@site/src/10" target="_blank" rel="noopener noreferrer"><strong>Improve this map</strong></a>';
const satelliteLayer = new VectorLayer({
source: new VectorSource({ attributions: satelliteAttribution }), // empty besides attribution
});
map.addLayer(satelliteLayer);
}

// Append to global window for reference in tests
window.olMap = import.meta.env.VITEST ? this.map : undefined;

Expand Down
1 change: 1 addition & 0 deletions src/vite-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ interface ImportMetaEnv {
VITE_APP_OS_VECTOR_TILES_API_KEY: string;
VITE_APP_OS_FEATURES_API_KEY: string;
VITE_APP_OS_PLACES_API_KEY: string;
VITE_APP_MAPBOX_ACCESS_TOKEN: string;
}

declare module "ol-mapbox-style/dist/stylefunction";
Expand Down