Skip to content

Commit d53e162

Browse files
authored
Merge branch 'main' into fix-api-key-when-going-between-openlayers-and-maplibre
2 parents 388d03a + 535cb63 commit d53e162

13 files changed

+158
-56
lines changed

.github/workflows/auto-merge-dependabot.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
steps:
1212
- name: Dependabot metadata
1313
id: metadata
14-
uses: dependabot/fetch-metadata@v2.2.0
14+
uses: dependabot/fetch-metadata@v2.3.0
1515
with:
1616
github-token: "${{ secrets.GITHUB_TOKEN }}"
1717
- name: Approve Dependabot PRs

cypress/e2e/modals.cy.ts

+18
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,24 @@ describe("modals", () => {
8484
});
8585
});
8686

87+
it("add new pmtiles source", () => {
88+
const sourceId = "pmtilestest";
89+
when.setValue("modal:sources.add.source_id", sourceId);
90+
when.select("modal:sources.add.source_type", "pmtiles_vector");
91+
when.setValue("modal:sources.add.source_url", "https://data.source.coop/protomaps/openstreetmap/v4.pmtiles");
92+
when.click("modal:sources.add.add_source");
93+
when.click("modal:sources.add.add_source");
94+
when.wait(200);
95+
then(get.styleFromLocalStorage()).shouldDeepNestedInclude({
96+
sources: {
97+
pmtilestest: {
98+
type: "vector",
99+
url: "pmtiles://https://data.source.coop/protomaps/openstreetmap/v4.pmtiles",
100+
},
101+
},
102+
});
103+
});
104+
87105
it("add new raster source", () => {
88106
const sourceId = "rastertest";
89107
when.setValue("modal:sources.add.source_id", sourceId);

package-lock.json

+54-27
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+7-6
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@
3333
"array-move": "^4.0.0",
3434
"buffer": "^6.0.3",
3535
"classnames": "^2.5.1",
36-
"codemirror": "^5.65.2",
36+
"codemirror": "^5.65.18",
3737
"color": "^4.2.3",
3838
"detect-browser": "^5.3.0",
3939
"events": "^3.3.0",
4040
"file-saver": "^2.0.5",
41-
"i18next": "^24.2.1",
41+
"i18next": "^24.2.2",
4242
"i18next-browser-languagedetector": "^8.0.2",
4343
"i18next-resources-to-backend": "^1.2.1",
4444
"json-stringify-pretty-compact": "^4.0.0",
@@ -54,6 +54,7 @@
5454
"maputnik-design": "github:maputnik/design#172b06c",
5555
"ol": "^10.3.1",
5656
"ol-mapbox-style": "^12.4.0",
57+
"pmtiles": "^4.1.0",
5758
"prop-types": "^15.8.1",
5859
"react": "^18.2.0",
5960
"react-accessible-accordion": "^5.0.0",
@@ -93,15 +94,15 @@
9394
},
9495
"devDependencies": {
9596
"@cypress/code-coverage": "^3.13.10",
96-
"@eslint/js": "^9.18.0",
97+
"@eslint/js": "^9.19.0",
9798
"@istanbuljs/nyc-config-typescript": "^1.0.2",
9899
"@rollup/plugin-replace": "^6.0.2",
99100
"@shellygo/cypress-test-utils": "^4.1.11",
100101
"@types/codemirror": "^5.60.15",
101102
"@types/color": "^4.2.0",
102103
"@types/cors": "^2.8.17",
103104
"@types/file-saver": "^2.0.7",
104-
"@types/geojson": "^7946.0.15",
105+
"@types/geojson": "^7946.0.16",
105106
"@types/json-to-ast": "^2.1.4",
106107
"@types/lodash.capitalize": "^4.2.9",
107108
"@types/lodash.clamp": "^4.0.9",
@@ -126,7 +127,7 @@
126127
"cors": "^2.8.5",
127128
"cypress": "^14.0.0",
128129
"cypress-plugin-tab": "^1.0.5",
129-
"eslint": "^9.18.0",
130+
"eslint": "^9.19.0",
130131
"eslint-plugin-react": "^7.37.4",
131132
"eslint-plugin-react-hooks": "^5.1.0",
132133
"eslint-plugin-react-refresh": "^0.4.18",
@@ -136,7 +137,7 @@
136137
"postcss": "^8.5.1",
137138
"react-hot-loader": "^4.13.1",
138139
"sass": "^1.83.4",
139-
"stylelint": "^16.13.2",
140+
"stylelint": "^16.14.0",
140141
"stylelint-config-recommended-scss": "^14.1.0",
141142
"stylelint-scss": "^6.10.1",
142143
"typescript": "^5.7.3",

src/components/App.tsx

+30-21
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import get from 'lodash.get'
88
import {unset} from 'lodash'
99
import {arrayMoveMutable} from 'array-move'
1010
import hash from "string-hash";
11+
import { PMTiles } from "pmtiles";
1112
import {Map, LayerSpecification, StyleSpecification, ValidationError, SourceSpecification} from 'maplibre-gl'
1213
import {latest, validateStyleMin} from '@maplibre/maplibre-gl-style-spec'
1314

@@ -660,33 +661,41 @@ export default class App extends React.Component<any, AppState> {
660661
console.warn("Failed to setFetchAccessToken: ", err);
661662
}
662663

663-
fetch(url!, {
664-
mode: 'cors',
665-
})
666-
.then(response => response.json())
667-
.then(json => {
664+
const setVectorLayers = (json:any) => {
665+
if(!Object.prototype.hasOwnProperty.call(json, "vector_layers")) {
666+
return;
667+
}
668668

669-
if(!Object.prototype.hasOwnProperty.call(json, "vector_layers")) {
670-
return;
671-
}
669+
// Create new objects before setState
670+
const sources = Object.assign({}, {
671+
[key]: this.state.sources[key],
672+
});
672673

673-
// Create new objects before setState
674-
const sources = Object.assign({}, {
675-
[key]: this.state.sources[key],
676-
});
674+
for(const layer of json.vector_layers) {
675+
(sources[key] as any).layers.push(layer.id)
676+
}
677677

678-
for(const layer of json.vector_layers) {
679-
(sources[key] as any).layers.push(layer.id)
680-
}
678+
this.setState({
679+
sources: sources
680+
});
681+
};
681682

682-
console.debug("Updating source: "+key);
683-
this.setState({
684-
sources: sources
683+
if (url!.startsWith("pmtiles://")) {
684+
(new PMTiles(url!.substr(10))).getTileJson("")
685+
.then(json => setVectorLayers(json))
686+
.catch(err => {
687+
console.error("Failed to process sources for '%s'", url, err);
685688
});
689+
} else {
690+
fetch(url!, {
691+
mode: 'cors',
686692
})
687-
.catch(err => {
688-
console.error("Failed to process sources for '%s'", url, err);
689-
});
693+
.then(response => response.json())
694+
.then(json => setVectorLayers(json))
695+
.catch(err => {
696+
console.error("Failed to process sources for '%s'", url, err);
697+
});
698+
}
690699
}
691700
else {
692701
sourceList[key] = this.state.sources[key] || this.state.mapStyle.sources[key];

src/components/MapMaplibreGl.tsx

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import MaplibreGeocoder, { MaplibreGeocoderApi, MaplibreGeocoderApiConfig } from
1515
import '@maplibre/maplibre-gl-geocoder/dist/maplibre-gl-geocoder.css';
1616
import { withTranslation, WithTranslation } from 'react-i18next'
1717
import i18next from 'i18next'
18+
import { Protocol } from "pmtiles";
1819

1920
function renderPopup(popup: JSX.Element, mountNode: ReactDOM.Container): HTMLElement {
2021
ReactDOM.render(popup, mountNode);
@@ -148,6 +149,8 @@ class MapMaplibreGlInternal extends React.Component<MapMaplibreGlInternalProps,
148149
localIdeographFontFamily: false
149150
} satisfies MapOptions;
150151

152+
const protocol = new Protocol({metadata: true});
153+
MapLibreGl.addProtocol("pmtiles",protocol.tile);
151154
const map = new MapLibreGl.Map(mapOpts);
152155

153156
const mapViewChange = () => {

0 commit comments

Comments
 (0)