如何使用NodeJS从GeoJSON文件创建PBF文件



我有一个名为countries.geojson的Geojson文件。

我可以用get方法读取这个文件。我想从这个GeoJSON文件创建PBF文件。我该怎么做?

var express = require("express");
const fs = require('fs');
var Pbf = require('pbf');
var protobuf = require('protocol-buffers')
const path = require('path');
var app = express();
app.get("/get/data", (req, res, next) => {
fs.readFile('data/countries.geojson', (err, data) => {
if (err) throw err;
let country = JSON.parse(data);
res.send(country);
});
});
app.get("/", (req, res, next) => {
// Create PBF file
});
app.listen(3000, () => {
console.log("Server running on port 3000");
});

要进行编码,请使用Horatio Jeflea在该答案中提供的示例。

要进行解码,就像在web浏览器中使用响应一样,可以使用类似的东西或(更现代的是,fetch lib(。

var xmlhttp = new XMLHttpRequest();
// Get the encoded geobuf file
xmlhttp.open("GET", "http://<somedomain>/uscty.pbf", true);
xmlhttp.responseType = "arraybuffer"; // important to know what the data type is
xmlhttp.onload = function () {
// convert the raw response to a pbf
var pbf = new Pbf(new Uint8Array(xmlhttp.response));
// use geobuf lib to decode it to GeoJSON
var decoded = geobuf.decode(pbf);
console.log("Decoded GeoJSON", decoded);
};
xmlhttp.send();

使用geobuf库:

var buffer = geobuf.encode(geojson, new Pbf());

最新更新