Skip to content
Open
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
9 changes: 9 additions & 0 deletions apps/examples/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import ExampleMaplibreRaw from '@/examples/Example-Maplibre.vue?raw'
import ExampleMaplibre from '@/examples/Example-Maplibre.vue'
import ExampleGeoTIFF from '@/examples/Example-GeoTIFF.vue'
import ExampleGeoTIFFRaw from '@/examples/Example-GeoTIFF.vue?raw'
import ExampleMaplibreCOG from '@/examples/Example-MaplibreCOG.vue'
import ExampleMaplibreCOGRaw from '@/examples/Example-MaplibreCOG.vue?raw'
import { onMounted, ref } from 'vue'
import hljs from 'highlight.js'
import '@geospatial-sdk/elements'
Expand Down Expand Up @@ -105,6 +107,13 @@ onMounted(() => {
>
<ExampleGeoTIFF></ExampleGeoTIFF>
</ExampleContainer>
<ExampleContainer
example-name="Example 9: Maplibre COG"
example-id="example09"
:source-code="ExampleMaplibreCOGRaw"
>
<ExampleMaplibreCOG></ExampleMaplibreCOG>
</ExampleContainer>
</div>
</template>

Expand Down
34 changes: 34 additions & 0 deletions apps/examples/src/examples/Example-MaplibreCOG.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<script setup lang="ts">
import { onMounted, ref } from 'vue'
import { createMapFromContext } from '@geospatial-sdk/maplibre'
import { type MapContext } from '@geospatial-sdk/core'

const mapRoot = ref<HTMLElement>()

onMounted(async () => {
const context: MapContext = {
layers: [
{
type: 'xyz',
url: 'https://tile.openstreetmap.org/{z}/{x}/{y}.png'
},
{
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The cog being addressed as geotiff (as defined in MapContextLayerGeotiff from packages/core/lib/model/map-context.ts) is misleading and could lead to think that any geotif would work, and not just cog. So unless all geotiff works, including non-cloud-optimized, then I would advice renaming the type cog and also all the internal terminology that refers to "geotiff".

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I was also surprised, @jahow why did you choose this keyword ?

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.

because it works with plain geotiffs in OL

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.

we can throw an error in MapLibre if it doesn't support non-COG geotiff

type: 'geotiff',
url: 'https://labs.geomatico.es/maplibre-cog-protocol/data/image.tif',
}
],
view: {
center: [1.83369, 41.5937],
zoom: 14
}
}

await createMapFromContext(context, {
container: mapRoot.value as HTMLElement
})
})
</script>

<template>
<div ref="mapRoot" class="w-full h-full"></div>
</template>
144 changes: 143 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions packages/maplibre/lib/map/create-map.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { beforeEach, describe, expect, it, vi } from "vitest";
import {
MAP_CTX_LAYER_GEOJSON_FIXTURE,
MAP_CTX_LAYER_GEOJSON_REMOTE_FIXTURE,
MAP_CTX_LAYER_GEOTIFF_FIXTURE,
MAP_CTX_LAYER_OGCAPI_FIXTURE,
MAP_CTX_LAYER_WFS_FIXTURE,
MAP_CTX_LAYER_WMS_FIXTURE,
Expand Down Expand Up @@ -370,6 +371,38 @@ describe("MapContextService", () => {
});
});

describe("GeoTIFF", () => {
beforeEach(async () => {
layerModel = MAP_CTX_LAYER_GEOTIFF_FIXTURE;
style = (await createLayer(layerModel)) as PartialStyleSpecification;
});
it("create a raster layer and source", () => {
const sourceId = "123456";
const sourcesIds = Object.keys(style.sources);
expect(sourcesIds.length).toBe(1);
expect(sourcesIds[0]).toBe(sourceId);

const source = style.sources[sourceId] as RasterSourceSpecification;
expect(source.type).toBe("raster");
expect(source.url).toBe(
"cog://https://sentinel-cogs.s3.us-west-2.amazonaws.com/sentinel-s2-l2a-cogs/36/Q/WD/2020/7/S2A_36QWD_20200701_0_L2A/TCI.tif",
);
expect(source.tileSize).toBe(256);
});
it("create a layer with correct properties", () => {
expect(style.layers.length).toBe(1);
const layer = style.layers[0] as RasterLayerSpecification;
const metadata = layer.metadata as LayerMetadataSpecification;

expect(layer.id).toBe("123456");
expect(layer.type).toBe("raster");
expect(layer.source).toBe("123456");
expect(layer.paint?.["raster-opacity"]).toBe(1);
expect(layer.layout?.visibility).toBe("visible");
expect(metadata.layerHash).toBeTypeOf("string");
});
});

describe("WMTS", () => {
beforeEach(async () => {
layerModel = MAP_CTX_LAYER_WMTS_FIXTURE;
Expand Down
35 changes: 34 additions & 1 deletion packages/maplibre/lib/map/create-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
ViewByZoomAndCenter,
} from "@geospatial-sdk/core";

import { LayerSpecification, Map, MapOptions } from "maplibre-gl";
import { addProtocol, LayerSpecification, Map, MapOptions } from "maplibre-gl";
import { cogProtocol } from "@geomatico/maplibre-cog-protocol";
import { FeatureCollection, Geometry } from "geojson";
import {
OgcApiEndpoint,
Expand All @@ -22,6 +23,8 @@ import {
PartialStyleSpecification,
} from "../maplibre.models.js";

let cogProtocolRegistered = false;

const featureCollection: FeatureCollection<Geometry | null> = {
type: "FeatureCollection",
features: [],
Expand Down Expand Up @@ -153,6 +156,36 @@ export async function createLayer(
],
};
}
case "geotiff": {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Instead of using the string "geotiff" in many occasion, it would be nice to have a variables instead. Also true for the other types, because a string is more prone to errors.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

There are many cases here, the switch scope in its entirety is 150 lines already and is expected to contain even more in the future. To reduce reading complexity and improve maintainability (and onboarding of new devs), it would be nice each case enty calls a dedicated function (that are not exported and lay in the same file, at least for a start)

if (!cogProtocolRegistered) {
addProtocol("cog", cogProtocol);
cogProtocolRegistered = true;
}
const sourceId = layerId;
return {
sources: {
[sourceId]: {
type: "raster",
url: `cog://${layerModel.url}`,
tileSize: 256,
},
},
layers: [
{
id: layerId,
type: "raster",
source: sourceId,
paint: {
"raster-opacity": layerModel.opacity ?? 1,
},
layout: {
visibility: layerModel.visibility === false ? "none" : "visible",
},
metadata,
},
],
};
}
case "wmts": {
console.warn(`WMTS layers are not yet supported in Maplibre`, layerModel);
return null;
Expand Down
6 changes: 4 additions & 2 deletions packages/maplibre/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@
"build": "tsc"
},
"devDependencies": {
"maplibre-gl": "^5.19.0"
"maplibre-gl": "^5.19.0",
"@geomatico/maplibre-cog-protocol": "^0.8.0"
},
"peerDependencies": {
"maplibre-gl": "^5.19.0"
"maplibre-gl": "^5.19.0",
"@geomatico/maplibre-cog-protocol": "^0.8.0"
},
"dependencies": {
"@geospatial-sdk/core": "^0.0.5-alpha.2"
Expand Down
Loading