Merge pull request #1061 from tschaub/geom-refactor

Remove shared structures backing geometries.
This commit is contained in:
Tim Schaub
2013-09-27 16:08:08 -07:00
36 changed files with 357 additions and 1508 deletions
+1 -1
View File
@@ -32,7 +32,7 @@ var style = new ol.style.Style({
var vector = new ol.layer.Vector({ var vector = new ol.layer.Vector({
source: new ol.source.Vector({ source: new ol.source.Vector({
parser: new ol.parser.KML({dimension: 2}), parser: new ol.parser.KML(),
url: 'data/kml/2012_Earthquakes_Mag5.kml' url: 'data/kml/2012_Earthquakes_Mag5.kml'
}), }),
style: style style: style
+1 -1
View File
@@ -54,7 +54,7 @@ var style = new ol.style.Style({
var vector = new ol.layer.Vector({ var vector = new ol.layer.Vector({
source: new ol.source.Vector({ source: new ol.source.Vector({
parser: new ol.parser.KML({dimension: 2}), parser: new ol.parser.KML(),
url: 'data/kml/timezones.kml' url: 'data/kml/timezones.kml'
}), }),
style: style style: style
+1 -1
View File
@@ -22,7 +22,7 @@ var raster = new ol.layer.Tile({
var vector = new ol.layer.Vector({ var vector = new ol.layer.Vector({
source: new ol.source.Vector({ source: new ol.source.Vector({
parser: new ol.parser.KML({ parser: new ol.parser.KML({
maxDepth: 1, dimension: 2, extractStyles: true, extractAttributes: true maxDepth: 1, extractStyles: true, extractAttributes: true
}), }),
url: 'data/kml/lines.kml' url: 'data/kml/lines.kml'
}) })
+1
View File
@@ -4,6 +4,7 @@
<meta charset="utf-8"> <meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="chrome=1"> <meta http-equiv="X-UA-Compatible" content="chrome=1">
<meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width"> <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
<link rel="stylesheet" href="../css/ol.css" type="text/css">
<link rel="stylesheet" href="../resources/bootstrap/css/bootstrap.min.css" type="text/css"> <link rel="stylesheet" href="../resources/bootstrap/css/bootstrap.min.css" type="text/css">
<link rel="stylesheet" href="../resources/layout.css" type="text/css"> <link rel="stylesheet" href="../resources/layout.css" type="text/css">
<link rel="stylesheet" href="../resources/bootstrap/css/bootstrap-responsive.min.css" type="text/css"> <link rel="stylesheet" href="../resources/bootstrap/css/bootstrap-responsive.min.css" type="text/css">
-2
View File
@@ -412,8 +412,6 @@
/** /**
* @typedef {Object} ol.parser.KMLOptions * @typedef {Object} ol.parser.KMLOptions
* @property {number|undefined} dimension Create geometries with `dimension`
* dimensions. Default is 3.
* @property {boolean|undefined} extractAttributes Should we extract attributes * @property {boolean|undefined} extractAttributes Should we extract attributes
* from the KML? Default is `true´. * from the KML? Default is `true´.
* @property {boolean|undefined} extractStyles Should we extract styles from the * @property {boolean|undefined} extractStyles Should we extract styles from the
+12 -5
View File
@@ -14,11 +14,6 @@ goog.require('ol.geom.Geometry');
ol.geom.AbstractCollection = function() { ol.geom.AbstractCollection = function() {
goog.base(this); goog.base(this);
/**
* @type {number}
*/
this.dimension;
/** /**
* @type {Array.<ol.geom.Geometry>} * @type {Array.<ol.geom.Geometry>}
*/ */
@@ -67,3 +62,15 @@ ol.geom.AbstractCollection.prototype.getCoordinates = function() {
* @inheritDoc * @inheritDoc
*/ */
ol.geom.AbstractCollection.prototype.getType = goog.abstractMethod; ol.geom.AbstractCollection.prototype.getType = goog.abstractMethod;
/**
* @inheritDoc
*/
ol.geom.AbstractCollection.prototype.transform = function(transform) {
var components = this.components;
for (var i = 0, ii = components.length; i < ii; ++i) {
components[i].transform(transform);
}
this.bounds = null;
};
+11 -32
View File
@@ -2,41 +2,22 @@ goog.provide('ol.geom.Geometry');
goog.provide('ol.geom.GeometryType'); goog.provide('ol.geom.GeometryType');
goog.require('ol.Extent'); goog.require('ol.Extent');
goog.require('ol.geom.SharedVertices'); goog.require('ol.TransformFunction');
/** /**
* @constructor * @constructor
*/ */
ol.geom.Geometry = function() { ol.geom.Geometry = function() {};
/**
* @type {ol.geom.SharedVertices}
* @protected
*/
this.vertices = null;
};
/** /**
* The dimension of this geometry (2 or 3). * Create a clone of this geometry.
* @type {number}
*/
ol.geom.Geometry.prototype.dimension;
/**
* Create a clone of this geometry. The clone will not be represented in any
* shared structure.
* @return {ol.geom.Geometry} The cloned geometry. * @return {ol.geom.Geometry} The cloned geometry.
*/ */
ol.geom.Geometry.prototype.clone = function() { ol.geom.Geometry.prototype.clone = function() {
var clone = new this.constructor(this.getCoordinates()); return new this.constructor(this.getCoordinates());
clone.bounds_ = this.bounds_;
clone.dimension = this.dimension;
return clone;
}; };
@@ -53,15 +34,6 @@ ol.geom.Geometry.prototype.getBounds = goog.abstractMethod;
ol.geom.Geometry.prototype.getCoordinates = goog.abstractMethod; ol.geom.Geometry.prototype.getCoordinates = goog.abstractMethod;
/**
* Get the shared vertices for this geometry.
* @return {ol.geom.SharedVertices} The shared vertices.
*/
ol.geom.Geometry.prototype.getSharedVertices = function() {
return this.vertices;
};
/** /**
* Get the geometry type. * Get the geometry type.
* @return {ol.geom.GeometryType} The geometry type. * @return {ol.geom.GeometryType} The geometry type.
@@ -69,6 +41,13 @@ ol.geom.Geometry.prototype.getSharedVertices = function() {
ol.geom.Geometry.prototype.getType = goog.abstractMethod; ol.geom.Geometry.prototype.getType = goog.abstractMethod;
/**
* Transform a geometry in place.
* @param {ol.TransformFunction} transform Transform function.
*/
ol.geom.Geometry.prototype.transform = goog.abstractMethod;
/** /**
* Geometry types. * Geometry types.
* *
-14
View File
@@ -23,20 +23,6 @@ ol.geom.GeometryCollection = function(geometries) {
*/ */
this.components = geometries; this.components = geometries;
var dimension = 0;
for (var i = 0, ii = geometries.length; i < ii; ++i) {
if (goog.isDef(dimension)) {
dimension = geometries[i].dimension;
} else {
goog.asserts.assert(dimension == geometries[i].dimension);
}
}
/**
* @type {number}
*/
this.dimension = dimension;
}; };
goog.inherits(ol.geom.GeometryCollection, ol.geom.AbstractCollection); goog.inherits(ol.geom.GeometryCollection, ol.geom.AbstractCollection);
+2 -4
View File
@@ -3,7 +3,6 @@ goog.provide('ol.geom.LinearRing');
goog.require('ol.CoordinateArray'); goog.require('ol.CoordinateArray');
goog.require('ol.geom.GeometryType'); goog.require('ol.geom.GeometryType');
goog.require('ol.geom.LineString'); goog.require('ol.geom.LineString');
goog.require('ol.geom.SharedVertices');
@@ -12,10 +11,9 @@ goog.require('ol.geom.SharedVertices');
* @extends {ol.geom.LineString} * @extends {ol.geom.LineString}
* @param {ol.CoordinateArray} coordinates Vertex array (e.g. * @param {ol.CoordinateArray} coordinates Vertex array (e.g.
* [[x0, y0], [x1, y1]]). * [[x0, y0], [x1, y1]]).
* @param {ol.geom.SharedVertices=} opt_shared Shared vertices.
*/ */
ol.geom.LinearRing = function(coordinates, opt_shared) { ol.geom.LinearRing = function(coordinates) {
goog.base(this, coordinates, opt_shared); goog.base(this, coordinates);
/** /**
* We're intentionally not enforcing that rings be closed right now. This * We're intentionally not enforcing that rings be closed right now. This
+30 -74
View File
@@ -2,48 +2,29 @@ goog.provide('ol.geom.LineString');
goog.require('goog.asserts'); goog.require('goog.asserts');
goog.require('ol.CoordinateArray'); goog.require('ol.CoordinateArray');
goog.require('ol.extent');
goog.require('ol.geom'); goog.require('ol.geom');
goog.require('ol.geom.Geometry'); goog.require('ol.geom.Geometry');
goog.require('ol.geom.GeometryType'); goog.require('ol.geom.GeometryType');
goog.require('ol.geom.SharedVertices');
/** /**
* @constructor * @constructor
* @extends {ol.geom.Geometry} * @extends {ol.geom.Geometry}
* @param {ol.CoordinateArray} coordinates Vertex array (e.g. * @param {ol.CoordinateArray} coordinates Array of coordinates (e.g.
* [[x0, y0], [x1, y1]]). * [[x0, y0], [x1, y1]]).
* @param {ol.geom.SharedVertices=} opt_shared Shared vertices.
*/ */
ol.geom.LineString = function(coordinates, opt_shared) { ol.geom.LineString = function(coordinates) {
goog.base(this); goog.base(this);
goog.asserts.assert(goog.isArray(coordinates[0])); goog.asserts.assert(goog.isArray(coordinates[0]));
var vertices = opt_shared,
dimension;
if (!goog.isDef(vertices)) {
dimension = coordinates[0].length;
vertices = new ol.geom.SharedVertices({dimension: dimension});
}
/** /**
* @type {ol.geom.SharedVertices} * Array of coordinates.
*/ * @type {ol.CoordinateArray}
this.vertices = vertices;
/**
* @type {number}
* @private * @private
*/ */
this.sharedId_ = vertices.add(coordinates); this.coordinates_ = coordinates;
/**
* @type {number}
*/
this.dimension = vertices.getDimension();
goog.asserts.assert(this.dimension >= 2);
/** /**
* @type {ol.Extent} * @type {ol.Extent}
@@ -62,7 +43,9 @@ goog.inherits(ol.geom.LineString, ol.geom.Geometry);
* @return {number} The vertex coordinate value. * @return {number} The vertex coordinate value.
*/ */
ol.geom.LineString.prototype.get = function(index, dim) { ol.geom.LineString.prototype.get = function(index, dim) {
return this.vertices.get(this.sharedId_, index, dim); var coordinates = this.getCoordinates();
goog.asserts.assert(coordinates.length > index);
return coordinates[index][dim];
}; };
@@ -71,17 +54,7 @@ ol.geom.LineString.prototype.get = function(index, dim) {
* @return {ol.CoordinateArray} Coordinates array. * @return {ol.CoordinateArray} Coordinates array.
*/ */
ol.geom.LineString.prototype.getCoordinates = function() { ol.geom.LineString.prototype.getCoordinates = function() {
var count = this.getCount(); return this.coordinates_;
var coordinates = new Array(count);
var vertex;
for (var i = 0; i < count; ++i) {
vertex = new Array(this.dimension);
for (var j = 0; j < this.dimension; ++j) {
vertex[j] = this.get(i, j);
}
coordinates[i] = vertex;
}
return coordinates;
}; };
@@ -90,7 +63,7 @@ ol.geom.LineString.prototype.getCoordinates = function() {
* @return {number} The vertex count. * @return {number} The vertex count.
*/ */
ol.geom.LineString.prototype.getCount = function() { ol.geom.LineString.prototype.getCount = function() {
return this.vertices.getCount(this.sharedId_); return this.getCoordinates().length;
}; };
@@ -99,34 +72,12 @@ ol.geom.LineString.prototype.getCount = function() {
*/ */
ol.geom.LineString.prototype.getBounds = function() { ol.geom.LineString.prototype.getBounds = function() {
if (goog.isNull(this.bounds_)) { if (goog.isNull(this.bounds_)) {
var dimension = this.dimension, var coordinates = this.getCoordinates();
vertices = this.vertices, var extent = ol.extent.createEmpty();
id = this.sharedId_, for (var i = 0, ii = coordinates.length; i < ii; ++i) {
count = vertices.getCount(id), ol.extent.extendCoordinate(extent, coordinates[i]);
start = vertices.getStart(id),
end = start + (count * dimension),
coordinates = vertices.coordinates,
minX, maxX,
minY, maxY,
x, y, i;
minX = maxX = coordinates[start];
minY = maxY = coordinates[start + 1];
for (i = start + dimension; i < end; i += dimension) {
x = coordinates[i];
y = coordinates[i + 1];
if (x < minX) {
minX = x;
} else if (x > maxX) {
maxX = x;
}
if (y < minY) {
minY = y;
} else if (y > maxY) {
maxY = y;
}
} }
this.bounds_ = [minX, minY, maxX, maxY]; this.bounds_ = extent;
} }
return this.bounds_; return this.bounds_;
}; };
@@ -140,15 +91,6 @@ ol.geom.LineString.prototype.getType = function() {
}; };
/**
* Get the identifier used to mark this line in the shared vertices structure.
* @return {number} The identifier.
*/
ol.geom.LineString.prototype.getSharedId = function() {
return this.sharedId_;
};
/** /**
* Calculate the distance from a coordinate to this linestring. * Calculate the distance from a coordinate to this linestring.
* *
@@ -164,3 +106,17 @@ ol.geom.LineString.prototype.distanceFromCoordinate = function(coordinate) {
} }
return Math.sqrt(dist2); return Math.sqrt(dist2);
}; };
/**
* @inheritDoc
*/
ol.geom.LineString.prototype.transform = function(transform) {
var coordinates = this.getCoordinates();
var coord;
for (var i = 0, ii = coordinates.length; i < ii; ++i) {
coord = coordinates[i];
transform(coord, coord, coord.length);
}
this.bounds_ = null;
};
+4 -21
View File
@@ -5,7 +5,6 @@ goog.require('ol.CoordinateArray');
goog.require('ol.geom.AbstractCollection'); goog.require('ol.geom.AbstractCollection');
goog.require('ol.geom.GeometryType'); goog.require('ol.geom.GeometryType');
goog.require('ol.geom.LineString'); goog.require('ol.geom.LineString');
goog.require('ol.geom.SharedVertices');
@@ -13,21 +12,11 @@ goog.require('ol.geom.SharedVertices');
* @constructor * @constructor
* @extends {ol.geom.AbstractCollection} * @extends {ol.geom.AbstractCollection}
* @param {Array.<ol.CoordinateArray>} coordinates Coordinates array. * @param {Array.<ol.CoordinateArray>} coordinates Coordinates array.
* @param {ol.geom.SharedVertices=} opt_shared Shared vertices.
*/ */
ol.geom.MultiLineString = function(coordinates, opt_shared) { ol.geom.MultiLineString = function(coordinates) {
goog.base(this); goog.base(this);
goog.asserts.assert(goog.isArray(coordinates[0][0])); goog.asserts.assert(goog.isArray(coordinates[0][0]));
var vertices = opt_shared,
dimension;
if (!goog.isDef(vertices)) {
// try to get dimension from first vertex in first line
dimension = coordinates[0][0].length;
vertices = new ol.geom.SharedVertices({dimension: dimension});
}
var numParts = coordinates.length; var numParts = coordinates.length;
/** /**
@@ -35,14 +24,9 @@ ol.geom.MultiLineString = function(coordinates, opt_shared) {
*/ */
this.components = new Array(numParts); this.components = new Array(numParts);
for (var i = 0; i < numParts; ++i) { for (var i = 0; i < numParts; ++i) {
this.components[i] = new ol.geom.LineString(coordinates[i], vertices); this.components[i] = new ol.geom.LineString(coordinates[i]);
} }
/**
* @type {number}
*/
this.dimension = vertices.getDimension();
}; };
goog.inherits(ol.geom.MultiLineString, ol.geom.AbstractCollection); goog.inherits(ol.geom.MultiLineString, ol.geom.AbstractCollection);
@@ -78,14 +62,13 @@ ol.geom.MultiLineString.prototype.distanceFromCoordinate =
* Create a multi-linestring geometry from an array of linestring geometries. * Create a multi-linestring geometry from an array of linestring geometries.
* *
* @param {Array.<ol.geom.LineString>} geometries Array of geometries. * @param {Array.<ol.geom.LineString>} geometries Array of geometries.
* @param {ol.geom.SharedVertices=} opt_shared Shared vertices.
* @return {ol.geom.MultiLineString} A new geometry. * @return {ol.geom.MultiLineString} A new geometry.
*/ */
ol.geom.MultiLineString.fromParts = function(geometries, opt_shared) { ol.geom.MultiLineString.fromParts = function(geometries) {
var count = geometries.length; var count = geometries.length;
var coordinates = new Array(count); var coordinates = new Array(count);
for (var i = 0; i < count; ++i) { for (var i = 0; i < count; ++i) {
coordinates[i] = geometries[i].getCoordinates(); coordinates[i] = geometries[i].getCoordinates();
} }
return new ol.geom.MultiLineString(coordinates, opt_shared); return new ol.geom.MultiLineString(coordinates);
}; };
+4 -26
View File
@@ -5,7 +5,6 @@ goog.require('ol.CoordinateArray');
goog.require('ol.geom.AbstractCollection'); goog.require('ol.geom.AbstractCollection');
goog.require('ol.geom.GeometryType'); goog.require('ol.geom.GeometryType');
goog.require('ol.geom.Point'); goog.require('ol.geom.Point');
goog.require('ol.geom.SharedVertices');
@@ -13,26 +12,11 @@ goog.require('ol.geom.SharedVertices');
* @constructor * @constructor
* @extends {ol.geom.AbstractCollection} * @extends {ol.geom.AbstractCollection}
* @param {ol.CoordinateArray} coordinates Coordinates array. * @param {ol.CoordinateArray} coordinates Coordinates array.
* @param {ol.geom.SharedVertices=} opt_shared Shared vertices.
*/ */
ol.geom.MultiPoint = function(coordinates, opt_shared) { ol.geom.MultiPoint = function(coordinates) {
goog.base(this); goog.base(this);
goog.asserts.assert(goog.isArray(coordinates[0])); goog.asserts.assert(goog.isArray(coordinates[0]));
var vertices = opt_shared,
dimension;
if (!goog.isDef(vertices)) {
// try to get dimension from first vertex
dimension = coordinates[0].length;
vertices = new ol.geom.SharedVertices({dimension: dimension});
}
/**
* @type {ol.geom.SharedVertices}
*/
this.vertices = vertices;
var numParts = coordinates.length; var numParts = coordinates.length;
/** /**
@@ -40,14 +24,9 @@ ol.geom.MultiPoint = function(coordinates, opt_shared) {
*/ */
this.components = new Array(numParts); this.components = new Array(numParts);
for (var i = 0; i < numParts; ++i) { for (var i = 0; i < numParts; ++i) {
this.components[i] = new ol.geom.Point(coordinates[i], vertices); this.components[i] = new ol.geom.Point(coordinates[i]);
} }
/**
* @type {number}
*/
this.dimension = vertices.getDimension();
}; };
goog.inherits(ol.geom.MultiPoint, ol.geom.AbstractCollection); goog.inherits(ol.geom.MultiPoint, ol.geom.AbstractCollection);
@@ -64,14 +43,13 @@ ol.geom.MultiPoint.prototype.getType = function() {
* Create a multi-point geometry from an array of point geometries. * Create a multi-point geometry from an array of point geometries.
* *
* @param {Array.<ol.geom.Point>} geometries Array of geometries. * @param {Array.<ol.geom.Point>} geometries Array of geometries.
* @param {ol.geom.SharedVertices=} opt_shared Shared vertices.
* @return {ol.geom.MultiPoint} A new geometry. * @return {ol.geom.MultiPoint} A new geometry.
*/ */
ol.geom.MultiPoint.fromParts = function(geometries, opt_shared) { ol.geom.MultiPoint.fromParts = function(geometries) {
var count = geometries.length; var count = geometries.length;
var coordinates = new Array(count); var coordinates = new Array(count);
for (var i = 0; i < count; ++i) { for (var i = 0; i < count; ++i) {
coordinates[i] = geometries[i].getCoordinates(); coordinates[i] = geometries[i].getCoordinates();
} }
return new ol.geom.MultiPoint(coordinates, opt_shared); return new ol.geom.MultiPoint(coordinates);
}; };
+4 -21
View File
@@ -5,7 +5,6 @@ goog.require('ol.CoordinateArray');
goog.require('ol.geom.AbstractCollection'); goog.require('ol.geom.AbstractCollection');
goog.require('ol.geom.GeometryType'); goog.require('ol.geom.GeometryType');
goog.require('ol.geom.Polygon'); goog.require('ol.geom.Polygon');
goog.require('ol.geom.SharedVertices');
@@ -14,21 +13,11 @@ goog.require('ol.geom.SharedVertices');
* @extends {ol.geom.AbstractCollection} * @extends {ol.geom.AbstractCollection}
* @param {Array.<Array.<ol.CoordinateArray>>} coordinates Coordinates * @param {Array.<Array.<ol.CoordinateArray>>} coordinates Coordinates
* array. * array.
* @param {ol.geom.SharedVertices=} opt_shared Shared vertices.
*/ */
ol.geom.MultiPolygon = function(coordinates, opt_shared) { ol.geom.MultiPolygon = function(coordinates) {
goog.base(this); goog.base(this);
goog.asserts.assert(goog.isArray(coordinates[0][0][0])); goog.asserts.assert(goog.isArray(coordinates[0][0][0]));
var vertices = opt_shared,
dimension;
if (!goog.isDef(vertices)) {
// try to get dimension from first vertex in first ring of the first poly
dimension = coordinates[0][0][0].length;
vertices = new ol.geom.SharedVertices({dimension: dimension});
}
var numParts = coordinates.length; var numParts = coordinates.length;
/** /**
@@ -36,14 +25,9 @@ ol.geom.MultiPolygon = function(coordinates, opt_shared) {
*/ */
this.components = new Array(numParts); this.components = new Array(numParts);
for (var i = 0; i < numParts; ++i) { for (var i = 0; i < numParts; ++i) {
this.components[i] = new ol.geom.Polygon(coordinates[i], vertices); this.components[i] = new ol.geom.Polygon(coordinates[i]);
} }
/**
* @type {number}
*/
this.dimension = vertices.getDimension();
}; };
goog.inherits(ol.geom.MultiPolygon, ol.geom.AbstractCollection); goog.inherits(ol.geom.MultiPolygon, ol.geom.AbstractCollection);
@@ -78,14 +62,13 @@ ol.geom.MultiPolygon.prototype.containsCoordinate = function(coordinate) {
* Create a multi-polygon geometry from an array of polygon geometries. * Create a multi-polygon geometry from an array of polygon geometries.
* *
* @param {Array.<ol.geom.Polygon>} geometries Array of geometries. * @param {Array.<ol.geom.Polygon>} geometries Array of geometries.
* @param {ol.geom.SharedVertices=} opt_shared Shared vertices.
* @return {ol.geom.MultiPolygon} A new geometry. * @return {ol.geom.MultiPolygon} A new geometry.
*/ */
ol.geom.MultiPolygon.fromParts = function(geometries, opt_shared) { ol.geom.MultiPolygon.fromParts = function(geometries) {
var count = geometries.length; var count = geometries.length;
var coordinates = new Array(count); var coordinates = new Array(count);
for (var i = 0; i < count; ++i) { for (var i = 0; i < count; ++i) {
coordinates[i] = geometries[i].getCoordinates(); coordinates[i] = geometries[i].getCoordinates();
} }
return new ol.geom.MultiPolygon(coordinates, opt_shared); return new ol.geom.MultiPolygon(coordinates);
}; };
+12 -35
View File
@@ -4,43 +4,23 @@ goog.require('goog.asserts');
goog.require('ol.Coordinate'); goog.require('ol.Coordinate');
goog.require('ol.geom.Geometry'); goog.require('ol.geom.Geometry');
goog.require('ol.geom.GeometryType'); goog.require('ol.geom.GeometryType');
goog.require('ol.geom.SharedVertices');
/** /**
* @constructor * @constructor
* @extends {ol.geom.Geometry} * @extends {ol.geom.Geometry}
* @param {ol.Coordinate} coordinates Coordinates array (e.g. [x, y]). * @param {ol.Coordinate} coordinates Coordinate values (e.g. [x, y]).
* @param {ol.geom.SharedVertices=} opt_shared Shared vertices.
*/ */
ol.geom.Point = function(coordinates, opt_shared) { ol.geom.Point = function(coordinates) {
goog.base(this); goog.base(this);
var vertices = opt_shared,
dimension;
if (!goog.isDef(vertices)) {
dimension = coordinates.length;
vertices = new ol.geom.SharedVertices({dimension: dimension});
}
/** /**
* @type {ol.geom.SharedVertices} * Point coordinate values.
*/ * @type {ol.Coordinate}
this.vertices = vertices;
/**
* @type {number}
* @private * @private
*/ */
this.sharedId_ = vertices.add([coordinates]); this.coordinates_ = coordinates;
/**
* @type {number}
*/
this.dimension = vertices.getDimension();
goog.asserts.assert(this.dimension >= 2);
/** /**
* @type {ol.Extent} * @type {ol.Extent}
@@ -57,7 +37,7 @@ goog.inherits(ol.geom.Point, ol.geom.Geometry);
* @return {number} The coordinate value. * @return {number} The coordinate value.
*/ */
ol.geom.Point.prototype.get = function(dim) { ol.geom.Point.prototype.get = function(dim) {
return this.vertices.get(this.sharedId_, 0, dim); return this.getCoordinates()[dim];
}; };
@@ -79,11 +59,7 @@ ol.geom.Point.prototype.getBounds = function() {
* @return {ol.Coordinate} Coordinates array. * @return {ol.Coordinate} Coordinates array.
*/ */
ol.geom.Point.prototype.getCoordinates = function() { ol.geom.Point.prototype.getCoordinates = function() {
var coordinates = new Array(this.dimension); return this.coordinates_;
for (var i = 0; i < this.dimension; ++i) {
coordinates[i] = this.get(i);
}
return coordinates;
}; };
@@ -96,9 +72,10 @@ ol.geom.Point.prototype.getType = function() {
/** /**
* Get the identifier used to mark this point in the shared vertices structure. * @inheritDoc
* @return {number} The identifier.
*/ */
ol.geom.Point.prototype.getSharedId = function() { ol.geom.Point.prototype.transform = function(transform) {
return this.sharedId_; var coordinates = this.getCoordinates();
transform(coordinates, coordinates, coordinates.length);
this.bounds_ = null;
}; };
+13 -24
View File
@@ -6,7 +6,6 @@ goog.require('ol.extent');
goog.require('ol.geom.Geometry'); goog.require('ol.geom.Geometry');
goog.require('ol.geom.GeometryType'); goog.require('ol.geom.GeometryType');
goog.require('ol.geom.LinearRing'); goog.require('ol.geom.LinearRing');
goog.require('ol.geom.SharedVertices');
@@ -21,32 +20,17 @@ goog.require('ol.geom.SharedVertices');
* @extends {ol.geom.Geometry} * @extends {ol.geom.Geometry}
* @param {Array.<ol.CoordinateArray>} coordinates Array of rings. First * @param {Array.<ol.CoordinateArray>} coordinates Array of rings. First
* is outer, any remaining are inner. * is outer, any remaining are inner.
* @param {ol.geom.SharedVertices=} opt_shared Shared vertices.
*/ */
ol.geom.Polygon = function(coordinates, opt_shared) { ol.geom.Polygon = function(coordinates) {
goog.base(this); goog.base(this);
goog.asserts.assert(goog.isArray(coordinates[0][0])); goog.asserts.assert(goog.isArray(coordinates[0][0]));
var vertices = opt_shared,
dimension;
if (!goog.isDef(vertices)) {
// try to get dimension from first vertex in first ring
dimension = coordinates[0][0].length;
vertices = new ol.geom.SharedVertices({dimension: dimension});
}
/** /**
* @private * @private
* @type {ol.Coordinate} * @type {ol.Coordinate}
*/ */
this.labelPoint_ = null; this.labelPoint_ = null;
/**
* @type {ol.geom.SharedVertices}
*/
this.vertices = vertices;
var numRings = coordinates.length; var numRings = coordinates.length;
/** /**
@@ -67,15 +51,9 @@ ol.geom.Polygon = function(coordinates, opt_shared) {
ringCoords.reverse(); ringCoords.reverse();
} }
} }
this.rings[i] = new ol.geom.LinearRing(ringCoords, vertices); this.rings[i] = new ol.geom.LinearRing(ringCoords);
} }
/**
* @type {number}
*/
this.dimension = vertices.getDimension();
goog.asserts.assert(this.dimension >= 2);
}; };
goog.inherits(ol.geom.Polygon, ol.geom.Geometry); goog.inherits(ol.geom.Polygon, ol.geom.Geometry);
@@ -179,3 +157,14 @@ ol.geom.Polygon.prototype.getInteriorPoint = function() {
return this.labelPoint_; return this.labelPoint_;
}; };
/**
* @inheritDoc
*/
ol.geom.Polygon.prototype.transform = function(transform) {
var rings = this.rings;
for (var i = 0, ii = rings.length; i < ii; ++i) {
rings[i].transform(transform);
}
};
-157
View File
@@ -1,157 +0,0 @@
goog.provide('ol.geom.SharedVertices');
goog.require('goog.asserts');
goog.require('ol.Coordinate');
goog.require('ol.CoordinateArray');
/**
* @typedef {{dimension: (number),
* offset: (ol.Coordinate|undefined)}}
*/
ol.geom.SharedVerticesOptions;
/**
* Provides methods for dealing with shared, flattened arrays of vertices.
*
* @constructor
* @param {ol.geom.SharedVerticesOptions=} opt_options Shared vertices options.
*/
ol.geom.SharedVertices = function(opt_options) {
var options = opt_options ? opt_options : {};
/**
* @type {Array.<number>}
*/
this.coordinates = [];
/**
* @type {Array.<number>}
* @private
*/
this.starts_ = [];
/**
* @type {Array.<number>}
* @private
*/
this.counts_ = [];
/**
* Number of dimensions per vertex. Default is 2.
* @type {number}
* @private
*/
this.dimension_ = options.dimension || 2;
/**
* Vertex offset.
* @type {Array.<number>}
* @private
*/
this.offset_ = options.offset || null;
goog.asserts.assert(goog.isNull(this.offset_) ||
this.offset_.length === this.dimension_);
};
/**
* Adds a vertex array to the shared coordinate array.
* @param {ol.CoordinateArray} vertices Array of vertices.
* @return {number} Index used to reference the added vertex array.
*/
ol.geom.SharedVertices.prototype.add = function(vertices) {
var start = this.coordinates.length;
var offset = this.offset_;
var dimension = this.dimension_;
var count = vertices.length;
var vertex, index;
for (var i = 0; i < count; ++i) {
vertex = vertices[i];
index = start + (i * dimension);
for (var j = 0; j < dimension; ++j) {
this.coordinates[index + j] = vertex[j] - (offset ? offset[j] : 0);
}
}
var length = this.starts_.push(start);
this.counts_.push(count);
return length - 1;
};
/**
* @param {number} id The vertex array identifier (returned by add).
* @param {number} index The vertex index.
* @param {number} dim The coordinate dimension.
* @return {number} The coordinate value.
*/
ol.geom.SharedVertices.prototype.get = function(id, index, dim) {
goog.asserts.assert(id < this.starts_.length);
goog.asserts.assert(dim <= this.dimension_);
goog.asserts.assert(index < this.counts_[id]);
var start = this.starts_[id];
var value = this.coordinates[start + (index * this.dimension_) + dim];
if (this.offset_) {
value += this.offset_[dim];
}
return value;
};
/**
* @param {number} id The vertex array identifier (returned by add).
* @return {number} The number of vertices in the referenced array.
*/
ol.geom.SharedVertices.prototype.getCount = function(id) {
goog.asserts.assert(id < this.counts_.length);
return this.counts_[id];
};
/**
* Get the array of counts. The index returned by the add method can be used
* to look up the number of vertices.
*
* @return {Array.<number>} The counts array.
*/
ol.geom.SharedVertices.prototype.getCounts = function() {
return this.counts_;
};
/**
* @return {number} The dimension of each vertex in the array.
*/
ol.geom.SharedVertices.prototype.getDimension = function() {
return this.dimension_;
};
/**
* @return {Array.<number>} The offset array for vertex coordinates (or null).
*/
ol.geom.SharedVertices.prototype.getOffset = function() {
return this.offset_;
};
/**
* @param {number} id The vertex array identifier (returned by add).
* @return {number} The start index in the shared vertices array.
*/
ol.geom.SharedVertices.prototype.getStart = function(id) {
goog.asserts.assert(id < this.starts_.length);
return this.starts_[id];
};
/**
* Get the array of start indexes.
* @return {Array.<number>} The starts array.
*/
ol.geom.SharedVertices.prototype.getStarts = function() {
return this.starts_;
};
+12 -80
View File
@@ -13,7 +13,6 @@ goog.require('ol.expr.LogicalOp');
goog.require('ol.expr.functions'); goog.require('ol.expr.functions');
goog.require('ol.extent'); goog.require('ol.extent');
goog.require('ol.geom.GeometryType'); goog.require('ol.geom.GeometryType');
goog.require('ol.geom.SharedVertices');
goog.require('ol.layer.Layer'); goog.require('ol.layer.Layer');
goog.require('ol.proj'); goog.require('ol.proj');
goog.require('ol.source.Vector'); goog.require('ol.source.Vector');
@@ -286,27 +285,6 @@ ol.layer.Vector = function(options) {
this.transformFeatureInfo_ = goog.isDef(options.transformFeatureInfo) ? this.transformFeatureInfo_ = goog.isDef(options.transformFeatureInfo) ?
options.transformFeatureInfo : ol.layer.Vector.uidTransformFeatureInfo; options.transformFeatureInfo : ol.layer.Vector.uidTransformFeatureInfo;
/**
* TODO: this means we need to know dimension at construction
* @type {ol.geom.SharedVertices}
* @private
*/
this.pointVertices_ = new ol.geom.SharedVertices();
/**
* TODO: this means we need to know dimension at construction
* @type {ol.geom.SharedVertices}
* @private
*/
this.lineVertices_ = new ol.geom.SharedVertices();
/**
* TODO: this means we need to know dimension at construction
* @type {ol.geom.SharedVertices}
* @private
*/
this.polygonVertices_ = new ol.geom.SharedVertices();
/** /**
* True if this is a temporary layer. * True if this is a temporary layer.
* @type {boolean} * @type {boolean}
@@ -399,30 +377,6 @@ ol.layer.Vector.prototype.getFeaturesObjectForExtent = function(extent,
}; };
/**
* @return {ol.geom.SharedVertices} Shared line vertices.
*/
ol.layer.Vector.prototype.getLineVertices = function() {
return this.lineVertices_;
};
/**
* @return {ol.geom.SharedVertices} Shared point vertices.
*/
ol.layer.Vector.prototype.getPointVertices = function() {
return this.pointVertices_;
};
/**
* @return {ol.geom.SharedVertices} Shared polygon vertices.
*/
ol.layer.Vector.prototype.getPolygonVertices = function() {
return this.polygonVertices_;
};
/** /**
* @param {Object.<string, ol.Feature>} features Features. * @param {Object.<string, ol.Feature>} features Features.
* @param {number} resolution Map resolution. * @param {number} resolution Map resolution.
@@ -497,17 +451,6 @@ ol.layer.Vector.prototype.getFeatureWithUid = function(uid) {
* view in one projection. * view in one projection.
*/ */
ol.layer.Vector.prototype.parseFeatures = function(data, parser, projection) { ol.layer.Vector.prototype.parseFeatures = function(data, parser, projection) {
var lookup = {};
lookup[ol.geom.GeometryType.POINT] = this.pointVertices_;
lookup[ol.geom.GeometryType.LINESTRING] = this.lineVertices_;
lookup[ol.geom.GeometryType.POLYGON] = this.polygonVertices_;
lookup[ol.geom.GeometryType.MULTIPOINT] = this.pointVertices_;
lookup[ol.geom.GeometryType.MULTILINESTRING] = this.lineVertices_;
lookup[ol.geom.GeometryType.MULTIPOLYGON] = this.polygonVertices_;
var callback = function(feature, type) {
return lookup[type];
};
var addFeatures = function(data) { var addFeatures = function(data) {
var features = data.features; var features = data.features;
@@ -516,46 +459,35 @@ ol.layer.Vector.prototype.parseFeatures = function(data, parser, projection) {
sourceProjection = data.metadata.projection; sourceProjection = data.metadata.projection;
} }
var transform = ol.proj.getTransform(sourceProjection, projection); var transform = ol.proj.getTransform(sourceProjection, projection);
var geometry = null;
transform( for (var i = 0, ii = features.length; i < ii; ++i) {
this.pointVertices_.coordinates, geometry = features[i].getGeometry();
this.pointVertices_.coordinates, if (!goog.isNull(geometry)) {
this.pointVertices_.getDimension()); geometry.transform(transform);
}
transform( }
this.lineVertices_.coordinates,
this.lineVertices_.coordinates,
this.lineVertices_.getDimension());
transform(
this.polygonVertices_.coordinates,
this.polygonVertices_.coordinates,
this.polygonVertices_.getDimension());
this.addFeatures(features); this.addFeatures(features);
}; };
var options = {callback: callback}, result; var result;
if (goog.isString(data)) { if (goog.isString(data)) {
if (goog.isFunction(parser.readFeaturesFromStringAsync)) { if (goog.isFunction(parser.readFeaturesFromStringAsync)) {
parser.readFeaturesFromStringAsync(data, goog.bind(addFeatures, this), parser.readFeaturesFromStringAsync(data, goog.bind(addFeatures, this));
options);
} else { } else {
goog.asserts.assert( goog.asserts.assert(
goog.isFunction(parser.readFeaturesFromString), goog.isFunction(parser.readFeaturesFromString),
'Expected parser with a readFeaturesFromString method.'); 'Expected parser with a readFeaturesFromString method.');
result = parser.readFeaturesFromString(data, options); result = parser.readFeaturesFromString(data);
addFeatures.call(this, result); addFeatures.call(this, result);
} }
} else if (goog.isObject(data)) { } else if (goog.isObject(data)) {
if (goog.isFunction(parser.readFeaturesFromObjectAsync)) { if (goog.isFunction(parser.readFeaturesFromObjectAsync)) {
parser.readFeaturesFromObjectAsync(data, goog.bind(addFeatures, this), parser.readFeaturesFromObjectAsync(data, goog.bind(addFeatures, this));
options);
} else { } else {
goog.asserts.assert( goog.asserts.assert(
goog.isFunction(parser.readFeaturesFromObject), goog.isFunction(parser.readFeaturesFromObject),
'Expected parser with a readFeaturesFromObject method.'); 'Expected parser with a readFeaturesFromObject method.');
result = parser.readFeaturesFromObject(data, options); result = parser.readFeaturesFromObject(data);
addFeatures.call(this, result); addFeatures.call(this, result);
} }
} else { } else {
-18
View File
@@ -2,7 +2,6 @@ goog.provide('ol.parser.AsyncObjectFeatureParser');
goog.provide('ol.parser.AsyncStringFeatureParser'); goog.provide('ol.parser.AsyncStringFeatureParser');
goog.provide('ol.parser.DomFeatureParser'); goog.provide('ol.parser.DomFeatureParser');
goog.provide('ol.parser.ObjectFeatureParser'); goog.provide('ol.parser.ObjectFeatureParser');
goog.provide('ol.parser.ReadFeaturesOptions');
goog.provide('ol.parser.ReadFeaturesResult'); goog.provide('ol.parser.ReadFeaturesResult');
goog.provide('ol.parser.StringFeatureParser'); goog.provide('ol.parser.StringFeatureParser');
@@ -18,7 +17,6 @@ ol.parser.DomFeatureParser = function() {};
/** /**
* @param {Element|Document} node Document or element node. * @param {Element|Document} node Document or element node.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Feature reading options.
* @return {ol.parser.ReadFeaturesResult} Features and metadata. * @return {ol.parser.ReadFeaturesResult} Features and metadata.
*/ */
ol.parser.DomFeatureParser.prototype.readFeaturesFromNode = ol.parser.DomFeatureParser.prototype.readFeaturesFromNode =
@@ -34,7 +32,6 @@ ol.parser.ObjectFeatureParser = function() {};
/** /**
* @param {Object} obj Object representing features. * @param {Object} obj Object representing features.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Feature reading options.
* @return {ol.parser.ReadFeaturesResult} Features and metadata. * @return {ol.parser.ReadFeaturesResult} Features and metadata.
*/ */
ol.parser.ObjectFeatureParser.prototype.readFeaturesFromObject = ol.parser.ObjectFeatureParser.prototype.readFeaturesFromObject =
@@ -50,7 +47,6 @@ ol.parser.StringFeatureParser = function() {};
/** /**
* @param {string} data String data. * @param {string} data String data.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Feature reading options.
* @return {ol.parser.ReadFeaturesResult} Features and metadata. * @return {ol.parser.ReadFeaturesResult} Features and metadata.
*/ */
ol.parser.StringFeatureParser.prototype.readFeaturesFromString = ol.parser.StringFeatureParser.prototype.readFeaturesFromString =
@@ -68,7 +64,6 @@ ol.parser.AsyncStringFeatureParser = function() {};
* @param {string} data String data. * @param {string} data String data.
* @param {function(ol.parser.ReadFeaturesResult)} callback Callback which is * @param {function(ol.parser.ReadFeaturesResult)} callback Callback which is
* called after parsing. * called after parsing.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Feature reading options.
*/ */
ol.parser.AsyncStringFeatureParser.prototype.readFeaturesFromStringAsync = ol.parser.AsyncStringFeatureParser.prototype.readFeaturesFromStringAsync =
goog.abstractMethod; goog.abstractMethod;
@@ -85,30 +80,17 @@ ol.parser.AsyncObjectFeatureParser = function() {};
* @param {Object} obj Object representing features. * @param {Object} obj Object representing features.
* @param {function(ol.parser.ReadFeaturesResult)} callback Callback which is * @param {function(ol.parser.ReadFeaturesResult)} callback Callback which is
* called after parsing. * called after parsing.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Feature reading options.
*/ */
ol.parser.AsyncObjectFeatureParser.prototype.readFeaturesFromObjectAsync = ol.parser.AsyncObjectFeatureParser.prototype.readFeaturesFromObjectAsync =
goog.abstractMethod; goog.abstractMethod;
/**
* @typedef {function(ol.Feature, ol.geom.GeometryType):ol.geom.SharedVertices}
*/
ol.parser.ReadFeaturesCallback;
/** /**
* @typedef {{projection: ol.proj.ProjectionLike}} * @typedef {{projection: ol.proj.ProjectionLike}}
*/ */
ol.parser.ReadFeaturesMetadata; ol.parser.ReadFeaturesMetadata;
/**
* @typedef {{callback: ol.parser.ReadFeaturesCallback}}
*/
ol.parser.ReadFeaturesOptions;
/** /**
* @typedef {{features: Array.<ol.Feature>, * @typedef {{features: Array.<ol.Feature>,
* metadata: ol.parser.ReadFeaturesMetadata}} * metadata: ol.parser.ReadFeaturesMetadata}}
+36 -69
View File
@@ -12,9 +12,7 @@ goog.require('ol.geom.MultiPoint');
goog.require('ol.geom.MultiPolygon'); goog.require('ol.geom.MultiPolygon');
goog.require('ol.geom.Point'); goog.require('ol.geom.Point');
goog.require('ol.geom.Polygon'); goog.require('ol.geom.Polygon');
goog.require('ol.geom.SharedVertices');
goog.require('ol.parser.Parser'); goog.require('ol.parser.Parser');
goog.require('ol.parser.ReadFeaturesOptions');
goog.require('ol.parser.ReadFeaturesResult'); goog.require('ol.parser.ReadFeaturesResult');
goog.require('ol.parser.StringFeatureParser'); goog.require('ol.parser.StringFeatureParser');
@@ -60,13 +58,11 @@ ol.parser.GeoJSON.read = function(str) {
/** /**
* Parse a GeoJSON feature collection. * Parse a GeoJSON feature collection.
* @param {string} str GeoJSON feature collection. * @param {string} str GeoJSON feature collection.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Reader options.
* @return {ol.parser.ReadFeaturesResult} Features and metadata. * @return {ol.parser.ReadFeaturesResult} Features and metadata.
*/ */
ol.parser.GeoJSON.prototype.readFeaturesFromString = ol.parser.GeoJSON.prototype.readFeaturesFromString = function(str) {
function(str, opt_options) {
var json = /** @type {GeoJSONFeatureCollection} */ (JSON.parse(str)); var json = /** @type {GeoJSONFeatureCollection} */ (JSON.parse(str));
return this.parseAsFeatureCollection_(json, opt_options); return this.parseAsFeatureCollection_(json);
}; };
@@ -74,43 +70,38 @@ ol.parser.GeoJSON.prototype.readFeaturesFromString =
* Parse a GeoJSON feature collection from decoded JSON. * Parse a GeoJSON feature collection from decoded JSON.
* @param {GeoJSONFeatureCollection} object GeoJSON feature collection decoded * @param {GeoJSONFeatureCollection} object GeoJSON feature collection decoded
* from JSON. * from JSON.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Reader options.
* @return {ol.parser.ReadFeaturesResult} Features and metadata. * @return {ol.parser.ReadFeaturesResult} Features and metadata.
*/ */
ol.parser.GeoJSON.prototype.readFeaturesFromObject = ol.parser.GeoJSON.prototype.readFeaturesFromObject = function(object) {
function(object, opt_options) { return this.parseAsFeatureCollection_(object);
return this.parseAsFeatureCollection_(object, opt_options);
}; };
/** /**
* Parse any GeoJSON object. Note that this method should not be called * Parse any GeoJSON object.
* recursively due to the shared vertex creation.
* *
* @param {GeoJSONObject} json GeoJSON object. * @param {GeoJSONObject} json GeoJSON object.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Reader options.
* @return {ol.Feature|Array.<ol.Feature>| * @return {ol.Feature|Array.<ol.Feature>|
* ol.geom.Geometry|Array.<ol.geom.Geometry>} Parsed geometry or array * ol.geom.Geometry|Array.<ol.geom.Geometry>} Parsed geometry or array
* of geometries. * of geometries.
* @private * @private
*/ */
ol.parser.GeoJSON.prototype.parse_ = function(json, opt_options) { ol.parser.GeoJSON.prototype.parse_ = function(json) {
var result; var result;
if (json.type === 'FeatureCollection') { if (json.type === 'FeatureCollection') {
result = this.parseFeatureCollection_( result = this.parseFeatureCollection_(
/** @type {GeoJSONFeatureCollection} */ (json), opt_options); /** @type {GeoJSONFeatureCollection} */ (json));
} else if (json.type === 'Feature') { } else if (json.type === 'Feature') {
result = this.parseFeature_( result = this.parseFeature_(
/** @type {GeoJSONFeature} */ (json), opt_options); /** @type {GeoJSONFeature} */ (json));
} else if (json.type === 'GeometryCollection') { } else if (json.type === 'GeometryCollection') {
result = this.parseGeometryCollection_( result = this.parseGeometryCollection_(
/** @type {GeoJSONGeometryCollection} */ (json), opt_options); /** @type {GeoJSONGeometryCollection} */ (json));
} else { } else {
// we've been called with a geometry or an unknown object // we've been called with a geometry or an unknown object
// create a feature to get shared vertices handling // create a feature to get shared vertices handling
var feature = this.parseFeature_( var feature = this.parseFeature_(
/** @type {GeoJSONFeature} */ ({type: 'Feature', geometry: json}), /** @type {GeoJSONFeature} */ ({type: 'Feature', geometry: json}));
opt_options);
result = feature.getGeometry(); result = feature.getGeometry();
} }
return result; return result;
@@ -119,14 +110,12 @@ ol.parser.GeoJSON.prototype.parse_ = function(json, opt_options) {
/** /**
* @param {GeoJSONObject} json GeoJSON object. * @param {GeoJSONObject} json GeoJSON object.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Reader options.
* @return {ol.parser.ReadFeaturesResult} Parsed object coerced into array of * @return {ol.parser.ReadFeaturesResult} Parsed object coerced into array of
* features. * features.
* @private * @private
*/ */
ol.parser.GeoJSON.prototype.parseAsFeatureCollection_ = function(json, ol.parser.GeoJSON.prototype.parseAsFeatureCollection_ = function(json) {
opt_options) { var obj = this.parse_(json);
var obj = this.parse_(json, opt_options);
var features = []; var features = [];
var feature; var feature;
if (obj instanceof ol.Feature) { if (obj instanceof ol.Feature) {
@@ -164,45 +153,36 @@ ol.parser.GeoJSON.prototype.parseAsFeatureCollection_ = function(json,
/** /**
* @param {GeoJSONFeature} json GeoJSON feature. * @param {GeoJSONFeature} json GeoJSON feature.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Read options.
* @return {ol.Feature} Parsed feature. * @return {ol.Feature} Parsed feature.
* @private * @private
*/ */
ol.parser.GeoJSON.prototype.parseFeature_ = function(json, opt_options) { ol.parser.GeoJSON.prototype.parseFeature_ = function(json) {
var geomJson = json.geometry, var geomJson = json.geometry,
geometry = null, geometry = null;
options = opt_options || {};
var feature = new ol.Feature(json.properties); var feature = new ol.Feature(json.properties);
if (goog.isDef(json.id)) { if (goog.isDef(json.id)) {
feature.setId(json.id); feature.setId(json.id);
} }
if (geomJson) { if (geomJson) {
var type = geomJson.type; var type = geomJson.type;
var callback = options.callback;
var sharedVertices;
if (callback) {
goog.asserts.assert(type in ol.parser.GeoJSON.GeometryType,
'Bad geometry type: ' + type);
sharedVertices = callback(feature, ol.parser.GeoJSON.GeometryType[type]);
}
switch (type) { switch (type) {
case 'Point': case 'Point':
geometry = this.parsePoint_(geomJson, sharedVertices); geometry = this.parsePoint_(geomJson);
break; break;
case 'LineString': case 'LineString':
geometry = this.parseLineString_(geomJson, sharedVertices); geometry = this.parseLineString_(geomJson);
break; break;
case 'Polygon': case 'Polygon':
geometry = this.parsePolygon_(geomJson, sharedVertices); geometry = this.parsePolygon_(geomJson);
break; break;
case 'MultiPoint': case 'MultiPoint':
geometry = this.parseMultiPoint_(geomJson, sharedVertices); geometry = this.parseMultiPoint_(geomJson);
break; break;
case 'MultiLineString': case 'MultiLineString':
geometry = this.parseMultiLineString_(geomJson, sharedVertices); geometry = this.parseMultiLineString_(geomJson);
break; break;
case 'MultiPolygon': case 'MultiPolygon':
geometry = this.parseMultiPolygon_(geomJson, sharedVertices); geometry = this.parseMultiPolygon_(geomJson);
break; break;
default: default:
throw new Error('Bad geometry type: ' + type); throw new Error('Bad geometry type: ' + type);
@@ -215,20 +195,17 @@ ol.parser.GeoJSON.prototype.parseFeature_ = function(json, opt_options) {
/** /**
* @param {GeoJSONFeatureCollection} json GeoJSON feature collection. * @param {GeoJSONFeatureCollection} json GeoJSON feature collection.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Read options.
* @return {Array.<ol.Feature>} Parsed array of features. * @return {Array.<ol.Feature>} Parsed array of features.
* @private * @private
*/ */
ol.parser.GeoJSON.prototype.parseFeatureCollection_ = function( ol.parser.GeoJSON.prototype.parseFeatureCollection_ = function(json) {
json, opt_options) {
var features = json.features, var features = json.features,
len = features.length, len = features.length,
result = new Array(len), result = new Array(len),
i; i;
for (i = 0; i < len; ++i) { for (i = 0; i < len; ++i) {
result[i] = this.parseFeature_( result[i] = this.parseFeature_(/** @type {GeoJSONFeature} */ (features[i]));
/** @type {GeoJSONFeature} */ (features[i]), opt_options);
} }
return result; return result;
}; };
@@ -236,20 +213,17 @@ ol.parser.GeoJSON.prototype.parseFeatureCollection_ = function(
/** /**
* @param {GeoJSONGeometryCollection} json GeoJSON geometry collection. * @param {GeoJSONGeometryCollection} json GeoJSON geometry collection.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Read options.
* @return {Array.<ol.geom.Geometry>} Parsed array of geometries. * @return {Array.<ol.geom.Geometry>} Parsed array of geometries.
* @private * @private
*/ */
ol.parser.GeoJSON.prototype.parseGeometryCollection_ = function(json, ol.parser.GeoJSON.prototype.parseGeometryCollection_ = function(json) {
opt_options) {
var geometries = json.geometries, var geometries = json.geometries,
len = geometries.length, len = geometries.length,
result = new Array(len), result = new Array(len),
i; i;
for (i = 0; i < len; ++i) { for (i = 0; i < len; ++i) {
result[i] = this.parse_(/** @type {GeoJSONGeometry} */ (geometries[i]), result[i] = this.parse_(/** @type {GeoJSONGeometry} */ (geometries[i]));
opt_options);
} }
return result; return result;
}; };
@@ -257,68 +231,61 @@ ol.parser.GeoJSON.prototype.parseGeometryCollection_ = function(json,
/** /**
* @param {GeoJSONGeometry} json GeoJSON linestring. * @param {GeoJSONGeometry} json GeoJSON linestring.
* @param {ol.geom.SharedVertices=} opt_vertices Shared vertices.
* @return {ol.geom.LineString} Parsed linestring. * @return {ol.geom.LineString} Parsed linestring.
* @private * @private
*/ */
ol.parser.GeoJSON.prototype.parseLineString_ = function(json, opt_vertices) { ol.parser.GeoJSON.prototype.parseLineString_ = function(json) {
return new ol.geom.LineString(json.coordinates, opt_vertices); return new ol.geom.LineString(json.coordinates);
}; };
/** /**
* @param {GeoJSONGeometry} json GeoJSON multi-linestring. * @param {GeoJSONGeometry} json GeoJSON multi-linestring.
* @param {ol.geom.SharedVertices=} opt_vertices Shared vertices.
* @return {ol.geom.MultiLineString} Parsed multi-linestring. * @return {ol.geom.MultiLineString} Parsed multi-linestring.
* @private * @private
*/ */
ol.parser.GeoJSON.prototype.parseMultiLineString_ = function( ol.parser.GeoJSON.prototype.parseMultiLineString_ = function(json) {
json, opt_vertices) { return new ol.geom.MultiLineString(json.coordinates);
return new ol.geom.MultiLineString(json.coordinates, opt_vertices);
}; };
/** /**
* @param {GeoJSONGeometry} json GeoJSON multi-point. * @param {GeoJSONGeometry} json GeoJSON multi-point.
* @param {ol.geom.SharedVertices=} opt_vertices Shared vertices.
* @return {ol.geom.MultiPoint} Parsed multi-point. * @return {ol.geom.MultiPoint} Parsed multi-point.
* @private * @private
*/ */
ol.parser.GeoJSON.prototype.parseMultiPoint_ = function(json, opt_vertices) { ol.parser.GeoJSON.prototype.parseMultiPoint_ = function(json) {
return new ol.geom.MultiPoint(json.coordinates, opt_vertices); return new ol.geom.MultiPoint(json.coordinates);
}; };
/** /**
* @param {GeoJSONGeometry} json GeoJSON multi-polygon. * @param {GeoJSONGeometry} json GeoJSON multi-polygon.
* @param {ol.geom.SharedVertices=} opt_vertices Shared vertices.
* @return {ol.geom.MultiPolygon} Parsed multi-polygon. * @return {ol.geom.MultiPolygon} Parsed multi-polygon.
* @private * @private
*/ */
ol.parser.GeoJSON.prototype.parseMultiPolygon_ = function(json, opt_vertices) { ol.parser.GeoJSON.prototype.parseMultiPolygon_ = function(json) {
return new ol.geom.MultiPolygon(json.coordinates, opt_vertices); return new ol.geom.MultiPolygon(json.coordinates);
}; };
/** /**
* @param {GeoJSONGeometry} json GeoJSON point. * @param {GeoJSONGeometry} json GeoJSON point.
* @param {ol.geom.SharedVertices=} opt_vertices Shared vertices.
* @return {ol.geom.Point} Parsed point. * @return {ol.geom.Point} Parsed point.
* @private * @private
*/ */
ol.parser.GeoJSON.prototype.parsePoint_ = function(json, opt_vertices) { ol.parser.GeoJSON.prototype.parsePoint_ = function(json) {
return new ol.geom.Point(json.coordinates, opt_vertices); return new ol.geom.Point(json.coordinates);
}; };
/** /**
* @param {GeoJSONGeometry} json GeoJSON polygon. * @param {GeoJSONGeometry} json GeoJSON polygon.
* @param {ol.geom.SharedVertices=} opt_vertices Shared vertices.
* @return {ol.geom.Polygon} Parsed polygon. * @return {ol.geom.Polygon} Parsed polygon.
* @private * @private
*/ */
ol.parser.GeoJSON.prototype.parsePolygon_ = function(json, opt_vertices) { ol.parser.GeoJSON.prototype.parsePolygon_ = function(json) {
return new ol.geom.Polygon(json.coordinates, opt_vertices); return new ol.geom.Polygon(json.coordinates);
}; };
+5 -30
View File
@@ -9,7 +9,6 @@ goog.require('ol.geom.Point');
goog.require('ol.geom.Polygon'); goog.require('ol.geom.Polygon');
goog.require('ol.parser.DomFeatureParser'); goog.require('ol.parser.DomFeatureParser');
goog.require('ol.parser.ObjectFeatureParser'); goog.require('ol.parser.ObjectFeatureParser');
goog.require('ol.parser.ReadFeaturesOptions');
goog.require('ol.parser.StringFeatureParser'); goog.require('ol.parser.StringFeatureParser');
goog.require('ol.parser.XML'); goog.require('ol.parser.XML');
@@ -58,14 +57,7 @@ ol.parser.GPX = function(opt_options) {
parseFloat(node.getAttribute('lat'))]; parseFloat(node.getAttribute('lat'))];
this.readChildNodes(node, properties); this.readChildNodes(node, properties);
var feature = new ol.Feature(properties); var feature = new ol.Feature(properties);
var sharedVertices; var geometry = new ol.geom.Point(coordinates);
if (this.readFeaturesOptions_) {
var callback = this.readFeaturesOptions_.callback;
if (callback) {
sharedVertices = callback(feature, ol.geom.GeometryType.POINT);
}
}
var geometry = new ol.geom.Point(coordinates, sharedVertices);
feature.setGeometry(geometry); feature.setGeometry(geometry);
obj.features.push(feature); obj.features.push(feature);
} }
@@ -82,15 +74,7 @@ ol.parser.GPX = function(opt_options) {
}; };
this.readChildNodes(node, container); this.readChildNodes(node, container);
var feature = new ol.Feature(container.properties); var feature = new ol.Feature(container.properties);
var sharedVertices; var geometry = new ol.geom.LineString(container.geometry.coordinates);
if (this.readFeaturesOptions_) {
var callback = this.readFeaturesOptions_.callback;
if (callback) {
sharedVertices = callback(feature, type);
}
}
var geometry = new ol.geom.LineString(container.geometry.coordinates,
sharedVertices);
feature.setGeometry(geometry); feature.setGeometry(geometry);
obj.features.push(feature); obj.features.push(feature);
} }
@@ -255,12 +239,9 @@ ol.parser.GPX.prototype.read = function(data) {
/** /**
* Parse a GPX document provided as a string. * Parse a GPX document provided as a string.
* @param {string} str GPX document. * @param {string} str GPX document.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Reader options.
* @return {ol.parser.ReadFeaturesResult} Features and metadata. * @return {ol.parser.ReadFeaturesResult} Features and metadata.
*/ */
ol.parser.GPX.prototype.readFeaturesFromString = ol.parser.GPX.prototype.readFeaturesFromString = function(str) {
function(str, opt_options) {
this.readFeaturesOptions_ = opt_options;
return this.read(str); return this.read(str);
}; };
@@ -268,24 +249,18 @@ ol.parser.GPX.prototype.readFeaturesFromString =
/** /**
* Parse a GPX document provided as a DOM structure. * Parse a GPX document provided as a DOM structure.
* @param {Element|Document} node Document or element node. * @param {Element|Document} node Document or element node.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Feature reading options.
* @return {ol.parser.ReadFeaturesResult} Features and metadata. * @return {ol.parser.ReadFeaturesResult} Features and metadata.
*/ */
ol.parser.GPX.prototype.readFeaturesFromNode = ol.parser.GPX.prototype.readFeaturesFromNode = function(node) {
function(node, opt_options) {
this.readFeaturesOptions_ = opt_options;
return this.read(node); return this.read(node);
}; };
/** /**
* @param {Object} obj Object representing features. * @param {Object} obj Object representing features.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Feature reading options.
* @return {ol.parser.ReadFeaturesResult} Features and metadata. * @return {ol.parser.ReadFeaturesResult} Features and metadata.
*/ */
ol.parser.GPX.prototype.readFeaturesFromObject = ol.parser.GPX.prototype.readFeaturesFromObject = function(obj) {
function(obj, opt_options) {
this.readFeaturesOptions_ = opt_options;
return this.read(obj); return this.read(obj);
}; };
+17 -58
View File
@@ -20,11 +20,9 @@ goog.require('ol.geom.MultiPoint');
goog.require('ol.geom.MultiPolygon'); goog.require('ol.geom.MultiPolygon');
goog.require('ol.geom.Point'); goog.require('ol.geom.Point');
goog.require('ol.geom.Polygon'); goog.require('ol.geom.Polygon');
goog.require('ol.geom.SharedVertices');
goog.require('ol.parser.AsyncObjectFeatureParser'); goog.require('ol.parser.AsyncObjectFeatureParser');
goog.require('ol.parser.AsyncStringFeatureParser'); goog.require('ol.parser.AsyncStringFeatureParser');
goog.require('ol.parser.DomFeatureParser'); goog.require('ol.parser.DomFeatureParser');
goog.require('ol.parser.ReadFeaturesOptions');
goog.require('ol.parser.StringFeatureParser'); goog.require('ol.parser.StringFeatureParser');
goog.require('ol.parser.XML'); goog.require('ol.parser.XML');
goog.require('ol.style.Fill'); goog.require('ol.style.Fill');
@@ -54,8 +52,6 @@ ol.parser.KML = function(opt_options) {
options.extractStyles : false; options.extractStyles : false;
this.schemaLocation = 'http://www.opengis.net/kml/2.2 ' + this.schemaLocation = 'http://www.opengis.net/kml/2.2 ' +
'http://schemas.opengis.net/kml/2.2.0/ogckml22.xsd'; 'http://schemas.opengis.net/kml/2.2.0/ogckml22.xsd';
// TODO re-evaluate once shared structures support 3D
this.dimension = goog.isDef(options.dimension) ? options.dimension : 3;
this.maxDepth = goog.isDef(options.maxDepth) ? options.maxDepth : 0; this.maxDepth = goog.isDef(options.maxDepth) ? options.maxDepth : 0;
this.trackAttributes = goog.isDef(options.trackAttributes) ? this.trackAttributes = goog.isDef(options.trackAttributes) ?
options.trackAttributes : null; options.trackAttributes : null;
@@ -104,7 +100,6 @@ ol.parser.KML = function(opt_options) {
}, },
'Placemark': function(node, obj) { 'Placemark': function(node, obj) {
var container = {properties: {}}; var container = {properties: {}};
var sharedVertices, callback;
var id = node.getAttribute('id'); var id = node.getAttribute('id');
this.readChildNodes(node, container); this.readChildNodes(node, container);
if (goog.isDef(container.track)) { if (goog.isDef(container.track)) {
@@ -132,15 +127,7 @@ ol.parser.KML = function(opt_options) {
} }
var geom = track.points[i]; var geom = track.points[i];
if (geom) { if (geom) {
sharedVertices = undefined; var geometry = this.createGeometry_({geometry: geom});
if (this.readFeaturesOptions_) {
callback = this.readFeaturesOptions_.callback;
if (callback) {
sharedVertices = callback(feature, geom.type);
}
}
var geometry = this.createGeometry_({geometry: geom},
sharedVertices);
if (goog.isDef(geometry)) { if (goog.isDef(geometry)) {
feature.setGeometry(geometry); feature.setGeometry(geometry);
} }
@@ -159,14 +146,7 @@ ol.parser.KML = function(opt_options) {
feature.setId(id); feature.setId(id);
} }
if (container.geometry) { if (container.geometry) {
sharedVertices = undefined; geometry = this.createGeometry_(container);
if (this.readFeaturesOptions_) {
callback = this.readFeaturesOptions_.callback;
if (callback) {
sharedVertices = callback(feature, container.geometry.type);
}
}
geometry = this.createGeometry_(container, sharedVertices);
if (goog.isDef(geometry)) { if (goog.isDef(geometry)) {
feature.setGeometry(geometry); feature.setGeometry(geometry);
} }
@@ -281,8 +261,7 @@ ol.parser.KML = function(opt_options) {
for (var i = 0, ii = coords.length; i < ii; i++) { for (var i = 0, ii = coords.length; i < ii; i++) {
var array = coords[i].replace(reg.removeSpace, '').split(','); var array = coords[i].replace(reg.removeSpace, '').split(',');
var pair = []; var pair = [];
var jj = Math.min(array.length, this.dimension); for (var j = 0, jj = array.length; j < jj; j++) {
for (var j = 0; j < jj; j++) {
pair.push(parseFloat(array[j])); pair.push(parseFloat(array[j]));
} }
coordArray.push(pair); coordArray.push(pair);
@@ -562,7 +541,7 @@ ol.parser.KML = function(opt_options) {
'coord': function(node, container) { 'coord': function(node, container) {
var str = this.getChildValue(node); var str = this.getChildValue(node);
var coords = str.replace(this.regExes.trimSpace, '').split(/\s+/); var coords = str.replace(this.regExes.trimSpace, '').split(/\s+/);
for (var i = 0, ii = this.dimension; i < ii; ++i) { for (var i = 0, ii = coords.length; i < ii; ++i) {
coords[i] = parseFloat(coords[i]); coords[i] = parseFloat(coords[i]);
} }
var point = { var point = {
@@ -850,11 +829,8 @@ goog.inherits(ol.parser.KML, ol.parser.XML);
* @param {Object} obj Object representing features. * @param {Object} obj Object representing features.
* @param {function(ol.parser.ReadFeaturesResult)} callback Callback which is * @param {function(ol.parser.ReadFeaturesResult)} callback Callback which is
* called after parsing. * called after parsing.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Feature reading options.
*/ */
ol.parser.KML.prototype.readFeaturesFromObjectAsync = ol.parser.KML.prototype.readFeaturesFromObjectAsync = function(obj, callback) {
function(obj, callback, opt_options) {
this.readFeaturesOptions_ = opt_options;
this.read(obj, callback); this.read(obj, callback);
}; };
@@ -863,11 +839,8 @@ ol.parser.KML.prototype.readFeaturesFromObjectAsync =
* @param {string} str String data. * @param {string} str String data.
* @param {function(ol.parser.ReadFeaturesResult)} * @param {function(ol.parser.ReadFeaturesResult)}
* callback Callback which is called after parsing. * callback Callback which is called after parsing.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Feature reading options.
*/ */
ol.parser.KML.prototype.readFeaturesFromStringAsync = ol.parser.KML.prototype.readFeaturesFromStringAsync = function(str, callback) {
function(str, callback, opt_options) {
this.readFeaturesOptions_ = opt_options;
this.read(str, callback); this.read(str, callback);
}; };
@@ -875,12 +848,9 @@ ol.parser.KML.prototype.readFeaturesFromStringAsync =
/** /**
* Parse a KML document provided as a string. * Parse a KML document provided as a string.
* @param {string} str KML document. * @param {string} str KML document.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Reader options.
* @return {ol.parser.ReadFeaturesResult} Features and metadata. * @return {ol.parser.ReadFeaturesResult} Features and metadata.
*/ */
ol.parser.KML.prototype.readFeaturesFromString = ol.parser.KML.prototype.readFeaturesFromString = function(str) {
function(str, opt_options) {
this.readFeaturesOptions_ = opt_options;
return /** @type {ol.parser.ReadFeaturesResult} */ (this.read(str)); return /** @type {ol.parser.ReadFeaturesResult} */ (this.read(str));
}; };
@@ -888,24 +858,18 @@ ol.parser.KML.prototype.readFeaturesFromString =
/** /**
* Parse a KML document provided as a DOM structure. * Parse a KML document provided as a DOM structure.
* @param {Element|Document} node Document or element node. * @param {Element|Document} node Document or element node.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Feature reading options.
* @return {ol.parser.ReadFeaturesResult} Features and metadata. * @return {ol.parser.ReadFeaturesResult} Features and metadata.
*/ */
ol.parser.KML.prototype.readFeaturesFromNode = ol.parser.KML.prototype.readFeaturesFromNode = function(node) {
function(node, opt_options) {
this.readFeaturesOptions_ = opt_options;
return /** @type {ol.parser.ReadFeaturesResult} */ (this.read(node)); return /** @type {ol.parser.ReadFeaturesResult} */ (this.read(node));
}; };
/** /**
* @param {Object} obj Object representing features. * @param {Object} obj Object representing features.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Feature reading options.
* @return {ol.parser.ReadFeaturesResult} Features and metadata. * @return {ol.parser.ReadFeaturesResult} Features and metadata.
*/ */
ol.parser.KML.prototype.readFeaturesFromObject = ol.parser.KML.prototype.readFeaturesFromObject = function(obj) {
function(obj, opt_options) {
this.readFeaturesOptions_ = opt_options;
return /** @type {ol.parser.ReadFeaturesResult} */ (this.read(obj)); return /** @type {ol.parser.ReadFeaturesResult} */ (this.read(obj));
}; };
@@ -1028,52 +992,47 @@ ol.parser.KML.prototype.applyStyle_ = function(feature, styles,
/** /**
* @private * @private
* @param {Object} container Geometry container. * @param {Object} container Geometry container.
* @param {ol.geom.SharedVertices=} opt_vertices Shared vertices.
* @return {ol.geom.Geometry} The geometry created. * @return {ol.geom.Geometry} The geometry created.
*/ */
ol.parser.KML.prototype.createGeometry_ = function(container, ol.parser.KML.prototype.createGeometry_ = function(container) {
opt_vertices) {
var geometry = null, coordinates, i, ii; var geometry = null, coordinates, i, ii;
switch (container.geometry.type) { switch (container.geometry.type) {
case ol.geom.GeometryType.POINT: case ol.geom.GeometryType.POINT:
geometry = new ol.geom.Point(container.geometry.coordinates, geometry = new ol.geom.Point(container.geometry.coordinates);
opt_vertices);
break; break;
case ol.geom.GeometryType.LINESTRING: case ol.geom.GeometryType.LINESTRING:
geometry = new ol.geom.LineString(container.geometry.coordinates, geometry = new ol.geom.LineString(container.geometry.coordinates);
opt_vertices);
break; break;
case ol.geom.GeometryType.POLYGON: case ol.geom.GeometryType.POLYGON:
geometry = new ol.geom.Polygon(container.geometry.coordinates, geometry = new ol.geom.Polygon(container.geometry.coordinates);
opt_vertices);
break; break;
case ol.geom.GeometryType.MULTIPOINT: case ol.geom.GeometryType.MULTIPOINT:
coordinates = []; coordinates = [];
for (i = 0, ii = container.geometry.parts.length; i < ii; i++) { for (i = 0, ii = container.geometry.parts.length; i < ii; i++) {
coordinates.push(container.geometry.parts[i].coordinates); coordinates.push(container.geometry.parts[i].coordinates);
} }
geometry = new ol.geom.MultiPoint(coordinates, opt_vertices); geometry = new ol.geom.MultiPoint(coordinates);
break; break;
case ol.geom.GeometryType.MULTILINESTRING: case ol.geom.GeometryType.MULTILINESTRING:
coordinates = []; coordinates = [];
for (i = 0, ii = container.geometry.parts.length; i < ii; i++) { for (i = 0, ii = container.geometry.parts.length; i < ii; i++) {
coordinates.push(container.geometry.parts[i].coordinates); coordinates.push(container.geometry.parts[i].coordinates);
} }
geometry = new ol.geom.MultiLineString(coordinates, opt_vertices); geometry = new ol.geom.MultiLineString(coordinates);
break; break;
case ol.geom.GeometryType.MULTIPOLYGON: case ol.geom.GeometryType.MULTIPOLYGON:
coordinates = []; coordinates = [];
for (i = 0, ii = container.geometry.parts.length; i < ii; i++) { for (i = 0, ii = container.geometry.parts.length; i < ii; i++) {
coordinates.push(container.geometry.parts[i].coordinates); coordinates.push(container.geometry.parts[i].coordinates);
} }
geometry = new ol.geom.MultiPolygon(coordinates, opt_vertices); geometry = new ol.geom.MultiPolygon(coordinates);
break; break;
case ol.geom.GeometryType.GEOMETRYCOLLECTION: case ol.geom.GeometryType.GEOMETRYCOLLECTION:
var geometries = []; var geometries = [];
for (i = 0, ii = container.geometry.parts.length; i < ii; i++) { for (i = 0, ii = container.geometry.parts.length; i < ii; i++) {
geometries.push(this.createGeometry_({ geometries.push(this.createGeometry_({
geometry: container.geometry.parts[i] geometry: container.geometry.parts[i]
}, opt_vertices)); }));
} }
geometry = new ol.geom.GeometryCollection(geometries); geometry = new ol.geom.GeometryCollection(geometries);
break; break;
+11 -28
View File
@@ -303,15 +303,7 @@ ol.parser.ogc.GML = function(opt_options) {
var feature = new ol.Feature(container.properties); var feature = new ol.Feature(container.properties);
var geom = container.geometry; var geom = container.geometry;
if (geom) { if (geom) {
var sharedVertices = undefined; var geometry = this.createGeometry({geometry: geom});
if (this.readFeaturesOptions_) {
var callback = this.readFeaturesOptions_.callback;
if (callback) {
sharedVertices = callback(feature, geom.type);
}
}
var geometry = this.createGeometry({geometry: geom},
sharedVertices);
if (goog.isDef(geometry)) { if (goog.isDef(geometry)) {
feature.setGeometry(geometry); feature.setGeometry(geometry);
} }
@@ -551,57 +543,51 @@ ol.parser.ogc.GML.prototype.readNode = function(node, obj, opt_first) {
/** /**
* @param {Object} container Geometry container. * @param {Object} container Geometry container.
* @param {ol.geom.SharedVertices=} opt_vertices Shared vertices.
* @return {ol.geom.Geometry} The geometry created. * @return {ol.geom.Geometry} The geometry created.
*/ */
// TODO use a mixin since this is also used in the KML parser // TODO use a mixin since this is also used in the KML parser
ol.parser.ogc.GML.prototype.createGeometry = function(container, ol.parser.ogc.GML.prototype.createGeometry = function(container) {
opt_vertices) {
var geometry = null, coordinates, i, ii; var geometry = null, coordinates, i, ii;
switch (container.geometry.type) { switch (container.geometry.type) {
case ol.geom.GeometryType.POINT: case ol.geom.GeometryType.POINT:
geometry = new ol.geom.Point(container.geometry.coordinates, geometry = new ol.geom.Point(container.geometry.coordinates);
opt_vertices);
break; break;
case ol.geom.GeometryType.LINEARRING: case ol.geom.GeometryType.LINEARRING:
geometry = new ol.geom.LinearRing(container.geometry.coordinates, geometry = new ol.geom.LinearRing(container.geometry.coordinates);
opt_vertices);
break; break;
case ol.geom.GeometryType.LINESTRING: case ol.geom.GeometryType.LINESTRING:
geometry = new ol.geom.LineString(container.geometry.coordinates, geometry = new ol.geom.LineString(container.geometry.coordinates);
opt_vertices);
break; break;
case ol.geom.GeometryType.POLYGON: case ol.geom.GeometryType.POLYGON:
geometry = new ol.geom.Polygon(container.geometry.coordinates, geometry = new ol.geom.Polygon(container.geometry.coordinates);
opt_vertices);
break; break;
case ol.geom.GeometryType.MULTIPOINT: case ol.geom.GeometryType.MULTIPOINT:
coordinates = []; coordinates = [];
for (i = 0, ii = container.geometry.parts.length; i < ii; i++) { for (i = 0, ii = container.geometry.parts.length; i < ii; i++) {
coordinates.push(container.geometry.parts[i].coordinates); coordinates.push(container.geometry.parts[i].coordinates);
} }
geometry = new ol.geom.MultiPoint(coordinates, opt_vertices); geometry = new ol.geom.MultiPoint(coordinates);
break; break;
case ol.geom.GeometryType.MULTILINESTRING: case ol.geom.GeometryType.MULTILINESTRING:
coordinates = []; coordinates = [];
for (i = 0, ii = container.geometry.parts.length; i < ii; i++) { for (i = 0, ii = container.geometry.parts.length; i < ii; i++) {
coordinates.push(container.geometry.parts[i].coordinates); coordinates.push(container.geometry.parts[i].coordinates);
} }
geometry = new ol.geom.MultiLineString(coordinates, opt_vertices); geometry = new ol.geom.MultiLineString(coordinates);
break; break;
case ol.geom.GeometryType.MULTIPOLYGON: case ol.geom.GeometryType.MULTIPOLYGON:
coordinates = []; coordinates = [];
for (i = 0, ii = container.geometry.parts.length; i < ii; i++) { for (i = 0, ii = container.geometry.parts.length; i < ii; i++) {
coordinates.push(container.geometry.parts[i].coordinates); coordinates.push(container.geometry.parts[i].coordinates);
} }
geometry = new ol.geom.MultiPolygon(coordinates, opt_vertices); geometry = new ol.geom.MultiPolygon(coordinates);
break; break;
case ol.geom.GeometryType.GEOMETRYCOLLECTION: case ol.geom.GeometryType.GEOMETRYCOLLECTION:
var geometries = []; var geometries = [];
for (i = 0, ii = container.geometry.parts.length; i < ii; i++) { for (i = 0, ii = container.geometry.parts.length; i < ii; i++) {
geometries.push(this.createGeometry({ geometries.push(this.createGeometry({
geometry: container.geometry.parts[i] geometry: container.geometry.parts[i]
}, opt_vertices)); }));
} }
geometry = new ol.geom.GeometryCollection(geometries); geometry = new ol.geom.GeometryCollection(geometries);
break; break;
@@ -615,12 +601,9 @@ ol.parser.ogc.GML.prototype.createGeometry = function(container,
/** /**
* Parse a GML document provided as a string. * Parse a GML document provided as a string.
* @param {string} str GML document. * @param {string} str GML document.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Reader options.
* @return {ol.parser.ReadFeaturesResult} Features and metadata. * @return {ol.parser.ReadFeaturesResult} Features and metadata.
*/ */
ol.parser.ogc.GML.prototype.readFeaturesFromString = ol.parser.ogc.GML.prototype.readFeaturesFromString = function(str) {
function(str, opt_options) {
this.readFeaturesOptions_ = opt_options;
return this.read(str); return this.read(str);
}; };
+39 -101
View File
@@ -3,7 +3,6 @@ goog.provide('ol.parser.TopoJSON');
goog.require('ol.Coordinate'); goog.require('ol.Coordinate');
goog.require('ol.CoordinateArray'); goog.require('ol.CoordinateArray');
goog.require('ol.Feature'); goog.require('ol.Feature');
goog.require('ol.geom.GeometryType');
goog.require('ol.geom.LineString'); goog.require('ol.geom.LineString');
goog.require('ol.geom.MultiLineString'); goog.require('ol.geom.MultiLineString');
goog.require('ol.geom.MultiPoint'); goog.require('ol.geom.MultiPoint');
@@ -11,7 +10,6 @@ goog.require('ol.geom.MultiPolygon');
goog.require('ol.geom.Point'); goog.require('ol.geom.Point');
goog.require('ol.geom.Polygon'); goog.require('ol.geom.Polygon');
goog.require('ol.parser.Parser'); goog.require('ol.parser.Parser');
goog.require('ol.parser.ReadFeaturesOptions');
goog.require('ol.parser.StringFeatureParser'); goog.require('ol.parser.StringFeatureParser');
@@ -23,18 +21,7 @@ goog.require('ol.parser.StringFeatureParser');
* @implements {ol.parser.StringFeatureParser} * @implements {ol.parser.StringFeatureParser}
* @extends {ol.parser.Parser} * @extends {ol.parser.Parser}
*/ */
ol.parser.TopoJSON = function() { ol.parser.TopoJSON = function() {};
/**
* Common feature for all shared vertex creation.
* // TODO: make feature optional in shared vertex callback
*
* @type {ol.Feature}
* @private
*/
this.feature_ = new ol.Feature();
};
goog.inherits(ol.parser.TopoJSON, ol.parser.Parser); goog.inherits(ol.parser.TopoJSON, ol.parser.Parser);
goog.addSingletonGetter(ol.parser.TopoJSON); goog.addSingletonGetter(ol.parser.TopoJSON);
@@ -65,6 +52,10 @@ ol.parser.TopoJSON.prototype.concatenateArcs_ = function(indices, arcs) {
} }
coordinates.push.apply(coordinates, arc); coordinates.push.apply(coordinates, arc);
} }
// provide fresh copies of coordinate arrays
for (var j = 0, jj = coordinates.length; j < jj; ++j) {
coordinates[j] = coordinates[j].slice();
}
return coordinates; return coordinates;
}; };
@@ -84,17 +75,17 @@ ol.parser.TopoJSON.prototype.read = function(str) {
* Create features from a TopoJSON topology string. * Create features from a TopoJSON topology string.
* *
* @param {string} str TopoJSON topology string. * @param {string} str TopoJSON topology string.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Reader options.
* @return {ol.parser.ReadFeaturesResult} Features and metadata. * @return {ol.parser.ReadFeaturesResult} Features and metadata.
*/ */
ol.parser.TopoJSON.prototype.readFeaturesFromString = ol.parser.TopoJSON.prototype.readFeaturesFromString = function(str) {
function(str, opt_options) {
var topology = /** @type {TopoJSONTopology} */ (JSON.parse(str)); var topology = /** @type {TopoJSONTopology} */ (JSON.parse(str));
if (topology.type !== 'Topology') { if (topology.type !== 'Topology') {
throw new Error('Not a "Topology" type object'); throw new Error('Not a "Topology" type object');
} }
return {features: this.readFeaturesFromTopology_(topology, opt_options), return {
metadata: {projection: 'EPSG:4326'}}; features: this.readFeaturesFromTopology_(topology),
metadata: {projection: 'EPSG:4326'}
};
}; };
@@ -102,16 +93,16 @@ ol.parser.TopoJSON.prototype.readFeaturesFromString =
* Create features from a TopoJSON topology object. * Create features from a TopoJSON topology object.
* *
* @param {TopoJSONTopology} topology TopoJSON topology object. * @param {TopoJSONTopology} topology TopoJSON topology object.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Reader options.
* @return {ol.parser.ReadFeaturesResult} Features and metadata. * @return {ol.parser.ReadFeaturesResult} Features and metadata.
*/ */
ol.parser.TopoJSON.prototype.readFeaturesFromObject = ol.parser.TopoJSON.prototype.readFeaturesFromObject = function(topology) {
function(topology, opt_options) {
if (topology.type !== 'Topology') { if (topology.type !== 'Topology') {
throw new Error('Not a "Topology" type object'); throw new Error('Not a "Topology" type object');
} }
return {features: this.readFeaturesFromTopology_(topology, opt_options), return {
metadata: {projection: 'EPSG:4326'}}; features: this.readFeaturesFromTopology_(topology),
metadata: {projection: 'EPSG:4326'}
};
}; };
@@ -122,32 +113,30 @@ ol.parser.TopoJSON.prototype.readFeaturesFromObject =
* @param {Array.<ol.CoordinateArray>} arcs Array of arcs. * @param {Array.<ol.CoordinateArray>} arcs Array of arcs.
* @param {Array.<number>} scale Scale for each dimension. * @param {Array.<number>} scale Scale for each dimension.
* @param {Array.<number>} translate Translation for each dimension. * @param {Array.<number>} translate Translation for each dimension.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Reader options.
* @return {ol.Feature} Feature. * @return {ol.Feature} Feature.
* @private * @private
*/ */
ol.parser.TopoJSON.prototype.readFeatureFromGeometry_ = function(object, arcs, ol.parser.TopoJSON.prototype.readFeatureFromGeometry_ = function(object, arcs,
scale, translate, opt_options) { scale, translate) {
var geometry; var geometry;
var type = object.type; var type = object.type;
if (type === 'Point') { if (type === 'Point') {
geometry = this.readPoint_(/** @type {TopoJSONPoint} */ (object), scale, geometry = this.readPoint_(/** @type {TopoJSONPoint} */ (object), scale,
translate, opt_options); translate);
} else if (type === 'LineString') { } else if (type === 'LineString') {
geometry = this.readLineString_(/** @type {TopoJSONLineString} */ (object), geometry = this.readLineString_(/** @type {TopoJSONLineString} */ (object),
arcs, opt_options); arcs);
} else if (type === 'Polygon') { } else if (type === 'Polygon') {
geometry = this.readPolygon_(/** @type {TopoJSONPolygon} */ (object), arcs, geometry = this.readPolygon_(/** @type {TopoJSONPolygon} */ (object), arcs);
opt_options);
} else if (type === 'MultiPoint') { } else if (type === 'MultiPoint') {
geometry = this.readMultiPoint_(/** @type {TopoJSONMultiPoint} */ (object), geometry = this.readMultiPoint_(/** @type {TopoJSONMultiPoint} */ (object),
scale, translate, opt_options); scale, translate);
} else if (type === 'MultiLineString') { } else if (type === 'MultiLineString') {
geometry = this.readMultiLineString_( geometry = this.readMultiLineString_(
/** @type {TopoJSONMultiLineString} */(object), arcs, opt_options); /** @type {TopoJSONMultiLineString} */(object), arcs);
} else if (type === 'MultiPolygon') { } else if (type === 'MultiPolygon') {
geometry = this.readMultiPolygon_( geometry = this.readMultiPolygon_(
/** @type {TopoJSONMultiPolygon} */ (object), arcs, opt_options); /** @type {TopoJSONMultiPolygon} */ (object), arcs);
} else { } else {
throw new Error('Unsupported geometry type: ' + type); throw new Error('Unsupported geometry type: ' + type);
} }
@@ -168,18 +157,17 @@ ol.parser.TopoJSON.prototype.readFeatureFromGeometry_ = function(object, arcs,
* @param {Array.<ol.CoordinateArray>} arcs Array of arcs. * @param {Array.<ol.CoordinateArray>} arcs Array of arcs.
* @param {Array.<number>} scale Scale for each dimension. * @param {Array.<number>} scale Scale for each dimension.
* @param {Array.<number>} translate Translation for each dimension. * @param {Array.<number>} translate Translation for each dimension.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Reader options.
* @return {Array.<ol.Feature>} Array of features. * @return {Array.<ol.Feature>} Array of features.
* @private * @private
*/ */
ol.parser.TopoJSON.prototype.readFeaturesFromGeometryCollection_ = function( ol.parser.TopoJSON.prototype.readFeaturesFromGeometryCollection_ = function(
collection, arcs, scale, translate, opt_options) { collection, arcs, scale, translate) {
var geometries = collection.geometries; var geometries = collection.geometries;
var num = geometries.length; var num = geometries.length;
var features = new Array(num); var features = new Array(num);
for (var i = 0; i < num; ++i) { for (var i = 0; i < num; ++i) {
features[i] = this.readFeatureFromGeometry_(geometries[i], arcs, scale, features[i] = this.readFeatureFromGeometry_(geometries[i], arcs, scale,
translate, opt_options); translate);
} }
return features; return features;
}; };
@@ -187,12 +175,10 @@ ol.parser.TopoJSON.prototype.readFeaturesFromGeometryCollection_ = function(
/** /**
* @param {TopoJSONTopology} topology TopoJSON object. * @param {TopoJSONTopology} topology TopoJSON object.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Reader options.
* @return {Array.<ol.Feature>} Parsed features. * @return {Array.<ol.Feature>} Parsed features.
* @private * @private
*/ */
ol.parser.TopoJSON.prototype.readFeaturesFromTopology_ = function( ol.parser.TopoJSON.prototype.readFeaturesFromTopology_ = function(topology) {
topology, opt_options) {
var transform = topology.transform; var transform = topology.transform;
var scale = transform.scale; var scale = transform.scale;
var translate = transform.translate; var translate = transform.translate;
@@ -204,11 +190,11 @@ ol.parser.TopoJSON.prototype.readFeaturesFromTopology_ = function(
if (objects[key].type === 'GeometryCollection') { if (objects[key].type === 'GeometryCollection') {
features.push.apply(features, this.readFeaturesFromGeometryCollection_( features.push.apply(features, this.readFeaturesFromGeometryCollection_(
/** @type {TopoJSONGeometryCollection} */ (objects[key]), /** @type {TopoJSONGeometryCollection} */ (objects[key]),
arcs, scale, translate, opt_options)); arcs, scale, translate));
} else { } else {
features.push(this.readFeatureFromGeometry_( features.push(this.readFeatureFromGeometry_(
/** @type {TopoJSONGeometry} */ (objects[key]), /** @type {TopoJSONGeometry} */ (objects[key]),
arcs, scale, translate, opt_options)); arcs, scale, translate));
} }
} }
return features; return features;
@@ -220,20 +206,12 @@ ol.parser.TopoJSON.prototype.readFeaturesFromTopology_ = function(
* *
* @param {TopoJSONLineString} object TopoJSON object. * @param {TopoJSONLineString} object TopoJSON object.
* @param {Array.<ol.CoordinateArray>} arcs Array of arcs. * @param {Array.<ol.CoordinateArray>} arcs Array of arcs.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Reader options.
* @return {ol.geom.LineString} Geometry. * @return {ol.geom.LineString} Geometry.
* @private * @private
*/ */
ol.parser.TopoJSON.prototype.readLineString_ = function(object, arcs, ol.parser.TopoJSON.prototype.readLineString_ = function(object, arcs) {
opt_options) {
var coordinates = this.concatenateArcs_(object.arcs, arcs); var coordinates = this.concatenateArcs_(object.arcs, arcs);
// TODO: make feature optional in callback return new ol.geom.LineString(coordinates);
var callback = opt_options && opt_options.callback;
var sharedVertices;
if (callback) {
sharedVertices = callback(this.feature_, ol.geom.GeometryType.LINESTRING);
}
return new ol.geom.LineString(coordinates, sharedVertices);
}; };
@@ -242,26 +220,17 @@ ol.parser.TopoJSON.prototype.readLineString_ = function(object, arcs,
* *
* @param {TopoJSONMultiLineString} object TopoJSON object. * @param {TopoJSONMultiLineString} object TopoJSON object.
* @param {Array.<ol.CoordinateArray>} arcs Array of arcs. * @param {Array.<ol.CoordinateArray>} arcs Array of arcs.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Reader options.
* @return {ol.geom.MultiLineString} Geometry. * @return {ol.geom.MultiLineString} Geometry.
* @private * @private
*/ */
ol.parser.TopoJSON.prototype.readMultiLineString_ = function(object, arcs, ol.parser.TopoJSON.prototype.readMultiLineString_ = function(object, arcs) {
opt_options) {
var array = object.arcs; // I'm out of good names var array = object.arcs; // I'm out of good names
var num = array.length; var num = array.length;
var coordinates = new Array(num); var coordinates = new Array(num);
for (var i = 0; i < num; ++i) { for (var i = 0; i < num; ++i) {
coordinates[i] = this.concatenateArcs_(array[i], arcs); coordinates[i] = this.concatenateArcs_(array[i], arcs);
} }
// TODO: make feature optional in callback return new ol.geom.MultiLineString(coordinates);
var callback = opt_options && opt_options.callback;
var sharedVertices;
if (callback) {
sharedVertices = callback(this.feature_,
ol.geom.GeometryType.MULTILINESTRING);
}
return new ol.geom.MultiLineString(coordinates, sharedVertices);
}; };
@@ -271,23 +240,16 @@ ol.parser.TopoJSON.prototype.readMultiLineString_ = function(object, arcs,
* @param {TopoJSONMultiPoint} object TopoJSON object. * @param {TopoJSONMultiPoint} object TopoJSON object.
* @param {Array.<number>} scale Scale for each dimension. * @param {Array.<number>} scale Scale for each dimension.
* @param {Array.<number>} translate Translation for each dimension. * @param {Array.<number>} translate Translation for each dimension.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Reader options.
* @return {ol.geom.MultiPoint} Geometry. * @return {ol.geom.MultiPoint} Geometry.
* @private * @private
*/ */
ol.parser.TopoJSON.prototype.readMultiPoint_ = function(object, scale, ol.parser.TopoJSON.prototype.readMultiPoint_ = function(object, scale,
translate, opt_options) { translate) {
var coordinates = object.coordinates; var coordinates = object.coordinates;
for (var i = 0, ii = coordinates.length; i < ii; ++i) { for (var i = 0, ii = coordinates.length; i < ii; ++i) {
this.transformVertex_(coordinates[i], scale, translate); this.transformVertex_(coordinates[i], scale, translate);
} }
// TODO: make feature optional in callback return new ol.geom.MultiPoint(coordinates);
var callback = opt_options && opt_options.callback;
var sharedVertices;
if (callback) {
sharedVertices = callback(this.feature_, ol.geom.GeometryType.MULTIPOINT);
}
return new ol.geom.MultiPoint(coordinates, sharedVertices);
}; };
@@ -296,12 +258,10 @@ ol.parser.TopoJSON.prototype.readMultiPoint_ = function(object, scale,
* *
* @param {TopoJSONMultiPolygon} object TopoJSON object. * @param {TopoJSONMultiPolygon} object TopoJSON object.
* @param {Array.<ol.CoordinateArray>} arcs Array of arcs. * @param {Array.<ol.CoordinateArray>} arcs Array of arcs.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Reader options.
* @return {ol.geom.MultiPolygon} Geometry. * @return {ol.geom.MultiPolygon} Geometry.
* @private * @private
*/ */
ol.parser.TopoJSON.prototype.readMultiPolygon_ = function(object, arcs, ol.parser.TopoJSON.prototype.readMultiPolygon_ = function(object, arcs) {
opt_options) {
var array = object.arcs; var array = object.arcs;
var numPolys = array.length; var numPolys = array.length;
var coordinates = new Array(numPolys); var coordinates = new Array(numPolys);
@@ -317,13 +277,7 @@ ol.parser.TopoJSON.prototype.readMultiPolygon_ = function(object, arcs,
} }
coordinates[i] = ringCoords; coordinates[i] = ringCoords;
} }
// TODO: make feature optional in callback return new ol.geom.MultiPolygon(coordinates);
var callback = opt_options && opt_options.callback;
var sharedVertices;
if (callback) {
sharedVertices = callback(this.feature_, ol.geom.GeometryType.MULTIPOLYGON);
}
return new ol.geom.MultiPolygon(coordinates, sharedVertices);
}; };
@@ -333,21 +287,13 @@ ol.parser.TopoJSON.prototype.readMultiPolygon_ = function(object, arcs,
* @param {TopoJSONPoint} object TopoJSON object. * @param {TopoJSONPoint} object TopoJSON object.
* @param {Array.<number>} scale Scale for each dimension. * @param {Array.<number>} scale Scale for each dimension.
* @param {Array.<number>} translate Translation for each dimension. * @param {Array.<number>} translate Translation for each dimension.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Reader options.
* @return {ol.geom.Point} Geometry. * @return {ol.geom.Point} Geometry.
* @private * @private
*/ */
ol.parser.TopoJSON.prototype.readPoint_ = function(object, scale, translate, ol.parser.TopoJSON.prototype.readPoint_ = function(object, scale, translate) {
opt_options) {
var coordinates = object.coordinates; var coordinates = object.coordinates;
this.transformVertex_(coordinates, scale, translate); this.transformVertex_(coordinates, scale, translate);
// TODO: make feature optional in callback return new ol.geom.Point(coordinates);
var callback = opt_options && opt_options.callback;
var sharedVertices;
if (callback) {
sharedVertices = callback(this.feature_, ol.geom.GeometryType.POINT);
}
return new ol.geom.Point(coordinates, sharedVertices);
}; };
@@ -356,25 +302,17 @@ ol.parser.TopoJSON.prototype.readPoint_ = function(object, scale, translate,
* *
* @param {TopoJSONPolygon} object TopoJSON object. * @param {TopoJSONPolygon} object TopoJSON object.
* @param {Array.<ol.CoordinateArray>} arcs Array of arcs. * @param {Array.<ol.CoordinateArray>} arcs Array of arcs.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Reader options.
* @return {ol.geom.Polygon} Geometry. * @return {ol.geom.Polygon} Geometry.
* @private * @private
*/ */
ol.parser.TopoJSON.prototype.readPolygon_ = function(object, arcs, ol.parser.TopoJSON.prototype.readPolygon_ = function(object, arcs) {
opt_options) {
var array = object.arcs; // I'm out of good names var array = object.arcs; // I'm out of good names
var num = array.length; var num = array.length;
var coordinates = new Array(num); var coordinates = new Array(num);
for (var i = 0; i < num; ++i) { for (var i = 0; i < num; ++i) {
coordinates[i] = this.concatenateArcs_(array[i], arcs); coordinates[i] = this.concatenateArcs_(array[i], arcs);
} }
// TODO: make feature optional in callback return new ol.geom.Polygon(coordinates);
var callback = opt_options && opt_options.callback;
var sharedVertices;
if (callback) {
sharedVertices = callback(this.feature_, ol.geom.GeometryType.POLYGON);
}
return new ol.geom.Polygon(coordinates, sharedVertices);
}; };
@@ -165,7 +165,7 @@ ol.renderer.canvas.VectorRenderer.prototype.renderLineStringFeatures_ =
function(features, symbolizer) { function(features, symbolizer) {
var context = this.context_, var context = this.context_,
i, ii, feature, id, currentSize, geometry, components, j, jj, line, dim, i, ii, feature, id, currentSize, geometry, components, j, jj, line,
k, kk, vec, strokeSize; k, kk, vec, strokeSize;
context.globalAlpha = symbolizer.opacity; context.globalAlpha = symbolizer.opacity;
@@ -197,7 +197,6 @@ ol.renderer.canvas.VectorRenderer.prototype.renderLineStringFeatures_ =
} }
for (j = 0, jj = components.length; j < jj; ++j) { for (j = 0, jj = components.length; j < jj; ++j) {
line = components[j]; line = components[j];
dim = line.dimension;
for (k = 0, kk = line.getCount(); k < kk; ++k) { for (k = 0, kk = line.getCount(); k < kk; ++k) {
vec = [line.get(k, 0), line.get(k, 1), 0]; vec = [line.get(k, 0), line.get(k, 1), 0];
goog.vec.Mat4.multVec3(this.transform_, vec, vec); goog.vec.Mat4.multVec3(this.transform_, vec, vec);
@@ -347,7 +346,7 @@ ol.renderer.canvas.VectorRenderer.prototype.renderPolygonFeatures_ =
fillOpacity = symbolizer.fillOpacity, fillOpacity = symbolizer.fillOpacity,
globalAlpha, globalAlpha,
i, ii, geometry, components, j, jj, poly, i, ii, geometry, components, j, jj, poly,
rings, numRings, ring, dim, k, kk, vec, feature; rings, numRings, ring, k, kk, vec, feature;
if (strokeColor) { if (strokeColor) {
context.strokeStyle = strokeColor; context.strokeStyle = strokeColor;
@@ -384,7 +383,6 @@ ol.renderer.canvas.VectorRenderer.prototype.renderPolygonFeatures_ =
} }
for (j = 0, jj = components.length; j < jj; ++j) { for (j = 0, jj = components.length; j < jj; ++j) {
poly = components[j]; poly = components[j];
dim = poly.dimension;
rings = poly.rings; rings = poly.rings;
numRings = rings.length; numRings = rings.length;
if (numRings > 0) { if (numRings > 0) {
@@ -36,25 +36,6 @@ describe('ol.geom.GeometryCollection', function() {
}); });
describe('#dimension', function() {
it('can be 2', function() {
var point = new ol.geom.Point([10, 20]);
var line = new ol.geom.LineString([[10, 20], [30, 40]]);
var poly = new ol.geom.Polygon([outer, inner1, inner2]);
var multi = new ol.geom.GeometryCollection([point, line, poly]);
expect(multi.dimension).to.be(2);
});
it('can be 3', function() {
var multi = new ol.geom.GeometryCollection([
new ol.geom.Point([30, 40, 50])
]);
expect(multi.dimension).to.be(3);
});
});
describe('#clone()', function() { describe('#clone()', function() {
it('has a working clone method', function() { it('has a working clone method', function() {
-14
View File
@@ -11,20 +11,6 @@ describe('ol.geom.LinearRing', function() {
}); });
describe('#dimension', function() {
it('can be 2', function() {
var ring = new ol.geom.LinearRing([[10, 20], [30, 40]]);
expect(ring.dimension).to.be(2);
});
it('can be 3', function() {
var ring = new ol.geom.LinearRing([[10, 20, 30], [40, 50, 60]]);
expect(ring.dimension).to.be(3);
});
});
describe('#getCoordinates()', function() { describe('#getCoordinates()', function() {
it('is an array', function() { it('is an array', function() {
+25 -50
View File
@@ -10,28 +10,6 @@ describe('ol.geom.LineString', function() {
expect(line).to.be.a(ol.geom.Geometry); expect(line).to.be.a(ol.geom.Geometry);
}); });
it('accepts shared vertices', function() {
var vertices = new ol.geom.SharedVertices();
var l1 = new ol.geom.LineString([[10, 20], [30, 40]], vertices);
var l2 = new ol.geom.LineString([[50, 60], [70, 80]], vertices);
expect(l1.getCoordinates()).to.eql([[10, 20], [30, 40]]);
expect(l2.getCoordinates()).to.eql([[50, 60], [70, 80]]);
});
});
describe('#dimension', function() {
it('can be 2', function() {
var line = new ol.geom.LineString([[10, 20], [30, 40]]);
expect(line.dimension).to.be(2);
});
it('can be 3', function() {
var line = new ol.geom.LineString([[10, 20, 30], [40, 50, 60]]);
expect(line.dimension).to.be(3);
});
}); });
describe('#getBounds()', function() { describe('#getBounds()', function() {
@@ -56,36 +34,33 @@ describe('ol.geom.LineString', function() {
}); });
describe('#getSharedId()', function() { describe('#transform()', function() {
it('returns identifiers', function() { var forward = ol.proj.getTransform('EPSG:4326', 'EPSG:3857');
var vertices = new ol.geom.SharedVertices(); var inverse = ol.proj.getTransform('EPSG:3857', 'EPSG:4326');
var l1 = new ol.geom.LineString([[10, 20], [30, 40]], vertices); it('forward transforms a linestring in place', function() {
var l2 = new ol.geom.LineString( var line = new ol.geom.LineString([[10, 20], [20, 30], [30, 40]]);
[[50, 60], [70, 80], [90, 100]], vertices); line.transform(forward);
expect(line.get(0, 0)).to.roughlyEqual(1113195, 1);
expect(line.get(0, 1)).to.roughlyEqual(2273031, 1);
expect(line.get(1, 0)).to.roughlyEqual(2226390, 1);
expect(line.get(1, 1)).to.roughlyEqual(3503550, 1);
expect(line.get(2, 0)).to.roughlyEqual(3339585, 1);
expect(line.get(2, 1)).to.roughlyEqual(4865942, 1);
});
var id1 = l1.getSharedId(); it('inverse transforms a linestring in place', function() {
var id2 = l2.getSharedId(); var line = new ol.geom.LineString([
[1113195, 2273031], [2226390, 3503550], [3339585, 4865942]
expect(vertices.coordinates).to.eql( ]);
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]); line.transform(inverse);
expect(line.get(0, 0)).to.roughlyEqual(10, 0.001);
expect(vertices.getStart(id1)).to.be(0); expect(line.get(0, 1)).to.roughlyEqual(20, 0.001);
expect(vertices.getCount(id1)).to.be(2); expect(line.get(1, 0)).to.roughlyEqual(20, 0.001);
expect(vertices.get(id1, 0, 0)).to.be(10); expect(line.get(1, 1)).to.roughlyEqual(30, 0.001);
expect(vertices.get(id1, 0, 1)).to.be(20); expect(line.get(2, 0)).to.roughlyEqual(30, 0.001);
expect(vertices.get(id1, 1, 0)).to.be(30); expect(line.get(2, 1)).to.roughlyEqual(40, 0.001);
expect(vertices.get(id1, 1, 1)).to.be(40);
expect(vertices.getStart(id2)).to.be(4);
expect(vertices.getCount(id2)).to.be(3);
expect(vertices.get(id2, 0, 0)).to.be(50);
expect(vertices.get(id2, 0, 1)).to.be(60);
expect(vertices.get(id2, 1, 0)).to.be(70);
expect(vertices.get(id2, 1, 1)).to.be(80);
expect(vertices.get(id2, 2, 0)).to.be(90);
expect(vertices.get(id2, 2, 1)).to.be(100);
}); });
}); });
@@ -94,4 +69,4 @@ describe('ol.geom.LineString', function() {
goog.require('ol.geom.Geometry'); goog.require('ol.geom.Geometry');
goog.require('ol.geom.LineString'); goog.require('ol.geom.LineString');
goog.require('ol.geom.SharedVertices'); goog.require('ol.proj');
-25
View File
@@ -12,13 +12,6 @@ describe('ol.geom.MultiLineString', function() {
expect(multi).to.be.a(ol.geom.Geometry); expect(multi).to.be.a(ol.geom.Geometry);
}); });
it('throws when given with insufficient dimensions', function() {
expect(function() {
var multi = new ol.geom.MultiLineString([1]);
multi = multi; // suppress gjslint warning about unused variable
}).to.throwException();
});
}); });
describe('#components', function() { describe('#components', function() {
@@ -36,24 +29,6 @@ describe('ol.geom.MultiLineString', function() {
}); });
describe('#dimension', function() {
it('can be 2', function() {
var multi = new ol.geom.MultiLineString([
[[10, 20], [30, 40]],
[[20, 30], [40, 50]]]);
expect(multi.dimension).to.be(2);
});
it('can be 3', function() {
var multi = new ol.geom.MultiLineString([
[[10, 20, 30], [30, 40, 50]],
[[20, 30, 40], [40, 50, 60]]]);
expect(multi.dimension).to.be(3);
});
});
describe('#getBounds()', function() { describe('#getBounds()', function() {
it('returns the bounding extent', function() { it('returns the bounding extent', function() {
+29 -21
View File
@@ -10,13 +10,6 @@ describe('ol.geom.MultiPoint', function() {
expect(multi).to.be.a(ol.geom.Geometry); expect(multi).to.be.a(ol.geom.Geometry);
}); });
it('throws when given with insufficient dimensions', function() {
expect(function() {
var multi = new ol.geom.MultiPoint([1]);
multi = multi; // suppress gjslint warning about unused variable
}).to.throwException();
});
}); });
describe('#components', function() { describe('#components', function() {
@@ -32,20 +25,6 @@ describe('ol.geom.MultiPoint', function() {
}); });
describe('#dimension', function() {
it('can be 2', function() {
var multi = new ol.geom.MultiPoint([[10, 20], [30, 40]]);
expect(multi.dimension).to.be(2);
});
it('can be 3', function() {
var multi = new ol.geom.MultiPoint([[10, 20, 30], [30, 40, 50]]);
expect(multi.dimension).to.be(3);
});
});
describe('#getBounds()', function() { describe('#getBounds()', function() {
it('returns the bounding extent', function() { it('returns the bounding extent', function() {
@@ -68,8 +47,37 @@ describe('ol.geom.MultiPoint', function() {
}); });
describe('#transform', function() {
var forward = ol.proj.getTransform('EPSG:4326', 'EPSG:3857');
var inverse = ol.proj.getTransform('EPSG:3857', 'EPSG:4326');
it('forward transforms a multi-point', function() {
var multi = new ol.geom.MultiPoint([[10, 20], [30, 40]]);
multi.transform(forward);
expect(multi.components[0].get(0)).to.roughlyEqual(1113195, 1);
expect(multi.components[0].get(1)).to.roughlyEqual(2273031, 1);
expect(multi.components[1].get(0)).to.roughlyEqual(3339584, 1);
expect(multi.components[1].get(1)).to.roughlyEqual(4865942, 1);
});
it('inverse transforms a multi-point', function() {
var multi = new ol.geom.MultiPoint(
[[1113195, 2273031], [3339584, 4865942]]);
multi.transform(inverse);
expect(multi.components[0].get(0)).to.roughlyEqual(10, 0.001);
expect(multi.components[0].get(1)).to.roughlyEqual(20, 0.001);
expect(multi.components[1].get(0)).to.roughlyEqual(30, 0.001);
expect(multi.components[1].get(1)).to.roughlyEqual(40, 0.001);
});
});
}); });
goog.require('ol.geom.Geometry'); goog.require('ol.geom.Geometry');
goog.require('ol.geom.MultiPoint'); goog.require('ol.geom.MultiPoint');
goog.require('ol.geom.Point'); goog.require('ol.geom.Point');
goog.require('ol.proj');
-16
View File
@@ -41,22 +41,6 @@ describe('ol.geom.MultiPolygon', function() {
}); });
describe('#dimension', function() {
it('can be 2', function() {
var multi = new ol.geom.MultiPolygon([
[outer1, inner1a, inner1b],
[outer2]]);
expect(multi.dimension).to.be(2);
});
it('can be 3', function() {
var multi = new ol.geom.MultiPolygon([[[[10, 20, 30], [40, 50, 60]]]]);
expect(multi.dimension).to.be(3);
});
});
describe('#getBounds()', function() { describe('#getBounds()', function() {
it('returns the bounding extent', function() { it('returns the bounding extent', function() {
+15 -60
View File
@@ -10,37 +10,6 @@ describe('ol.geom.Point', function() {
expect(point).to.be.a(ol.geom.Geometry); expect(point).to.be.a(ol.geom.Geometry);
}); });
it('accepts shared vertices', function() {
var vertices = new ol.geom.SharedVertices();
var p1 = new ol.geom.Point([10, 20], vertices);
var p2 = new ol.geom.Point([30, 40], vertices);
var p3 = new ol.geom.Point([50, 60], vertices);
expect(p1.getCoordinates()).to.eql([10, 20]);
expect(p2.getCoordinates()).to.eql([30, 40]);
expect(p3.getCoordinates()).to.eql([50, 60]);
});
it('throws when given with insufficient dimensions', function() {
expect(function() {
var point = new ol.geom.Point([1]);
point = point; // suppress gjslint warning about unused variable
}).to.throwException();
});
});
describe('#dimension', function() {
it('can be 2', function() {
var point = new ol.geom.Point([10, 20]);
expect(point.dimension).to.be(2);
});
it('can be 3', function() {
var point = new ol.geom.Point([10, 20, 30]);
expect(point.dimension).to.be(3);
});
}); });
describe('#getBounds()', function() { describe('#getBounds()', function() {
@@ -65,37 +34,23 @@ describe('ol.geom.Point', function() {
}); });
describe('#transform()', function() {
describe('#getSharedId()', function() { var forward = ol.proj.getTransform('EPSG:4326', 'EPSG:3857');
var inverse = ol.proj.getTransform('EPSG:3857', 'EPSG:4326');
it('returns identifiers', function() { it('forward transforms a point in place', function() {
var vertices = new ol.geom.SharedVertices(); var point = new ol.geom.Point([10, 20]);
point.transform(forward);
expect(point.get(0)).to.roughlyEqual(1113195, 1);
expect(point.get(1)).to.roughlyEqual(2273031, 1);
});
var p1 = new ol.geom.Point([10, 20], vertices); it('inverse transforms a point in place', function() {
var p2 = new ol.geom.Point([30, 40], vertices); var point = new ol.geom.Point([1113195, 2273031]);
var p3 = new ol.geom.Point([50, 60], vertices); point.transform(inverse);
expect(point.get(0)).to.roughlyEqual(10, 0.001);
var id1 = p1.getSharedId(); expect(point.get(1)).to.roughlyEqual(20, 0.001);
var id2 = p2.getSharedId();
var id3 = p3.getSharedId();
expect(vertices.coordinates).to.eql(
[10, 20, 30, 40, 50, 60]);
expect(vertices.getStart(id1)).to.be(0);
expect(vertices.getCount(id1)).to.be(1);
expect(vertices.get(id1, 0, 0)).to.be(10);
expect(vertices.get(id1, 0, 1)).to.be(20);
expect(vertices.getStart(id2)).to.be(2);
expect(vertices.getCount(id2)).to.be(1);
expect(vertices.get(id2, 0, 0)).to.be(30);
expect(vertices.get(id2, 0, 1)).to.be(40);
expect(vertices.getStart(id3)).to.be(4);
expect(vertices.getCount(id3)).to.be(1);
expect(vertices.get(id3, 0, 0)).to.be(50);
expect(vertices.get(id3, 0, 1)).to.be(60);
}); });
}); });
@@ -104,4 +59,4 @@ describe('ol.geom.Point', function() {
goog.require('ol.geom.Geometry'); goog.require('ol.geom.Geometry');
goog.require('ol.geom.Point'); goog.require('ol.geom.Point');
goog.require('ol.geom.SharedVertices'); goog.require('ol.proj');
+60 -25
View File
@@ -14,16 +14,6 @@ describe('ol.geom.Polygon', function() {
expect(poly).to.be.a(ol.geom.Geometry); expect(poly).to.be.a(ol.geom.Geometry);
}); });
it('accepts shared vertices', function() {
var vertices = new ol.geom.SharedVertices();
var p1 = new ol.geom.Polygon([outer], vertices);
var p2 = new ol.geom.Polygon([outer, inner1], vertices);
var p3 = new ol.geom.Polygon([outer, inner2], vertices);
expect(p1.getCoordinates()).to.eql([outer]);
expect(p2.getCoordinates()).to.eql([outer, inner1]);
expect(p3.getCoordinates()).to.eql([outer, inner2]);
});
}); });
describe('#rings', function() { describe('#rings', function() {
@@ -61,20 +51,6 @@ describe('ol.geom.Polygon', function() {
}); });
describe('#dimension', function() {
it('can be 2', function() {
var poly = new ol.geom.Polygon([outer, inner1, inner2]);
expect(poly.dimension).to.be(2);
});
it('can be 3', function() {
var poly = new ol.geom.Polygon([[[10, 20, 30], [40, 50, 60]]]);
expect(poly.dimension).to.be(3);
});
});
describe('#getBounds()', function() { describe('#getBounds()', function() {
it('returns the bounding extent', function() { it('returns the bounding extent', function() {
@@ -97,9 +73,68 @@ describe('ol.geom.Polygon', function() {
}); });
describe('#transform()', function() {
var forward = ol.proj.getTransform('EPSG:4326', 'EPSG:3857');
var inverse = ol.proj.getTransform('EPSG:3857', 'EPSG:4326');
var gg, sm;
beforeEach(function() {
gg = [
[[0, 0], [0, 10], [10, 10], [10, 0], [0, 0]],
[[1, 1], [2, 1], [2, 2], [1, 2], [1, 1]],
[[8, 8], [9, 8], [9, 9], [8, 9], [8, 8]]
];
sm = [[
[0, 0], [0, 1118890], [1113195, 1118890], [1113195, 0], [0, 0]
], [
[111319, 111325], [222639, 111325], [222639, 222684],
[111319, 222684], [111319, 111325]
], [
[890556, 893464], [1001875, 893464], [1001875, 1006021],
[890556, 1006021], [890556, 893464]
]];
});
it('forward transforms a polygon in place', function() {
var poly = new ol.geom.Polygon(gg);
poly.transform(forward);
var coordinates = poly.getCoordinates();
var ring;
for (var i = 0, ii = coordinates.length; i < ii; ++i) {
var ring = coordinates[i];
for (var j = 0, jj = ring.length; j < jj; ++j) {
expect(ring[j][0]).to.roughlyEqual(sm[i][j][0], 1);
expect(ring[j][1]).to.roughlyEqual(sm[i][j][1], 1);
}
}
});
it('inverse transforms a polygon in place', function() {
var poly = new ol.geom.Polygon(sm);
poly.transform(inverse);
var coordinates = poly.getCoordinates();
var ring;
for (var i = 0, ii = coordinates.length; i < ii; ++i) {
var ring = coordinates[i];
for (var j = 0, jj = ring.length; j < jj; ++j) {
expect(ring[j][0]).to.roughlyEqual(gg[i][j][0], 0.001);
expect(ring[j][1]).to.roughlyEqual(gg[i][j][1], 0.001);
}
}
});
});
}); });
goog.require('ol.geom.Geometry'); goog.require('ol.geom.Geometry');
goog.require('ol.geom.LinearRing'); goog.require('ol.geom.LinearRing');
goog.require('ol.geom.Polygon'); goog.require('ol.geom.Polygon');
goog.require('ol.geom.SharedVertices'); goog.require('ol.proj');
-221
View File
@@ -1,221 +0,0 @@
goog.provide('ol.test.geom.SharedVertices');
describe('ol.geom.SharedVertices', function() {
describe('constructor', function() {
it('creates an instance', function() {
var vertices = new ol.geom.SharedVertices();
expect(vertices).to.be.a(ol.geom.SharedVertices);
});
it('accepts options', function() {
var vertices = new ol.geom.SharedVertices({
dimension: 4,
offset: [1, 2, 3, 4]
});
expect(vertices.getDimension()).to.be(4);
expect(vertices.getOffset()).to.eql([1, 2, 3, 4]);
});
});
describe('offset option', function() {
it('offsets the internally stored vertex coordinates', function() {
var vertices = new ol.geom.SharedVertices({offset: [3, -1]});
vertices.add([[3, -1], [0, 0]]);
vertices.add([[10, 20]]);
expect(vertices.coordinates).to.eql([0, 0, -3, 1, 7, 21]);
});
});
describe('#add()', function() {
it('adds vertex arrays to the shared coordinates', function() {
var vertices = new ol.geom.SharedVertices();
expect(vertices.coordinates.length).to.be(0);
vertices.add([[1, 2], [3, 4]]);
expect(vertices.coordinates).to.eql([1, 2, 3, 4]);
vertices.add([[5, 6]]);
expect(vertices.coordinates).to.eql([1, 2, 3, 4, 5, 6]);
});
it('ignores extra dimensions', function() {
var vertices = new ol.geom.SharedVertices({dimension: 2});
expect(vertices.coordinates.length).to.be(0);
vertices.add([[1, 2], [3, 4, 5], [6, 7]]);
expect(vertices.coordinates).to.eql([1, 2, 3, 4, 6, 7]);
vertices.add([[8, 9, 10]]);
expect(vertices.coordinates).to.eql([1, 2, 3, 4, 6, 7, 8, 9]);
});
it('pads with NaN when dimension not provided', function() {
var vertices = new ol.geom.SharedVertices({dimension: 3});
expect(vertices.coordinates.length).to.be(0);
vertices.add([[1, 2], [3, 4, 5], [6, 7]]);
expect(vertices.coordinates).to.eql([1, 2, NaN, 3, 4, 5, 6, 7, NaN]);
});
it('returns an identifier for coordinate access', function() {
var vertices = new ol.geom.SharedVertices();
var id = vertices.add([[1, 2], [3, 4]]);
expect(typeof id).to.be('number');
});
it('returns the index of the added vertices', function() {
var vertices = new ol.geom.SharedVertices();
var first = vertices.add([[1, 2]]);
var second = vertices.add([[3, 4], [5, 6]]);
var third = vertices.add([[7, 8], [9, 10], [11, 12]]);
expect(vertices.coordinates).to.eql(
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]);
expect(first).to.be(0);
expect(second).to.be(1);
expect(third).to.be(2);
});
});
describe('#get()', function() {
it('provides access to vertex coordinates', function() {
var vertices = new ol.geom.SharedVertices();
var first = vertices.add([[1, 2], [3, 4]]);
var second = vertices.add([[5, 6]]);
expect(vertices.get(first, 0, 0)).to.be(1);
expect(vertices.get(first, 0, 1)).to.be(2);
expect(vertices.get(first, 1, 0)).to.be(3);
expect(vertices.get(first, 1, 1)).to.be(4);
expect(vertices.get(second, 0, 0)).to.be(5);
expect(vertices.get(second, 0, 1)).to.be(6);
});
it('works for non-2d vertices', function() {
var vertices = new ol.geom.SharedVertices({dimension: 3});
var id = vertices.add([[1, 2, 3], [4, 5, 6]]);
expect(vertices.get(id, 0, 0)).to.be(1);
expect(vertices.get(id, 0, 1)).to.be(2);
expect(vertices.get(id, 0, 2)).to.be(3);
expect(vertices.get(id, 1, 0)).to.be(4);
expect(vertices.get(id, 1, 1)).to.be(5);
expect(vertices.get(id, 1, 2)).to.be(6);
});
it('works when an offset is provided', function() {
var vertices = new ol.geom.SharedVertices({offset: [3, 3]});
var id = vertices.add([[1, 2], [3, 4], [5, 6]]);
expect(vertices.get(id, 0, 0)).to.be(1);
expect(vertices.get(id, 0, 1)).to.be(2);
expect(vertices.get(id, 1, 0)).to.be(3);
expect(vertices.get(id, 1, 1)).to.be(4);
expect(vertices.get(id, 2, 0)).to.be(5);
expect(vertices.get(id, 2, 1)).to.be(6);
});
});
describe('#getCount()', function() {
it('returns the length of an identified vertex array', function() {
var vertices = new ol.geom.SharedVertices();
var first = vertices.add([[2, 3], [3, 4], [4, 5]]);
var second = vertices.add([[5, 6], [6, 6]]);
expect(vertices.getCount(first)).to.be(3);
expect(vertices.getCount(second)).to.be(2);
});
});
describe('#getCounts()', function() {
it('returns the counts array', function() {
var vertices = new ol.geom.SharedVertices();
vertices.add([[2, 3], [3, 4], [4, 5]]);
vertices.add([[5, 6], [6, 6]]);
vertices.add([[7, 8]]);
expect(vertices.getCounts()).to.eql([3, 2, 1]);
});
});
describe('#getDimension()', function() {
it('returns 2 by default', function() {
var vertices = new ol.geom.SharedVertices();
expect(vertices.getDimension()).to.be(2);
});
it('returns the dimension provided to the constructor', function() {
var vertices = new ol.geom.SharedVertices({dimension: 10});
expect(vertices.getDimension()).to.be(10);
});
});
describe('#getOffset()', function() {
it('returns null by default', function() {
var vertices = new ol.geom.SharedVertices();
expect(vertices.getOffset()).to.be(null);
});
it('returns the offset provided to the constructor', function() {
var vertices = new ol.geom.SharedVertices({offset: [1, 2]});
expect(vertices.getOffset()).to.eql([1, 2]);
});
});
describe('#getStart()', function() {
it('returns the start index of an identified vertex array', function() {
var vertices = new ol.geom.SharedVertices();
var first = vertices.add([[2, 3], [4, 5], [6, 7]]);
var second = vertices.add([[8, 9], [10, 11]]);
var third = vertices.add([[12, 13]]);
expect(vertices.coordinates).to.eql(
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]);
// 0 1 2 3 4 5 6 7 8 9 10 11
expect(vertices.getStart(first)).to.be(0);
expect(vertices.getStart(second)).to.be(6);
expect(vertices.getStart(third)).to.be(10);
});
});
describe('#getStarts()', function() {
it('returns the counts array', function() {
var vertices = new ol.geom.SharedVertices();
vertices.add([[2, 3], [3, 4], [4, 5]]);
vertices.add([[5, 6], [6, 6]]);
vertices.add([[7, 8]]);
expect(vertices.getStarts()).to.eql([0, 6, 10]);
});
});
describe('#coordinates', function() {
it('is a flat array of all coordinate values', function() {
var vertices = new ol.geom.SharedVertices();
vertices.add([[1, 2], [3, 4]]);
vertices.add([[5, 6]]);
vertices.add([[7, 8], [9, 10], [11, 12]]);
expect(vertices.coordinates).to.eql(
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]);
});
it('is not reassigned', function() {
var vertices = new ol.geom.SharedVertices();
vertices.add([[1, 2], [3, 4]]);
var coordinates = vertices.coordinates;
vertices.add([[5, 6]]);
expect(vertices.coordinates).to.be(coordinates);
});
});
});
goog.require('ol.geom.SharedVertices');
+8 -224
View File
@@ -218,74 +218,11 @@ describe('ol.parser.GeoJSON', function() {
}); });
}); });
it('parses countries.geojson with shared vertices', function() {
afterLoadText('spec/ol/parser/geojson/countries.geojson', function(text) {
var pointVertices = new ol.geom.SharedVertices();
var lineVertices = new ol.geom.SharedVertices();
var polygonVertices = new ol.geom.SharedVertices();
var lookup = {
'point': pointVertices,
'linestring': lineVertices,
'polygon': polygonVertices,
'multipoint': pointVertices,
'multilinstring': lineVertices,
'multipolygon': polygonVertices
};
var callback = function(feature, type) {
return lookup[type];
};
var result = parser.readFeaturesFromString(text,
{callback: callback}).features;
expect(result.length).to.be(179);
expect(pointVertices.coordinates.length).to.be(0);
expect(lineVertices.coordinates.length).to.be(0);
expect(polygonVertices.coordinates.length).to.be(21344);
var first = result[0];
expect(first).to.be.a(ol.Feature);
expect(first.get('name')).to.be('Afghanistan');
var firstGeom = first.getGeometry();
expect(firstGeom).to.be.a(ol.geom.Polygon);
expect(ol.extent.equals(firstGeom.getBounds(),
[60.52843, 29.318572, 75.158028, 38.486282]))
.to.be(true);
var last = result[178];
expect(last).to.be.a(ol.Feature);
expect(last.get('name')).to.be('Zimbabwe');
var lastGeom = last.getGeometry();
expect(lastGeom).to.be.a(ol.geom.Polygon);
expect(ol.extent.equals(lastGeom.getBounds(),
[25.264226, -22.271612, 32.849861, -15.507787]))
.to.be(true);
});
});
}); });
describe('#parseAsFeatureCollection_()', function() { describe('#parseAsFeatureCollection_()', function() {
it('generates an array of features for FeatureCollection', function() { it('generates an array of features for FeatureCollection', function() {
var pointVertices = new ol.geom.SharedVertices();
var lineVertices = new ol.geom.SharedVertices();
var polygonVertices = new ol.geom.SharedVertices();
var lookup = {
'point': pointVertices,
'linestring': lineVertices,
'polygon': polygonVertices,
'multipoint': pointVertices,
'multilinstring': lineVertices,
'multipolygon': polygonVertices
};
var callback = function(feature, type) {
return lookup[type];
};
var parser = new ol.parser.GeoJSON(); var parser = new ol.parser.GeoJSON();
var json = { var json = {
@@ -310,8 +247,7 @@ describe('ol.parser.GeoJSON', function() {
} }
}] }]
}; };
var result = parser.parseAsFeatureCollection_(json, var result = parser.parseAsFeatureCollection_(json);
{callback: callback});
var features = result.features; var features = result.features;
expect(features.length).to.be(2); expect(features.length).to.be(2);
@@ -326,30 +262,10 @@ describe('ol.parser.GeoJSON', function() {
expect(second.get('bam')).to.be('baz'); expect(second.get('bam')).to.be('baz');
expect(second.getGeometry()).to.be.a(ol.geom.LineString); expect(second.getGeometry()).to.be.a(ol.geom.LineString);
expect(pointVertices.coordinates.length).to.be(2);
expect(lineVertices.coordinates.length).to.be(4);
expect(polygonVertices.coordinates.length).to.be(0);
expect(result.metadata.projection).to.be('EPSG:4326'); expect(result.metadata.projection).to.be('EPSG:4326');
}); });
it('reads named crs from top-level object', function() { it('reads named crs from top-level object', function() {
var pointVertices = new ol.geom.SharedVertices();
var lineVertices = new ol.geom.SharedVertices();
var polygonVertices = new ol.geom.SharedVertices();
var lookup = {
'point': pointVertices,
'linestring': lineVertices,
'polygon': polygonVertices,
'multipoint': pointVertices,
'multilinstring': lineVertices,
'multipolygon': polygonVertices
};
var callback = function(feature, type) {
return lookup[type];
};
var parser = new ol.parser.GeoJSON(); var parser = new ol.parser.GeoJSON();
var json = { var json = {
@@ -380,8 +296,7 @@ describe('ol.parser.GeoJSON', function() {
} }
}] }]
}; };
var result = parser.parseAsFeatureCollection_(json, var result = parser.parseAsFeatureCollection_(json);
{callback: callback});
var features = result.features; var features = result.features;
expect(features.length).to.be(2); expect(features.length).to.be(2);
@@ -396,30 +311,10 @@ describe('ol.parser.GeoJSON', function() {
expect(second.get('bam')).to.be('baz'); expect(second.get('bam')).to.be('baz');
expect(second.getGeometry()).to.be.a(ol.geom.LineString); expect(second.getGeometry()).to.be.a(ol.geom.LineString);
expect(pointVertices.coordinates.length).to.be(2);
expect(lineVertices.coordinates.length).to.be(4);
expect(polygonVertices.coordinates.length).to.be(0);
expect(result.metadata.projection).to.be('EPSG:1234'); expect(result.metadata.projection).to.be('EPSG:1234');
}); });
it('accepts null crs', function() { it('accepts null crs', function() {
var pointVertices = new ol.geom.SharedVertices();
var lineVertices = new ol.geom.SharedVertices();
var polygonVertices = new ol.geom.SharedVertices();
var lookup = {
'point': pointVertices,
'linestring': lineVertices,
'polygon': polygonVertices,
'multipoint': pointVertices,
'multilinstring': lineVertices,
'multipolygon': polygonVertices
};
var callback = function(feature, type) {
return lookup[type];
};
var parser = new ol.parser.GeoJSON(); var parser = new ol.parser.GeoJSON();
var json = { var json = {
@@ -445,8 +340,7 @@ describe('ol.parser.GeoJSON', function() {
} }
}] }]
}; };
var result = parser.parseAsFeatureCollection_(json, var result = parser.parseAsFeatureCollection_(json);
{callback: callback});
var features = result.features; var features = result.features;
expect(features.length).to.be(2); expect(features.length).to.be(2);
@@ -461,30 +355,10 @@ describe('ol.parser.GeoJSON', function() {
expect(second.get('bam')).to.be('baz'); expect(second.get('bam')).to.be('baz');
expect(second.getGeometry()).to.be.a(ol.geom.LineString); expect(second.getGeometry()).to.be.a(ol.geom.LineString);
expect(pointVertices.coordinates.length).to.be(2);
expect(lineVertices.coordinates.length).to.be(4);
expect(polygonVertices.coordinates.length).to.be(0);
expect(result.metadata.projection).to.be('EPSG:4326'); expect(result.metadata.projection).to.be('EPSG:4326');
}); });
it('generates an array of features for Feature', function() { it('generates an array of features for Feature', function() {
var pointVertices = new ol.geom.SharedVertices();
var lineVertices = new ol.geom.SharedVertices();
var polygonVertices = new ol.geom.SharedVertices();
var lookup = {
'point': pointVertices,
'linestring': lineVertices,
'polygon': polygonVertices,
'multipoint': pointVertices,
'multilinstring': lineVertices,
'multipolygon': polygonVertices
};
var callback = function(feature, type) {
return lookup[type];
};
var parser = new ol.parser.GeoJSON(); var parser = new ol.parser.GeoJSON();
var json = { var json = {
@@ -497,8 +371,7 @@ describe('ol.parser.GeoJSON', function() {
coordinates: [[1, 2], [3, 4]] coordinates: [[1, 2], [3, 4]]
} }
}; };
var result = parser.parseAsFeatureCollection_(json, var result = parser.parseAsFeatureCollection_(json);
{callback: callback});
var features = result.features; var features = result.features;
expect(features.length).to.be(1); expect(features.length).to.be(1);
@@ -508,30 +381,10 @@ describe('ol.parser.GeoJSON', function() {
expect(first.get('bam')).to.be('baz'); expect(first.get('bam')).to.be('baz');
expect(first.getGeometry()).to.be.a(ol.geom.LineString); expect(first.getGeometry()).to.be.a(ol.geom.LineString);
expect(pointVertices.coordinates.length).to.be(0);
expect(lineVertices.coordinates.length).to.be(4);
expect(polygonVertices.coordinates.length).to.be(0);
expect(result.metadata.projection).to.be('EPSG:4326'); expect(result.metadata.projection).to.be('EPSG:4326');
}); });
it('generates an array of features for GeometryCollection', function() { it('generates an array of features for GeometryCollection', function() {
var pointVertices = new ol.geom.SharedVertices();
var lineVertices = new ol.geom.SharedVertices();
var polygonVertices = new ol.geom.SharedVertices();
var lookup = {
'point': pointVertices,
'linestring': lineVertices,
'polygon': polygonVertices,
'multipoint': pointVertices,
'multilinstring': lineVertices,
'multipolygon': polygonVertices
};
var callback = function(feature, type) {
return lookup[type];
};
var parser = new ol.parser.GeoJSON(); var parser = new ol.parser.GeoJSON();
var json = { var json = {
@@ -547,8 +400,7 @@ describe('ol.parser.GeoJSON', function() {
coordinates: [[[7, 8], [9, 10], [11, 12], [7, 8]]] coordinates: [[[7, 8], [9, 10], [11, 12], [7, 8]]]
}] }]
}; };
var result = parser.parseAsFeatureCollection_(json, var result = parser.parseAsFeatureCollection_(json);
{callback: callback});
var features = result.features; var features = result.features;
expect(features.length).to.be(3); expect(features.length).to.be(3);
@@ -557,124 +409,57 @@ describe('ol.parser.GeoJSON', function() {
expect(features[1].getGeometry()).to.be.a(ol.geom.LineString); expect(features[1].getGeometry()).to.be.a(ol.geom.LineString);
expect(features[2].getGeometry()).to.be.a(ol.geom.Polygon); expect(features[2].getGeometry()).to.be.a(ol.geom.Polygon);
expect(pointVertices.coordinates.length).to.be(2);
expect(lineVertices.coordinates.length).to.be(4);
expect(polygonVertices.coordinates.length).to.be(8);
expect(result.metadata.projection).to.be('EPSG:4326'); expect(result.metadata.projection).to.be('EPSG:4326');
}); });
it('generates an array of features for Point', function() { it('generates an array of features for Point', function() {
var pointVertices = new ol.geom.SharedVertices();
var lineVertices = new ol.geom.SharedVertices();
var polygonVertices = new ol.geom.SharedVertices();
var lookup = {
'point': pointVertices,
'linestring': lineVertices,
'polygon': polygonVertices,
'multipoint': pointVertices,
'multilinstring': lineVertices,
'multipolygon': polygonVertices
};
var callback = function(feature, type) {
return lookup[type];
};
var parser = new ol.parser.GeoJSON(); var parser = new ol.parser.GeoJSON();
var json = { var json = {
type: 'Point', type: 'Point',
coordinates: [1, 2] coordinates: [1, 2]
}; };
var result = parser.parseAsFeatureCollection_(json, var result = parser.parseAsFeatureCollection_(json);
{callback: callback});
var features = result.features; var features = result.features;
expect(features.length).to.be(1); expect(features.length).to.be(1);
expect(features[0].getGeometry()).to.be.a(ol.geom.Point); expect(features[0].getGeometry()).to.be.a(ol.geom.Point);
expect(pointVertices.coordinates.length).to.be(2);
expect(lineVertices.coordinates.length).to.be(0);
expect(polygonVertices.coordinates.length).to.be(0);
expect(result.metadata.projection).to.be('EPSG:4326'); expect(result.metadata.projection).to.be('EPSG:4326');
}); });
it('generates an array of features for LineString', function() { it('generates an array of features for LineString', function() {
var pointVertices = new ol.geom.SharedVertices();
var lineVertices = new ol.geom.SharedVertices();
var polygonVertices = new ol.geom.SharedVertices();
var lookup = {
'point': pointVertices,
'linestring': lineVertices,
'polygon': polygonVertices,
'multipoint': pointVertices,
'multilinstring': lineVertices,
'multipolygon': polygonVertices
};
var callback = function(feature, type) {
return lookup[type];
};
var parser = new ol.parser.GeoJSON(); var parser = new ol.parser.GeoJSON();
var json = { var json = {
type: 'LineString', type: 'LineString',
coordinates: [[3, 4], [5, 6]] coordinates: [[3, 4], [5, 6]]
}; };
var result = parser.parseAsFeatureCollection_(json, var result = parser.parseAsFeatureCollection_(json);
{callback: callback});
var features = result.features; var features = result.features;
expect(features.length).to.be(1); expect(features.length).to.be(1);
expect(features[0].getGeometry()).to.be.a(ol.geom.LineString); expect(features[0].getGeometry()).to.be.a(ol.geom.LineString);
expect(pointVertices.coordinates.length).to.be(0);
expect(lineVertices.coordinates.length).to.be(4);
expect(polygonVertices.coordinates.length).to.be(0);
expect(result.metadata.projection).to.be('EPSG:4326'); expect(result.metadata.projection).to.be('EPSG:4326');
}); });
it('generates an array of features for Polygon', function() { it('generates an array of features for Polygon', function() {
var pointVertices = new ol.geom.SharedVertices();
var lineVertices = new ol.geom.SharedVertices();
var polygonVertices = new ol.geom.SharedVertices();
var lookup = {
'point': pointVertices,
'linestring': lineVertices,
'polygon': polygonVertices,
'multipoint': pointVertices,
'multilinstring': lineVertices,
'multipolygon': polygonVertices
};
var callback = function(feature, type) {
return lookup[type];
};
var parser = new ol.parser.GeoJSON(); var parser = new ol.parser.GeoJSON();
var json = { var json = {
type: 'Polygon', type: 'Polygon',
coordinates: [[[7, 8], [9, 10], [11, 12], [7, 8]]] coordinates: [[[7, 8], [9, 10], [11, 12], [7, 8]]]
}; };
var result = parser.parseAsFeatureCollection_(json, var result = parser.parseAsFeatureCollection_(json);
{callback: callback});
var features = result.features; var features = result.features;
expect(features.length).to.be(1); expect(features.length).to.be(1);
expect(features[0].getGeometry()).to.be.a(ol.geom.Polygon); expect(features[0].getGeometry()).to.be.a(ol.geom.Polygon);
expect(pointVertices.coordinates.length).to.be(0);
expect(lineVertices.coordinates.length).to.be(0);
expect(polygonVertices.coordinates.length).to.be(8);
expect(result.metadata.projection).to.be('EPSG:4326'); expect(result.metadata.projection).to.be('EPSG:4326');
}); });
@@ -690,5 +475,4 @@ goog.require('ol.geom.LinearRing');
goog.require('ol.geom.LineString'); goog.require('ol.geom.LineString');
goog.require('ol.geom.Point'); goog.require('ol.geom.Point');
goog.require('ol.geom.Polygon'); goog.require('ol.geom.Polygon');
goog.require('ol.geom.SharedVertices');
goog.require('ol.parser.GeoJSON'); goog.require('ol.parser.GeoJSON');
-3
View File
@@ -15,7 +15,6 @@ describe('ol.parser.KML', function() {
var geom = obj.features[0].getGeometry(); var geom = obj.features[0].getGeometry();
expect(obj.features[0].getId()).to.eql('KML.Polygon'); expect(obj.features[0].getId()).to.eql('KML.Polygon');
expect(geom instanceof ol.geom.Polygon).to.be.ok(); expect(geom instanceof ol.geom.Polygon).to.be.ok();
expect(geom.dimension).to.eql(3);
done(); done();
}); });
}); });
@@ -28,7 +27,6 @@ describe('ol.parser.KML', function() {
expect(obj.features.length).to.eql(2); expect(obj.features.length).to.eql(2);
var geom = obj.features[0].getGeometry(); var geom = obj.features[0].getGeometry();
expect(geom instanceof ol.geom.LineString).to.be.ok(); expect(geom instanceof ol.geom.LineString).to.be.ok();
expect(geom.dimension).to.eql(3);
geom = obj.features[1].getGeometry(); geom = obj.features[1].getGeometry();
expect(geom instanceof ol.geom.LineString).to.be.ok(); expect(geom instanceof ol.geom.LineString).to.be.ok();
done(); done();
@@ -43,7 +41,6 @@ describe('ol.parser.KML', function() {
expect(obj.features.length).to.eql(1); expect(obj.features.length).to.eql(1);
var geom = obj.features[0].getGeometry(); var geom = obj.features[0].getGeometry();
expect(geom instanceof ol.geom.Point).to.be.ok(); expect(geom instanceof ol.geom.Point).to.be.ok();
expect(geom.dimension).to.eql(3);
done(); done();
}); });
}); });
+2 -24
View File
@@ -56,33 +56,12 @@ describe('ol.parser.TopoJSON', function() {
describe('#readFeaturesFromString()', function() { describe('#readFeaturesFromString()', function() {
it('parses world-110m.geojson with shared vertices', function(done) { it('parses world-110m.geojson', function(done) {
afterLoadText('spec/ol/parser/topojson/world-110m.json', function(text) { afterLoadText('spec/ol/parser/topojson/world-110m.json', function(text) {
var pointVertices = new ol.geom.SharedVertices(); var result = parser.readFeaturesFromString(text);
var lineVertices = new ol.geom.SharedVertices();
var polygonVertices = new ol.geom.SharedVertices();
var lookup = {
'point': pointVertices,
'linestring': lineVertices,
'polygon': polygonVertices,
'multipoint': pointVertices,
'multilinstring': lineVertices,
'multipolygon': polygonVertices
};
var callback = function(feature, type) {
return lookup[type];
};
var result = parser.readFeaturesFromString(text, {callback: callback});
expect(result.features.length).to.be(178); expect(result.features.length).to.be(178);
expect(pointVertices.coordinates.length).to.be(0);
expect(lineVertices.coordinates.length).to.be(0);
expect(polygonVertices.coordinates.length).to.be(31400);
var first = result.features[0]; var first = result.features[0];
expect(first).to.be.a(ol.Feature); expect(first).to.be.a(ol.Feature);
var firstGeom = first.getGeometry(); var firstGeom = first.getGeometry();
@@ -110,6 +89,5 @@ describe('ol.parser.TopoJSON', function() {
goog.require('ol.Feature'); goog.require('ol.Feature');
goog.require('ol.geom.MultiPolygon'); goog.require('ol.geom.MultiPolygon');
goog.require('ol.geom.Polygon'); goog.require('ol.geom.Polygon');
goog.require('ol.geom.SharedVertices');
goog.require('ol.parser.Parser'); goog.require('ol.parser.Parser');
goog.require('ol.parser.TopoJSON'); goog.require('ol.parser.TopoJSON');