Skip to content

Commit

Permalink
Use imageOverlay function. And add some extra error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanwins committed Apr 25, 2022
1 parent 6bdbfdb commit a08e9ba
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 26 deletions.
66 changes: 41 additions & 25 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,14 @@ function getDataType(data) {
}
}

function addOverviewAssetForFeature (feature, layerGroup, errorCallback) {
async function addOverviewAssetForFeature (feature, layerGroup, crossOrigin, errorCallback) {
const { asset } = getOverviewAsset(feature.assets);
if (isImageType(asset.type)) {
const lyr = L.imageOverlay(asset.href, [[feature.bbox[1], feature.bbox[0]], [feature.bbox[3], feature.bbox[2]]])
const lyr = await imageOverlay(asset.href, [[feature.bbox[1], feature.bbox[0]], [feature.bbox[3], feature.bbox[2]]], crossOrigin);
if (lyr === null) {
errorCallback()
return
}
layerGroup.addLayer(lyr)
lyr.on('error', () => {
layerGroup.removeLayer(lyr)
Expand All @@ -89,10 +93,14 @@ function addOverviewAssetForFeature (feature, layerGroup, errorCallback) {
}
}

function addThumbnailAssetForFeature (feature, layerGroup, errorCallback) {
async function addThumbnailAssetForFeature (feature, layerGroup, crossOrigin, errorCallback) {
const { asset } = findAsset(feature.assets, 'thumbnail');
if (isImageType(asset.type)) {
const lyr = L.imageOverlay(asset.href, [[feature.bbox[1], feature.bbox[0]], [feature.bbox[3], feature.bbox[2]]])
const lyr = await imageOverlay(asset.href, [[feature.bbox[1], feature.bbox[0]], [feature.bbox[3], feature.bbox[2]]], crossOrigin);
if (lyr === null) {
errorCallback()
return
}
layerGroup.addLayer(lyr)
lyr.on('error', () => {
layerGroup.removeLayer(lyr)
Expand Down Expand Up @@ -227,15 +235,15 @@ const stacLayer = async (data, options = {}) => {
if (displayPreview) {
// If we've got a thumnail asset add it
if (hasAsset(f.assets, "thumbnail")) {
addThumbnailAssetForFeature(f, layerGroup, () => {
addThumbnailAssetForFeature(f, layerGroup, options.crossOrigin, () => {
// If some reason it's broken try for an overview asset
if (hasAsset(f.assets, "overview")) {
addOverviewAssetForFeature(f, layerGroup)
addOverviewAssetForFeature(f, layerGroup, options.crossOrigin)
}
})
} else if (!hasAsset(f.assets, "thumbnail") && hasAsset(f.assets, "overview")) {
// If we don't have a thumbail let's try for an overview asset
addOverviewAssetForFeature(f, layerGroup)
addOverviewAssetForFeature(f, layerGroup, options.crossOrigin)
}
}

Expand Down Expand Up @@ -322,13 +330,15 @@ const stacLayer = async (data, options = {}) => {

if (isImageType(type)) {
const overviewLayer = await imageOverlay(href, bounds, options.crossOrigin);
bindDataToClickEvent(overviewLayer, asset);
// there probably aren't eo:bands attached to an overview
// but we include this here just in case
layerGroup.stac = { assets: [{ key, asset }], bands: asset?.["eo:bands"] };
layerGroup.addLayer(overviewLayer);
addedImagery = true;
if (debugLevel >= 1) console.log("[stac-layer] succesfully added overview layer");
if (overviewLayer !== null) {
bindDataToClickEvent(overviewLayer, asset);
// there probably aren't eo:bands attached to an overview
// but we include this here just in case
layerGroup.stac = { assets: [{ key, asset }], bands: asset?.["eo:bands"] };
layerGroup.addLayer(overviewLayer);
addedImagery = true;
if (debugLevel >= 1) console.log("[stac-layer] succesfully added overview layer");
}
} else if (isAssetCOG(asset)) {
if (preferTileLayer) {
await addTileLayer({ asset, href, isCOG: true, isVisual: true, key });
Expand Down Expand Up @@ -367,10 +377,12 @@ const stacLayer = async (data, options = {}) => {

if (isImageType(type)) {
const thumbLayer = await imageOverlay(href, bounds, options.crossOrigin);
bindDataToClickEvent(thumbLayer, data);
layerGroup.addLayer(thumbLayer);
addedImagery = true;
if (debugLevel >= 1) console.log("[stac-layer] succesfully added thumbnail layer");
if (thumbLayer !== null) {
bindDataToClickEvent(thumbLayer, data);
layerGroup.addLayer(thumbLayer);
addedImagery = true;
if (debugLevel >= 1) console.log("[stac-layer] succesfully added thumbnail layer");
}
}
} catch (error) {
if (debugLevel >= 1)
Expand All @@ -388,10 +400,12 @@ const stacLayer = async (data, options = {}) => {

if (isImageType(type)) {
const previewLayer = await imageOverlay(href, bounds, options.crossOrigin);
bindDataToClickEvent(previewLayer, data);
layerGroup.addLayer(previewLayer);
addedImagery = true;
if (debugLevel >= 1) console.log("[stac-layer] succesfully added preview layer");
if (previewLayer !== null) {
bindDataToClickEvent(previewLayer, data);
layerGroup.addLayer(previewLayer);
addedImagery = true;
if (debugLevel >= 1) console.log("[stac-layer] succesfully added preview layer");
}
}
} catch (error) {
if (debugLevel >= 1)
Expand Down Expand Up @@ -550,9 +564,11 @@ const stacLayer = async (data, options = {}) => {
}

const lyr = await imageOverlay(href, bounds, options.crossOrigin);
bindDataToClickEvent(lyr);
layerGroup.addLayer(lyr);
fillOpacity = 0;
if (lyr !== null) {
bindDataToClickEvent(lyr);
layerGroup.addLayer(lyr);
fillOpacity = 0;
}
} else if (MIME_TYPES.GEOTIFF.includes(type)) {
const addTileLayer = async () => {
try {
Expand Down
8 changes: 7 additions & 1 deletion src/utils/image-overlay.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ import loadImage from "easy-image-loader";
// (3) rejects the promise if it takes more than 5 seconds for the image to load
export default async function imageOverlay(url, bounds, crossOrigin, options) {
const timeout = 5 * 1000; // 5 seconds
const img = await loadImage(url, { crossOrigin, timeout });
let img = null
try {
img = await loadImage(url, { crossOrigin, timeout });
}
catch {
return null
}
const lyr = L.imageOverlay(img, bounds, options);
return lyr;
}

0 comments on commit a08e9ba

Please sign in to comment.