Skip to content

Instantly share code, notes, and snippets.

@max-mapper
Last active September 26, 2023 20:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save max-mapper/a5e2973b413d4077fec20de6509d30dc to your computer and use it in GitHub Desktop.
Save max-mapper/a5e2973b413d4077fec20de6509d30dc to your computer and use it in GitHub Desktop.
Save Felt.com rendered map features to GeoJSON feature collection

Save bookmarklet.js as a bookmarklet and on any Felt map the currently rendered features will be saved as a file features.geojson (note: some features do not render at low zoom levels)

javascript:(function()%7B%2F%2F%20Function%20to%20download%20data%20to%20a%20file%0Afunction%20download(data%2C%20filename%2C%20type)%20%7B%0A%20%20%20%20var%20file%20%3D%20new%20Blob(%5Bdata%5D%2C%20%7Btype%3A%20type%7D)%3B%0A%20%20%20%20if%20(window.navigator.msSaveOrOpenBlob)%20%2F%2F%20IE10%2B%0A%20%20%20%20%20%20%20%20window.navigator.msSaveOrOpenBlob(file%2C%20filename)%3B%0A%20%20%20%20else%20%7B%20%2F%2F%20Others%0A%20%20%20%20%20%20%20%20var%20a%20%3D%20document.createElement(%22a%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20url%20%3D%20URL.createObjectURL(file)%3B%0A%20%20%20%20%20%20%20%20a.href%20%3D%20url%3B%0A%20%20%20%20%20%20%20%20a.download%20%3D%20filename%3B%0A%20%20%20%20%20%20%20%20document.body.appendChild(a)%3B%0A%20%20%20%20%20%20%20%20a.click()%3B%0A%20%20%20%20%20%20%20%20setTimeout(function()%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20document.body.removeChild(a)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20window.URL.revokeObjectURL(url)%3B%20%20%0A%20%20%20%20%20%20%20%20%7D%2C%200)%3B%20%0A%20%20%20%20%7D%0A%7D%0A%0Avar%20fc%20%3D%20%7B%0A%20%20%20%20%22type%22%3A%20%22FeatureCollection%22%2C%0A%20%20%20%20%22features%22%3A%20%5B%5D%0A%7D%0A%0Afelt.mapLibreMap.getMap().queryRenderedFeatures().forEach((feature)%20%3D%3E%20%7B%0A%20%20%20%20var%20f%20%3D%20feature.toJSON()%0A%20%20%20%20if%20(feature.source.indexOf('datasource')%20%3D%3D%3D%20-1)%20return%20%2F%2F%20skip%20basemap%20vectors%0A%20%20%20%20delete%20f.layer%0A%20%20%20%20delete%20f.sourceLayer%0A%20%20%20%20delete%20f.source%0A%20%20%20%20fc.features.push(f)%0A%7D)%0A%0Adownload(JSON.stringify(fc%2C%20null%2C%20'%20%20')%2C%20'features.json'%2C%20'application%2Fjson')%7D)()%3B
// Function to download data to a file
function download(data, filename, type) {
var file = new Blob([data], {type: type});
if (window.navigator.msSaveOrOpenBlob) // IE10+
window.navigator.msSaveOrOpenBlob(file, filename);
else { // Others
var a = document.createElement("a"),
url = URL.createObjectURL(file);
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
setTimeout(function() {
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
}, 0);
}
}
var fc = {
"type": "FeatureCollection",
"features": []
}
felt.mapLibreMap.getMap().queryRenderedFeatures().forEach((feature) => {
var f = feature.toJSON()
if (feature.source.indexOf('datasource') === -1) return // skip basemap vectors
delete f.layer
delete f.sourceLayer
delete f.source
fc.features.push(f)
})
download(JSON.stringify(fc, null, ' '), 'features.json', 'application/json')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment