adding features
This commit is contained in:
89
src/api/feature.js
Normal file
89
src/api/feature.js
Normal file
@@ -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();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -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');
|
||||
|
||||
|
||||
76
src/ol/Feature.js
Normal file
76
src/ol/Feature.js
Normal file
@@ -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];
|
||||
}
|
||||
};
|
||||
@@ -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);
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -53,12 +53,14 @@
|
||||
<script type="text/javascript" src="spec/api/map.test.js"></script>
|
||||
<script type="text/javascript" src="spec/api/projection.test.js"></script>
|
||||
<script type="text/javascript" src="spec/api/layer/xyz.test.js"></script>
|
||||
<script type="text/javascript" src="spec/api/feature.test.js"></script>
|
||||
<script type="text/javascript" src="spec/ol/Events.test.js"></script>
|
||||
<script type="text/javascript" src="spec/ol/Projection.test.js"></script>
|
||||
<script type="text/javascript" src="spec/ol/Tile.test.js"></script>
|
||||
<script type="text/javascript" src="spec/ol/TileSet.test.js"></script>
|
||||
<script type="text/javascript" src="spec/ol/geom/Point.test.js"></script>
|
||||
<script type="text/javascript" src="spec/ol/layer/XYZ.test.js"></script>
|
||||
<script type="text/javascript" src="spec/ol/Feature.test.js"></script>
|
||||
|
||||
<script type="text/javascript">
|
||||
(function() {
|
||||
|
||||
79
test/spec/api/feature.test.js
Normal file
79
test/spec/api/feature.test.js
Normal file
@@ -0,0 +1,79 @@
|
||||
describe("ol.feature", function() {
|
||||
|
||||
it("should be easy to make a feature", function() {
|
||||
var feat = ol.feature();
|
||||
|
||||
expect(feat).toBeA(ol.Feature);
|
||||
});
|
||||
|
||||
it("should be easy to set feature attribute", function() {
|
||||
|
||||
var feat = ol.feature();
|
||||
feat.set('foo', 'bar');
|
||||
|
||||
expect(feat).toBeA(ol.Feature);
|
||||
expect(feat.get('foo')).toBe('bar');
|
||||
});
|
||||
|
||||
it("calling set with one argument", function() {
|
||||
|
||||
var feat = ol.feature();
|
||||
feat.set('foo');
|
||||
|
||||
expect(feat.get('foo')).toBe(undefined);
|
||||
});
|
||||
|
||||
it("should be easy to set feature geometry", function() {
|
||||
|
||||
var feat = ol.feature();
|
||||
var point = ol.geom.point([21, 4]);
|
||||
feat.geometry(point);
|
||||
|
||||
var geom = feat.geometry();
|
||||
expect(feat).toBeA(ol.Feature);
|
||||
expect(geom.getX()).toBe(21);
|
||||
expect(geom.getY()).toBe(4);
|
||||
});
|
||||
|
||||
it("should be easy to create a feature from object literals", function() {
|
||||
|
||||
var feat = ol.feature({
|
||||
attrs: {
|
||||
foo: 'bar',
|
||||
two: 'deux',
|
||||
size: 3,
|
||||
flag: true
|
||||
},
|
||||
geom: ol.geom.point([56, 22])
|
||||
});
|
||||
|
||||
var geom = feat.geometry();
|
||||
expect(feat).toBeA(ol.Feature);
|
||||
expect(geom.getX()).toBe(56);
|
||||
expect(geom.getY()).toBe(22);
|
||||
expect(feat.get('foo')).toBe('bar');
|
||||
expect(feat.get('two')).toBe('deux');
|
||||
expect(feat.get('size')).toBe(3);
|
||||
expect(feat.get('flag')).toBe(true);
|
||||
});
|
||||
|
||||
/*
|
||||
it("should be easy to create a feature from GeoJSON", function() {
|
||||
|
||||
var geoJson = {
|
||||
type: "Feature",
|
||||
geometry: {type: "Point", coordinates: [102.0, 0.5]},
|
||||
properties: {prop0: "value0"}
|
||||
};
|
||||
var feat = ol.feature(geoJson);
|
||||
|
||||
var geom = feat.geometry();
|
||||
expect(feat).toBeA(ol.Feature);
|
||||
expect(geom.getX()).toBe(102.0);
|
||||
expect(geom.getY()).toBe(0.5);
|
||||
expect(feat.get('prop0')).toBe('value0');
|
||||
});
|
||||
*/
|
||||
|
||||
});
|
||||
|
||||
@@ -137,6 +137,10 @@ describe("ol.map", function() {
|
||||
expect(center.x().toFixed(3)).toBe("1113194.908");
|
||||
expect(center.y().toFixed(3)).toBe("2273030.927");
|
||||
|
||||
//make sure the center doesn't change
|
||||
expect(center.x().toFixed(3)).toBe("1113194.908");
|
||||
expect(center.y().toFixed(3)).toBe("2273030.927");
|
||||
|
||||
});
|
||||
|
||||
it("provides feedback when you mess up", function() {
|
||||
|
||||
79
test/spec/ol/Feature.test.js
Normal file
79
test/spec/ol/Feature.test.js
Normal file
@@ -0,0 +1,79 @@
|
||||
describe("ol.Feature", function() {
|
||||
|
||||
it("should be easy to make a feature", function() {
|
||||
var feat = new ol.Feature();
|
||||
|
||||
expect(feat).toBeA(ol.Feature);
|
||||
});
|
||||
|
||||
it("should be easy to set feature attribute", function() {
|
||||
|
||||
var feat = new ol.Feature();
|
||||
feat.setAttribute('foo', 'bar');
|
||||
|
||||
expect(feat).toBeA(ol.Feature);
|
||||
expect(feat.getAttribute('foo')).toBe('bar');
|
||||
});
|
||||
|
||||
it("calling set with one argument", function() {
|
||||
|
||||
var feat = new ol.Feature();
|
||||
feat.setAttribute('foo');
|
||||
|
||||
expect(feat.getAttribute('foo')).toBe(undefined);
|
||||
});
|
||||
|
||||
it("should be easy to set feature geometry", function() {
|
||||
|
||||
var feat = new ol.Feature();
|
||||
var point = ol.geom.point([21, 4]);
|
||||
feat.setGeometry(point);
|
||||
|
||||
var geom = feat.getGeometry();
|
||||
expect(feat).toBeA(ol.Feature);
|
||||
expect(geom).toBeA(ol.geom.Geometry);
|
||||
expect(geom.getX()).toBe(21);
|
||||
expect(geom.getY()).toBe(4);
|
||||
});
|
||||
|
||||
it("should be able to set attributes from object literals", function() {
|
||||
|
||||
var feat = new ol.Feature();
|
||||
feat.setAttributes({
|
||||
foo: 'bar',
|
||||
two: 'deux',
|
||||
size: 3,
|
||||
flag: true
|
||||
});
|
||||
|
||||
expect(feat).toBeA(ol.Feature);
|
||||
expect(feat.getAttribute('foo')).toBe('bar');
|
||||
expect(feat.getAttribute('two')).toBe('deux');
|
||||
expect(feat.getAttribute('size')).toBe(3);
|
||||
expect(feat.getAttribute('flag')).toBe(true);
|
||||
});
|
||||
|
||||
it("should be able to set attributes keeping existing ones", function() {
|
||||
|
||||
var feat = new ol.Feature();
|
||||
feat.setAttributes({
|
||||
foo: 'bar',
|
||||
size: 3
|
||||
});
|
||||
|
||||
expect(feat).toBeA(ol.Feature);
|
||||
expect(feat.getAttribute('size')).toBe(3);
|
||||
|
||||
feat.setAttributes({
|
||||
two: 'deux',
|
||||
size: -99
|
||||
});
|
||||
|
||||
expect(feat.getAttribute('two')).toBe('deux');
|
||||
expect(feat.getAttribute('foo')).toBe('bar');
|
||||
expect(feat.getAttribute('size')).toBe(-99);
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user