Skip to content

Commit

Permalink
Encode GeoJSON Multipoint correctly (#20)
Browse files Browse the repository at this point in the history
Refs #17 

* add .gitignore

* options for 'fromGeojsonVt()'

* configure eslint

* support MultiPoint

* add test for GeoJSON MultiPoint

* move some changes to other pr
  • Loading branch information
wsw0108 authored and anandthakker committed Sep 6, 2017
1 parent 9a96144 commit 21f0c8b
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 4 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
vector-tile-pb.js
4 changes: 4 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"root": true,
"extends": "standard"
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
12 changes: 8 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function prepareLayer (layer) {

for (var i = 0; i < layer.length; i++) {
var feature = layer.feature(i)
feature.geometry = encodeGeometry(feature.loadGeometry())
feature.geometry = encodeGeometry(feature.loadGeometry(), feature.type)

var tags = []
for (var key in feature.properties) {
Expand Down Expand Up @@ -101,16 +101,20 @@ function zigzag (num) {
* @param {Array} Rings, each being an array of [x, y] tile-space coordinates
* @return {Array} encoded geometry
*/
function encodeGeometry (geometry) {
function encodeGeometry (geometry, type) {
var encoded = []
var x = 0
var y = 0
var rings = geometry.length
for (var r = 0; r < rings; r++) {
var ring = geometry[r]
encoded.push(command(1, 1)) // moveto
var count = 1
if (type === 1) {
count = ring.length
}
encoded.push(command(1, count)) // moveto
for (var i = 0; i < ring.length; i++) {
if (i === 1) {
if (i === 1 && type !== 1) {
encoded.push(command(2, ring.length - 1)) // lineto
}
var dx = ring[i].x - x
Expand Down
24 changes: 24 additions & 0 deletions test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,30 @@ test('geojson-vt tiles', function (t) {
})
})

test('geojson-vt multipoint', function (t) {
var orig = JSON.parse(fs.readFileSync(__dirname + '/fixtures/multi-points.json'))
var tileindex = geojsonVt(orig)
var tile = tileindex.getTile(1, 0, 0)

var buff = serialize.fromGeojsonVt({
'multipoint': tile
})

var tile3 = new VectorTile(new Pbf(buff))
var layer = tile3.layers['multipoint']
var features = []
for (var i = 0; i < layer.length; i++) {
var feat = layer.feature(i).toGeoJSON(0, 0, 1)
features.push(feat)
}

t.plan(orig.features.length)
orig.features.forEach(function (expected) {
var actual = features.shift()
t.ok(eq.compare(actual, expected))
})
})

test('vector-tile-js tiles', function (t) {
var data = fs.readFileSync(__dirname + '/fixtures/rectangle-1.0.0.pbf')
var tile = new VectorTile(new Pbf(data))
Expand Down
17 changes: 17 additions & 0 deletions test/fixtures/multi-points.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"id": 1,
"properties": {},
"geometry": {
"type": "MultiPoint",
"coordinates": [
[ -74.6685791015625, 48.4838455701099 ],
[ -75.12451171875, 47.669086647137576 ]
]
}
}
]
}

0 comments on commit 21f0c8b

Please sign in to comment.