Created
August 10, 2018 16:26
-
-
Save flippmoke/ad025392c0880658acf47a5a07a50e5f to your computer and use it in GitHub Desktop.
LiDAR fun
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
"use strict"; | |
const vtpbf = require('vt-pbf'); | |
const fs = require('fs'); | |
const geojsonVt = require('geojson-vt'); | |
const SphericalMercator = require('@mapbox/sphericalmercator'); | |
const util = require('util'); | |
const cp = require('child_process'); | |
const PromisePool = require('es6-promise-pool') | |
function execute_pdal(options, callback) { | |
var merc = new SphericalMercator({ | |
size: 4096 | |
}); | |
var bbox = merc.bbox(options.x, options.y, options.zoom, false, "900913"); | |
var bounds = "([" + bbox[0] + "," + bbox[2] + "],[" + bbox[1] + "," + bbox[3] + "])"; | |
var width = (Math.abs(bbox[2] - bbox[0]) / options.extent) * 16; | |
var pdal_obj = { | |
"pipeline": [ | |
options.input_file, | |
{ | |
"type":"filters.crop", | |
"bounds": bounds | |
}, | |
{ | |
"type":"filters.voxelcentroidnearestneighbor", | |
"cell": width | |
}, | |
{ | |
"type":"filters.reprojection", | |
"in_srs":"EPSG:3857", | |
"out_srs":"EPSG:4326" | |
}, | |
{ | |
"type":"writers.text", | |
"format":"geojson", | |
"order":"Red:0,Green:0,Blue:0", | |
"precision": 6, | |
"keep_unspecified":"false", | |
"filename": "STDOUT" | |
} | |
] | |
}; | |
let tmp_pipeline_file = 'tmp/pdal_config_' + options.zoom + '_' + options.x + '_' + options.y + '.json' | |
fs.writeFile(tmp_pipeline_file, JSON.stringify(pdal_obj), (err) => { | |
if (err) throw err; | |
cp.exec('pdal pipeline ' + tmp_pipeline_file, {maxBuffer: 1024 * 1024 * 1024}, callback); | |
}); | |
} | |
const create_tile = (options, callback) => { | |
console.log('Creating tile: ' + options.zoom + ',' + options.x + ',' + options.y) | |
execute_pdal(options, (err, stdout, stderr) => { | |
if (err) { | |
console.log(err); | |
console.log(stderr); | |
} | |
try { | |
var orig = JSON.parse(stdout); | |
var tileindex = geojsonVt(orig, { | |
buffer: 0, | |
dimensions: 3, | |
maxZoom: 19}); | |
var tile = tileindex.getTile(options.zoom, options.x, options.y); | |
if (!tile) { | |
console.log("no data to tile"); | |
callback(); | |
} | |
var buff = vtpbf.fromGeojsonVt({ 'geojsonLayer': tile }); | |
fs.writeFile('tiles/'+ options.zoom + '_' + options.x + '_' + options.y + '.mvt', buff, callback); | |
} catch (error) { | |
console.log(error); | |
callback(); | |
} | |
}); | |
} | |
const tile_promise = util.promisify(create_tile); | |
const generateTilePromises = function * () { | |
let start_z = 16; | |
let end_z = 16; | |
// start and end in start_z coordinates | |
let start_x = 18740; | |
let end_x = 18748; | |
let start_y = 25071; | |
let end_y = 25073; | |
for (let z = start_z; z <= end_z; z++) { | |
let s_x = start_x * (1 << (z - start_z)); | |
let e_x = (end_x + 1) * (1 << (z - start_z)); | |
let s_y = start_y * (1 << (z - start_z)); | |
let e_y = (end_y + 1) * (1 << (z - start_z)); | |
for (let x = s_x; x < e_x; x++) { | |
for (let y = s_y; y < e_y; y++) { | |
yield tile_promise({ | |
zoom: z, | |
x: x, | |
y: y, | |
extent: 4096, | |
input_file: "color.las" | |
}); | |
} | |
} | |
} | |
} | |
const concurrency = 2; | |
const promiseTileIterator = generateTilePromises(); | |
// Create a pool. | |
var pool = new PromisePool(promiseTileIterator, concurrency); | |
// Start the pool. | |
var poolPromise = pool.start(); | |
// Wait for the pool to settle. | |
poolPromise.then(function () { | |
console.log('All promises fulfilled') | |
}, function (error) { | |
console.log('Some promise rejected: ' + error.message) | |
}); |
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": "mypoints", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"dependencies": { | |
"@mapbox/point-geometry": "https://github.com/mapbox/point-geometry/tarball/3d", | |
"@mapbox/vector-tile": "https://github.com/mapbox/vector-tile-js/tarball/vt3-3d", | |
"geojson-vt": "https://github.com/mapbox/geojson-vt/tarball/3d", | |
"vt-pbf": "https://github.com/mapbox/vt-pbf/tarball/vt3-3d", | |
"@mapbox/sphericalmercator": "^1.0.5", | |
"es6-promise-pool": "2.5.0" | |
}, | |
"devDependencies": {}, | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "", | |
"license": "ISC" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment