0

我有一个 Geojson 文件,名称为countries.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");
});
4

2 回答 2

1

要进行编码,请使用 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();
于 2021-02-11T20:15:38.910 回答
0

使用geobuf库:

var buffer = geobuf.encode(geojson, new Pbf());
于 2020-02-18T14:59:38.460 回答