Last active
November 26, 2024 15:50
-
-
Save jkeefe/583c051b27ffe1b76fbafbb184bac3cd to your computer and use it in GitHub Desktop.
Importing mapshaper in ES6
This file contains hidden or 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
// npm install mapshaper --save | |
import mapshaper from 'mapshaper' | |
// Example of runCommands: converting a directory of Shapefiles to GeoJSON | |
mapshaper.runCommands('-i shapefiles/*.shp -o geojson/ format=geojson'); | |
// ---- | |
// Example of applyCommands: processing a json array | |
const makeGeojson = async (data) => { | |
const input = { 'input.json': data }; | |
const cmd = '-i input.json -points x=lon y=lat -o output.geojson'; | |
const output = await mapshaper.applyCommands(cmd, input); | |
return output; | |
}; | |
// ... then: | |
const geojson = await makeGeojson(some_json_data); | |
// this is now an object that looks like this: | |
// { | |
// 'output.geojson': <Buffer 7b 22 74 79 70 65 22 3a 22 46 65 ... 409108 more bytes> | |
// } | |
// so to use, need to convert the buffer | |
console.log(geojson['output.geojson'].toString(), false, 2); | |
// => | |
// {"type":"FeatureCollection", "features": [ | |
// {"type":"Feature","geometry":{"type":"Point","coordinates":[171.84,50.169]},"properties":{"locationName":"DART 21415","locationCountry":"United States","nyt_location_override":"An ocean sensor","nyt_place_elaboration":"180 miles south of Attu Island","nyt_state":"Alaska","data_geo_link":"https://www.google.com/maps/place/50.169,171.840","nyt_location_slug":"dart21415|50.169|171.840","lat":"50.169","lon":"171.84","tz_override_location":"Attu Island","tz_override_latitude":"52.9025","tz_override_longitude":"172.909444","geocode_verify":"Alaska, United States"}}, | |
// {"type":"Feature","geometry":{"type":"Point","coordinates":[173.6,52.38]},"properties":{"locationName":"Karab Cove","locationCountry":"United States","nyt_location_override":"Agattu Island","nyt_place_elaboration":"","nyt_state":"Alaska","data_geo_link":"https://www.google.com/maps/place/52.380,173.600","nyt_location_slug":"karabcove|52.380|173.600","lat":"52.38","lon":"173.6","tz_override_location":"","tz_override_latitude":"","tz_override_longitude":"","geocode_verify":""}}, | |
// ...etc | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment