Implement read transforms for ol.format.TopoJSON

This commit is contained in:
tsauerwein
2014-08-08 11:43:53 +02:00
parent 3090a0af37
commit e446738491
2 changed files with 49 additions and 7 deletions

View File

@@ -3,6 +3,7 @@ goog.provide('ol.format.TopoJSON');
goog.require('goog.asserts');
goog.require('goog.object');
goog.require('ol.Feature');
goog.require('ol.format.Feature');
goog.require('ol.format.JSONFeature');
goog.require('ol.geom.LineString');
goog.require('ol.geom.MultiLineString');
@@ -218,17 +219,18 @@ ol.format.TopoJSON.prototype.getExtensions = function() {
* @param {Array.<Array.<ol.Coordinate>>} arcs Array of arcs.
* @param {Array.<number>} scale Scale for each dimension.
* @param {Array.<number>} translate Translation for each dimension.
* @param {olx.format.ReadOptions=} opt_options Read options.
* @return {Array.<ol.Feature>} Array of features.
* @private
*/
ol.format.TopoJSON.readFeaturesFromGeometryCollection_ = function(
collection, arcs, scale, translate) {
collection, arcs, scale, translate, opt_options) {
var geometries = collection.geometries;
var features = [];
var i, ii;
for (i = 0, ii = geometries.length; i < ii; ++i) {
features[i] = ol.format.TopoJSON.readFeatureFromGeometry_(
geometries[i], arcs, scale, translate);
geometries[i], arcs, scale, translate, opt_options);
}
return features;
};
@@ -241,11 +243,12 @@ ol.format.TopoJSON.readFeaturesFromGeometryCollection_ = function(
* @param {Array.<Array.<ol.Coordinate>>} arcs Array of arcs.
* @param {Array.<number>} scale Scale for each dimension.
* @param {Array.<number>} translate Translation for each dimension.
* @param {olx.format.ReadOptions=} opt_options Read options.
* @return {ol.Feature} Feature.
* @private
*/
ol.format.TopoJSON.readFeatureFromGeometry_ = function(object, arcs,
scale, translate) {
scale, translate, opt_options) {
var geometry;
var type = object.type;
var geometryReader = ol.format.TopoJSON.GEOMETRY_READERS_[type];
@@ -256,7 +259,8 @@ ol.format.TopoJSON.readFeatureFromGeometry_ = function(object, arcs,
geometry = geometryReader(object, arcs);
}
var feature = new ol.Feature();
feature.setGeometry(geometry);
feature.setGeometry(ol.format.Feature.transformWithOptions(
geometry, false, opt_options));
if (goog.isDef(object.id)) {
feature.setId(object.id);
}
@@ -281,7 +285,8 @@ ol.format.TopoJSON.prototype.readFeatures;
/**
* @inheritDoc
*/
ol.format.TopoJSON.prototype.readFeaturesFromObject = function(object) {
ol.format.TopoJSON.prototype.readFeaturesFromObject = function(
object, opt_options) {
if (object.type == 'Topology') {
var topoJSONTopology = /** @type {TopoJSONTopology} */ (object);
var transform, scale = null, translate = null;
@@ -306,12 +311,12 @@ ol.format.TopoJSON.prototype.readFeaturesFromObject = function(object) {
(topoJSONFeatures[i]);
features.push.apply(features,
ol.format.TopoJSON.readFeaturesFromGeometryCollection_(
feature, arcs, scale, translate));
feature, arcs, scale, translate, opt_options));
} else {
feature = /** @type {TopoJSONGeometry} */
(topoJSONFeatures[i]);
features.push(ol.format.TopoJSON.readFeatureFromGeometry_(
feature, arcs, scale, translate));
feature, arcs, scale, translate, opt_options));
}
}
return features;

View File

@@ -1,5 +1,7 @@
goog.provide('ol.test.format.TopoJSON');
goog.require('ol.proj');
var aruba = {
type: 'Topology',
transform: {
@@ -89,6 +91,41 @@ describe('ol.format.TopoJSON', function() {
});
});
it('parses simple.json and transforms', function(done) {
afterLoadText('spec/ol/format/topojson/simple.json', function(text) {
var features = format.readFeatures(text, {
featureProjection: 'EPSG:3857'
});
expect(features.length).to.be(3);
var point = features[0].getGeometry();
expect(point.getType()).to.be('Point');
expect(features[0].getGeometry().getCoordinates()).to.eql(
ol.proj.transform([102.0, 0.5], 'EPSG:4326', 'EPSG:3857'));
var line = features[1].getGeometry();
expect(line.getType()).to.be('LineString');
expect(line.getCoordinates()).to.eql([
ol.proj.transform([102.0, 0.0], 'EPSG:4326', 'EPSG:3857'),
ol.proj.transform([103.0, 1.0], 'EPSG:4326', 'EPSG:3857'),
ol.proj.transform([104.0, 0.0], 'EPSG:4326', 'EPSG:3857'),
ol.proj.transform([105.0, 1.0], 'EPSG:4326', 'EPSG:3857')
]);
var polygon = features[2].getGeometry();
expect(polygon.getType()).to.be('Polygon');
expect(polygon.getCoordinates()).to.eql([[
ol.proj.transform([100.0, 0.0], 'EPSG:4326', 'EPSG:3857'),
ol.proj.transform([100.0, 1.0], 'EPSG:4326', 'EPSG:3857'),
ol.proj.transform([101.0, 1.0], 'EPSG:4326', 'EPSG:3857'),
ol.proj.transform([101.0, 0.0], 'EPSG:4326', 'EPSG:3857'),
ol.proj.transform([100.0, 0.0], 'EPSG:4326', 'EPSG:3857')
]]);
done();
});
});
it('parses world-110m.json', function(done) {
afterLoadText('spec/ol/format/topojson/world-110m.json', function(text) {