簡體   English   中英

如何使用 NodeJS 從 GeoJSON 文件創建 PBF 文件?

[英]How can I create PBF file from GeoJSON file using NodeJS?

我有一個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");
});

要進行編碼,請使用 Horatio Jeflea 在該答案中提供的示例。

要解碼,就像在 Web 瀏覽器中使用響應一樣,使用類似這樣的東西或(更現代的,獲取庫)。

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());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM