Skip to content

Commit

Permalink
Normalize geometry structure; classify polygon rings
Browse files Browse the repository at this point in the history
  • Loading branch information
jfirebaugh committed Oct 9, 2015
1 parent 3426fc4 commit 83da8bc
Show file tree
Hide file tree
Showing 13 changed files with 246 additions and 35 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
## vector-tile-js changelog

### 2.0.0 (in progress)

- The structure of the return value of `loadGeometry()` was normalized. For point features, `loadGeometry()` now returns
an array of points. For polygon features, `loadGeometry()` now classifies rings and groups them into outer and inner
rings, using an extra level of array nesting. I.e., the return value for polygons is an array of arrays of arrays of
Points. The structure for line features (array of arrays of points) is unchanged.
- `toGeoJSON()` now classifies polygons into `Polygon` and `MultiPolygon` types based on ring structure.

### 1.1.3 (2015-06-15)

- Workaround for https://github.com/mapbox/mapnik-vector-tile/issues/90
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ An object that contains the data for a single feature.

#### Methods

- **loadGeometry()** — parses feature geometry and returns an array of
[Point](https://github.com/mapbox/point-geometry) arrays (with each point having `x` and `y` properties)
- **loadGeometry()** — returns feature geometry. Since the vector tile specification does not distinguish between Point and MultiPoint, LineString and MultiLineString, or Polygon and MultiPolygon, the result is always treated as a "Multi" variant:
- For point features, the return value is an array of [`Point`](https://github.com/mapbox/point-geometry)s.
- For line features, it is an array of arrays of `Point`s.
- For polygon features, it is an array of arrays of arrays of `Point`s.
- **bbox()** — calculates and returns the bounding box of the feature in the form `[x1, y1, x2, y2]`
- **toGeoJSON(x, y, z)** — returns a GeoJSON representation of the feature. (`x`, `y`, and `z` refer to the containing tile's index.)
131 changes: 129 additions & 2 deletions fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var mapnik = require('mapnik');
var path = require('path');
var fs = require('fs');

mapnik.register_datasource(path.join(mapnik.settings.paths.input_plugins,'geojson.input'));
mapnik.register_datasource(path.join(mapnik.settings.paths.input_plugins, 'geojson.input'));

var fixtures = {
"zero-point": {
Expand Down Expand Up @@ -31,6 +31,19 @@ var fixtures = {
}
]
},
"zero-polygon": {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "MultiPolygon",
"coordinates": []
},
"properties": {}
}
]
},
"singleton-multi-point": {
"type": "FeatureCollection",
"features": [
Expand All @@ -57,6 +70,19 @@ var fixtures = {
}
]
},
"singleton-multi-polygon": {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "MultiPolygon",
"coordinates": [[[[0, 0], [1, 0], [1, 1], [0, 0]]]]
},
"properties": {}
}
]
},
"multi-point": {
"type": "FeatureCollection",
"features": [
Expand All @@ -82,11 +108,112 @@ var fixtures = {
"properties": {}
}
]
},
"multi-polygon": {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "MultiPolygon",
"coordinates": [[[[0, 0], [1, 0], [1, 1], [0, 0]]], [[[0, 0], [-1, 0], [-1, -1], [0, 0]]]]
},
"properties": {}
}
]
},
"polygon-with-inner": {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [[[-2, 2], [2, 2], [2, -2], [-2, -2], [-2, 2]], [[-1, 1], [1, 1], [1, -1], [-1, -1], [-1, 1]]]
},
"properties": {}
}
]
},
"multipolygon": {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "MultiPolygon",
"coordinates": [
[
[
[
-65.91796875,
-23.40276490540795
],
[
-65.91796875,
8.407168163601076
],
[
-35.859375,
8.407168163601076
],
[
-35.859375,
-23.40276490540795
],
[
-65.91796875,
-23.40276490540795
]
]
],
[
[
[
-9.84375,
12.897489183755892
],
[
-9.84375,
37.16031654673677
],
[
18.28125,
37.16031654673677
],
[
18.28125,
12.897489183755892
],
[
-9.84375,
12.897489183755892
]
]
]
]
}
}
]
},
"stacked-multipolygon": {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "MultiPolygon",
"coordinates": [[[[-2, 2], [2, 2], [2, -2], [-2, -2], [-2, 2]]], [[[-1, 1], [1, 1], [1, -1], [-1, -1], [-1, 1]]]]
},
"properties": {}
}
]
}
}

