Allow geometries to use a shared vertex array
The ol.geom.SharedVertices structure represents a flattened array of vertex coordinates. This is intended to support optimal WebGL rendering.
This commit is contained in:
79
src/ol/geom/abstractcollection.js
Normal file
79
src/ol/geom/abstractcollection.js
Normal file
@@ -0,0 +1,79 @@
|
||||
goog.provide('ol.geom.AbstractCollection');
|
||||
|
||||
goog.require('ol.Extent');
|
||||
goog.require('ol.geom.Geometry');
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* A collection of geometries. This constructor is not to be used directly.
|
||||
*
|
||||
* @constructor
|
||||
* @extends {ol.geom.Geometry}
|
||||
*/
|
||||
ol.geom.AbstractCollection = function() {
|
||||
goog.base(this);
|
||||
|
||||
/**
|
||||
* @type {number}
|
||||
*/
|
||||
this.dimension;
|
||||
|
||||
/**
|
||||
* @type {Array.<ol.geom.Geometry>}
|
||||
*/
|
||||
this.components = null;
|
||||
|
||||
/**
|
||||
* @type {ol.Extent}
|
||||
* @protected
|
||||
*/
|
||||
this.bounds = null;
|
||||
|
||||
};
|
||||
goog.inherits(ol.geom.AbstractCollection, ol.geom.Geometry);
|
||||
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
ol.geom.AbstractCollection.prototype.getBounds = function() {
|
||||
if (goog.isNull(this.bounds)) {
|
||||
var minX,
|
||||
minY = minX = Number.POSITIVE_INFINITY,
|
||||
maxX,
|
||||
maxY = maxX = Number.NEGATIVE_INFINITY,
|
||||
components = this.components,
|
||||
len = components.length,
|
||||
bounds, i;
|
||||
|
||||
for (i = 0; i < len; ++i) {
|
||||
bounds = components[i].getBounds();
|
||||
minX = Math.min(bounds.minX, minX);
|
||||
minY = Math.min(bounds.minY, minY);
|
||||
maxX = Math.max(bounds.maxX, maxX);
|
||||
maxY = Math.max(bounds.maxY, maxY);
|
||||
}
|
||||
this.bounds = new ol.Extent(minX, minY, maxX, maxY);
|
||||
}
|
||||
return this.bounds;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
ol.geom.AbstractCollection.prototype.getCoordinates = function() {
|
||||
var count = this.components.length;
|
||||
var coordinates = new Array(count);
|
||||
for (var i = 0; i < count; ++i) {
|
||||
coordinates[i] = this.components[i].getCoordinates();
|
||||
}
|
||||
return coordinates;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
ol.geom.AbstractCollection.prototype.getType = goog.abstractMethod;
|
||||
14
src/ol/geom/base.js
Normal file
14
src/ol/geom/base.js
Normal file
@@ -0,0 +1,14 @@
|
||||
goog.provide('ol.geom.Vertex');
|
||||
goog.provide('ol.geom.VertexArray');
|
||||
|
||||
|
||||
/**
|
||||
* @typedef {Array.<number>}
|
||||
*/
|
||||
ol.geom.Vertex;
|
||||
|
||||
|
||||
/**
|
||||
* @typedef {Array.<ol.geom.Vertex>}
|
||||
*/
|
||||
ol.geom.VertexArray;
|
||||
@@ -1,15 +1,23 @@
|
||||
goog.require('ol.Extent');
|
||||
goog.provide('ol.geom.Coordinate');
|
||||
goog.provide('ol.geom.CoordinateArray');
|
||||
goog.provide('ol.geom.Geometry');
|
||||
goog.provide('ol.geom.GeometryType');
|
||||
|
||||
goog.require('ol.Extent');
|
||||
goog.require('ol.geom.SharedVertices');
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
*/
|
||||
ol.geom.Geometry = function() {};
|
||||
ol.geom.Geometry = function() {
|
||||
|
||||
/**
|
||||
* @type {ol.geom.SharedVertices}
|
||||
* @protected
|
||||
*/
|
||||
this.vertices = null;
|
||||
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
@@ -26,6 +34,21 @@ ol.geom.Geometry.prototype.dimension;
|
||||
ol.geom.Geometry.prototype.getBounds = goog.abstractMethod;
|
||||
|
||||
|
||||
/**
|
||||
* @return {Array} The GeoJSON style coordinates array for the geometry.
|
||||
*/
|
||||
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.
|
||||
* @return {ol.geom.GeometryType} The geometry type.
|
||||
@@ -33,18 +56,6 @@ ol.geom.Geometry.prototype.getBounds = goog.abstractMethod;
|
||||
ol.geom.Geometry.prototype.getType = goog.abstractMethod;
|
||||
|
||||
|
||||
/**
|
||||
* @typedef {Array.<number>}
|
||||
*/
|
||||
ol.geom.Coordinate;
|
||||
|
||||
|
||||
/**
|
||||
* @typedef {Array.<ol.geom.Coordinate>}
|
||||
*/
|
||||
ol.geom.CoordinateArray;
|
||||
|
||||
|
||||
/**
|
||||
* @enum {string}
|
||||
*/
|
||||
|
||||
@@ -1,65 +1,43 @@
|
||||
goog.provide('ol.geom.GeometryCollection');
|
||||
|
||||
goog.require('ol.Extent');
|
||||
goog.require('ol.geom.AbstractCollection');
|
||||
goog.require('ol.geom.Geometry');
|
||||
goog.require('ol.geom.GeometryType');
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* A collection of geometries. This constructor should not called. Instead
|
||||
* create one of the fixed type collections.
|
||||
* A mixed collection of geometries. Used one of the fixed type multi-part
|
||||
* constructors for collections of the same type.
|
||||
*
|
||||
* @constructor
|
||||
* @extends {ol.geom.Geometry}
|
||||
* @extends {ol.geom.AbstractCollection}
|
||||
* @param {Array.<ol.geom.Geometry>} geometries Array of geometries.
|
||||
*/
|
||||
ol.geom.GeometryCollection = function() {
|
||||
|
||||
ol.geom.GeometryCollection = function(geometries) {
|
||||
goog.base(this);
|
||||
|
||||
/**
|
||||
* @type {Array.<ol.geom.Geometry>}
|
||||
*/
|
||||
this.components = null;
|
||||
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;
|
||||
|
||||
/**
|
||||
* @type {ol.Extent}
|
||||
* @protected
|
||||
*/
|
||||
this.bounds = null;
|
||||
this.dimension = dimension;
|
||||
|
||||
};
|
||||
goog.inherits(ol.geom.GeometryCollection, ol.geom.Geometry);
|
||||
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
ol.geom.GeometryCollection.prototype.getBounds = function() {
|
||||
if (goog.isNull(this.bounds)) {
|
||||
var minX,
|
||||
minY = minX = Number.POSITIVE_INFINITY,
|
||||
maxX,
|
||||
maxY = maxX = Number.NEGATIVE_INFINITY,
|
||||
components = this.components,
|
||||
len = components.length,
|
||||
bounds, i;
|
||||
|
||||
for (i = 0; i < len; ++i) {
|
||||
bounds = components[i].getBounds();
|
||||
minX = Math.min(bounds.minX, minX);
|
||||
minY = Math.min(bounds.minY, minY);
|
||||
maxX = Math.max(bounds.maxX, maxX);
|
||||
maxY = Math.max(bounds.maxY, maxY);
|
||||
}
|
||||
this.bounds = new ol.Extent(minX, minY, maxX, maxY);
|
||||
}
|
||||
return this.bounds;
|
||||
};
|
||||
goog.inherits(ol.geom.GeometryCollection, ol.geom.AbstractCollection);
|
||||
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,20 +1,21 @@
|
||||
goog.provide('ol.geom.LinearRing');
|
||||
|
||||
goog.require('ol.geom.CoordinateArray');
|
||||
goog.require('ol.geom.GeometryType');
|
||||
goog.require('ol.geom.LineString');
|
||||
goog.require('ol.geom.SharedVertices');
|
||||
goog.require('ol.geom.VertexArray');
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
* @extends {ol.geom.LineString}
|
||||
* @param {ol.geom.CoordinateArray} coordinates Coordinates array (e.g.
|
||||
* [[x0, y0], [x1, y1], [x0, y0]]).
|
||||
* @param {ol.geom.VertexArray} coordinates Vertex array (e.g.
|
||||
* [[x0, y0], [x1, y1]]).
|
||||
* @param {ol.geom.SharedVertices=} opt_shared Shared vertices.
|
||||
*/
|
||||
ol.geom.LinearRing = function(coordinates) {
|
||||
|
||||
goog.base(this, coordinates);
|
||||
ol.geom.LinearRing = function(coordinates, opt_shared) {
|
||||
goog.base(this, coordinates, opt_shared);
|
||||
|
||||
/**
|
||||
* We're intentionally not enforcing that rings be closed right now. This
|
||||
@@ -30,5 +31,5 @@ goog.inherits(ol.geom.LinearRing, ol.geom.LineString);
|
||||
* @inheritDoc
|
||||
*/
|
||||
ol.geom.LinearRing.prototype.getType = function() {
|
||||
return ol.geom.GeometryType.GEOMETRYCOLLECTION;
|
||||
return ol.geom.GeometryType.LINEARRING;
|
||||
};
|
||||
|
||||
@@ -2,44 +2,47 @@ goog.provide('ol.geom.LineString');
|
||||
|
||||
goog.require('goog.asserts');
|
||||
goog.require('ol.Extent');
|
||||
goog.require('ol.geom.CoordinateArray');
|
||||
goog.require('ol.geom.Geometry');
|
||||
goog.require('ol.geom.GeometryType');
|
||||
goog.require('ol.geom.SharedVertices');
|
||||
goog.require('ol.geom.VertexArray');
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
* @extends {ol.geom.Geometry}
|
||||
* @param {ol.geom.CoordinateArray} coordinates Coordinates array (e.g.
|
||||
* @param {ol.geom.VertexArray} coordinates Vertex array (e.g.
|
||||
* [[x0, y0], [x1, y1]]).
|
||||
* @param {ol.geom.SharedVertices=} opt_shared Shared vertices.
|
||||
*/
|
||||
ol.geom.LineString = function(coordinates) {
|
||||
|
||||
ol.geom.LineString = function(coordinates, opt_shared) {
|
||||
goog.base(this);
|
||||
goog.asserts.assert(goog.isArray(coordinates[0]));
|
||||
|
||||
// assume the same dimension for all coordinates
|
||||
var dimension = coordinates[0].length,
|
||||
count = coordinates.length,
|
||||
length = count * dimension;
|
||||
var vertices = opt_shared,
|
||||
dimension;
|
||||
|
||||
if (!goog.isDef(vertices)) {
|
||||
dimension = coordinates[0].length;
|
||||
vertices = new ol.geom.SharedVertices({dimension: dimension});
|
||||
}
|
||||
|
||||
/**
|
||||
* @type {Array}
|
||||
* @type {ol.geom.SharedVertices}
|
||||
*/
|
||||
this.coordinates = new Array(length);
|
||||
var i, offset, j;
|
||||
for (i = 0; i < count; ++i) {
|
||||
goog.asserts.assert(coordinates[i].length === dimension);
|
||||
offset = i * dimension;
|
||||
for (j = 0; j < dimension; ++j) {
|
||||
this.coordinates[offset + j] = coordinates[i][j];
|
||||
}
|
||||
}
|
||||
this.vertices = vertices;
|
||||
|
||||
/**
|
||||
* @type {string}
|
||||
* @private
|
||||
*/
|
||||
this.sharedId_ = vertices.add(coordinates);
|
||||
|
||||
/**
|
||||
* @type {number}
|
||||
*/
|
||||
this.dimension = dimension;
|
||||
this.dimension = vertices.getDimension();
|
||||
goog.asserts.assert(this.dimension >= 2);
|
||||
|
||||
/**
|
||||
@@ -52,6 +55,45 @@ ol.geom.LineString = function(coordinates) {
|
||||
goog.inherits(ol.geom.LineString, ol.geom.Geometry);
|
||||
|
||||
|
||||
/**
|
||||
* Get a vertex coordinate value for the given dimension.
|
||||
* @param {number} index Vertex index.
|
||||
* @param {number} dim Coordinate dimension.
|
||||
* @return {number} The vertex coordinate value.
|
||||
*/
|
||||
ol.geom.LineString.prototype.get = function(index, dim) {
|
||||
return this.vertices.get(this.sharedId_, index, dim);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
* @return {ol.geom.VertexArray} Coordinates array.
|
||||
*/
|
||||
ol.geom.LineString.prototype.getCoordinates = function() {
|
||||
var count = this.getCount();
|
||||
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;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Get the count of vertices in this linestring.
|
||||
* @return {number} The vertex count.
|
||||
*/
|
||||
ol.geom.LineString.prototype.getCount = function() {
|
||||
return this.vertices.getCount(this.sharedId_);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
@@ -61,14 +103,15 @@ ol.geom.LineString.prototype.getBounds = function() {
|
||||
minY = minX = Number.POSITIVE_INFINITY,
|
||||
maxX,
|
||||
maxY = maxX = Number.NEGATIVE_INFINITY,
|
||||
coordinates = this.coordinates,
|
||||
len = coordinates.length,
|
||||
dim = this.dimension,
|
||||
vertices = this.vertices,
|
||||
id = this.sharedId_,
|
||||
count = vertices.getCount(id),
|
||||
dimension = this.dimension,
|
||||
x, y, i;
|
||||
|
||||
for (i = 0; i < len; i += dim) {
|
||||
x = coordinates[i];
|
||||
y = coordinates[i + 1];
|
||||
for (i = 0; i < count; ++i) {
|
||||
x = vertices.get(id, i, 0);
|
||||
y = vertices.get(id, i, 1);
|
||||
minX = Math.min(minX, x);
|
||||
minY = Math.min(minY, y);
|
||||
maxX = Math.max(maxX, x);
|
||||
@@ -86,3 +129,12 @@ ol.geom.LineString.prototype.getBounds = function() {
|
||||
ol.geom.LineString.prototype.getType = function() {
|
||||
return ol.geom.GeometryType.LINESTRING;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Get the identifier used to mark this line in the shared vertices structure.
|
||||
* @return {string} The identifier.
|
||||
*/
|
||||
ol.geom.LineString.prototype.getSharedId = function() {
|
||||
return this.sharedId_;
|
||||
};
|
||||
|
||||
@@ -1,45 +1,50 @@
|
||||
goog.provide('ol.geom.MultiLineString');
|
||||
|
||||
goog.require('goog.asserts');
|
||||
goog.require('ol.geom.CoordinateArray');
|
||||
goog.require('ol.geom.GeometryCollection');
|
||||
goog.require('ol.geom.AbstractCollection');
|
||||
goog.require('ol.geom.GeometryType');
|
||||
goog.require('ol.geom.LineString');
|
||||
goog.require('ol.geom.SharedVertices');
|
||||
goog.require('ol.geom.VertexArray');
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
* @extends {ol.geom.GeometryCollection}
|
||||
* @param {Array.<ol.geom.CoordinateArray>} coordinates Coordinates array.
|
||||
* @extends {ol.geom.AbstractCollection}
|
||||
* @param {Array.<ol.geom.VertexArray>} coordinates Coordinates array.
|
||||
* @param {ol.geom.SharedVertices=} opt_shared Shared vertices.
|
||||
*/
|
||||
ol.geom.MultiLineString = function(coordinates) {
|
||||
ol.geom.MultiLineString = function(coordinates, opt_shared) {
|
||||
goog.base(this);
|
||||
goog.asserts.assert(goog.isArray(coordinates[0][0]));
|
||||
|
||||
var numParts = coordinates.length,
|
||||
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;
|
||||
|
||||
/**
|
||||
* @type {Array.<ol.geom.LineString>}
|
||||
*/
|
||||
this.components = new Array(numParts);
|
||||
for (var i = 0; i < numParts; ++i) {
|
||||
this.components[i] = new ol.geom.LineString(coordinates[i]);
|
||||
if (!goog.isDef(dimension)) {
|
||||
dimension = this.components[i].dimension;
|
||||
} else {
|
||||
goog.asserts.assert(this.components[i].dimension === dimension);
|
||||
}
|
||||
this.components[i] = new ol.geom.LineString(coordinates[i], vertices);
|
||||
}
|
||||
|
||||
/**
|
||||
* @type {number}
|
||||
*/
|
||||
this.dimension = dimension;
|
||||
goog.asserts.assert(this.dimension >= 2);
|
||||
this.dimension = vertices.getDimension();
|
||||
|
||||
};
|
||||
goog.inherits(ol.geom.MultiLineString, ol.geom.GeometryCollection);
|
||||
goog.inherits(ol.geom.MultiLineString, ol.geom.AbstractCollection);
|
||||
|
||||
|
||||
/**
|
||||
@@ -48,3 +53,20 @@ goog.inherits(ol.geom.MultiLineString, ol.geom.GeometryCollection);
|
||||
ol.geom.MultiLineString.prototype.getType = function() {
|
||||
return ol.geom.GeometryType.MULTILINESTRING;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Create a multi-linestring geometry from an array of linestring 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.
|
||||
*/
|
||||
ol.geom.MultiLineString.fromParts = function(geometries, opt_shared) {
|
||||
var count = geometries.length;
|
||||
var coordinates = new Array(count);
|
||||
for (var i = 0; i < count; ++i) {
|
||||
coordinates[i] = geometries[i].getCoordinates();
|
||||
}
|
||||
return new ol.geom.MultiLineString(coordinates, opt_shared);
|
||||
};
|
||||
|
||||
@@ -1,45 +1,55 @@
|
||||
goog.provide('ol.geom.MultiPoint');
|
||||
|
||||
goog.require('goog.asserts');
|
||||
goog.require('ol.geom.CoordinateArray');
|
||||
goog.require('ol.geom.GeometryCollection');
|
||||
goog.require('ol.geom.AbstractCollection');
|
||||
goog.require('ol.geom.GeometryType');
|
||||
goog.require('ol.geom.Point');
|
||||
goog.require('ol.geom.SharedVertices');
|
||||
goog.require('ol.geom.VertexArray');
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
* @extends {ol.geom.GeometryCollection}
|
||||
* @param {ol.geom.CoordinateArray} coordinates Coordinates array.
|
||||
* @extends {ol.geom.AbstractCollection}
|
||||
* @param {ol.geom.VertexArray} coordinates Coordinates array.
|
||||
* @param {ol.geom.SharedVertices=} opt_shared Shared vertices.
|
||||
*/
|
||||
ol.geom.MultiPoint = function(coordinates) {
|
||||
ol.geom.MultiPoint = function(coordinates, opt_shared) {
|
||||
goog.base(this);
|
||||
goog.asserts.assert(goog.isArray(coordinates[0]));
|
||||
|
||||
var numParts = coordinates.length,
|
||||
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;
|
||||
|
||||
/**
|
||||
* @type {Array.<ol.geom.Point>}
|
||||
*/
|
||||
this.components = new Array(numParts);
|
||||
for (var i = 0; i < numParts; ++i) {
|
||||
this.components[i] = new ol.geom.Point(coordinates[i]);
|
||||
if (!goog.isDef(dimension)) {
|
||||
dimension = this.components[i].dimension;
|
||||
} else {
|
||||
goog.asserts.assert(this.components[i].dimension === dimension);
|
||||
}
|
||||
this.components[i] = new ol.geom.Point(coordinates[i], vertices);
|
||||
}
|
||||
|
||||
/**
|
||||
* @type {number}
|
||||
*/
|
||||
this.dimension = dimension;
|
||||
goog.asserts.assert(this.dimension >= 2);
|
||||
this.dimension = vertices.getDimension();
|
||||
|
||||
};
|
||||
goog.inherits(ol.geom.MultiPoint, ol.geom.GeometryCollection);
|
||||
goog.inherits(ol.geom.MultiPoint, ol.geom.AbstractCollection);
|
||||
|
||||
|
||||
/**
|
||||
@@ -48,3 +58,20 @@ goog.inherits(ol.geom.MultiPoint, ol.geom.GeometryCollection);
|
||||
ol.geom.MultiPoint.prototype.getType = function() {
|
||||
return ol.geom.GeometryType.MULTIPOINT;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Create a multi-point geometry from an array of point 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.
|
||||
*/
|
||||
ol.geom.MultiPoint.fromParts = function(geometries, opt_shared) {
|
||||
var count = geometries.length;
|
||||
var coordinates = new Array(count);
|
||||
for (var i = 0; i < count; ++i) {
|
||||
coordinates[i] = geometries[i].getCoordinates();
|
||||
}
|
||||
return new ol.geom.MultiPoint(coordinates, opt_shared);
|
||||
};
|
||||
|
||||
@@ -1,46 +1,51 @@
|
||||
goog.provide('ol.geom.MultiPolygon');
|
||||
|
||||
goog.require('goog.asserts');
|
||||
goog.require('ol.geom.CoordinateArray');
|
||||
goog.require('ol.geom.GeometryCollection');
|
||||
goog.require('ol.geom.AbstractCollection');
|
||||
goog.require('ol.geom.GeometryType');
|
||||
goog.require('ol.geom.Polygon');
|
||||
goog.require('ol.geom.SharedVertices');
|
||||
goog.require('ol.geom.VertexArray');
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
* @extends {ol.geom.GeometryCollection}
|
||||
* @param {Array.<Array.<ol.geom.CoordinateArray>>} coordinates Coordinates
|
||||
* @extends {ol.geom.AbstractCollection}
|
||||
* @param {Array.<Array.<ol.geom.VertexArray>>} coordinates Coordinates
|
||||
* array.
|
||||
* @param {ol.geom.SharedVertices=} opt_shared Shared vertices.
|
||||
*/
|
||||
ol.geom.MultiPolygon = function(coordinates) {
|
||||
ol.geom.MultiPolygon = function(coordinates, opt_shared) {
|
||||
goog.base(this);
|
||||
goog.asserts.assert(goog.isArray(coordinates[0][0][0]));
|
||||
|
||||
var numParts = coordinates.length,
|
||||
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;
|
||||
|
||||
/**
|
||||
* @type {Array.<ol.geom.Polygon>}
|
||||
*/
|
||||
this.components = new Array(numParts);
|
||||
for (var i = 0; i < numParts; ++i) {
|
||||
this.components[i] = new ol.geom.Polygon(coordinates[i]);
|
||||
if (!goog.isDef(dimension)) {
|
||||
dimension = this.components[i].dimension;
|
||||
} else {
|
||||
goog.asserts.assert(this.components[i].dimension === dimension);
|
||||
}
|
||||
this.components[i] = new ol.geom.Polygon(coordinates[i], vertices);
|
||||
}
|
||||
|
||||
/**
|
||||
* @type {number}
|
||||
*/
|
||||
this.dimension = dimension;
|
||||
goog.asserts.assert(this.dimension >= 2);
|
||||
this.dimension = vertices.getDimension();
|
||||
|
||||
};
|
||||
goog.inherits(ol.geom.MultiPolygon, ol.geom.GeometryCollection);
|
||||
goog.inherits(ol.geom.MultiPolygon, ol.geom.AbstractCollection);
|
||||
|
||||
|
||||
/**
|
||||
@@ -49,3 +54,20 @@ goog.inherits(ol.geom.MultiPolygon, ol.geom.GeometryCollection);
|
||||
ol.geom.MultiPolygon.prototype.getType = function() {
|
||||
return ol.geom.GeometryType.MULTIPOLYGON;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Create a multi-polygon geometry from an array of polygon 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.
|
||||
*/
|
||||
ol.geom.MultiPolygon.fromParts = function(geometries, opt_shared) {
|
||||
var count = geometries.length;
|
||||
var coordinates = new Array(count);
|
||||
for (var i = 0; i < count; ++i) {
|
||||
coordinates[i] = geometries[i].getCoordinates();
|
||||
}
|
||||
return new ol.geom.MultiPolygon(coordinates, opt_shared);
|
||||
};
|
||||
|
||||
@@ -2,30 +2,45 @@ goog.provide('ol.geom.Point');
|
||||
|
||||
goog.require('goog.asserts');
|
||||
goog.require('ol.Extent');
|
||||
goog.require('ol.geom.Coordinate');
|
||||
goog.require('ol.geom.Geometry');
|
||||
goog.require('ol.geom.GeometryType');
|
||||
goog.require('ol.geom.SharedVertices');
|
||||
goog.require('ol.geom.Vertex');
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
* @extends {ol.geom.Geometry}
|
||||
* @param {ol.geom.Coordinate} coordinates Coordinates array (e.g. [x, y]).
|
||||
* @param {ol.geom.Vertex} coordinates Coordinates array (e.g. [x, y]).
|
||||
* @param {ol.geom.SharedVertices=} opt_shared Shared vertices.
|
||||
*/
|
||||
ol.geom.Point = function(coordinates) {
|
||||
|
||||
ol.geom.Point = function(coordinates, opt_shared) {
|
||||
goog.base(this);
|
||||
|
||||
var vertices = opt_shared,
|
||||
dimension;
|
||||
|
||||
if (!goog.isDef(vertices)) {
|
||||
dimension = coordinates.length;
|
||||
vertices = new ol.geom.SharedVertices({dimension: dimension});
|
||||
}
|
||||
|
||||
/**
|
||||
* @type {Array}
|
||||
* @type {ol.geom.SharedVertices}
|
||||
*/
|
||||
this.coordinates = coordinates;
|
||||
this.vertices = vertices;
|
||||
|
||||
/**
|
||||
* @type {string}
|
||||
* @private
|
||||
*/
|
||||
this.sharedId_ = vertices.add([coordinates]);
|
||||
|
||||
/**
|
||||
* @type {number}
|
||||
*/
|
||||
this.dimension = coordinates.length;
|
||||
this.dimension = vertices.getDimension();
|
||||
goog.asserts.assert(this.dimension >= 2);
|
||||
|
||||
/**
|
||||
@@ -38,22 +53,53 @@ ol.geom.Point = function(coordinates) {
|
||||
goog.inherits(ol.geom.Point, ol.geom.Geometry);
|
||||
|
||||
|
||||
/**
|
||||
* @param {number} dim Coordinate dimension.
|
||||
* @return {number} The coordinate value.
|
||||
*/
|
||||
ol.geom.Point.prototype.get = function(dim) {
|
||||
return this.vertices.get(this.sharedId_, 0, dim);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
ol.geom.Point.prototype.getBounds = function() {
|
||||
if (goog.isNull(this.bounds_)) {
|
||||
var x = this.coordinates[0],
|
||||
y = this.coordinates[1];
|
||||
var x = this.get(0),
|
||||
y = this.get(1);
|
||||
this.bounds_ = new ol.Extent(x, y, x, y);
|
||||
}
|
||||
return this.bounds_;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
* @return {ol.geom.Vertex} Coordinates array.
|
||||
*/
|
||||
ol.geom.Point.prototype.getCoordinates = function() {
|
||||
var coordinates = new Array(this.dimension);
|
||||
for (var i = 0; i < this.dimension; ++i) {
|
||||
coordinates[i] = this.get(i);
|
||||
}
|
||||
return coordinates;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
ol.geom.Point.prototype.getType = function() {
|
||||
return ol.geom.GeometryType.POINT;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Get the identifier used to mark this point in the shared vertices structure.
|
||||
* @return {string} The identifier.
|
||||
*/
|
||||
ol.geom.Point.prototype.getSharedId = function() {
|
||||
return this.sharedId_;
|
||||
};
|
||||
|
||||
@@ -2,43 +2,53 @@ goog.provide('ol.geom.Polygon');
|
||||
|
||||
goog.require('goog.asserts');
|
||||
goog.require('ol.Extent');
|
||||
goog.require('ol.geom.CoordinateArray');
|
||||
goog.require('ol.geom.Geometry');
|
||||
goog.require('ol.geom.GeometryType');
|
||||
goog.require('ol.geom.LinearRing');
|
||||
goog.require('ol.geom.SharedVertices');
|
||||
goog.require('ol.geom.VertexArray');
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
* @extends {ol.geom.Geometry}
|
||||
* @param {Array.<ol.geom.CoordinateArray>} coordinates Array of rings. First
|
||||
* @param {Array.<ol.geom.VertexArray>} coordinates Array of rings. First
|
||||
* is outer, any remaining are inner.
|
||||
* @param {ol.geom.SharedVertices=} opt_shared Shared vertices.
|
||||
*/
|
||||
ol.geom.Polygon = function(coordinates) {
|
||||
|
||||
ol.geom.Polygon = function(coordinates, opt_shared) {
|
||||
goog.base(this);
|
||||
goog.asserts.assert(goog.isArray(coordinates[0][0]));
|
||||
|
||||
var numRings = coordinates.length,
|
||||
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});
|
||||
}
|
||||
|
||||
/**
|
||||
* @type {ol.geom.SharedVertices}
|
||||
*/
|
||||
this.vertices = vertices;
|
||||
|
||||
var numRings = coordinates.length;
|
||||
|
||||
/**
|
||||
* @type {Array.<ol.geom.LinearRing>}
|
||||
*/
|
||||
this.rings = new Array(numRings);
|
||||
for (var i = 0; i < numRings; ++i) {
|
||||
this.rings[i] = new ol.geom.LinearRing(coordinates[i]);
|
||||
if (!goog.isDef(dimension)) {
|
||||
dimension = this.rings[i].dimension;
|
||||
} else {
|
||||
goog.asserts.assert(this.rings[i].dimension === dimension);
|
||||
}
|
||||
this.rings[i] = new ol.geom.LinearRing(coordinates[i], vertices);
|
||||
}
|
||||
|
||||
/**
|
||||
* @type {number}
|
||||
*/
|
||||
this.dimension = dimension;
|
||||
this.dimension = vertices.getDimension();
|
||||
goog.asserts.assert(this.dimension >= 2);
|
||||
|
||||
/**
|
||||
@@ -59,6 +69,19 @@ ol.geom.Polygon.prototype.getBounds = function() {
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @return {Array.<ol.geom.VertexArray>} Coordinates array.
|
||||
*/
|
||||
ol.geom.Polygon.prototype.getCoordinates = function() {
|
||||
var count = this.rings.length;
|
||||
var coordinates = new Array(count);
|
||||
for (var i = 0; i < count; ++i) {
|
||||
coordinates[i] = this.rings[i].getCoordinates();
|
||||
}
|
||||
return coordinates;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
|
||||
195
src/ol/geom/sharedvertices.js
Normal file
195
src/ol/geom/sharedvertices.js
Normal file
@@ -0,0 +1,195 @@
|
||||
goog.provide('ol.geom.SharedVertices');
|
||||
|
||||
goog.require('goog.asserts');
|
||||
goog.require('ol.geom.Vertex');
|
||||
goog.require('ol.geom.VertexArray');
|
||||
|
||||
|
||||
/**
|
||||
* @typedef {{dimension: (number),
|
||||
* offset: (ol.geom.Vertex|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 = goog.isDef(opt_options) ? opt_options : {};
|
||||
|
||||
/**
|
||||
* @type {number}
|
||||
* @private
|
||||
*/
|
||||
this.counter_ = 0;
|
||||
|
||||
/**
|
||||
* @type {Array.<number>}
|
||||
*/
|
||||
this.coordinates = [];
|
||||
|
||||
/**
|
||||
* 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_);
|
||||
|
||||
/**
|
||||
* @type {Object}
|
||||
* @private
|
||||
*/
|
||||
this.lookup_ = {};
|
||||
|
||||
/**
|
||||
* @type {Array.<string>}
|
||||
* @private
|
||||
*/
|
||||
this.ids_ = [];
|
||||
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Adds a vertex array to the shared coordinate array.
|
||||
* @param {ol.geom.VertexArray} vertices Array of vertices.
|
||||
* @return {string} 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];
|
||||
goog.asserts.assert(vertex.length == dimension);
|
||||
if (!offset) {
|
||||
Array.prototype.push.apply(this.coordinates, vertex);
|
||||
} else {
|
||||
index = start + (i * dimension);
|
||||
for (var j = 0; j < dimension; ++j) {
|
||||
this.coordinates[index + j] = vertex[j] - offset[j];
|
||||
}
|
||||
}
|
||||
}
|
||||
var id = this.getId_();
|
||||
var idIndex = this.ids_.push(id) - 1;
|
||||
this.lookup_[id] = {
|
||||
idIndex: idIndex,
|
||||
start: start,
|
||||
count: count
|
||||
};
|
||||
return id;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {string} 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(dim <= this.dimension_);
|
||||
goog.asserts.assert(this.lookup_.hasOwnProperty(id));
|
||||
goog.asserts.assert(index < this.lookup_[id].count);
|
||||
var start = this.lookup_[id].start;
|
||||
var value = this.coordinates[start + (index * this.dimension_) + dim];
|
||||
if (this.offset_) {
|
||||
value += this.offset_[dim];
|
||||
}
|
||||
return value;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {string} 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(this.lookup_.hasOwnProperty(id));
|
||||
return this.lookup_[id].count;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Gets an identifier that is unique for this instance.
|
||||
* @return {string} Identifier.
|
||||
* @private
|
||||
*/
|
||||
ol.geom.SharedVertices.prototype.getId_ = function() {
|
||||
return String(++this.counter_);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @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 {string} 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(this.lookup_.hasOwnProperty(id));
|
||||
return this.lookup_[id].start;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {number} id The vertex array identifier (returned by add).
|
||||
* @return {ol.geom.VertexArray} The removed vertex array.
|
||||
*/
|
||||
ol.geom.SharedVertices.prototype.remove = function(id) {
|
||||
goog.asserts.assert(this.lookup_.hasOwnProperty(id));
|
||||
var info = this.lookup_[id];
|
||||
var dimension = this.dimension_;
|
||||
var length = info.count * dimension;
|
||||
var removed = this.coordinates.splice(info.start, length);
|
||||
var offset = this.offset_;
|
||||
var array = new Array(info.count);
|
||||
var vertex;
|
||||
for (var i = 0; i < info.count; ++i) {
|
||||
vertex = new Array(dimension);
|
||||
for (var j = 0; j < dimension; ++j) {
|
||||
vertex[j] = removed[(i * dimension) + j] + (offset ? offset[j] : 0);
|
||||
}
|
||||
array[i] = vertex;
|
||||
}
|
||||
delete this.lookup_[id];
|
||||
this.ids_.splice(info.idIndex, 1);
|
||||
var afterInfo;
|
||||
for (var k = info.idIndex, kk = this.ids_.length; k < kk; ++k) {
|
||||
afterInfo = this.lookup_[this.ids_[k]];
|
||||
afterInfo.idIndex -= 1;
|
||||
afterInfo.start -= length;
|
||||
}
|
||||
return array;
|
||||
};
|
||||
@@ -8,6 +8,9 @@ goog.require('ol.Pixel');
|
||||
goog.require('ol.canvas');
|
||||
goog.require('ol.geom.Geometry');
|
||||
goog.require('ol.geom.GeometryType');
|
||||
goog.require('ol.geom.LineString');
|
||||
goog.require('ol.geom.Point');
|
||||
goog.require('ol.geom.Polygon');
|
||||
goog.require('ol.style.LineLiteral');
|
||||
goog.require('ol.style.PointLiteral');
|
||||
goog.require('ol.style.PolygonLiteral');
|
||||
@@ -108,20 +111,18 @@ ol.renderer.canvas.Renderer.prototype.renderLineStringFeatures_ =
|
||||
function(features, symbolizer) {
|
||||
|
||||
var context = this.context_,
|
||||
i, ii, line, coords, dim, j, jj, x, y;
|
||||
i, ii, line, dim, j, jj, x, y;
|
||||
|
||||
context.globalAlpha = symbolizer.opacity;
|
||||
context.strokeStyle = symbolizer.strokeStyle;
|
||||
context.lineWidth = symbolizer.strokeWidth * this.inverseScale_;
|
||||
context.beginPath();
|
||||
|
||||
for (i = 0, ii = features.length; i < ii; ++i) {
|
||||
line = features[i].getGeometry();
|
||||
line = /** @type {ol.geom.LineString} */ features[i].getGeometry();
|
||||
dim = line.dimension;
|
||||
coords = line.coordinates;
|
||||
for (j = 0, jj = coords.length; j < jj; j += dim) {
|
||||
x = coords[j];
|
||||
y = coords[j + 1];
|
||||
for (j = 0, jj = line.getCount(); j < jj; ++j) {
|
||||
x = line.get(j, 0);
|
||||
y = line.get(j, 1);
|
||||
if (j === 0) {
|
||||
context.moveTo(x, y);
|
||||
} else {
|
||||
@@ -143,7 +144,7 @@ ol.renderer.canvas.Renderer.prototype.renderPointFeatures_ =
|
||||
function(features, symbolizer) {
|
||||
|
||||
var context = this.context_,
|
||||
canvas, i, ii, coords, vec;
|
||||
canvas, i, ii, point, vec;
|
||||
|
||||
if (symbolizer instanceof ol.style.ShapeLiteral) {
|
||||
canvas = ol.renderer.canvas.Renderer.renderShape(symbolizer);
|
||||
@@ -156,9 +157,9 @@ ol.renderer.canvas.Renderer.prototype.renderPointFeatures_ =
|
||||
context.setTransform(1, 0, 0, 1, -mid, -mid);
|
||||
context.globalAlpha = 1;
|
||||
for (i = 0, ii = features.length; i < ii; ++i) {
|
||||
coords = features[i].getGeometry().coordinates;
|
||||
point = /** @type {ol.geom.Point} */ features[i].getGeometry();
|
||||
vec = goog.vec.Mat4.multVec3(
|
||||
this.transform_, [coords[0], coords[1], 0], []);
|
||||
this.transform_, [point.get(0), point.get(1), 0], []);
|
||||
context.drawImage(canvas, vec[0], vec[1]);
|
||||
}
|
||||
context.restore();
|
||||
@@ -176,7 +177,7 @@ ol.renderer.canvas.Renderer.prototype.renderPolygonFeatures_ =
|
||||
var context = this.context_,
|
||||
strokeStyle = symbolizer.strokeStyle,
|
||||
fillStyle = symbolizer.fillStyle,
|
||||
i, ii, poly, rings, numRings, coords, dim, j, jj, x, y;
|
||||
i, ii, poly, rings, numRings, ring, dim, j, jj, x, y;
|
||||
|
||||
context.globalAlpha = symbolizer.opacity;
|
||||
if (strokeStyle) {
|
||||
@@ -196,7 +197,7 @@ ol.renderer.canvas.Renderer.prototype.renderPolygonFeatures_ =
|
||||
*/
|
||||
context.beginPath();
|
||||
for (i = 0, ii = features.length; i < ii; ++i) {
|
||||
poly = features[i].getGeometry();
|
||||
poly = /** @type {ol.geom.Polygon} */ features[i].getGeometry();
|
||||
dim = poly.dimension;
|
||||
rings = poly.rings;
|
||||
numRings = rings.length;
|
||||
@@ -205,10 +206,10 @@ ol.renderer.canvas.Renderer.prototype.renderPolygonFeatures_ =
|
||||
// TODO: use sketch canvas to render outer and punch holes for inner rings
|
||||
throw new Error('Rendering holes not implemented');
|
||||
} else {
|
||||
coords = rings[0].coordinates;
|
||||
for (j = 0, jj = coords.length; j < jj; j += dim) {
|
||||
x = coords[j];
|
||||
y = coords[j + 1];
|
||||
ring = rings[0];
|
||||
for (j = 0, jj = ring.getCount(); j < jj; ++j) {
|
||||
x = ring.get(j, 0);
|
||||
y = ring.get(j, 1);
|
||||
if (j === 0) {
|
||||
context.moveTo(x, y);
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user