To create a downloadable GeoJSON file from a JavaScript object, you can use the Blob API and create a temporary download link. Here's how:
Basic Function
javascript
function downloadGeoJSON(geojsonObject, filename = 'data.geojson') {
// Convert the GeoJSON object to a string
const geojsonString = JSON.stringify(geojsonObject, null, 2);
// Create a Blob from the string
const blob = new Blob([geojsonString], { type: 'application/json' });
// Create a temporary URL for the blob
const url = URL.createObjectURL(blob);
// Create a temporary anchor element and trigger download
const a = document.createElement('a');
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
// Clean up
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
// Example usage
const geojsonData = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [-74.006, 40.7128]
},
"properties": {
"name": "New York City"
}
}
]
};
downloadGeoJSON(geojsonData, 'my-map-data.geojson');
React Example
jsx
import React from 'react';
function GeoJSONDownloader() {
const geojsonData = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [[
[-74.0, 40.7],
[-74.0, 40.8],
[-73.9, 40.8],
[-73.9, 40.7],
[-74.0, 40.7]
]]
},
"properties": {
"name": "Sample Area"
}
}
]
};
const handleDownload = () => {
const dataStr = JSON.stringify(geojsonData, null, 2);
const blob = new Blob([dataStr], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.download = 'map-data.geojson';
link.click();
URL.revokeObjectURL(url);
};
return (
<button onClick={handleDownload}>
Download GeoJSON
</button>
);
}
export default GeoJSONDownloader;
Alternative Using Data URI
For smaller files, you can also use a data URI approach:
javascript
function downloadGeoJSONDataURI(geojsonObject, filename = 'data.geojson') {
const dataStr = "data:text/json;charset=utf-8," +
encodeURIComponent(JSON.stringify(geojsonObject, null, 2));
const a = document.createElement('a');
a.href = dataStr;
a.download = filename;
a.click();
}
With Error Handling
javascript
function downloadGeoJSONSafe(geojsonObject, filename = 'data.geojson') {
try {
// Validate it's a proper object
if (!geojsonObject || typeof geojsonObject !== 'object') {
throw new Error('Invalid GeoJSON object');
}
// Convert to string with pretty printing
const geojsonString = JSON.stringify(geojsonObject, null, 2);
// Create and download
const blob = new Blob([geojsonString], { type: 'application/geo+json' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.style.display = 'none';
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
// Cleanup
setTimeout(() => {
document.body.removeChild(a);
URL.revokeObjectURL(url);
}, 100);
return true;
} catch (error) {
console.error('Error downloading GeoJSON:', error);
return false;
}
}
Vue.js Example
vue
<template>
<button @click="downloadGeoJSON">Download GeoJSON</button>
</template>
<script>
export default {
methods: {
downloadGeoJSON() {
const geojsonData = {
type: "FeatureCollection",
features: [
{
type: "Feature",
geometry: {
type: "LineString",
coordinates: [
[-74.006, 40.7128],
[-73.935242, 40.730610]
]
},
properties: {
name: "Route 1"
}
}
]
};
const json = JSON.stringify(geojsonData, null, 2);
const blob = new Blob([json], { type: 'application/json' });
const href = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = href;
link.download = 'route.geojson';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(href);
}
}
}
</script>
Key Points
- MIME Type: Use
application/json or the more specific application/geo+json
- Pretty Printing: The
null, 2 arguments in JSON.stringify() add indentation for readability
- Cleanup: Always revoke the object URL after use to free memory
- File Extension: Use
.geojson or .json as the file extension
- Browser Compatibility: This works in all modern browsers
The downloaded file will be a standard GeoJSON file that can be opened in GIS software, mapping libraries, or any text editor.