for (var fixture in fixtures) {
var vtile = new mapnik.VectorTile(0,0,0);
var vtile = new mapnik.VectorTile(0, 0, 0);
vtile.addGeoJSON(JSON.stringify(fixtures[fixture]), "geojson");
fs.writeFileSync('./test/fixtures/' + fixture + '.pbf', vtile.getData());
}
108 changes: 87 additions & 21 deletions lib/vectortilefeature.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ VectorTileFeature.prototype.loadGeometry = function() {
length = 0,
x = 0,
y = 0,
lines = [],
line;
line = [],
lines = [line];

while (pbf.pos < end) {
if (!length) {
Expand All @@ -59,21 +59,23 @@ VectorTileFeature.prototype.loadGeometry = function() {

length--;

if (cmd === 1 || cmd === 2) {
if (cmd === 2 || (cmd === 1 && this.type === 1)) { // MLLLC
x += pbf.readSVarint();
y += pbf.readSVarint();
line.push(new Point(x, y));

if (cmd === 1) { // moveTo
if (line) lines.push(line);
} else if (cmd === 1) {
x += pbf.readSVarint();
y += pbf.readSVarint();
if (line.length) {
line = [];
lines.push(line);
}

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

} else if (cmd === 7) {

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

Expand All @@ -82,9 +84,19 @@ VectorTileFeature.prototype.loadGeometry = function() {
}
}

if (line) lines.push(line);

return lines;
switch (this.type) {
case 1:
return line;
break;
case 2:
return lines;
break;
case 3:
return classifyRings(lines);
break;
default:
throw new Error('Unknown vector tile feature type: ' + this.type);
}
};

VectorTileFeature.prototype.bbox = function() {
Expand Down Expand Up @@ -133,8 +145,7 @@ VectorTileFeature.prototype.toGeoJSON = function(x, y, z) {
coords = this.loadGeometry(),
type = VectorTileFeature.types[this.type];

for (var i = 0; i < coords.length; i++) {
var line = coords[i];
function project(line) {
for (var j = 0; j < line.length; j++) {
var p = line[j], y2 = 180 - (p.y + y0) * 360 / size;
line[j] = [
Expand All @@ -144,15 +155,30 @@ VectorTileFeature.prototype.toGeoJSON = function(x, y, z) {
}
}

if (type === 'Point' && coords.length === 1) {
coords = coords[0][0];
} else if (type === 'Point') {
coords = coords[0];
type = 'MultiPoint';
} else if (type === 'LineString' && coords.length === 1) {
switch (this.type) {
case 1:
project(coords);
break;

case 2:
for (var i = 0; i < coords.length; i++) {
project(coords[i]);
}
break;

case 3:
for (var i = 0; i < coords.length; i++) {
for (var j = 0; j < coords[i].length; j++) {
project(coords[i][j]);
}
}
break;
}

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

return {
Expand All @@ -164,3 +190,43 @@ VectorTileFeature.prototype.toGeoJSON = function(x, y, z) {
properties: this.properties
};
};

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

function classifyRings(rings) {
var len = rings.length;

if (len <= 1) return [rings];

var polygons = [],
polygon,
ccw;

for (var i = 0; i < len; i++) {
var area = signedArea(rings[i]);
if (area === 0) continue;

if (!ccw) ccw = area < 0;

if (ccw === area < 0) {
if (polygon) polygons.push(polygon);
polygon = [rings[i]];

} else {
polygon.push(rings[i]);
}
}
if (polygon) polygons.push(polygon);

return polygons;
}

function signedArea(ring) {
var sum = 0;
for (var i = 0, len = ring.length, j = len - 1, p1, p2; i < len; j = i++) {
p1 = ring[i];
p2 = ring[j];
sum += (p2.x - p1.x) * (p1.y + p2.y);
}
return sum;
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"benchmark": "^1.0.0",
"coveralls": "~2.11.2",
"istanbul": "~0.3.6",
"mapnik": "^3.1.6",
"mapnik": "^3.4.8",
"jshint": "^2.6.3",
"pbf": "^1.3.2",
"tape": "~3.5.0"
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/multi-point.pbf
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@

geojson" � � .-(� x

geojson"� �.-(� x
Expand Down
Binary file added test/fixtures/multi-polygon.pbf
Binary file not shown.
Binary file added test/fixtures/multipolygon.pbf
Binary file not shown.
Binary file added test/fixtures/polygon-with-inner.pbf
Binary file not shown.
Binary file added test/fixtures/singleton-multi-polygon.pbf
Binary file not shown.
Binary file added test/fixtures/stacked-multipolygon.pbf
Binary file not shown.
Empty file added test/fixtures/zero-polygon.pbf
Empty file.
Loading

0 comments on commit 83da8bc

Please sign in to comment.