Last active
October 2, 2019 11:30
-
-
Save erick-otenyo/0d2c4374b41d4220977c2b0480e83b9b to your computer and use it in GitHub Desktop.
Fetch geonode layers from api and format for Terra JS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const geonode2Terriajs = async (geonodeUrl,geonodeGeoserverUrl,proxyUrl)=> { | |
const catalog = [] | |
try{ | |
// fetch layers from geonode api, using proxy if provided | |
const layers = await fetch(proxyUrl?`${proxyUrl}/${geonodeUrl}`:geonodeUrl).then(res=>res.json()).then(layers=>layers.objects) | |
// group the layers by category | |
const layersByCategory = layers.reduce((all,layer)=>{ | |
let wmsUrl = geonodeGeoserverUrl | |
// use proxy for wms url if provided | |
if(proxyUrl)wmsUrl = `${proxyUrl}/${wmsUrl}` | |
// create Terriajs WMS catalog item | |
const catalogItem = {name:layer.title,type:"wms",url:wmsUrl,layers:layer.typename} | |
if(all[layer.category__gn_description]){ | |
all[layer.category__gn_description].push(catalogItem) | |
} else { | |
all[layer.category__gn_description] = [catalogItem] | |
} | |
return all | |
},{}) | |
// create groups from the items | |
for (const [category,items] of Object.entries(layersByCategory)){ | |
catalog.push({name:category,type:"group",items:items}) | |
} | |
return catalog | |
} catch(error){ | |
console.log(error) | |
} | |
} | |
// Usage | |
const GEONODE_URL = "http://geoportal.icpac.net/api/layers" | |
const GEONODE_GEOSERVER_WMS_URL = "http://geoportal.icpac.net/geoserver/wms" | |
const PROXY_URL = "http://127.0.0.1:8000/proxy" | |
// remember the return value is still a promise. Use promise.then or await, | |
geonode2Terriajs(GEONODE_URL,GEONODE_GEOSERVER_WMS_URL,PROXY_URL).then(items=> { | |
// do something with the catalogItems. Maybe add to Terria JS map | |
console.log(items) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment