Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add back stripped tags like Geometry lengths, dimensions and precision that are used by Golang Geobuf Library. #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions bin/json2geobufb64
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env node

var encode = require('../encode'),
Pbf = require('pbf'),
fs = require('fs'),
concat = require('concat-stream');

var input = process.stdin.isTTY ? fs.createReadStream(process.argv[2]) : process.stdin;

input.pipe(concat(function(buf) {
var geojson = JSON.parse(buf.toString());

var pbf = encode(geojson, new Pbf());
var buffer = Buffer.allocUnsafe ? Buffer.from(pbf) : new Buffer(pbf);
var test = buffer.toString("base64");

process.stdout.write(test);
}));
26 changes: 12 additions & 14 deletions encode.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ function encode(obj, pbf) {
var precision = Math.ceil(Math.log(e) / Math.LN10);

for (var i = 0; i < keysArr.length; i++) pbf.writeStringField(1, keysArr[i]);
if (dim !== 2) pbf.writeVarintField(2, dim);
if (precision !== 6) pbf.writeVarintField(3, precision);
pbf.writeVarintField(2, dim);
pbf.writeVarintField(3, precision);

if (obj.type === 'FeatureCollection') pbf.writeMessage(4, writeFeatureCollection, obj);
else if (obj.type === 'Feature') pbf.writeMessage(5, writeFeature, obj);
Expand Down Expand Up @@ -172,12 +172,12 @@ function writeLine(line, pbf) {
function writeMultiLine(lines, pbf, closed) {
var len = lines.length,
i;
if (len !== 1) {
var lengths = [];
for (i = 0; i < len; i++) lengths.push(lines[i].length - (closed ? 1 : 0));
pbf.writePackedVarint(2, lengths);
// TODO faster with custom writeMessage?
var lengths = [];
for (i = 0; i < len; i++) {
lengths.push(lines[i].length - (closed ? 1 : 0));
}
pbf.writePackedVarint(2, lengths);

var coords = [];
for (i = 0; i < len; i++) populateLine(coords, lines[i], closed);
pbf.writePackedSVarint(3, coords);
Expand All @@ -186,14 +186,12 @@ function writeMultiLine(lines, pbf, closed) {
function writeMultiPolygon(polygons, pbf) {
var len = polygons.length,
i, j;
if (len !== 1 || polygons[0].length !== 1) {
var lengths = [len];
for (i = 0; i < len; i++) {
lengths.push(polygons[i].length);
for (j = 0; j < polygons[i].length; j++) lengths.push(polygons[i][j].length - 1);
}
pbf.writePackedVarint(2, lengths);
var lengths = [len];
for (i = 0; i < len; i++) {
lengths.push(polygons[i].length);
for (j = 0; j < polygons[i].length; j++) lengths.push(polygons[i][j].length - 1);
}
pbf.writePackedVarint(2, lengths);

var coords = [];
for (i = 0; i < len; i++) {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"bin": {
"geobuf2json": "bin/geobuf2json",
"json2geobuf": "bin/json2geobuf",
"shp2geobuf": "bin/shp2geobuf"
"shp2geobuf": "bin/shp2geobuf",
"json2geobufb64": "bin/json2geobufb64"
},
"scripts": {
"pretest": "eslint *.js test/*.js",
Expand Down