首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用NodeJS从GeoJSON文件创建PBF文件?

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

Stack Overflow用户
提问于 2020-02-18 22:46:47
回答 2查看 666关注 0票数 0

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

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

代码语言:javascript
复制
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");
});
EN

回答 2

Stack Overflow用户

发布于 2021-02-12 04:15:39

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

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

代码语言:javascript
复制
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();
票数 1
EN

Stack Overflow用户

发布于 2020-02-18 22:59:38

使用geobuf库:

代码语言:javascript
复制
var buffer = geobuf.encode(geojson, new Pbf());
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60283495

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档