mask-to-geojson converts Mapwarper GML masks to GeoJSON polygons, using gdaltransform. Given a map ID, mask-to-geojson reads the map's mask and ground control points (GCPs) from the Mapwarper API and transforms the mask's pixel coordinates to latitude/longitude coordinates.
Important: you need to have GDAL installed for mask-to-geojson to work!
As a Node.js module:
npm install --save nypl-spacetime/mask-to-geojson
As a standalone command line tool:
npm install -g nypl-spacetime/mask-to-geojson
As a Node.js module:
var maskToGeoJSON = require('mask-to-geojson')
maskToGeoJSON.getMaskAndTransform({
mapId: 27378
}, (err, geojson, gcps, mask) => {
if (err) {
console.error(err)
} else {
console.log('Mask:', mask)
console.log('GCPs:', gcps)
console.log('GeoJSON:', JSON.stringify(geojson))
}
})
As a standalone command-line tool:
mask-to-geojson 27378
To check if the user has GDAL installed, you can use the gdalInstalled
function:
maskToGeoJSON.gdalInstalled((err, version) => {
if (err) {
console.log('No!')
} else {
console.log('Yes!', version)
}
})
Like this!
For example, take this 1901 map of Long Island City:
Using NYPL's Mapwarper, this map is both georectified and cropped:
Mapwarper has an API endpoint for GCPs and cropping masks:
Because the mask is in pixel coordinates of the original TIFF file, we need to use gdaltransform to convert the mask to geographic coordinates with the map's GCPs as command line arguments:
gdaltransform -gcp 3831.7098930481, 1242.09759358287, 40.7745047932, -73.923830011 -gcp 3868.59759358287, 3395.41711229945, 40.7697272423, -73.9278640533 -gcp 2416.14438502671, 3340.0855614973, 40.7719373086, -73.9321985031 -gcp 2416.14438502669, 1214.43181818179, 40.7765847081, -73.9281215454
Now, inputting pixel coordinates 6183 1866
(Astoria Boulevard & 31st Street) yields the output 40.7697658590603 -73.9178309436292
!
On MacOS:
brew install gdal
On Ubuntu:
sudo apt-get install gdal-bin