From 50dbfb403f7e136a93e71e14f26efc8ab9702385 Mon Sep 17 00:00:00 2001 From: Tony Obradovic Date: Sun, 18 Feb 2024 23:16:30 -0600 Subject: [PATCH 1/5] Removed C&P code in VIEWER.consumeForGeoJSON. Code was consolidated into three helper functions: getResourceProperties, getManifestProperties, and getStructures. Still Error checking and looking for bugs. --- app.js | 334 +++++++++++++++++++-------------------------------------- 1 file changed, 108 insertions(+), 226 deletions(-) diff --git a/app.js b/app.js index ccd99a1..310ece5 100644 --- a/app.js +++ b/app.js @@ -506,130 +506,123 @@ VIEWER.consumeForGeoJSON = async function(dataURL) { } return prev.concat(curr) }, []) - - /** - * Below this is helping people who did not put their properties in the Features. - * It will help along a Manifest or Canvas with navPlaces devoid of properties. - * Imagine being able to delete all this code if people just did their own properties! - * - * TODO -- Too much C&P in this switch. This should be broken down into helper functions. - */ - switch(resourceType){ - // This logic is the same as the Manifest logic, except looped over Collection.items which are Manifests. - case "Collection": - let coll_geos = [] - if (VIEWER.resource.hasOwnProperty("navPlace")) { - if (VIEWER.resource.navPlace.features) { - VIEWER.resource.navPlace.features = VIEWER.resource.navPlace.features.map(f => { + /* + * Get the visible properties of the resource such as the thumbnail, description, and label. + */ + function getResourceProperties(dtype) { + if (VIEWER.resource.hasOwnProperty("navPlace")) { + if (VIEWER.resource.navPlace.features){ + VIEWER.resource.navPlace.features = VIEWER.resource.navPlace.features.map(f => { + if (!f.properties.thumbnail) { + if(VIEWER.resource.thumbnail){ f.properties.thumbnail = VIEWER.resource.thumbnail } + else if (dtype != "collection" && VIEWER.resource.hasOwnProperty("items") && VIEWER.resource.items.length && VIEWER.resource.items[0].items.length && VIEWER.resource.items[0].items[0].items.length) { + if (VIEWER.resource.items[0].items[0].items[0].body) { + let thumburl = VIEWER.resource.items[0].items[0].items[0].body.id ?? "" + f.properties.thumbnail = [{"id":thumburl}] + } + } + } + if (!f.properties.hasOwnProperty("summary")) { f.properties.summary = VIEWER.resource.summary ?? "" } + if (!f.properties.hasOwnProperty("label")) { f.properties.label = VIEWER.resource.label ?? "" } + if (!f.properties.hasOwnProperty(dtype)) { + if (resourceType === "Manifest") { f.properties.manifest = manifest["@id"] ?? manifest["id"] ?? "Yikes" } + else { f.properties[dtype] = VIEWER.resource["@id"] ?? VIEWER.resource["id"] ?? "Yikes" } + } + if (dtype === "collection") { f.properties.collection = VIEWER.resource["@id"] ?? VIEWER.resource["id"] ?? "Yikes" } + return f + }) + } + } + return VIEWER.resource.navPlace + } + + function getManifestProperties() { + let manifest_geos = []; + VIEWER.resource.items.map(async (manifest) => { + if (manifest.hasOwnProperty("navPlace")) { + if (manifest.navPlace.features) { + manifest.navPlace.features = manifest.navPlace.features.map(f => { //FIXME support referenced Features even though the spec encourages embedded Features? if (!f.properties.thumbnail) { - if(VIEWER.resource.thumbnail){ - f.properties.thumbnail = VIEWER.resource.thumbnail + //Then lets grab the image URL from the annotation of the first Canvas item if available. + if(manifest.thumbnail){ + f.properties.thumbnail = manifest.thumbnail + } + else if (manifest.hasOwnProperty("items") && manifest.items.length && manifest.items[0].items.length && manifest.items[0].items[0].items.length) { + if (manifest.items[0].items[0].items[0].body) { + let thumburl = manifest.items[0].items[0].items[0].body.id ?? "" + f.properties.thumbnail = [{"id":thumburl}] + } } } - if (!f.properties.hasOwnProperty("summary")) { - f.properties.summary = VIEWER.resource.summary ?? "" - } - if (!f.properties.hasOwnProperty("label")) { - f.properties.label = VIEWER.resource.label ?? "" - } - if (!f.properties.hasOwnProperty("collection")) { - f.properties.label = VIEWER.resource.label ?? "" + if (!f.properties.hasOwnProperty("summary")) { f.properties.summary = manifest.summary ?? "" } + if (!f.properties.hasOwnProperty("label")) { f.properties.label = manifest.label ?? "" } + if (!f.properties.hasOwnProperty("manifest")) { + if (resourceType === "Manifest") { f.properties.manifest = manifest["@id"] ?? manifest["id"] ?? "Yikes" } } - f.properties.collection = VIEWER.resource["@id"] ?? VIEWER.resource["id"] ?? "Yikes" return f }) - coll_geos.push(VIEWER.resource.navPlace) + manifest_geos = manifest.navPlace } } - geoJSONFeatures = coll_geos - VIEWER.resource.items.map(async (manifest) => { - let manifest_geos = [] //For the top level resource.navPlace - let canvasGeos = [] //For resource.item navPlaces - let structuresGeos = []// For resource.structures navPlaces - if (manifest.hasOwnProperty("navPlace")) { - if (manifest.navPlace.features) { - manifest.navPlace.features = manifest.navPlace.features.map(f => { + }) + return manifest_geos + } + + async function getStructures(alias) { + let structuresGeos = [] + let canvasGeos = [] + if (alias.hasOwnProperty("structures") && alias.structures.length) { + structuresGeos = await Promise.all(alias.structures.map(async (s) => { + //This range may contain other ranges and has the same complexity as a Collection... + let structureGeo = await VIEWER.findAllFeatures(s, "navPlace", [], false) + return structureGeo + })) + } + else if (alias.hasOwnProperty("items") && alias.items.length) { + canvasGeos = alias.items + .filter(item => { + //We only care about Canvases I think. Ignore everything else + let itemType = item.type ?? item["@type"] ?? "Yikes" + return item.hasOwnProperty("navPlace") && (itemType === "Canvas") + }) + .map(canvas => { + //Add data from the canvas or the manifest here. + if(canvas.navPlace.features){ + canvas.navPlace.features.forEach(feature => { //FIXME support referenced Features even though the spec encourages embedded Features? - if (!f.properties.thumbnail) { - //Then lets grab the image URL from the annotation of the first Canvas item if available. - if(manifest.thumbnail){ - f.properties.thumbnail = manifest.thumbnail - } - else if (manifest.hasOwnProperty("items") && manifest.items.length && manifest.items[0].items.length && manifest.items[0].items[0].items.length) { - if (manifest.items[0].items[0].items[0].body) { - let thumburl = manifest.items[0].items[0].items[0].body.id ?? "" - f.properties.thumbnail = [{"id":thumburl}] - } + if (!feature.properties.hasOwnProperty("thumbnail")) { + //Then lets grab the image URL from the painting annotation + if(canvas.thumbnail) { feature.properties.thumbnail = canvas.thumbnail } + else if (canvas.items && canvas.items[0] && canvas.items[0].items && canvas.items[0].items[0].body) { + let thumburl = canvas.items[0].items[0].body.id ?? "" + feature.properties.thumbnail = [{"id":thumburl}] } } - if (!f.properties.hasOwnProperty("summary")) { - f.properties.summary = manifest.summary ?? "" - } - if (!f.properties.hasOwnProperty("label")) { - f.properties.label = manifest.label ?? "" - } - if (!f.properties.hasOwnProperty("manifest")) { - if (resourceType === "Manifest") { - f.properties.manifest = manifest["@id"] ?? manifest["id"] ?? "Yikes" - } - } - return f - }) - manifest_geos.push(manifest.navPlace) + if (!feature.properties.hasOwnProperty("summary")) { feature.properties.summary = canvas.summary ?? "" } + if (!feature.properties.hasOwnProperty("label")) { feature.properties.label = canvas.label ?? "" } + if (!feature.properties.hasOwnProperty("canvas")) { feature.properties.canvas = canvas["@id"] ?? canvas["id"] ?? "Yikes" } + }) + return canvas.navPlace } - } - - /* - * Preference Manifest.structures manifest_geos over Manifest.items - */ - if (manifest.hasOwnProperty("structures") && manifest.structures.length) { - structuresGeos = await Promise.all(manifest.structures.map(async (s) => { - //This range may contain other ranges and has the same complexity as a Collection... - let structureGeo = await VIEWER.findAllFeatures(s, "navPlace", [], false) - return structureGeo - })) - } - else if (manifest.hasOwnProperty("items") && manifest.items.length) { - canvasGeos = manifest.items - .filter(item => { - //We only care about Canvases I think. Ignore everything else - let itemType = item.type ?? item["@type"] ?? "Yikes" - return item.hasOwnProperty("navPlace") && (itemType === "Canvas") - }) - .map(canvas => { - //Add data from the canvas or the manifest here. - if(canvas.navPlace.features){ - canvas.navPlace.features.forEach(feature => { - //FIXME support referenced Features even though the spec encourages embedded Features? - if (!feature.properties.hasOwnProperty("thumbnail")) { - //Then lets grab the image URL from the painting annotation - if(canvas.thumbnail){ - feature.properties.thumbnail = canvas.thumbnail - } - else if (canvas.items && canvas.items[0] && canvas.items[0].items && canvas.items[0].items[0].body) { - let thumburl = canvas.items[0].items[0].body.id ?? "" - feature.properties.thumbnail = [{"id":thumburl}] - } - } - if (!feature.properties.hasOwnProperty("summary")) { - feature.properties.summary = canvas.summary ?? "" - } - if (!feature.properties.hasOwnProperty("label")) { - feature.properties.label = canvas.label ?? "" - } - if (!feature.properties.hasOwnProperty("canvas")) { - feature.properties.canvas = canvas["@id"] ?? canvas["id"] ?? "Yikes" - } - }) - return canvas.navPlace - } - }) - } - //Combine them together so that they are all drawn on the web map - geoJSONFeatures = geoJSONFeatures.concat([...manifest_geos, ...structuresGeos, ...canvasGeos]) - }) + }) + return structuresGeos, canvasGeos + } + } + + /** + * Below this is helping people who did not put their properties in the Features. + * It will help along a Manifest or Canvas with navPlaces devoid of properties. + * Imagine being able to delete all this code if people just did their own properties! + */ + switch(resourceType){ + // This logic is the same as the Manifest logic, except looped over Collection.items which are Manifests. + case "Collection": + geoJSONFeatures = getResourceProperties("collection") + let manifestGeos = getManifestProperties() + let structGeos, itemGeos = getStructures(manifest) + geoJSONFeatures = geoJSONFeatures.concat([...manifestGeos, structGeos, itemGeos]) return geoJSONFeatures break case "Range": @@ -637,125 +630,14 @@ VIEWER.consumeForGeoJSON = async function(dataURL) { return geoJSONFeatures break case "Manifest": - let geos = [] //For the top level resource.navPlace - let itemsGeos = [] //For resource.item navPlaces - let structuresGeos = []// For resource.structures navPlaces - if (VIEWER.resource.hasOwnProperty("navPlace")) { - if (VIEWER.resource.navPlace.features) { - VIEWER.resource.navPlace.features = VIEWER.resource.navPlace.features.map(f => { - //FIXME support referenced Features even though the spec encourages embedded Features? - if (!f.properties.thumbnail) { - //Then lets grab the image URL from the annotation of the first Canvas item if available. - if(VIEWER.resource.thumbnail){ - f.properties.thumbnail = VIEWER.resource.thumbnail - } - else if (VIEWER.resource.hasOwnProperty("items") && VIEWER.resource.items.length && VIEWER.resource.items[0].items.length && VIEWER.resource.items[0].items[0].items.length) { - if (VIEWER.resource.items[0].items[0].items[0].body) { - let thumburl = VIEWER.resource.items[0].items[0].items[0].body.id ?? "" - f.properties.thumbnail = [{"id":thumburl}] - } - } - } - if (!f.properties.hasOwnProperty("summary")) { - f.properties.summary = VIEWER.resource.summary ?? "" - } - if (!f.properties.hasOwnProperty("label")) { - f.properties.label = VIEWER.resource.label ?? "" - } - if (!f.properties.hasOwnProperty("manifest")) { - if (resourceType === "Manifest") { - f.properties.manifest = VIEWER.resource["@id"] ?? VIEWER.resource["id"] ?? "Yikes" - } - } - return f - }) - geos.push(VIEWER.resource.navPlace) - } - } - - /* - * Preference Manifest.structures geos over Manifest.items - */ - if (VIEWER.resource.hasOwnProperty("structures") && VIEWER.resource.structures.length) { - structuresGeos = await Promise.all(VIEWER.resource.structures.map(async (s) => { - //This range may contain other ranges and has the same complexity as a Collection... - let structureGeo = await VIEWER.findAllFeatures(s, "navPlace", [], false) - return structureGeo - })) - } - else if (VIEWER.resource.hasOwnProperty("items") && VIEWER.resource.items.length) { - itemsGeos = VIEWER.resource.items - .filter(item => { - //We only care about Canvases I think. Ignore everything else - let itemType = item.type ?? item["@type"] ?? "Yikes" - return item.hasOwnProperty("navPlace") && (itemType === "Canvas") - }) - .map(canvas => { - //Add data from the canvas or the VIEWER.resource here. - if(canvas.navPlace.features){ - canvas.navPlace.features.forEach(feature => { - //FIXME support referenced Features even though the spec encourages embedded Features? - if (!feature.properties.hasOwnProperty("thumbnail")) { - //Then lets grab the image URL from the painting annotation - if(canvas.thumbnail){ - feature.properties.thumbnail = canvas.thumbnail - } - else if (canvas.items && canvas.items[0] && canvas.items[0].items && canvas.items[0].items[0].body) { - let thumburl = canvas.items[0].items[0].body.id ?? "" - feature.properties.thumbnail = [{"id":thumburl}] - } - } - if (!feature.properties.hasOwnProperty("summary")) { - feature.properties.summary = canvas.summary ?? "" - } - if (!feature.properties.hasOwnProperty("label")) { - feature.properties.label = canvas.label ?? "" - } - if (!feature.properties.hasOwnProperty("canvas")) { - feature.properties.canvas = canvas["@id"] ?? canvas["id"] ?? "Yikes" - } - }) - return canvas.navPlace - } - }) - } - //Combine them together so that they are all drawn on the web map - geoJSONFeatures = [...geos, ...structuresGeos, ...itemsGeos] + let geos = getResourceProperties("manifest") + let structureGeo, itemsGeos = getStructures(VIEWER.resource) + geoJSONFeatures = [...geos, structureGeo, itemsGeos] return geoJSONFeatures break case "Canvas": - let canvasGeo = {} - if (VIEWER.resource.hasOwnProperty("navPlace")) { - if (VIEWER.resource.navPlace.features) { - VIEWER.resource.navPlace.features = VIEWER.resource.navPlace.features.map(f => { - //FIXME support referenced Features even though the spec encourages embedded Features? - if (!f.properties.thumbnail) { - //Then lets grab the image URL from the annotation of the first Canvas item if available. - if(VIEWER.resource.thumbnail){ - f.properties.thumbnail = VIEWER.resource.thumbnail - } - else if (VIEWER.resource.hasOwnProperty("items") && VIEWER.resource.items.length && VIEWER.resource.items[0].items.length && VIEWER.resource.items[0].items[0].items.length) { - if (VIEWER.resource.items[0].items[0].items[0].body) { - let thumburl = VIEWER.resource.items[0].items[0].items[0].body.id ?? "" - f.properties.thumbnail = [{"id":thumburl}] - } - } - } - if (!f.properties.hasOwnProperty("summary")) { - f.properties.summary = VIEWER.resource.summary ?? "" - } - if (!f.properties.hasOwnProperty("label")) { - f.properties.label = VIEWER.resource.label ?? "" - } - if (!f.properties.hasOwnProperty("canvas")) { - f.properties.canvas = VIEWER.resource["@id"] ?? VIEWER.resource["id"] ?? "Yikes" - } - return f - }) - } - geoJSONFeatures = VIEWER.resource.navPlace - return geoJSONFeatures - } + geoJSONFeatures = getResourceProperties("canvas") + return geoJSONFeatures break case "AnnotationPage": case "Annotation": From 263e7d710d60c53af0cbb61c78df832750558df9 Mon Sep 17 00:00:00 2001 From: Tony Obradovic Date: Tue, 20 Feb 2024 21:59:05 -0600 Subject: [PATCH 2/5] bug fixes --- app.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/app.js b/app.js index 310ece5..a0907af 100644 --- a/app.js +++ b/app.js @@ -511,6 +511,7 @@ VIEWER.consumeForGeoJSON = async function(dataURL) { * Get the visible properties of the resource such as the thumbnail, description, and label. */ function getResourceProperties(dtype) { + let coll_geos = [] if (VIEWER.resource.hasOwnProperty("navPlace")) { if (VIEWER.resource.navPlace.features){ VIEWER.resource.navPlace.features = VIEWER.resource.navPlace.features.map(f => { @@ -527,16 +528,21 @@ VIEWER.consumeForGeoJSON = async function(dataURL) { if (!f.properties.hasOwnProperty("label")) { f.properties.label = VIEWER.resource.label ?? "" } if (!f.properties.hasOwnProperty(dtype)) { if (resourceType === "Manifest") { f.properties.manifest = manifest["@id"] ?? manifest["id"] ?? "Yikes" } + else if (resourceType === "Collection") {f.properties.label = VIEWER.resource.label ?? "" } else { f.properties[dtype] = VIEWER.resource["@id"] ?? VIEWER.resource["id"] ?? "Yikes" } } if (dtype === "collection") { f.properties.collection = VIEWER.resource["@id"] ?? VIEWER.resource["id"] ?? "Yikes" } return f }) + coll_geos.push(VIEWER.resource.navPlace) } } - return VIEWER.resource.navPlace + return coll_geos } + /* + * Get the visible properties of the manifest such as the thumbnail, description, and label. + */ function getManifestProperties() { let manifest_geos = []; VIEWER.resource.items.map(async (manifest) => { @@ -563,7 +569,7 @@ VIEWER.consumeForGeoJSON = async function(dataURL) { } return f }) - manifest_geos = manifest.navPlace + manifest_geos.push(manifest.navPlace) } } }) @@ -636,7 +642,9 @@ VIEWER.consumeForGeoJSON = async function(dataURL) { return geoJSONFeatures break case "Canvas": - geoJSONFeatures = getResourceProperties("canvas") + let canvas_geos = getResourceProperties("canvas") + //Canvas is special because it does not need the return value to be an Array, so the Array must be unpacked + if (canvas_geos.length >= 1) {geoJSONFeatures = canvas_geos[0]} return geoJSONFeatures break case "AnnotationPage": From 9b4299952f6846fdbaeb4ca4a02f966847eed3d9 Mon Sep 17 00:00:00 2001 From: Tony Obradovic Date: Tue, 20 Feb 2024 22:53:36 -0600 Subject: [PATCH 3/5] Bug Fixes: fixed getStructures to properly return two values. Still fails manifests_and_canvases.json in tests. --- app.js | 88 ++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 48 insertions(+), 40 deletions(-) diff --git a/app.js b/app.js index a0907af..8f5c846 100644 --- a/app.js +++ b/app.js @@ -576,45 +576,50 @@ VIEWER.consumeForGeoJSON = async function(dataURL) { return manifest_geos } - async function getStructures(alias) { + async function getStructures() { let structuresGeos = [] let canvasGeos = [] - if (alias.hasOwnProperty("structures") && alias.structures.length) { - structuresGeos = await Promise.all(alias.structures.map(async (s) => { - //This range may contain other ranges and has the same complexity as a Collection... - let structureGeo = await VIEWER.findAllFeatures(s, "navPlace", [], false) - return structureGeo - })) - } - else if (alias.hasOwnProperty("items") && alias.items.length) { - canvasGeos = alias.items - .filter(item => { - //We only care about Canvases I think. Ignore everything else - let itemType = item.type ?? item["@type"] ?? "Yikes" - return item.hasOwnProperty("navPlace") && (itemType === "Canvas") - }) - .map(canvas => { - //Add data from the canvas or the manifest here. - if(canvas.navPlace.features){ - canvas.navPlace.features.forEach(feature => { - //FIXME support referenced Features even though the spec encourages embedded Features? - if (!feature.properties.hasOwnProperty("thumbnail")) { - //Then lets grab the image URL from the painting annotation - if(canvas.thumbnail) { feature.properties.thumbnail = canvas.thumbnail } - else if (canvas.items && canvas.items[0] && canvas.items[0].items && canvas.items[0].items[0].body) { - let thumburl = canvas.items[0].items[0].body.id ?? "" - feature.properties.thumbnail = [{"id":thumburl}] + VIEWER.resource.items.map(async (manifest) => { + if (manifest.hasOwnProperty("structures") && manifest.structures.length) { + structuresGeos = await Promise.all(manifest.structures.map(async (s) => { + //This range may contain other ranges and has the same complexity as a Collection... + let structureGeo = await VIEWER.findAllFeatures(s, "navPlace", [], false) + return structureGeo + })) + } + else if (manifest.hasOwnProperty("items") && manifest.items.length) { + console.log("ELSE IF") + canvasGeos = manifest.items + .filter(item => { + //We only care about Canvases I think. Ignore everything else + let itemType = item.type ?? item["@type"] ?? "Yikes" + return item.hasOwnProperty("navPlace") && (itemType === "Canvas") + }) + .map(canvas => { + //Add data from the canvas or the manifest here. + if(canvas.navPlace.features){ + canvas.navPlace.features.forEach(feature => { + //FIXME support referenced Features even though the spec encourages embedded Features? + if (!feature.properties.hasOwnProperty("thumbnail")) { + //Then lets grab the image URL from the painting annotation + if(canvas.thumbnail) { feature.properties.thumbnail = canvas.thumbnail } + else if (canvas.items && canvas.items[0] && canvas.items[0].items && canvas.items[0].items[0].body) { + let thumburl = canvas.items[0].items[0].body.id ?? "" + feature.properties.thumbnail = [{"id":thumburl}] + } } - } - if (!feature.properties.hasOwnProperty("summary")) { feature.properties.summary = canvas.summary ?? "" } - if (!feature.properties.hasOwnProperty("label")) { feature.properties.label = canvas.label ?? "" } - if (!feature.properties.hasOwnProperty("canvas")) { feature.properties.canvas = canvas["@id"] ?? canvas["id"] ?? "Yikes" } - }) - return canvas.navPlace - } - }) - return structuresGeos, canvasGeos - } + if (!feature.properties.hasOwnProperty("summary")) { feature.properties.summary = canvas.summary ?? "" } + if (!feature.properties.hasOwnProperty("label")) { feature.properties.label = canvas.label ?? "" } + if (!feature.properties.hasOwnProperty("canvas")) { feature.properties.canvas = canvas["@id"] ?? canvas["id"] ?? "Yikes" } + }) + return canvas.navPlace + } + }) + } + }) + console.log("STRUCTS BEFORE RETURN: ", structuresGeos) + console.log("itemGEOS BEFORE RETURN: ", canvasGeos) + return [structuresGeos, canvasGeos] } /** @@ -627,8 +632,11 @@ VIEWER.consumeForGeoJSON = async function(dataURL) { case "Collection": geoJSONFeatures = getResourceProperties("collection") let manifestGeos = getManifestProperties() - let structGeos, itemGeos = getStructures(manifest) - geoJSONFeatures = geoJSONFeatures.concat([...manifestGeos, structGeos, itemGeos]) + console.log("manifestGeos: ", manifestGeos) + let [structGeos, itemGeos] = await getStructures() + console.log("structGeos: ", structGeos) + console.log("itemGeos: ", itemGeos) + geoJSONFeatures = geoJSONFeatures.concat([...manifestGeos, ...structGeos, ...itemGeos]) return geoJSONFeatures break case "Range": @@ -637,8 +645,8 @@ VIEWER.consumeForGeoJSON = async function(dataURL) { break case "Manifest": let geos = getResourceProperties("manifest") - let structureGeo, itemsGeos = getStructures(VIEWER.resource) - geoJSONFeatures = [...geos, structureGeo, itemsGeos] + let [structureGeo, itemsGeos] = await getStructures() + geoJSONFeatures = [...geos, ...structureGeo, ...itemsGeos] return geoJSONFeatures break case "Canvas": From cd26fa6832166f81e3a904e91570976a996571ac Mon Sep 17 00:00:00 2001 From: Tony Obradovic Date: Wed, 21 Feb 2024 21:16:29 -0600 Subject: [PATCH 4/5] Bug Fixes: passes all tests in test folder. --- app.js | 90 +++++++++++++++++++++++++++------------------------------- 1 file changed, 42 insertions(+), 48 deletions(-) diff --git a/app.js b/app.js index 8f5c846..144a979 100644 --- a/app.js +++ b/app.js @@ -545,6 +545,8 @@ VIEWER.consumeForGeoJSON = async function(dataURL) { */ function getManifestProperties() { let manifest_geos = []; + let structs = []; + let items = []; VIEWER.resource.items.map(async (manifest) => { if (manifest.hasOwnProperty("navPlace")) { if (manifest.navPlace.features) { @@ -572,53 +574,49 @@ VIEWER.consumeForGeoJSON = async function(dataURL) { manifest_geos.push(manifest.navPlace) } } + [structs, items] = getStructures(manifest) }) - return manifest_geos + return [manifest_geos, structs, items] } - async function getStructures() { + async function getStructures(alias) { let structuresGeos = [] let canvasGeos = [] - VIEWER.resource.items.map(async (manifest) => { - if (manifest.hasOwnProperty("structures") && manifest.structures.length) { - structuresGeos = await Promise.all(manifest.structures.map(async (s) => { - //This range may contain other ranges and has the same complexity as a Collection... - let structureGeo = await VIEWER.findAllFeatures(s, "navPlace", [], false) - return structureGeo - })) - } - else if (manifest.hasOwnProperty("items") && manifest.items.length) { - console.log("ELSE IF") - canvasGeos = manifest.items - .filter(item => { - //We only care about Canvases I think. Ignore everything else - let itemType = item.type ?? item["@type"] ?? "Yikes" - return item.hasOwnProperty("navPlace") && (itemType === "Canvas") - }) - .map(canvas => { - //Add data from the canvas or the manifest here. - if(canvas.navPlace.features){ - canvas.navPlace.features.forEach(feature => { - //FIXME support referenced Features even though the spec encourages embedded Features? - if (!feature.properties.hasOwnProperty("thumbnail")) { - //Then lets grab the image URL from the painting annotation - if(canvas.thumbnail) { feature.properties.thumbnail = canvas.thumbnail } - else if (canvas.items && canvas.items[0] && canvas.items[0].items && canvas.items[0].items[0].body) { - let thumburl = canvas.items[0].items[0].body.id ?? "" - feature.properties.thumbnail = [{"id":thumburl}] - } + if (alias.hasOwnProperty("structures") && alias.structures.length) { + structuresGeos = await Promise.all(alias.structures.map(async (s) => { + //This range may contain other ranges and has the same complexity as a Collection... + let structureGeo = await VIEWER.findAllFeatures(s, "navPlace", [], false) + return structureGeo + })) + } + else if (alias.hasOwnProperty("items") && alias.items.length) { + canvasGeos = alias.items + .filter(item => { + //We only care about Canvases I think. Ignore everything else + let itemType = item.type ?? item["@type"] ?? "Yikes" + return item.hasOwnProperty("navPlace") && (itemType === "Canvas") + }) + .map(canvas => { + //Add data from the canvas or the alias here. + if(canvas.navPlace.features){ + canvas.navPlace.features.forEach(feature => { + //FIXME support referenced Features even though the spec encourages embedded Features? + if (!feature.properties.hasOwnProperty("thumbnail")) { + //Then lets grab the image URL from the painting annotation + if(canvas.thumbnail) { feature.properties.thumbnail = canvas.thumbnail } + else if (canvas.items && canvas.items[0] && canvas.items[0].items && canvas.items[0].items[0].body) { + let thumburl = canvas.items[0].items[0].body.id ?? "" + feature.properties.thumbnail = [{"id":thumburl}] } - if (!feature.properties.hasOwnProperty("summary")) { feature.properties.summary = canvas.summary ?? "" } - if (!feature.properties.hasOwnProperty("label")) { feature.properties.label = canvas.label ?? "" } - if (!feature.properties.hasOwnProperty("canvas")) { feature.properties.canvas = canvas["@id"] ?? canvas["id"] ?? "Yikes" } - }) - return canvas.navPlace - } - }) - } - }) - console.log("STRUCTS BEFORE RETURN: ", structuresGeos) - console.log("itemGEOS BEFORE RETURN: ", canvasGeos) + } + if (!feature.properties.hasOwnProperty("summary")) { feature.properties.summary = canvas.summary ?? "" } + if (!feature.properties.hasOwnProperty("label")) { feature.properties.label = canvas.label ?? "" } + if (!feature.properties.hasOwnProperty("canvas")) { feature.properties.canvas = canvas["@id"] ?? canvas["id"] ?? "Yikes" } + }) + return canvas.navPlace + } + }) + } return [structuresGeos, canvasGeos] } @@ -630,12 +628,8 @@ VIEWER.consumeForGeoJSON = async function(dataURL) { switch(resourceType){ // This logic is the same as the Manifest logic, except looped over Collection.items which are Manifests. case "Collection": - geoJSONFeatures = getResourceProperties("collection") - let manifestGeos = getManifestProperties() - console.log("manifestGeos: ", manifestGeos) - let [structGeos, itemGeos] = await getStructures() - console.log("structGeos: ", structGeos) - console.log("itemGeos: ", itemGeos) + geoJSONFeatures = geoJSONFeatures.concat(getResourceProperties("collection")) + let [manifestGeos,structGeos, itemGeos] = getManifestProperties() geoJSONFeatures = geoJSONFeatures.concat([...manifestGeos, ...structGeos, ...itemGeos]) return geoJSONFeatures break @@ -645,7 +639,7 @@ VIEWER.consumeForGeoJSON = async function(dataURL) { break case "Manifest": let geos = getResourceProperties("manifest") - let [structureGeo, itemsGeos] = await getStructures() + let [structureGeo, itemsGeos] = await getStructures(VIEWER.resource) geoJSONFeatures = [...geos, ...structureGeo, ...itemsGeos] return geoJSONFeatures break From 98844545b2dfe160f9a5fcb6c3b3d4ab182ac311 Mon Sep 17 00:00:00 2001 From: Tony Obradovic Date: Mon, 26 Feb 2024 13:04:09 -0600 Subject: [PATCH 5/5] Added javadoc documentation. Removed semicolons. --- app.js | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/app.js b/app.js index 144a979..503b798 100644 --- a/app.js +++ b/app.js @@ -507,8 +507,9 @@ VIEWER.consumeForGeoJSON = async function(dataURL) { return prev.concat(curr) }, []) - /* - * Get the visible properties of the resource such as the thumbnail, description, and label. + /** + * Retrieve the visible properties of the resource(dtype) such as the thumbnail, description, and label. + * @return {Array} */ function getResourceProperties(dtype) { let coll_geos = [] @@ -540,13 +541,14 @@ VIEWER.consumeForGeoJSON = async function(dataURL) { return coll_geos } - /* - * Get the visible properties of the manifest such as the thumbnail, description, and label. + /** + * Retrieve the visible properties of the manifest such as the thumbnail, description, and label. + * @return {Array} 3-item array containing the properties of the manifest and the results of getStructures */ function getManifestProperties() { - let manifest_geos = []; - let structs = []; - let items = []; + let manifest_geos = [] + let structs = [] + let items = [] VIEWER.resource.items.map(async (manifest) => { if (manifest.hasOwnProperty("navPlace")) { if (manifest.navPlace.features) { @@ -579,6 +581,12 @@ VIEWER.consumeForGeoJSON = async function(dataURL) { return [manifest_geos, structs, items] } + /** + * If the alias has a "structures" property, retrieve the structures using the findAllFeatures functions. + * Else, if the alias has an "items" property, loop through each item and grab the features of each item. + * @param {type} alias resource to grab structures and items information from. Varies for each resourceType + * @return {Array} 3-item array containing the structures from findAllFeatures and the item's surface level features like label, summary, and thumbnail + */ async function getStructures(alias) { let structuresGeos = [] let canvasGeos = []