[Commits] r1942 - in sandbox/fvanderbiest/openlayers: examples lib/OpenLayers/Control tests/Control
commits at geoext.org
commits at geoext.org
Tue Mar 9 14:58:31 CET 2010
Author: fvanderbiest
Date: 2010-03-09 14:58:31 +0100 (Tue, 09 Mar 2010)
New Revision: 1942
Modified:
sandbox/fvanderbiest/openlayers/examples/measure.html
sandbox/fvanderbiest/openlayers/lib/OpenLayers/Control/Measure.js
sandbox/fvanderbiest/openlayers/tests/Control/Measure.html
Log:
fvanderbiest sandbox: applied http://trac.openlayers.org/attachment/ticket/2511/openlayers_2511_A1.diff
Modified: sandbox/fvanderbiest/openlayers/examples/measure.html
===================================================================
--- sandbox/fvanderbiest/openlayers/examples/measure.html 2010-03-09 13:34:12 UTC (rev 1941)
+++ sandbox/fvanderbiest/openlayers/examples/measure.html 2010-03-09 13:58:31 UTC (rev 1942)
@@ -82,6 +82,14 @@
layerOptions: {styleMap: styleMap}
}
}
+ ),
+ point: new OpenLayers.Control.Measure(
+ OpenLayers.Handler.Point, {
+ persist: true,
+ handlerOptions: {
+ layerOptions: {styleMap: styleMap}
+ }
+ }
)
};
@@ -107,7 +115,9 @@
var measure = event.measure;
var element = document.getElementById('output');
var out = "";
- if(order == 1) {
+ if(order == 0) {
+ out += "measure: " + measure.x.toFixed(3) + "," + measure.y.toFixed(3) + " (" + units + ")";
+ } else if(order == 1) {
out += "measure: " + measure.toFixed(3) + " " + units;
} else {
out += "measure: " + measure.toFixed(3) + " " + units + "<sup>2</" + "sup>";
@@ -137,7 +147,7 @@
<body onload="init()">
<h1 id="title">OpenLayers Measure Example</h1>
<p id="shortdesc">
- Demonstrates the measure control to measure distances and areas.
+ Demonstrates the measure control to measure distances, areas and positions.
</p>
<div id="map" class="smallmap"></div>
<div id="options">
@@ -158,6 +168,10 @@
<label for="polygonToggle">measure area</label>
</li>
<li>
+ <input type="radio" name="type" value="point" id="pointToggle" onclick="toggleControl(this);" />
+ <label for="polygonPoint">measure position</label>
+ </li>
+ <li>
<input type="checkbox" name="geodesic" id="geodesicToggle" onclick="toggleGeodesic(this);" />
<label for="geodesicToggle">use geodesic measures</label>
</li>
Modified: sandbox/fvanderbiest/openlayers/lib/OpenLayers/Control/Measure.js
===================================================================
--- sandbox/fvanderbiest/openlayers/lib/OpenLayers/Control/Measure.js 2010-03-09 13:34:12 UTC (rev 1941)
+++ sandbox/fvanderbiest/openlayers/lib/OpenLayers/Control/Measure.js 2010-03-09 13:58:31 UTC (rev 1942)
@@ -30,7 +30,9 @@
* Supported control event types (in addition to those from <OpenLayers.Control>):
* measure - Triggered when a measurement sketch is complete. Listeners
* will receive an event with measure, units, order, and geometry
- * properties.
+ * properties. The measure value is a Number when measuring areas
+ * and distances, and an object with x and y properties when
+ * measuring positions.
* measurepartial - Triggered when a new point is added to the
* measurement sketch. Listeners receive an event with measure,
* units, order, and geometry.
@@ -52,7 +54,8 @@
/**
* Property: displaySystem
* {String} Display system for output measurements. Supported values
- * are 'english', 'metric', and 'geographic'. Default is 'metric'.
+ * are 'english', 'metric', and 'geographic'. Only applies to
+ * area and distance measurements. Default is 'metric'.
*/
displaySystem: 'metric',
@@ -60,7 +63,8 @@
* Property: geodesic
* {Boolean} Calculate geodesic metrics instead of planar metrics. This
* requires that geometries can be transformed into Geographic/WGS84
- * (if that is not already the map projection). Default is false.
+ * (if that is not already the map projection). Only applies to
+ * area and distance measurements. Default is false.
*/
geodesic: false,
@@ -103,6 +107,14 @@
persist: false,
/**
+ * APIProperty: displayProjection
+ * {<OpenLayers.Projection>} The projection of the position
+ * measure. Only applies to position measurements. Defaults
+ * to the map's projection.
+ */
+ displayProjection: null,
+
+ /**
* Constructor: OpenLayers.Control.Measure
*
* Parameters:
@@ -199,9 +211,20 @@
if(geometry.CLASS_NAME.indexOf('LineString') > -1) {
stat = this.getBestLength(geometry);
order = 1;
- } else {
+ } else if(geometry.CLASS_NAME.indexOf('Polygon') > -1) {
stat = this.getBestArea(geometry);
order = 2;
+ } else {
+ var units, displayProjection = this.displayProjection;
+ if(displayProjection) {
+ var mapProjection = this.map.getProjectionObject();
+ geometry.transform(mapProjection, displayProjection);
+ units = this.displayProjection.getUnits();
+ } else {
+ units = this.map.getUnits();
+ }
+ stat = [{x: geometry.x, y: geometry.y}, units];
+ order = 0;
}
this.events.triggerEvent(eventType, {
measure: stat[0],
Modified: sandbox/fvanderbiest/openlayers/tests/Control/Measure.html
===================================================================
--- sandbox/fvanderbiest/openlayers/tests/Control/Measure.html 2010-03-09 13:34:12 UTC (rev 1941)
+++ sandbox/fvanderbiest/openlayers/tests/Control/Measure.html 2010-03-09 13:58:31 UTC (rev 1942)
@@ -64,7 +64,82 @@
t.eq(control.handler.layer.features.length, 0, "feature is gone after cancel");
map.destroy();
+ }
+
+ function test_measure(t) {
+ t.plan(8);
+
+ // set up
+
+ var log;
+
+ var map = new OpenLayers.Map("map");
+
+ var layer = new OpenLayers.Layer(null, {
+ isBaseLayer: true
+ });
+ map.addLayer(layer);
+
+ map.zoomToMaxExtent();
+
+ var control = new OpenLayers.Control.Measure(OpenLayers.Handler.Path, {
+ persist: true
+ });
+ map.addControl(control);
+ control.activate();
+
+ control.events.on({
+ measure: function(e) {
+ log = e;
+ }
+ });
+ // test
+
+ control.measure(new OpenLayers.Geometry.Point(1, 2), "measure");
+ t.eq(log.order, 0,
+ "order value is ok (point)");
+ t.eq(log.units, "degrees",
+ "units value is ok (point)");
+ t.eq(log.measure, {x: 1, y:2},
+ "measure value is ok (point)");
+
+ control.displayProjection = new OpenLayers.Projection("EPSG:900913");
+ // the following line is because proj4js isn't loaded in this test page
+ control.displayProjection.getUnits = function() { return "meters"; };
+ control.measure(new OpenLayers.Geometry.Point(1, 2), "measure");
+ t.eq(log.units, "meters",
+ "units value is ok (point, 900913)");
+
+ control.measure(
+ new OpenLayers.Geometry.LineString([
+ new OpenLayers.Geometry.Point(1, 2),
+ new OpenLayers.Geometry.Point(2, 3)
+ ]),
+ "measure"
+ );
+ t.eq(log.order, 1,
+ "order value is ok (line)");
+ t.ok(log.units == "km" || log.units == "m",
+ "units value is ok (line)");
+
+ control.measure(
+ new OpenLayers.Geometry.Polygon([
+ new OpenLayers.Geometry.LinearRing([
+ new OpenLayers.Geometry.Point(1, 2),
+ new OpenLayers.Geometry.Point(2, 3)
+ ]),
+ ]),
+ "measure"
+ );
+ t.eq(log.order, 2,
+ "order value is ok (polygon)");
+ t.ok(log.units == "km" || log.units == "m",
+ "units value is ok (polygon)");
+
+ // tear down
+
+ map.destroy();
}
</script>
More information about the Commits
mailing list