Skip to content

Instantly share code, notes, and snippets.

@zhik
Last active April 13, 2020 19: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 zhik/21b0636481f63f1661ea63e10ab68b7a to your computer and use it in GitHub Desktop.
Save zhik/21b0636481f63f1661ea63e10ab68b7a to your computer and use it in GitHub Desktop.

This script pulls all visible layers from Get Food NYC and converts them to geojson.

How to run

  1. npm install
  2. npm start
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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()
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
{
"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"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment