|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var test = require('tap').test; |
| 4 | +var fs = require('fs'); |
| 5 | +var path = require('path'); |
| 6 | +var Protobuf = require('pbf'); |
| 7 | +var VectorTile = require('vector-tile').VectorTile; |
| 8 | +var classifyRings = require('../../../js/util/classify_rings'); |
| 9 | + |
| 10 | +// Load a fill feature from fixture tile. |
| 11 | +var vt = new VectorTile(new Protobuf(new Uint8Array(fs.readFileSync(path.join(__dirname, '/../../fixtures/mbsv5-6-18-23.vector.pbf'))))); |
| 12 | +var feature = vt.layers.water.feature(0); |
| 13 | + |
| 14 | +test('classifyRings', function(assert) { |
| 15 | + var geometry; |
| 16 | + var classified; |
| 17 | + |
| 18 | + geometry = [ |
| 19 | + [ |
| 20 | + {x:0, y:0}, |
| 21 | + {x:0, y:40}, |
| 22 | + {x:40, y:40}, |
| 23 | + {x:40, y:0}, |
| 24 | + {x:0, y:0} |
| 25 | + ] |
| 26 | + ]; |
| 27 | + classified = classifyRings(geometry); |
| 28 | + assert.equal(classified.length, 1, '1 polygon'); |
| 29 | + assert.equal(classified[0].length, 1, 'polygon 1 has 1 exterior'); |
| 30 | + |
| 31 | + geometry = [ |
| 32 | + [ |
| 33 | + {x:0, y:0}, |
| 34 | + {x:0, y:40}, |
| 35 | + {x:40, y:40}, |
| 36 | + {x:40, y:0}, |
| 37 | + {x:0, y:0} |
| 38 | + ], |
| 39 | + [ |
| 40 | + {x:60, y:0}, |
| 41 | + {x:60, y:40}, |
| 42 | + {x:100, y:40}, |
| 43 | + {x:100, y:0}, |
| 44 | + {x:60, y:0} |
| 45 | + ] |
| 46 | + ]; |
| 47 | + classified = classifyRings(geometry); |
| 48 | + assert.equal(classified.length, 2, '2 polygons'); |
| 49 | + assert.equal(classified[0].length, 1, 'polygon 1 has 1 exterior'); |
| 50 | + assert.equal(classified[1].length, 1, 'polygon 2 has 1 exterior'); |
| 51 | + |
| 52 | + geometry = [ |
| 53 | + [ |
| 54 | + {x:0, y:0}, |
| 55 | + {x:0, y:40}, |
| 56 | + {x:40, y:40}, |
| 57 | + {x:40, y:0}, |
| 58 | + {x:0, y:0} |
| 59 | + ], |
| 60 | + [ |
| 61 | + {x:10, y:10}, |
| 62 | + {x:20, y:10}, |
| 63 | + {x:20, y:20}, |
| 64 | + {x:10, y:10} |
| 65 | + ] |
| 66 | + ]; |
| 67 | + classified = classifyRings(geometry); |
| 68 | + assert.equal(classified.length, 1, '1 polygon'); |
| 69 | + assert.equal(classified[0].length, 2, 'polygon 1 has 1 exterior, 1 interior'); |
| 70 | + |
| 71 | + geometry = feature.loadGeometry(); |
| 72 | + classified = classifyRings(geometry); |
| 73 | + assert.equal(classified.length, 2, '2 polygons'); |
| 74 | + assert.equal(classified[0].length, 1, 'polygon 1 has 1 exterior'); |
| 75 | + assert.equal(classified[1].length, 10, 'polygon 2 has 1 exterior, 9 interior'); |
| 76 | + |
| 77 | + assert.end(); |
| 78 | +}); |
| 79 | + |
| 80 | +test('classifyRings + maxRings', function(t) { |
| 81 | + |
| 82 | + function createGeometry(options) { |
| 83 | + var geometry = [ |
| 84 | + // Outer ring, area = 3200 |
| 85 | + [ {x:0, y:0}, {x:0, y:40}, {x:40, y:40}, {x:40, y:0}, {x:0, y:0} ], |
| 86 | + // Inner ring, area = 100 |
| 87 | + [ {x:30, y:30}, {x:32, y:30}, {x:32, y:32}, {x:30, y:30} ], |
| 88 | + // Inner ring, area = 4 |
| 89 | + [ {x:10, y:10}, {x:20, y:10}, {x:20, y:20}, {x:10, y:10} ] |
| 90 | + ]; |
| 91 | + if (options && options.reverse) { |
| 92 | + geometry[0].reverse(); |
| 93 | + geometry[1].reverse(); |
| 94 | + geometry[2].reverse(); |
| 95 | + } |
| 96 | + return geometry; |
| 97 | + } |
| 98 | + |
| 99 | + |
| 100 | + t.test('maxRings=undefined', function(t) { |
| 101 | + var geometry = sortRings(classifyRings(createGeometry())); |
| 102 | + t.equal(geometry.length, 1); |
| 103 | + t.equal(geometry[0].length, 3); |
| 104 | + t.equal(geometry[0][0].area, 3200); |
| 105 | + t.equal(geometry[0][1].area, 100); |
| 106 | + t.equal(geometry[0][2].area, 4); |
| 107 | + t.end(); |
| 108 | + }); |
| 109 | + |
| 110 | + t.test('maxRings=2', function(t) { |
| 111 | + var geometry = sortRings(classifyRings(createGeometry(), 2)); |
| 112 | + t.equal(geometry.length, 1); |
| 113 | + t.equal(geometry[0].length, 2); |
| 114 | + t.equal(geometry[0][0].area, 3200); |
| 115 | + t.equal(geometry[0][1].area, 100); |
| 116 | + t.end(); |
| 117 | + }); |
| 118 | + |
| 119 | + t.test('maxRings=2, reversed geometry', function(t) { |
| 120 | + var geometry = sortRings(classifyRings(createGeometry({reverse: true}), 2)); |
| 121 | + t.equal(geometry.length, 1); |
| 122 | + t.equal(geometry[0].length, 2); |
| 123 | + t.equal(geometry[0][0].area, 3200); |
| 124 | + t.equal(geometry[0][1].area, 100); |
| 125 | + t.end(); |
| 126 | + }); |
| 127 | + |
| 128 | + t.test('maxRings=5, geometry from fixture', function(t) { |
| 129 | + var geometry = sortRings(classifyRings(feature.loadGeometry(), 5)); |
| 130 | + t.equal(geometry.length, 2); |
| 131 | + t.equal(geometry[0].length, 1); |
| 132 | + t.equal(geometry[1].length, 5); |
| 133 | + |
| 134 | + var areas = geometry[1].map(function(ring) { return ring.area; }); |
| 135 | + t.deepEqual(areas, [2763951, 21600, 8298, 4758, 3411]); |
| 136 | + t.end(); |
| 137 | + }); |
| 138 | + |
| 139 | + t.end(); |
| 140 | +}); |
| 141 | + |
| 142 | +function sortRings(geometry) { |
| 143 | + for (var i = 0; i < geometry.length; i++) { |
| 144 | + geometry[i] = geometry[i].sort(compareAreas); |
| 145 | + } |
| 146 | + return geometry; |
| 147 | +} |
| 148 | + |
| 149 | +function compareAreas(a, b) { |
| 150 | + return b.area - a.area; |
| 151 | +} |
0 commit comments