Created
September 12, 2022 22:03
-
-
Save ThomasG77/725ddcd24a42227a196e0cedf28ad1cc to your computer and use it in GitHub Desktop.
mapshaper-info-recreated-via-gdal
This file contains 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
import fs from 'fs' | |
import got from 'got' | |
import gdal from 'gdal-async' | |
import { pipeline } from 'stream/promises' | |
const url = 'https://github.com/deldersveld/topojson/raw/master/countries/germany/germany-regions.json' | |
const fileName = url.substring(url.lastIndexOf('/') + 1) | |
const downloadStream = got.stream(url) | |
const fileWriterStream = fs.createWriteStream(fileName) | |
// function getAllPropertyNames(obj) { | |
// var props = []; | |
// do { | |
// Object.getOwnPropertyNames(obj).forEach(function (prop) { | |
// if (props.indexOf(prop) === -1 ) { | |
// props.push( prop ); | |
// } | |
// }); | |
// } while (obj = Object.getPrototypeOf(obj)); | |
// return props; | |
// } | |
downloadStream.on("downloadProgress", ({ transferred, total, percent }) => { | |
const percentage = Math.round(percent * 100) | |
//console.error(`progress: ${transferred}/${total} (${percentage}%)`) | |
}); | |
async function main() { | |
try { | |
await pipeline(downloadStream, fileWriterStream) | |
// console.log(`File downloaded to ${fileName}`) | |
const ds = gdal.open(fileName) | |
console.log(`[info]\n==================================================`) | |
for (const layer of ds.layers) { | |
// console.log(layer.features) | |
console.log(`Layer: ${layer.name}`) | |
console.log('--------------------------------------------------') | |
console.log(`Type: ${gdal.Geometry.getName(layer.geomType)}`) | |
console.log(`Records: ${layer.features.count()}`) | |
const extent = layer.getExtent() | |
const {minX, minY, maxX, maxY} = extent | |
console.log(`Bounds: ${minX},${minY},${maxX},${maxY}`) | |
let crs = null | |
if (ds.driver.description == 'TopoJSON') { | |
crs = '+proj=longlat +datum=WGS84' | |
let json_content = fs.readFileSync(fileName) | |
json_content = JSON.parse(json_content) | |
// Issue with Mapshaper: it reports polygon whereas there are polygon and multipolygon | |
// (uncomment below to confirm) | |
// console.log(json_content.objects[layer.name].geometries.map(el => el.type)) | |
} else if (layer.srs) { | |
crs = layer.srs.toWKT() | |
} | |
console.log( | |
`CRS: ${crs}` | |
) | |
console.log(`Source: ${fileName}`) | |
// console.log(' Fields: ') | |
var first = layer.features.first() | |
// console.log(first) | |
const content = first.defn.fields.map(f => [f.name, first.fields.get(f.name)]) | |
let max_length_attribute_name = Math.max(...content.filter(el => !!(el[0])).map(el => el[0].length)) | |
if (max_length_attribute_name < 7) { | |
max_length_attribute_name = 7 | |
} | |
let max_length_attribute_value = Math.max(...content.filter(el => !!(el[1])).map(el => `${el[1]}`.length)) | |
if (max_length_attribute_value < 13) { | |
max_length_attribute_value = 13 | |
} | |
if (content.length > 0) { | |
console.log(`\nAttribute data`) | |
console.log(`${'-'.repeat(max_length_attribute_name + 2)}+--------------------------------------`) | |
console.log(` Field ${' '.repeat(max_length_attribute_name - 5)}| First value`) | |
console.log(`${'-'.repeat(max_length_attribute_name + 2)}+--------------------------------------`) | |
} | |
// console.log(max_length_attribute_name) | |
// console.log(max_length_attribute_value) | |
console.log(content | |
.filter(el => el[0] != 'id') | |
.slice() | |
.sort((a, b) => a[0].localeCompare(b[0])) | |
.map(el => ` ${el[0]} ${' '.repeat(max_length_attribute_name - el[0].length)}| ${el[1]}`) | |
.join('\n')) | |
console.log(`${'-'.repeat(max_length_attribute_name + 2)}+--------------------------------------`) | |
} | |
} catch (error) { | |
console.error(`Something went wrong. ${error.message}`); | |
} | |
} | |
main().catch(error => { | |
console.error(error) | |
process.exit(1) | |
}) |
This file contains 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
{ | |
"name": "mapshaper-info-recreated-via-gdal", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"type": "module", | |
"keywords": [], | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"gdal-async": "^3.5.1", | |
"got": "^12.4.1" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment