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

Return geometry positions in a 512x512 tile #2

Merged
merged 2 commits into from
Feb 11, 2020
Merged
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
68 changes: 63 additions & 5 deletions lib/vectortilefeature.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function readTag(pbf, feature) {

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

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

Expand Down Expand Up @@ -67,14 +67,14 @@ VectorTileFeature.prototype.loadGeometry = function() {
if (line) lines.push(line);
line = [];
}

line.push(new Point(x, y));
line.push(transformFunction(x, y));

} else if (cmd === 7) {

// Workaround for https://github.com/mapbox/mapnik-vector-tile/issues/90
if (line) {
line.push(line[0].clone()); // closePolygon
var firstLine = line[0].clone ? line[0].clone() : line[0].slice();
line.push(firstLine); // closePolygon
}

} else {
Expand Down Expand Up @@ -126,11 +126,16 @@ VectorTileFeature.prototype.bbox = function() {
return [x1, y1, x2, y2];
};


VectorTileFeature.prototype.toGeoJSON = function(x, y, z) {
var transformFunction = function (x, y) {
return new Point(x, y);
};

var size = this.extent * Math.pow(2, z),
x0 = this.extent * x,
y0 = this.extent * y,
coords = this.loadGeometry(),
coords = this.loadGeometry(transformFunction),
type = VectorTileFeature.types[this.type],
i, j;

Expand Down Expand Up @@ -192,6 +197,59 @@ VectorTileFeature.prototype.toGeoJSON = function(x, y, z) {
return result;
};

VectorTileFeature.prototype.toTileCoordsJSON = function() {
// Resize tiles to 512x512 bounding box
var tileSize = 512;
var tileExtent = this.extent;
var transformFunction = function (x, y) {
return [
(x/tileExtent) * tileSize,
(y/tileExtent) * tileSize
];
};

var type = VectorTileFeature.types[this.type];
var tileCoordsGeometry = this.loadGeometry(transformFunction);
var i;

switch (this.type) {
// Points
case 1:
jesusbotella marked this conversation as resolved.
Show resolved Hide resolved
var points = [];
for (i = 0; i < tileCoordsGeometry.length; i++) {
points[i] = tileCoordsGeometry[i][0];
}
tileCoordsGeometry = points;
break;

// Polygons
case 3:
tileCoordsGeometry = classifyRings(tileCoordsGeometry);
break;
}

if (tileCoordsGeometry.length === 1) {
tileCoordsGeometry = tileCoordsGeometry[0];
} else {
type = 'Multi' + type;
}

var jsonFeature = {
type: "Feature",
properties: this.properties,
geometry: {
type: type,
coordinates: tileCoordsGeometry
}
};

if ('id' in this) {
jesusbotella marked this conversation as resolved.
Show resolved Hide resolved
jsonFeature.id = this.id;
}

return jsonFeature;
};

// classifies an array of rings into polygons with outer rings and holes

function classifyRings(rings) {
Expand Down