This script pulls all visible layers from Get Food NYC and converts them to geojson.
npm install
npm start
This script pulls all visible layers from Get Food NYC and converts them to geojson.
npm install
npm start
import proj4 from 'proj4' | |
import { promises as fs } from 'fs' | |
import fetch from 'node-fetch' | |
proj4.defs('EPSG:4326', '+title=WGS 84 (long/lat) +proj=longlat +ellps=WGS84 +datum=WGS84 +units=degrees'); | |
proj4.defs('EPSG:3857','+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs') | |
function convertToWGS84(coords) { | |
return proj4('EPSG:3857', 'EPSG:4326', coords) | |
} | |
async function main() { | |
const webmapId = 'd171a1413c7f44e0aeef34de37355d4c' | |
//pull all layers for web map | |
const layers = await fetch(`https://www.arcgis.com/sharing/rest/content/items/${webmapId}/data?f=json`) | |
.then(res => res.json()) | |
.then(async ({ operationalLayers }) => { | |
//filter for only visible layers | |
const visibleLayers = operationalLayers.filter(layer => layer.visibility && layer.itemId) | |
//pull all data for each layer | |
return Promise.all(visibleLayers.map(async ({ title, itemId }) => { | |
const features = await fetch(`http://maps.arcgis.com/sharing/rest/content/items/${itemId}/data?f=json`) | |
.then(res => res.json()) | |
.then(data => { | |
//append all features in layers to a single feature collection | |
return data.layers.reduce((collection, layer) => { | |
//assumes esriGeometryPoint and EPSG:3857 | |
const features = layer.featureSet.features.map(feature => ({ | |
type: "Feature", | |
properties: feature.attributes, | |
geometry: { | |
type: "Point", | |
coordinates: convertToWGS84([feature.geometry.x, feature.geometry.y]) | |
} | |
})) | |
return [...collection, ...features] | |
}, []) | |
}).catch(error => error.message) | |
return { | |
title, | |
itemId, | |
features | |
} | |
})) | |
}) | |
layers.forEach(({ title, itemId, features }) => { | |
//write each layer to file | |
fs.writeFile(`./layers/${itemId}.geojson`, JSON.stringify({ | |
type: "FeatureCollection", | |
name: title, | |
features, | |
})); | |
}) | |
} | |
main() |
{ | |
"name": "nyc-food-map", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1", | |
"start": "node -r esm index.js" | |
}, | |
"keywords": [], | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"esm": "^3.2.25", | |
"node-fetch": "^2.6.0", | |
"proj4": "^2.6.1" | |
} | |
} |