Merge pull request #1496 from tonio/vector-api-topojson

[vector-api] Add TopoJSON format
This commit is contained in:
Antoine Abt
2014-01-16 05:55:40 -08:00
15 changed files with 497 additions and 59 deletions

View File

@@ -3,9 +3,8 @@ goog.require('ol.RendererHint');
goog.require('ol.View2D');
goog.require('ol.layer.Tile');
goog.require('ol.layer.Vector');
goog.require('ol.parser.TopoJSON');
goog.require('ol.source.TileJSON');
goog.require('ol.source.Vector');
goog.require('ol.source.TopoJSON');
goog.require('ol.style.Fill');
goog.require('ol.style.Stroke');
goog.require('ol.style.Style');
@@ -17,24 +16,24 @@ var raster = new ol.layer.Tile({
})
});
var vector = new ol.layer.Vector({
source: new ol.source.Vector({
url: 'data/topojson/world-110m.json',
parser: new ol.parser.TopoJSON()
source: new ol.source.TopoJSON({
url: 'data/topojson/world-110m.json'
}),
style: new ol.style.Style({
symbolizers: [
new ol.style.Fill({
color: '#BADA55',
opacity: 0.5
styleFunction: function(feature, resolution) {
var styleArray = [new ol.style.Style({
fill: new ol.style.Fill({
color: 'rgba(255, 255, 255, 0.6)'
}),
new ol.style.Stroke({
color: '#FFF',
opacity: 1,
width: 1.5
})
]
})
stroke: new ol.style.Stroke({
color: '#319FD3',
width: 1
}),
zIndex: (feature.getGeometry().getType() !== 'MultiPolygon') ? 2 : 1
})];
return styleArray;
}
});
var map = new ol.Map({

View File

@@ -20,7 +20,7 @@ TopoJSONTopology.prototype.type;
/**
* @type {TopoJSONTransform}
* @type {TopoJSONTransform|undefined}
*/
TopoJSONTopology.prototype.transform;

View File

@@ -255,6 +255,11 @@
* @property {ol.proj.ProjectionLike} defaultProjection Default projection.
*/
/**
* @typedef {Object} olx.format.TopoJSONOptions
* @property {ol.proj.ProjectionLike} defaultProjection Default projection.
*/
/**
* @typedef {Object} olx.format.IGCOptions
* @property {ol.format.IGCZ|undefined} altitudeMode Altitude mode.
@@ -551,6 +556,19 @@
* @property {Array.<string>|undefined} urls URLs.
*/
/**
* @typedef {Object} olx.source.TopoJSONOptions
* @property {Array.<ol.Attribution>|undefined} attributions Attributions.
* @property {ol.proj.ProjectionLike} defaultProjection Default projection.
* @property {ol.Extent|undefined} extent Extent.
* @property {string|undefined} logo Logo.
* @property {GeoJSONObject|undefined} object Object.
* @property {ol.proj.ProjectionLike} projection Projection.
* @property {ol.proj.ProjectionLike} reprojectTo Re-project to.
* @property {string|undefined} text Text.
* @property {string|undefined} url URL.
*/
/**
* @typedef {Object} olx.source.IGCOptions
* @property {ol.format.IGCZ|undefined} altitudeMode Altitude mode.

View File

@@ -1 +1,8 @@
@exportSymbol ol.format.GeoJSON
@exportProperty ol.format.GeoJSON.prototype.readFeature
@exportProperty ol.format.GeoJSON.prototype.readFeatures
@exportProperty ol.format.GeoJSON.prototype.readGeometry
@exportProperty ol.format.GeoJSON.prototype.readProjection
@exportProperty ol.format.GeoJSON.prototype.writeFeature
@exportProperty ol.format.GeoJSON.prototype.writeFeatures
@exportProperty ol.format.GeoJSON.prototype.writeGeometry

View File

@@ -1 +1,3 @@
@exportSymbol ol.format.IGC
@exportProperty ol.format.IGC.prototype.readFeature
@exportProperty ol.format.IGC.prototype.readFeatures

View File

@@ -1,7 +0,0 @@
@exportProperty ol.format.JSON.prototype.readFeature
@exportProperty ol.format.JSON.prototype.readFeatures
@exportProperty ol.format.JSON.prototype.readGeometry
@exportProperty ol.format.JSON.prototype.readProjection
@exportProperty ol.format.JSON.prototype.writeFeature
@exportProperty ol.format.JSON.prototype.writeFeatures
@exportProperty ol.format.JSON.prototype.writeGeometry

View File

@@ -1,2 +1,6 @@
@exportSymbol ol.format.KML
@exportProperty ol.format.KML.prototype.readName
@exportProperty ol.format.KML.prototype.readFeature
@exportProperty ol.format.KML.prototype.readFeatures
@exportProperty ol.format.KML.prototype.readGeometry
@exportProperty ol.format.KML.prototype.readProjection

View File

@@ -1,7 +0,0 @@
@exportProperty ol.format.Text.prototype.readFeature
@exportProperty ol.format.Text.prototype.readFeatures
@exportProperty ol.format.Text.prototype.readGeometry
@exportProperty ol.format.Text.prototype.readProjection
@exportProperty ol.format.Text.prototype.writeFeature
@exportProperty ol.format.Text.prototype.writeFeatures
@exportProperty ol.format.Text.prototype.writeGeometry

View File

@@ -0,0 +1,387 @@
goog.provide('ol.format.TopoJSON');
goog.require('goog.array');
goog.require('goog.asserts');
goog.require('goog.object');
goog.require('ol.Feature');
goog.require('ol.format.JSON');
goog.require('ol.geom.LineString');
goog.require('ol.geom.MultiLineString');
goog.require('ol.geom.MultiPoint');
goog.require('ol.geom.MultiPolygon');
goog.require('ol.geom.Point');
goog.require('ol.geom.Polygon');
goog.require('ol.proj');
/**
* @constructor
* @extends {ol.format.JSON}
* @param {olx.format.TopoJSONOptions=} opt_options Options.
*/
ol.format.TopoJSON = function(opt_options) {
var options = goog.isDef(opt_options) ? opt_options : {};
goog.base(this);
/**
* @private
* @type {ol.proj.Projection}
*/
this.defaultProjection_ =
ol.proj.get(options.defaultProjection || 'EPSG:4326');
};
goog.inherits(ol.format.TopoJSON, ol.format.JSON);
/**
* @const {Array.<string>}
* @private
*/
ol.format.TopoJSON.EXTENSIONS_ = ['.topojson'];
/**
* Concatenate arcs into a coordinate array.
* @param {Array.<number>} indices Indices of arcs to concatenate. Negative
* values indicate arcs need to be reversed.
* @param {Array.<Array.<ol.Coordinate>>} arcs Array of arcs (already
* transformed).
* @return {Array.<Array.<ol.Coordinate>>} Coordinates array.
* @private
*/
ol.format.TopoJSON.concatenateArcs_ = function(indices, arcs) {
var coordinates = [];
var index, arc;
var i, ii;
var j, jj;
for (i = 0, ii = indices.length; i < ii; ++i) {
index = indices[i];
if (i > 0) {
// splicing together arcs, discard last point
coordinates.pop();
}
if (index >= 0) {
// forward arc
arc = arcs[index];
} else {
// reverse arc
arc = arcs[~index].slice().reverse();
}
coordinates.push.apply(coordinates, arc);
}
// provide fresh copies of coordinate arrays
for (j = 0, jj = coordinates.length; j < jj; ++j) {
coordinates[j] = coordinates[j].slice();
}
return coordinates;
};
/**
* Create a point from a TopoJSON geometry object.
*
* @param {TopoJSONGeometry} object TopoJSON object.
* @param {Array.<number>} scale Scale for each dimension.
* @param {Array.<number>} translate Translation for each dimension.
* @return {ol.geom.Point} Geometry.
* @private
*/
ol.format.TopoJSON.readPointGeometry_ =
function(object, scale, translate) {
var coordinates = object.coordinates;
if (goog.isDef(scale) && goog.isDef(translate)) {
ol.format.TopoJSON.transformVertex_(coordinates, scale, translate);
}
return new ol.geom.Point(coordinates);
};
/**
* Create a multi-point from a TopoJSON geometry object.
*
* @param {TopoJSONGeometry} object TopoJSON object.
* @param {Array.<number>} scale Scale for each dimension.
* @param {Array.<number>} translate Translation for each dimension.
* @return {ol.geom.MultiPoint} Geometry.
* @private
*/
ol.format.TopoJSON.readMultiPointGeometry_ = function(object, scale,
translate) {
var coordinates = object.coordinates;
var i, ii;
if (goog.isDef(scale) && goog.isDef(translate)) {
for (i = 0, ii = coordinates.length; i < ii; ++i) {
ol.format.TopoJSON.transformVertex_(coordinates[i], scale, translate);
}
}
return new ol.geom.MultiPoint(coordinates);
};
/**
* Create a linestring from a TopoJSON geometry object.
*
* @param {TopoJSONGeometry} object TopoJSON object.
* @param {Array.<Array.<ol.Coordinate>>} arcs Array of arcs.
* @return {ol.geom.LineString} Geometry.
* @private
*/
ol.format.TopoJSON.readLineStringGeometry_ = function(object, arcs) {
var coordinates = ol.format.TopoJSON.concatenateArcs_(object.arcs, arcs);
return new ol.geom.LineString(goog.array.flatten(coordinates));
};
/**
* Create a multi-linestring from a TopoJSON geometry object.
*
* @param {TopoJSONGeometry} object TopoJSON object.
* @param {Array.<Array.<ol.Coordinate>>} arcs Array of arcs.
* @return {ol.geom.MultiLineString} Geometry.
* @private
*/
ol.format.TopoJSON.readMultiLineStringGeometry_ = function(object, arcs) {
var coordinates = [];
var i, ii;
for (i = 0, ii = object.arcs.length; i < ii; ++i) {
coordinates[i] = ol.format.TopoJSON.concatenateArcs_(object.arcs[i], arcs);
}
return new ol.geom.MultiLineString(coordinates);
};
/**
* Create a polygon from a TopoJSON geometry object.
*
* @param {TopoJSONGeometry} object TopoJSON object.
* @param {Array.<Array.<ol.Coordinate>>} arcs Array of arcs.
* @return {ol.geom.Polygon} Geometry.
* @private
*/
ol.format.TopoJSON.readPolygonGeometry_ = function(object, arcs) {
var coordinates = [];
var i, ii;
for (i = 0, ii = object.arcs.length; i < ii; ++i) {
coordinates[i] = ol.format.TopoJSON.concatenateArcs_(object.arcs[i], arcs);
}
return new ol.geom.Polygon(coordinates);
};
/**
* Create a multi-polygon from a TopoJSON geometry object.
*
* @param {TopoJSONGeometry} object TopoJSON object.
* @param {Array.<Array.<ol.Coordinate>>} arcs Array of arcs.
* @return {ol.geom.MultiPolygon} Geometry.
* @private
*/
ol.format.TopoJSON.readMultiPolygonGeometry_ = function(object, arcs) {
var coordinates = [];
var polyArray, ringCoords, j, jj;
var i, ii;
for (i = 0, ii = object.arcs.length; i < ii; ++i) {
// for each polygon
polyArray = object.arcs[i];
ringCoords = [];
for (j = 0, jj = polyArray.length; j < jj; ++j) {
// for each ring
ringCoords[j] = ol.format.TopoJSON.concatenateArcs_(polyArray[j], arcs);
}
coordinates[i] = ringCoords;
}
return new ol.geom.MultiPolygon(coordinates);
};
/**
* @inheritDoc
*/
ol.format.TopoJSON.prototype.getExtensions = function() {
return ol.format.TopoJSON.EXTENSIONS_;
};
/**
* Create features from a TopoJSON GeometryCollection object.
*
* @param {TopoJSONGeometryCollection} collection TopoJSON Geometry
* object.
* @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.
* @return {Array.<ol.Feature>} Array of features.
* @private
*/
ol.format.TopoJSON.readFeaturesFromGeometryCollection_ = function(
collection, arcs, scale, translate) {
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);
}
return features;
};
/**
* Create a feature from a TopoJSON geometry object.
*
* @param {TopoJSONGeometry} object TopoJSON geometry object.
* @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.
* @return {ol.Feature} Feature.
* @private
*/
ol.format.TopoJSON.readFeatureFromGeometry_ = function(object, arcs,
scale, translate) {
var geometry;
var type = object.type;
var geometryReader = ol.format.TopoJSON.GEOMETRY_READERS_[type];
goog.asserts.assert(goog.isDef(geometryReader));
if ((type === 'Point') || (type === 'MultiPoint')) {
geometry = geometryReader(object, scale, translate);
} else {
geometry = geometryReader(object, arcs);
}
var feature = new ol.Feature();
feature.setGeometry(geometry);
if (goog.isDef(object.id)) {
feature.setId(object.id);
}
if (goog.isDef(object.properties)) {
feature.setValues(object.properties);
}
return feature;
};
/**
* @inheritDoc
*/
ol.format.TopoJSON.prototype.readFeaturesFromObject = function(object) {
if (object.type == 'Topology') {
var topoJSONTopology = /** @type {TopoJSONTopology} */ (object);
var transform, scale = null, translate = null;
if (goog.isDef(topoJSONTopology.transform)) {
transform = /** @type {TopoJSONTransform} */
(topoJSONTopology.transform);
scale = transform.scale;
translate = transform.translate;
}
var arcs = topoJSONTopology.arcs;
if (goog.isDef(transform)) {
ol.format.TopoJSON.transformArcs_(arcs, scale, translate);
}
/** @type {Array.<ol.Feature>} */
var features = [];
var topoJSONFeatures = goog.object.getValues(topoJSONTopology.objects);
var i, ii;
var feature;
for (i = 0, ii = topoJSONFeatures.length; i < ii; ++i) {
if (topoJSONFeatures[i].type === 'GeometryCollection') {
feature = /** @type {TopoJSONGeometryCollection} */
(topoJSONFeatures[i]);
features.push.apply(features,
ol.format.TopoJSON.readFeaturesFromGeometryCollection_(
feature, arcs, scale, translate));
} else {
feature = /** @type {TopoJSONGeometry} */
(topoJSONFeatures[i]);
features.push(ol.format.TopoJSON.readFeatureFromGeometry_(
feature, arcs, scale, translate));
}
}
return features;
} else {
goog.asserts.fail();
return [];
}
};
/**
* Apply a linear transform to array of arcs. The provided array of arcs is
* modified in place.
*
* @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.
* @private
*/
ol.format.TopoJSON.transformArcs_ = function(arcs, scale, translate) {
var i, ii;
for (i = 0, ii = arcs.length; i < ii; ++i) {
ol.format.TopoJSON.transformArc_(arcs[i], scale, translate);
}
};
/**
* Apply a linear transform to an arc. The provided arc is modified in place.
*
* @param {Array.<ol.Coordinate>} arc Arc.
* @param {Array.<number>} scale Scale for each dimension.
* @param {Array.<number>} translate Translation for each dimension.
* @private
*/
ol.format.TopoJSON.transformArc_ = function(arc, scale, translate) {
var x = 0;
var y = 0;
var vertex;
var i, ii;
for (i = 0, ii = arc.length; i < ii; ++i) {
vertex = arc[i];
x += vertex[0];
y += vertex[1];
vertex[0] = x;
vertex[1] = y;
ol.format.TopoJSON.transformVertex_(vertex, scale, translate);
}
};
/**
* Apply a linear transform to a vertex. The provided vertex is modified in
* place.
*
* @param {ol.Coordinate} vertex Vertex.
* @param {Array.<number>} scale Scale for each dimension.
* @param {Array.<number>} translate Translation for each dimension.
* @private
*/
ol.format.TopoJSON.transformVertex_ = function(vertex, scale,
translate) {
vertex[0] = vertex[0] * scale[0] + translate[0];
vertex[1] = vertex[1] * scale[1] + translate[1];
};
/**
* @inheritDoc
*/
ol.format.TopoJSON.prototype.readProjection = function(object) {
return this.defaultProjection_;
};
/**
* @const
* @private
* @type {Object.<string, function(TopoJSONGeometry, Array, ...[Array]): ol.geom.Geometry>}
*/
ol.format.TopoJSON.GEOMETRY_READERS_ = {
'Point': ol.format.TopoJSON.readPointGeometry_,
'LineString': ol.format.TopoJSON.readLineStringGeometry_,
'Polygon': ol.format.TopoJSON.readPolygonGeometry_,
'MultiPoint': ol.format.TopoJSON.readMultiPointGeometry_,
'MultiLineString': ol.format.TopoJSON.readMultiLineStringGeometry_,
'MultiPolygon': ol.format.TopoJSON.readMultiPolygonGeometry_
};

View File

@@ -1,7 +0,0 @@
@exportProperty ol.format.XML.prototype.readFeature
@exportProperty ol.format.XML.prototype.readFeatures
@exportProperty ol.format.XML.prototype.readGeometry
@exportProperty ol.format.XML.prototype.readProjection
@exportProperty ol.format.XML.prototype.writeFeature
@exportProperty ol.format.XML.prototype.writeFeatures
@exportProperty ol.format.XML.prototype.writeGeometry

View File

@@ -0,0 +1 @@
@exportSymbol ol.source.TopoJSON

View File

@@ -0,0 +1,32 @@
goog.provide('ol.source.TopoJSON');
goog.require('ol.format.TopoJSON');
goog.require('ol.source.VectorFile');
/**
* @constructor
* @extends {ol.source.VectorFile}
* @param {olx.source.TopoJSONOptions=} opt_options Options.
*/
ol.source.TopoJSON = function(opt_options) {
var options = goog.isDef(opt_options) ? opt_options : {};
goog.base(this, {
attributions: options.attributions,
extent: options.extent,
format: new ol.format.TopoJSON({
defaultProjection: options.defaultProjection
}),
logo: options.logo,
object: options.object,
projection: options.projection,
reprojectTo: options.reprojectTo,
text: options.text,
url: options.url
});
};
goog.inherits(ol.source.TopoJSON, ol.source.VectorFile);

View File

@@ -1,4 +1,4 @@
goog.provide('ol.test.parser.TopoJSON');
goog.provide('ol.test.format.TopoJSON');
var aruba = {
type: 'Topology',
@@ -9,6 +9,9 @@ var aruba = {
objects: {
aruba: {
type: 'Polygon',
properties: {
prop0: 'value0'
},
arcs: [[0]],
id: 533
}
@@ -20,24 +23,24 @@ var aruba = {
};
describe('ol.parser.TopoJSON', function() {
describe('ol.format.TopoJSON', function() {
var parser;
var format;
before(function() {
parser = new ol.parser.TopoJSON();
format = new ol.format.TopoJSON();
});
describe('constructor', function() {
it('creates a new parser', function() {
expect(parser).to.be.a(ol.parser.Parser);
expect(parser).to.be.a(ol.parser.TopoJSON);
it('creates a new format', function() {
expect(format).to.be.a(ol.format.Format);
expect(format).to.be.a(ol.format.TopoJSON);
});
});
describe('#readFeaturesFromTopology_()', function() {
it('creates an array of features from a topology', function() {
var features = parser.readFeaturesFromTopology_(aruba);
var features = format.readFeaturesFromObject(aruba);
expect(features).to.have.length(1);
var feature = features[0];
@@ -46,7 +49,12 @@ describe('ol.parser.TopoJSON', function() {
var geometry = feature.getGeometry();
expect(geometry).to.be.a(ol.geom.Polygon);
expect(geometry.getBounds()).to.eql([
// Parses identifier
expect(feature.getId()).to.be(533);
// Parses properties
expect(feature.get('prop0')).to.be('value0');
expect(geometry.getExtent()).to.eql([
-70.08100810081008, 12.417091709170947,
-69.9009900990099, 12.608069195591469
]);
@@ -54,26 +62,26 @@ describe('ol.parser.TopoJSON', function() {
});
describe('#readFeaturesFromString()', function() {
describe('#readFeatures()', function() {
it('parses world-110m.geojson', function(done) {
afterLoadText('spec/ol/parser/topojson/world-110m.json', function(text) {
it('parses world-110m.json', function(done) {
afterLoadText('spec/ol/format/topojson/world-110m.json', function(text) {
var result = parser.readFeaturesFromString(text);
expect(result.features.length).to.be(178);
var features = format.readFeatures(text);
expect(features.length).to.be(178);
var first = result.features[0];
var first = features[0];
expect(first).to.be.a(ol.Feature);
var firstGeom = first.getGeometry();
expect(firstGeom).to.be.a(ol.geom.MultiPolygon);
expect(firstGeom.getBounds()).to.eql(
expect(firstGeom.getExtent()).to.eql(
[-180, -85.60903777459777, 180, 83.64513000000002]);
var last = result.features[177];
var last = features[177];
expect(last).to.be.a(ol.Feature);
var lastGeom = last.getGeometry();
expect(lastGeom).to.be.a(ol.geom.Polygon);
expect(lastGeom.getBounds()).to.eql([
expect(lastGeom.getExtent()).to.eql([
25.26325263252633, -22.271802279310577,
32.848528485284874, -15.50833810039586
]);
@@ -89,5 +97,5 @@ describe('ol.parser.TopoJSON', function() {
goog.require('ol.Feature');
goog.require('ol.geom.MultiPolygon');
goog.require('ol.geom.Polygon');
goog.require('ol.parser.Parser');
goog.require('ol.parser.TopoJSON');
goog.require('ol.format.Format');
goog.require('ol.format.TopoJSON');

File diff suppressed because one or more lines are too long