Cache GeoJSON transient

This commit is contained in:
John Firebaugh
2013-08-26 15:18:04 -07:00
parent 7215e91ff2
commit 0a23c395ed
2 changed files with 50 additions and 46 deletions
+20 -18
View File
@@ -134,24 +134,26 @@ _.extend(iD.Relation.prototype, {
},
asGeoJSON: function(resolver) {
if (this.isMultipolygon()) {
return {
type: 'Feature',
properties: this.tags,
geometry: {
type: 'MultiPolygon',
coordinates: this.multipolygon(resolver)
}
};
} else {
return {
type: 'FeatureCollection',
properties: this.tags,
features: this.members.map(function(member) {
return _.extend({role: member.role}, resolver.entity(member.id).asGeoJSON(resolver));
})
};
}
return resolver.transient(this, 'GeoJSON', function () {
if (this.isMultipolygon()) {
return {
type: 'Feature',
properties: this.tags,
geometry: {
type: 'MultiPolygon',
coordinates: this.multipolygon(resolver)
}
};
} else {
return {
type: 'FeatureCollection',
properties: this.tags,
features: this.members.map(function (member) {
return _.extend({role: member.role}, resolver.entity(member.id).asGeoJSON(resolver));
})
};
}
});
},
isMultipolygon: function() {
+30 -28
View File
@@ -137,39 +137,41 @@ _.extend(iD.Way.prototype, {
},
asGeoJSON: function(resolver, polygon) {
var nodes = resolver.childNodes(this);
return resolver.transient(this, 'GeoJSON', function() {
var nodes = resolver.childNodes(this);
if (this.isArea() && polygon && nodes.length >= 4) {
if (!this.isClosed()) {
nodes = nodes.concat([nodes[0]]);
}
var json = {
type: 'Feature',
properties: this.tags,
geometry: {
type: 'Polygon',
coordinates: [_.pluck(nodes, 'loc')]
if (this.isArea() && polygon && nodes.length >= 4) {
if (!this.isClosed()) {
nodes = nodes.concat([nodes[0]]);
}
};
// 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();
}
var json = {
type: 'Feature',
properties: this.tags,
geometry: {
type: 'Polygon',
coordinates: [_.pluck(nodes, 'loc')]
}
};
return json;
} else {
return {
type: 'Feature',
properties: this.tags,
geometry: {
type: 'LineString',
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();
}
};
}
return json;
} else {
return {
type: 'Feature',
properties: this.tags,
geometry: {
type: 'LineString',
coordinates: _.pluck(nodes, 'loc')
}
};
}
});
}
});