From 4ea2fe37013d50ac28a0effc1185350f09e0513c Mon Sep 17 00:00:00 2001 From: Mike Adair Date: Wed, 20 Jun 2012 13:33:20 -0400 Subject: [PATCH] adding features --- src/api/feature.js | 89 +++++++++++++++++++++++++++++++++++ src/ol.js | 2 + src/ol/Feature.js | 76 ++++++++++++++++++++++++++++++ src/ol/Map.js | 3 +- test/index.html | 2 + test/spec/api/feature.test.js | 79 +++++++++++++++++++++++++++++++ test/spec/api/map.test.js | 4 ++ test/spec/ol/Feature.test.js | 79 +++++++++++++++++++++++++++++++ 8 files changed, 332 insertions(+), 2 deletions(-) create mode 100644 src/api/feature.js create mode 100644 src/ol/Feature.js create mode 100644 test/spec/api/feature.test.js create mode 100644 test/spec/ol/Feature.test.js diff --git a/src/api/feature.js b/src/api/feature.js new file mode 100644 index 0000000000..f1d20283e7 --- /dev/null +++ b/src/api/feature.js @@ -0,0 +1,89 @@ +goog.provide('ol.feature'); + +goog.require('ol.Feature'); +goog.require('ol.geom.Geometry'); + + +/** + * @typedef {ol.Feature|Object|string} + */ +ol.FeatureLike; + + +/** + * @export + * @param {ol.FeatureLike=} opt_arg Argument. + * @return {ol.Feature} Feature. + */ +ol.feature = function(opt_arg){ + + /** @type {Object|undefined} */ + var attrs; + /** @type {ol.geom.Geometry|undefined} */ + var geom; + /** @type {string|undefined} */ + var type; + + if (arguments.length == 1) { + if (opt_arg instanceof ol.Feature) { + return opt_arg; + } + else if (goog.isObject(opt_arg)) { + attrs = opt_arg['attrs']; + geom = opt_arg['geom']; + type = opt_arg['type']; + } + else { + throw new Error('ol.feature'); + } + } + + var feature = new ol.Feature(); + if (goog.isDef(type) && type == 'Feature') { + //this means it is a GeoJSON object + //format.read(opt_arg); + + } else { + if (goog.isDef(attrs)) { + feature.setAttributes(attrs); + } + if (goog.isDef(geom)) { + feature.setGeometry(geom); + } + } + return feature; + +}; + +/** + * @param {!string} attr The name of the attribute to be set. + * @param {string|number|boolean} value The value of the attribute to be set. + * @returns {ol.Feature} The feature so calls can be chained + */ +ol.Feature.prototype.set = function(attr, value) { + this.setAttribute(attr, value); + return this; +}; + +/** + * @param {!string} attr The name of the attribute to be set. + * @returns {string|number|boolean|undefined} The attribute value requested. + */ +ol.Feature.prototype.get = function(attr) { + return this.getAttribute(attr); +}; + +/** + * @param {ol.geom.Geometry=} opt_arg + * @returns {ol.Feature|ol.geom.Geometry|undefined} get or set the geometry on a feature + */ +ol.Feature.prototype.geometry = function(opt_arg) { + if (arguments.length == 1 && goog.isDef(opt_arg)) { + this.setGeometry(opt_arg); + return this; + } else { + return this.getGeometry(); + } +}; + + diff --git a/src/ol.js b/src/ol.js index c35cd1c93a..a108bd3f3e 100644 --- a/src/ol.js +++ b/src/ol.js @@ -5,6 +5,7 @@ goog.require('ol.event.Events'); goog.require('ol.event.drag'); goog.require("ol.map"); goog.require("ol.loc"); +goog.require("ol.feature"); goog.require("ol.projection"); goog.require("ol.layer.xyz"); goog.require("ol.Tile"); @@ -12,3 +13,4 @@ goog.require("ol.TileSet"); goog.require("ol.geom.geometry"); goog.require("ol.geom.point"); goog.require('ol.layer.XYZ'); + diff --git a/src/ol/Feature.js b/src/ol/Feature.js new file mode 100644 index 0000000000..1df98df634 --- /dev/null +++ b/src/ol/Feature.js @@ -0,0 +1,76 @@ +goog.provide('ol.Feature'); + +goog.require('ol.geom.Geometry'); + + + +/** + * @constructor + */ +ol.Feature = function() { + + /** + * @private + * @type {ol.geom.Geometry} + */ + this.geometry_ = null; + + /** + * @private + * @type {Object} + */ + this.attributes_ = {}; + +}; + +/** + * @return {ol.geom.Geometry} The geometry associated with the feature. + */ +ol.Feature.prototype.getGeometry = function() { + return this.geometry_; +}; + +/** + * @param {ol.geom.Geometry} geom the geometry for the feature. + */ +ol.Feature.prototype.setGeometry = function(geom) { + this.geometry_ = geom; +}; + + +/** + * @param {!string} name the attribute value to retrieve. + @return {string|number|boolean} the attribute value. + */ +ol.Feature.prototype.getAttribute = function(name) { + return this.attributes_[name]; +}; + + +/** + * @param {!string} name of the attribute to set. + * @param {string|number|boolean} value the attribute value to set. + */ +ol.Feature.prototype.setAttribute = function(name, value) { + this.attributes_[name] = value; +}; + + +/** + * @param {Object} attrs An json structure containing key/value pairs. + */ +ol.Feature.prototype.setAttributes = function(attrs) { + for (var key in attrs) { + this.setAttribute(key, attrs[key]); + } +}; + + +/** +*/ +ol.Feature.prototype.destroy = function() { + //remove attributes and geometry, etc. + for (var key in this) { + delete this[key]; + } +}; diff --git a/src/ol/Map.js b/src/ol/Map.js index 385ee61dca..0f51c7cdb5 100644 --- a/src/ol/Map.js +++ b/src/ol/Map.js @@ -93,8 +93,7 @@ ol.Map.DEFAULT_TILE_SIZE = 256; */ ol.Map.prototype.getCenter = function() { var proj = this.getUserProjection(); - this.center_ = this.center_.transform(proj); - return this.center_; + return this.center_.transform(proj); }; diff --git a/test/index.html b/test/index.html index 1aae5a2d53..981c89b3cd 100644 --- a/test/index.html +++ b/test/index.html @@ -53,12 +53,14 @@ + +