Return geometries rather than features in asGeoJSON

No code needs the tags, and it eliminates one level
of function calls in the d3 stream pipeline.
This commit is contained in:
John Firebaugh
2013-10-16 09:40:01 -04:00
parent 1374b5bc5d
commit 26e38d7f8f
6 changed files with 23 additions and 47 deletions
+2 -6
View File
@@ -55,12 +55,8 @@ _.extend(iD.Node.prototype, {
asGeoJSON: function() {
return {
type: 'Feature',
properties: this.tags,
geometry: {
type: 'Point',
coordinates: this.loc
}
type: 'Point',
coordinates: this.loc
};
}
});
+2 -6
View File
@@ -141,12 +141,8 @@ _.extend(iD.Relation.prototype, {
return resolver.transient(this, 'GeoJSON', function () {
if (this.isMultipolygon()) {
return {
type: 'Feature',
properties: this.tags,
geometry: {
type: 'MultiPolygon',
coordinates: this.multipolygon(resolver)
}
type: 'MultiPolygon',
coordinates: this.multipolygon(resolver)
};
} else {
return {
+5 -13
View File
@@ -151,29 +151,21 @@ _.extend(iD.Way.prototype, {
}
var json = {
type: 'Feature',
properties: this.tags,
geometry: {
type: 'Polygon',
coordinates: [_.pluck(nodes, 'loc')]
}
type: 'Polygon',
coordinates: [_.pluck(nodes, 'loc')]
};
// Heuristic for detecting counterclockwise winding order. Assumes
// that OpenStreetMap polygons are not hemisphere-spanning.
if (d3.geo.area(json) > 2 * Math.PI) {
json.geometry.coordinates[0] = json.geometry.coordinates[0].reverse();
json.coordinates[0] = json.coordinates[0].reverse();
}
return json;
} else {
return {
type: 'Feature',
properties: this.tags,
geometry: {
type: 'LineString',
coordinates: _.pluck(nodes, 'loc')
}
type: 'LineString',
coordinates: _.pluck(nodes, 'loc')
};
}
});
+3 -5
View File
@@ -78,14 +78,12 @@ describe('iD.Node', function () {
});
describe("#asGeoJSON", function () {
it("converts to a GeoJSON Point features", function () {
it("converts to a GeoJSON Point geometry", function () {
var node = iD.Node({tags: {amenity: 'cafe'}, loc: [1, 2]}),
json = node.asGeoJSON();
expect(json.type).to.equal('Feature');
expect(json.properties).to.eql({amenity: 'cafe'});
expect(json.geometry.type).to.equal('Point');
expect(json.geometry.coordinates).to.eql([1, 2]);
expect(json.type).to.equal('Point');
expect(json.coordinates).to.eql([1, 2]);
});
});
});
+4 -6
View File
@@ -204,7 +204,7 @@ describe('iD.Relation', function () {
});
describe("#asGeoJSON", function (){
it('converts a multipolygon to a GeoJSON MultiPolygon feature', function() {
it('converts a multipolygon to a GeoJSON MultiPolygon geometry', function() {
var a = iD.Node({loc: [1, 1]}),
b = iD.Node({loc: [3, 3]}),
c = iD.Node({loc: [2, 2]}),
@@ -213,10 +213,8 @@ describe('iD.Relation', function () {
g = iD.Graph([a, b, c, w, r]),
json = r.asGeoJSON(g);
expect(json.type).to.equal('Feature');
expect(json.properties).to.eql({type: 'multipolygon'});
expect(json.geometry.type).to.equal('MultiPolygon');
expect(json.geometry.coordinates).to.eql([[[a.loc, b.loc, c.loc, a.loc]]]);
expect(json.type).to.equal('MultiPolygon');
expect(json.coordinates).to.eql([[[a.loc, b.loc, c.loc, a.loc]]]);
});
it('forces clockwise winding order for outer multipolygon ways', function() {
@@ -228,7 +226,7 @@ describe('iD.Relation', function () {
g = iD.Graph([a, b, c, w, r]),
json = r.asGeoJSON(g);
expect(json.geometry.coordinates[0][0]).to.eql([a.loc, b.loc, c.loc, a.loc]);
expect(json.coordinates[0][0]).to.eql([a.loc, b.loc, c.loc, a.loc]);
});
it('forces counterclockwise winding order for inner multipolygon ways', function() {
+7 -11
View File
@@ -282,20 +282,18 @@ describe('iD.Way', function() {
});
describe("#asGeoJSON", function () {
it("converts a line to a GeoJSON LineString feature", function () {
it("converts a line to a GeoJSON LineString geometry", function () {
var a = iD.Node({loc: [1, 2]}),
b = iD.Node({loc: [3, 4]}),
w = iD.Way({tags: {highway: 'residential'}, nodes: [a.id, b.id]}),
graph = iD.Graph([a, b, w]),
json = w.asGeoJSON(graph);
expect(json.type).to.equal('Feature');
expect(json.properties).to.eql({highway: 'residential'});
expect(json.geometry.type).to.equal('LineString');
expect(json.geometry.coordinates).to.eql([[1, 2], [3, 4]]);
expect(json.type).to.equal('LineString');
expect(json.coordinates).to.eql([[1, 2], [3, 4]]);
});
it("converts an area to a GeoJSON Polygon feature", function () {
it("converts an area to a GeoJSON Polygon geometry", function () {
var a = iD.Node({loc: [1, 2]}),
b = iD.Node({loc: [5, 6]}),
c = iD.Node({loc: [3, 4]}),
@@ -303,10 +301,8 @@ describe('iD.Way', function() {
graph = iD.Graph([a, b, c, w]),
json = w.asGeoJSON(graph, true);
expect(json.type).to.equal('Feature');
expect(json.properties).to.eql({area: 'yes'});
expect(json.geometry.type).to.equal('Polygon');
expect(json.geometry.coordinates).to.eql([[a.loc, b.loc, c.loc, a.loc]]);
expect(json.type).to.equal('Polygon');
expect(json.coordinates).to.eql([[a.loc, b.loc, c.loc, a.loc]]);
});
it("forces clockwise polygon winding order", function () {
@@ -317,7 +313,7 @@ describe('iD.Way', function() {
graph = iD.Graph([a, b, c, w]),
json = w.asGeoJSON(graph, true);
expect(json.geometry.coordinates).to.eql([[a.loc, b.loc, c.loc, a.loc]]);
expect(json.coordinates).to.eql([[a.loc, b.loc, c.loc, a.loc]]);
});
});
});