-
-
Save nextstopsun/f7b0e6c1a15c5a36c38d548ebbc1848f to your computer and use it in GitHub Desktop.
PostGIS ST_AsMVT JSONB properties encoding bug
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require("fs"); | |
const geobuf = require("geobuf"); | |
const Protobuf = require("pbf"); | |
const pgp = require("pg-promise")(); | |
const vt = require("vector-tile"); | |
const db = pgp(process.env.DATABASE_URL); | |
const tile_with_properties_as_jsonb = `WITH data (id, num1, num2, geom) AS ( | |
VALUES | |
(1, 1.1, 0.1, ST_GeomFromEWKT('SRID=3857;POINT(1 1)')), | |
(2, 2.2, 0.2, ST_GeomFromEWKT('SRID=3857;POINT(2 2)')), | |
(3, 3.3, 0.3, ST_GeomFromEWKT('SRID=3857;POINT(3 3)')) | |
) | |
SELECT | |
ST_AsMVT(tile, 'data', 4096, 'geom') FROM ( | |
SELECT | |
ST_AsMVTGeom( | |
geom, | |
ST_GeomFromEWKT('SRID=3857;POLYGON((-20037508.34 20037508.34,-20037508.34 -20037508.34,20037508.34 -20037508.34,20037508.34 20037508.34,-20037508.34 20037508.34))'), | |
4096, | |
0, | |
true | |
) as geom, | |
jsonb_build_object( | |
'id', id, | |
'num1', num1, | |
'num2', num2 | |
) as properties | |
FROM data | |
) as tile;`; | |
const tile_with_properties_as_json = `WITH data (id, num1, num2, geom) AS (VALUES | |
(1, 1.1, 0.1, ST_GeomFromEWKT('SRID=3857;POINT(1 1)')), | |
(2, 2.2, 0.2, ST_GeomFromEWKT('SRID=3857;POINT(2 2)')), | |
(3, 3.3, 0.3, ST_GeomFromEWKT('SRID=3857;POINT(3 3)')) | |
) | |
SELECT | |
ST_AsMVT(tile, 'data', 4096, 'geom') FROM ( | |
SELECT | |
ST_AsMVTGeom( | |
geom, | |
ST_GeomFromEWKT('SRID=3857;POLYGON((-20037508.34 20037508.34,-20037508.34 -20037508.34,20037508.34 -20037508.34,20037508.34 20037508.34,-20037508.34 20037508.34))'), | |
4096, | |
0, | |
true | |
) as geom, | |
json_build_object( | |
'id', id, | |
'num1', num1, | |
'num2', num2 | |
) as properties | |
FROM data | |
) as tile;`; | |
const tile_with_plain_properties = `WITH data (id, num1, num2, geom) AS ( | |
VALUES | |
(1, 1.1, 0.1, ST_GeomFromEWKT('SRID=3857;POINT(1 1)')), | |
(2, 2.2, 0.2, ST_GeomFromEWKT('SRID=3857;POINT(2 2)')), | |
(3, 3.3, 0.3, ST_GeomFromEWKT('SRID=3857;POINT(3 3)')) | |
) | |
SELECT | |
ST_AsMVT(tile, 'data', 4096, 'geom') FROM ( | |
SELECT | |
ST_AsMVTGeom( | |
geom, | |
ST_GeomFromEWKT('SRID=3857;POLYGON((-20037508.34 20037508.34,-20037508.34 -20037508.34,20037508.34 -20037508.34,20037508.34 20037508.34,-20037508.34 20037508.34))'), | |
4096, | |
0, | |
true | |
) as geom, | |
id, | |
num1, | |
num2 | |
FROM data | |
) as tile;`; | |
const tile2geojson = buffer => { | |
const tile = new vt.VectorTile(new Protobuf(buffer)); | |
const layers = Object.keys(tile.layers); | |
var collection = { type: "FeatureCollection", features: [] }; | |
layers.forEach(function(layerID) { | |
var layer = tile.layers[layerID]; | |
if (layer) { | |
for (var i = 0; i < layer.length; i++) { | |
var feature = layer.feature(i).toGeoJSON(0, 0, 0); | |
if (layers.length > 1) feature.properties.vt_layer = layerID; | |
collection.features.push(feature); | |
} | |
} | |
}); | |
return collection; | |
}; | |
Promise.all([ | |
db | |
.one(tile_with_properties_as_json) | |
.then(results => results.st_asmvt) | |
.then(tile2geojson), | |
db | |
.one(tile_with_properties_as_jsonb) | |
.then(results => results.st_asmvt) | |
.then(tile2geojson), | |
db | |
.one(tile_with_plain_properties) | |
.then(results => results.st_asmvt) | |
.then(tile2geojson) | |
]) | |
.then(results => { | |
const [json, jsonb, plain] = results; | |
fs.writeFileSync("tile_json.geojson", JSON.stringify(json, null, 2)); | |
fs.writeFileSync("tile_jsonb.geojson", JSON.stringify(jsonb, null, 2)); | |
fs.writeFileSync("tile_plain.geojson", JSON.stringify(plain, null, 2)); | |
}) | |
.then(() => db.$pool.end()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
WITH data (id, num1, num2, geom) AS ( | |
VALUES | |
(1, 1.1, 0.1, ST_GeomFromEWKT('SRID=3857;POINT(1 1)')), | |
(2, 2.2, 0.2, ST_GeomFromEWKT('SRID=3857;POINT(2 2)')), | |
(3, 3.3, 0.3, ST_GeomFromEWKT('SRID=3857;POINT(3 3)')) | |
) | |
SELECT | |
ST_AsMVT(tile, 'data', 4096, 'geom') FROM ( | |
SELECT | |
ST_AsMVTGeom( | |
geom, | |
ST_GeomFromEWKT('SRID=3857;POLYGON((-20037508.34 20037508.34,-20037508.34 -20037508.34,20037508.34 -20037508.34,20037508.34 20037508.34,-20037508.34 20037508.34))'), | |
4096, | |
0, | |
true | |
) as geom, | |
json_build_object( | |
'id', id, | |
'num1', num1, | |
'num2', num2 | |
) as properties | |
FROM data | |
) as tile; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
WITH data (id, num1, num2, geom) AS ( | |
VALUES | |
(1, 1.1, 0.1, ST_GeomFromEWKT('SRID=3857;POINT(1 1)')), | |
(2, 2.2, 0.2, ST_GeomFromEWKT('SRID=3857;POINT(2 2)')), | |
(3, 3.3, 0.3, ST_GeomFromEWKT('SRID=3857;POINT(3 3)')) | |
) | |
SELECT | |
ST_AsMVT(tile, 'data', 4096, 'geom') FROM ( | |
SELECT | |
ST_AsMVTGeom( | |
geom, | |
ST_GeomFromEWKT('SRID=3857;POLYGON((-20037508.34 20037508.34,-20037508.34 -20037508.34,20037508.34 -20037508.34,20037508.34 20037508.34,-20037508.34 20037508.34))'), | |
4096, | |
0, | |
true | |
) as geom, | |
jsonb_build_object( | |
'id', id, | |
'num1', num1, | |
'num2', num2 | |
) as properties | |
FROM data | |
) as tile; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
WITH data (id, num1, num2, geom) AS ( | |
VALUES | |
(1, 1.1, 0.1, ST_GeomFromEWKT('SRID=3857;POINT(1 1)')), | |
(2, 2.2, 0.2, ST_GeomFromEWKT('SRID=3857;POINT(2 2)')), | |
(3, 3.3, 0.3, ST_GeomFromEWKT('SRID=3857;POINT(3 3)')) | |
) | |
SELECT | |
ST_AsMVT(tile, 'data', 4096, 'geom') FROM ( | |
SELECT | |
ST_AsMVTGeom( | |
geom, | |
ST_GeomFromEWKT('SRID=3857;POLYGON((-20037508.34 20037508.34,-20037508.34 -20037508.34,20037508.34 -20037508.34,20037508.34 20037508.34,-20037508.34 20037508.34))'), | |
4096, | |
0, | |
true | |
) as geom, | |
id, | |
num1, | |
num2 | |
FROM data | |
) as tile; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment