Merge pull request #1830 from twpayne/geom-fixes

Extra geometry functions
This commit is contained in:
Tom Payne
2014-03-12 13:24:45 +01:00
15 changed files with 345 additions and 14 deletions

View File

@@ -1,4 +1,5 @@
@exportSymbol ol.geom.LineString
@exportProperty ol.geom.LineString.prototype.appendCoordinate
@exportProperty ol.geom.LineString.prototype.clone
@exportProperty ol.geom.LineString.prototype.getCoordinateAtM
@exportProperty ol.geom.LineString.prototype.getCoordinates

View File

@@ -1,5 +1,7 @@
goog.provide('ol.geom.LineString');
goog.require('goog.array');
goog.require('goog.asserts');
goog.require('ol.extent');
goog.require('ol.geom.GeometryType');
goog.require('ol.geom.SimpleGeometry');
@@ -50,6 +52,20 @@ ol.geom.LineString = function(coordinates, opt_layout) {
goog.inherits(ol.geom.LineString, ol.geom.SimpleGeometry);
/**
* @param {ol.Coordinate} coordinate Coordinate.
*/
ol.geom.LineString.prototype.appendCoordinate = function(coordinate) {
goog.asserts.assert(coordinate.length == this.stride);
if (goog.isNull(this.flatCoordinates)) {
this.flatCoordinates = coordinate.slice();
} else {
goog.array.extend(this.flatCoordinates, coordinate);
}
this.dispatchChangeEvent();
};
/**
* @inheritDoc
*/

View File

@@ -1,7 +1,9 @@
@exportSymbol ol.geom.MultiLineString
@exportProperty ol.geom.MultiLineString.prototype.appendLineString
@exportProperty ol.geom.MultiLineString.prototype.clone
@exportProperty ol.geom.MultiLineString.prototype.getCoordinateAtM
@exportProperty ol.geom.MultiLineString.prototype.getCoordinates
@exportProperty ol.geom.MultiLineString.prototype.getLineString
@exportProperty ol.geom.MultiLineString.prototype.getLineStrings
@exportProperty ol.geom.MultiLineString.prototype.getType
@exportProperty ol.geom.MultiLineString.prototype.setCoordinates

View File

@@ -47,6 +47,22 @@ ol.geom.MultiLineString = function(coordinates, opt_layout) {
goog.inherits(ol.geom.MultiLineString, ol.geom.SimpleGeometry);
/**
* @param {ol.geom.LineString} lineString LineString.
*/
ol.geom.MultiLineString.prototype.appendLineString = function(lineString) {
goog.asserts.assert(lineString.getLayout() == this.layout);
if (goog.isNull(this.flatCoordinates)) {
this.flatCoordinates = lineString.getFlatCoordinates().slice();
} else {
goog.array.extend(
this.flatCoordinates, lineString.getFlatCoordinates().slice());
}
this.ends_.push(this.flatCoordinates.length);
this.dispatchChangeEvent();
};
/**
* @inheritDoc
*/
@@ -131,17 +147,40 @@ ol.geom.MultiLineString.prototype.getEnds = function() {
};
/**
* @param {number} index Index.
* @return {ol.geom.LineString} LineString.
*/
ol.geom.MultiLineString.prototype.getLineString = function(index) {
goog.asserts.assert(0 <= index && index < this.ends_.length);
if (index < 0 || this.ends_.length <= index) {
return null;
}
var lineString = new ol.geom.LineString(null);
lineString.setFlatCoordinates(this.layout, this.flatCoordinates.slice(
index === 0 ? 0 : this.ends_[index - 1], this.ends_[index]));
return lineString;
};
/**
* @return {Array.<ol.geom.LineString>} LineStrings.
* @todo stability experimental
*/
ol.geom.MultiLineString.prototype.getLineStrings = function() {
// FIXME we should construct the line strings from the flat coordinates
var coordinates = this.getCoordinates();
var flatCoordinates = this.flatCoordinates;
var ends = this.ends_;
var layout = this.layout;
/** @type {Array.<ol.geom.LineString>} */
var lineStrings = [];
var offset = 0;
var i, ii;
for (i = 0, ii = coordinates.length; i < ii; ++i) {
lineStrings.push(new ol.geom.LineString(coordinates[i]));
for (i = 0, ii = ends.length; i < ii; ++i) {
var end = ends[i];
var lineString = new ol.geom.LineString(null);
lineString.setFlatCoordinates(layout, flatCoordinates.slice(offset, end));
lineStrings.push(lineString);
offset = end;
}
return lineStrings;
};
@@ -222,6 +261,13 @@ ol.geom.MultiLineString.prototype.setCoordinates =
*/
ol.geom.MultiLineString.prototype.setFlatCoordinates =
function(layout, flatCoordinates, ends) {
if (goog.isNull(flatCoordinates)) {
goog.asserts.assert(!goog.isNull(ends) && ends.length === 0);
} else if (ends.length === 0) {
goog.asserts.assert(flatCoordinates.length === 0);
} else {
goog.asserts.assert(flatCoordinates.length == ends[ends.length - 1]);
}
this.setFlatCoordinatesInternal(layout, flatCoordinates);
this.ends_ = ends;
this.dispatchChangeEvent();

View File

@@ -1,6 +1,8 @@
@exportSymbol ol.geom.MultiPoint
@exportProperty ol.geom.MultiPoint.prototype.appendPoint
@exportProperty ol.geom.MultiPoint.prototype.clone
@exportProperty ol.geom.MultiPoint.prototype.getCoordinates
@exportProperty ol.geom.MultiPoint.prototype.getPoint
@exportProperty ol.geom.MultiPoint.prototype.getPoints
@exportProperty ol.geom.MultiPoint.prototype.getType
@exportProperty ol.geom.MultiPoint.prototype.setCoordinates

View File

@@ -1,5 +1,7 @@
goog.provide('ol.geom.MultiPoint');
goog.require('goog.array');
goog.require('goog.asserts');
goog.require('ol.extent');
goog.require('ol.geom.GeometryType');
goog.require('ol.geom.Point');
@@ -22,6 +24,20 @@ ol.geom.MultiPoint = function(coordinates, opt_layout) {
goog.inherits(ol.geom.MultiPoint, ol.geom.SimpleGeometry);
/**
* @param {ol.geom.Point} point Point.
*/
ol.geom.MultiPoint.prototype.appendPoint = function(point) {
goog.asserts.assert(point.getLayout() == this.layout);
if (goog.isNull(this.flatCoordinates)) {
this.flatCoordinates = point.getFlatCoordinates().slice();
} else {
goog.array.extend(this.flatCoordinates, point.getFlatCoordinates());
}
this.dispatchChangeEvent();
};
/**
* @inheritDoc
*/
@@ -69,17 +85,39 @@ ol.geom.MultiPoint.prototype.getCoordinates = function() {
};
/**
* @param {number} index Index.
* @return {ol.geom.Point} Point.
*/
ol.geom.MultiPoint.prototype.getPoint = function(index) {
var n = goog.isNull(this.flatCoordinates) ?
0 : this.flatCoordinates.length / this.stride;
goog.asserts.assert(0 <= index && index < n);
if (index < 0 || n <= index) {
return null;
}
var point = new ol.geom.Point(null);
point.setFlatCoordinates(this.layout, this.flatCoordinates.slice(
index * this.stride, (index + 1) * this.stride));
return point;
};
/**
* @return {Array.<ol.geom.Point>} Points.
* @todo stability experimental
*/
ol.geom.MultiPoint.prototype.getPoints = function() {
// FIXME we should construct the points from the flat coordinates
var coordinates = this.getCoordinates();
var flatCoordinates = this.flatCoordinates;
var layout = this.layout;
var stride = this.stride;
/** @type {Array.<ol.geom.Point>} */
var points = [];
var i, ii;
for (i = 0, ii = coordinates.length; i < ii; ++i) {
points.push(new ol.geom.Point(coordinates[i]));
for (i = 0, ii = flatCoordinates.length; i < ii; i += stride) {
var point = new ol.geom.Point(null);
point.setFlatCoordinates(layout, flatCoordinates.slice(i, i + stride));
points.push(point);
}
return points;
};

View File

@@ -1,8 +1,10 @@
@exportSymbol ol.geom.MultiPolygon
@exportProperty ol.geom.MultiPolygon.prototype.appendPolygon
@exportProperty ol.geom.MultiPolygon.prototype.clone
@exportProperty ol.geom.MultiPolygon.prototype.getArea
@exportProperty ol.geom.MultiPolygon.prototype.getCoordinates
@exportProperty ol.geom.MultiPolygon.prototype.getInteriorPoints
@exportProperty ol.geom.MultiPolygon.prototype.getPolygon
@exportProperty ol.geom.MultiPolygon.prototype.getPolygons
@exportProperty ol.geom.MultiPolygon.prototype.getType
@exportProperty ol.geom.MultiPolygon.prototype.setCoordinates

View File

@@ -72,6 +72,31 @@ ol.geom.MultiPolygon = function(coordinates, opt_layout) {
goog.inherits(ol.geom.MultiPolygon, ol.geom.SimpleGeometry);
/**
* @param {ol.geom.Polygon} polygon Polygon.
*/
ol.geom.MultiPolygon.prototype.appendPolygon = function(polygon) {
goog.asserts.assert(polygon.getLayout() == this.layout);
/** @type {Array.<number>} */
var ends;
if (goog.isNull(this.flatCoordinates)) {
this.flatCoordinates = polygon.getFlatCoordinates().slice();
ends = polygon.getEnds().slice();
this.endss_.push();
} else {
var offset = this.flatCoordinates.length;
goog.array.extend(this.flatCoordinates, polygon.getFlatCoordinates());
ends = polygon.getEnds().slice();
var i, ii;
for (i = 0, ii = ends.length; i < ii; ++i) {
ends[i] += offset;
}
}
this.endss_.push(ends);
this.dispatchChangeEvent();
};
/**
* @inheritDoc
*/
@@ -205,6 +230,37 @@ ol.geom.MultiPolygon.prototype.getSimplifiedGeometryInternal =
};
/**
* @param {number} index Index.
* @return {ol.geom.Polygon} Polygon.
*/
ol.geom.MultiPolygon.prototype.getPolygon = function(index) {
goog.asserts.assert(0 <= index && index < this.endss_.length);
if (index < 0 || this.endss_.length <= index) {
return null;
}
var offset;
if (index === 0) {
offset = 0;
} else {
var prevEnds = this.endss_[index - 1];
offset = prevEnds[prevEnds.length - 1];
}
var ends = this.endss_[index].slice();
var end = ends[ends.length - 1];
if (offset !== 0) {
var i, ii;
for (i = 0, ii = ends.length; i < ii; ++i) {
ends[i] -= offset;
}
}
var polygon = new ol.geom.Polygon(null);
polygon.setFlatCoordinates(
this.layout, this.flatCoordinates.slice(offset, end), ends);
return polygon;
};
/**
* @return {Array.<ol.geom.Polygon>} Polygons.
* @todo stability experimental
@@ -217,17 +273,16 @@ ol.geom.MultiPolygon.prototype.getPolygons = function() {
var offset = 0;
var i, ii, j, jj;
for (i = 0, ii = endss.length; i < ii; ++i) {
var ends = endss[i];
var ends = endss[i].slice();
var end = ends[ends.length - 1];
var polygon = new ol.geom.Polygon(null);
var polygonEnds = ends.slice();
if (offset !== 0) {
for (j = 0, jj = polygonEnds.length; j < jj; ++j) {
polygonEnds[j] = polygonEnds[j] - offset;
for (j = 0, jj = ends.length; j < jj; ++j) {
ends[j] -= offset;
}
}
var polygon = new ol.geom.Polygon(null);
polygon.setFlatCoordinates(
layout, flatCoordinates.slice(offset, end), polygonEnds);
layout, flatCoordinates.slice(offset, end), ends);
polygons.push(polygon);
offset = end;
}
@@ -274,6 +329,14 @@ ol.geom.MultiPolygon.prototype.setCoordinates =
*/
ol.geom.MultiPolygon.prototype.setFlatCoordinates =
function(layout, flatCoordinates, endss) {
goog.asserts.assert(!goog.isNull(endss));
if (goog.isNull(flatCoordinates) || flatCoordinates.length === 0) {
goog.asserts.assert(endss.length === 0);
} else {
goog.asserts.assert(endss.length > 0);
var ends = endss[endss.length - 1];
goog.asserts.assert(flatCoordinates.length == ends[ends.length - 1]);
}
this.setFlatCoordinatesInternal(layout, flatCoordinates);
this.endss_ = endss;
this.dispatchChangeEvent();

View File

@@ -1,8 +1,10 @@
@exportSymbol ol.geom.Polygon
@exportProperty ol.geom.Polygon.prototype.appendLinearRing
@exportProperty ol.geom.Polygon.prototype.clone
@exportProperty ol.geom.Polygon.prototype.getArea
@exportProperty ol.geom.Polygon.prototype.getCoordinates
@exportProperty ol.geom.Polygon.prototype.getInteriorPoint
@exportProperty ol.geom.Polygon.prototype.getLinearRing
@exportProperty ol.geom.Polygon.prototype.getLinearRings
@exportProperty ol.geom.Polygon.prototype.getType
@exportProperty ol.geom.Polygon.prototype.setCoordinates

View File

@@ -1,5 +1,7 @@
goog.provide('ol.geom.Polygon');
goog.require('goog.array');
goog.require('goog.asserts');
goog.require('ol.extent');
goog.require('ol.geom.GeometryType');
goog.require('ol.geom.LinearRing');
@@ -70,6 +72,21 @@ ol.geom.Polygon = function(coordinates, opt_layout) {
goog.inherits(ol.geom.Polygon, ol.geom.SimpleGeometry);
/**
* @param {ol.geom.LinearRing} linearRing Linear ring.
*/
ol.geom.Polygon.prototype.appendLinearRing = function(linearRing) {
goog.asserts.assert(linearRing.getLayout() == this.layout);
if (goog.isNull(this.flatCoordinates)) {
this.flatCoordinates = linearRing.getFlatCoordinates().slice();
} else {
goog.array.extend(this.flatCoordinates, linearRing.getFlatCoordinates());
}
this.ends_.push(this.flatCoordinates.length);
this.dispatchChangeEvent();
};
/**
* @inheritDoc
*/
@@ -161,6 +178,22 @@ ol.geom.Polygon.prototype.getInteriorPoint = function() {
};
/**
* @param {number} index Index.
* @return {ol.geom.LinearRing} Linear ring.
*/
ol.geom.Polygon.prototype.getLinearRing = function(index) {
goog.asserts.assert(0 <= index && index < this.ends_.length);
if (index < 0 || this.ends_.length <= index) {
return null;
}
var linearRing = new ol.geom.LinearRing(null);
linearRing.setFlatCoordinates(this.layout, this.flatCoordinates.slice(
index === 0 ? 0 : this.ends_[index - 1], this.ends_[index]));
return linearRing;
};
/**
* @return {Array.<ol.geom.LinearRing>} Linear rings.
* @todo stability experimental
@@ -257,6 +290,13 @@ ol.geom.Polygon.prototype.setCoordinates = function(coordinates, opt_layout) {
*/
ol.geom.Polygon.prototype.setFlatCoordinates =
function(layout, flatCoordinates, ends) {
if (goog.isNull(flatCoordinates)) {
goog.asserts.assert(!goog.isNull(ends) && ends.length === 0);
} else if (ends.length === 0) {
goog.asserts.assert(flatCoordinates.length === 0);
} else {
goog.asserts.assert(flatCoordinates.length == ends[ends.length - 1]);
}
this.setFlatCoordinatesInternal(layout, flatCoordinates);
this.ends_ = ends;
this.dispatchChangeEvent();