Skip to content

Commit

Permalink
Replace loadGeometry() with a getter property
Browse files Browse the repository at this point in the history
  • Loading branch information
jfirebaugh committed Oct 9, 2015
1 parent c39d03f commit 19240fd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
11 changes: 9 additions & 2 deletions lib/vectortilefeature.js
Expand Up @@ -38,7 +38,14 @@ function readTag(pbf, feature) {

VectorTileFeature.types = ['Unknown', 'Point', 'LineString', 'Polygon'];

VectorTileFeature.prototype.loadGeometry = function() {
Object.defineProperty(VectorTileFeature.prototype, 'geometry', {
enumerable: true,
get: function() {
return this._loadGeometry();
}
});

VectorTileFeature.prototype._loadGeometry = function() {
var pbf = this._pbf;
pbf.pos = this._geometry;

Expand Down Expand Up @@ -139,7 +146,7 @@ VectorTileFeature.prototype.toGeoJSON = function(x, y, z) {
var size = this.extent * Math.pow(2, z),
x0 = this.extent * x,
y0 = this.extent * y,
coords = this.loadGeometry(),
coords = this.geometry,
type = VectorTileFeature.types[this.type],
i, j;

Expand Down
8 changes: 4 additions & 4 deletions test/parse.test.js
Expand Up @@ -36,17 +36,17 @@ test('parsing vector tiles', function(t) {
t.equal(park.properties.type, 'Park');

// Check point geometry
t.deepEqual(park.loadGeometry(), [ { x: 3898, y: 1731 } ]);
t.deepEqual(park.geometry, [ { x: 3898, y: 1731 } ]);

// Check line geometry
t.deepEqual(tile.layers.road.feature(656).loadGeometry(), [ [ { x: 1988, y: 306 }, { x: 1808, y: 321 }, { x: 1506, y: 347 } ] ]);
t.deepEqual(tile.layers.road.feature(656).geometry, [ [ { x: 1988, y: 306 }, { x: 1808, y: 321 }, { x: 1506, y: 347 } ] ]);
t.end();
});

t.test('changing first point of a polygon should not change last point', function(t) {
var tile = new VectorTile(new Protobuf(data));

var building = tile.layers.building.feature(0).loadGeometry();
var building = tile.layers.building.feature(0).geometry;
t.deepEqual(building, [ [ [ { x: 2039, y: -32 }, { x: 2035, y: -31 }, { x: 2032, y: -31 }, { x: 2032, y: -32 }, { x: 2039, y: -32 } ] ] ]);
building[0][0][0].x = 1;
building[0][0][0].y = 2;
Expand Down Expand Up @@ -159,6 +159,6 @@ test('https://github.com/mapbox/vector-tile-js/issues/15', function(t) {
test('https://github.com/mapbox/mapbox-gl-js/issues/1019', function(t) {
var data = fs.readFileSync(__dirname + '/fixtures/12-1143-1497.vector.pbf');
var tile = new VectorTile(new Protobuf(data));
t.ok(tile.layers["water"].feature(1).loadGeometry());
t.ok(tile.layers["water"].feature(1).geometry);
t.end();
});

0 comments on commit 19240fd

Please sign in to comment.