0

I have a Geojson file which name is countries.geojson.

I can read this file with get method. I want to create PBF files from this GeoJSON file. How can I do that?

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");
});
Penny Liu
  • 15,447
  • 5
  • 79
  • 98

2 Answers2

1

To encode, use the example provided by Horatio Jeflea in that answer.

To decode, as in using the response in a web browser, use something like this or (more modernly, the 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();
Stonetip
  • 1,150
  • 10
  • 20
0

Use geobuf library:

var buffer = geobuf.encode(geojson, new Pbf());
Horatiu Jeflea
  • 7,256
  • 6
  • 38
  • 67
  • It gives buffer values. I guess I need to use `protocol-buffer` then. How should i continue with that ? Can you give me some information about that? – buğra coşkun Feb 18 '20 at 16:04
  • @buğracoşkun if you're asking about how you might use (decode) the geobuf, e.g. in a client then you need to use https://github.com/mapbox/pbf (pbf.js) and the geobuf lib. I'm posting some (kinda old) sample code in an answer so it's formatted. – Stonetip Feb 11 '21 at 20:05