mapbox / geobuf

A compact binary encoding for geographic data.
ISC License
967 stars 84 forks source link

Is this not a standard pbf file, or, my geojson in question? #89

Closed wucangeo closed 6 years ago

wucangeo commented 6 years ago

I'm looking for a tool to convert geojson to pbf file in my map , geobuf is a good one, so is tilelive and tippecanoe.

In my code, geometries are already cutted into slices. So, geobuf is the most suitable tool.

But, the pbf files which geobuf converted could not pass in pbf parser, however, result of tilelive and tippecanoe could.

Is this not a standard pbf file, or, my geojson in question?

Here is my parse code, it's from website. It parse other pbf files correctly.

var VectorTile = require("@mapbox/vector-tile").VectorTile;
var Protobuf = require("pbf");
var fs = require("fs");

var x = 213,
  y = 104,
  z = 8,
  dic = "./sh_cs.pbf";
var coorArray = [];
var pbf = fs.readFileSync(dic);
var tile = new VectorTile(new Protobuf(pbf));
for (var key in tile.layers) {
  var lyr = tile.layers[key];
  lyr.parsedFeatures = [];
  var features = lyr._features;
  for (var k = 0, len = features.length; k < len; k++) {
    var vtf = lyr.feature(k);
    var geojson = vtf.toGeoJSON(x, y, z);
    coorArray.push(geojson);
  }
}

Here is my geojson.

{
  "type": "FeatureCollection",
  "properties": {
    "zoom": 14,
    "x": 13722,
    "y": 6699,
    "compressed": false
  },
  "features": [
    {
      "type": "FeatureCollection",
      "features": [
        {
          "id": 144,
          "type": "Feature",
          "geometry": {
            "type": "Point",
            "coordinates": [
              121.51998,
              31.13455
            ]
          },
          "properties": {
            "name": "莲都超市"
          }
        },
        {
          "id": 145,
          "type": "Feature",
          "geometry": {
            "type": "Point",
            "coordinates": [
              121.51627,
              31.1302
            ]
          },
          "properties": {
            "name": "农良超市"
          }
        }
      ]
    }
  ]
}

Here is tippecanoe's pbf file, I converted it to geojson.

{
  "type": "FeatureCollection",
  "properties": {
    "zoom": 8,
    "x": 213,
    "y": 104,
    "compressed": false
  },
  "features": [
    {
      "type": "FeatureCollection",
      "properties": {
        "layer": "tertiaryway",
        "version": 2,
        "extent": 4096
      },
      "features": [
        {
          "type": "Feature",
          "id": 1,
          "properties": {
            "bridge": 0,
            "maxspeed": 0,
            "name": "金山路",
            "oneway": 0,
            "osm_id": "24435706",
            "tunnel": 0,
            "type": "tertiary"
          },
          "geometry": {
            "type": "LineString",
            "coordinates": [
              [
                120.556068,
                31.299088
              ],
              [
                120.544739,
                31.297621
              ]
            ]
          }
        }
      ]
    }
  ]
}
e-n-f commented 6 years ago

Vector tiles and Geobuf are both encoded as protocol buffers (pbf) but with different .proto schemas.

The new Protobuf(pbf); should be able to do the first-level parsing of Geobuf, but the new VectorTile(new Protobuf(pbf)); will not know what to do with it since it decodes the vector tile schema, not the Geobuf one.

wucangeo commented 6 years ago

Got it! I'm new to this, thank you for your patient explanation. I'm still looking for a nodejs package to convert geojson to Protobuf directly, one json to one pbf, could you give me a hint ? Thanks a lot !

e-n-f commented 6 years ago

I think you're looking for https://github.com/mapbox/vt-pbf

wucangeo commented 6 years ago

@ericfischer That is I'm looking for, thanks a loooot! I appreciate it !