1 |
4307
|
leinfelder
|
/*
|
2 |
|
|
Author: imke doerge AT geodan.nl
|
3 |
|
|
License: LGPL as per: http://www.gnu.org/copyleft/lesser.html
|
4 |
|
|
$Id: Measurement.js 3821 2008-02-01 13:58:30Z ahocevar $
|
5 |
|
|
*/
|
6 |
|
|
|
7 |
|
|
// Ensure this object's dependencies are loaded.
|
8 |
|
|
mapbuilder.loadScript(baseDir+"/widget/EditButtonBase.js");
|
9 |
|
|
|
10 |
|
|
|
11 |
|
|
/**
|
12 |
|
|
* When this button is selected, clicks on the MapPane will add a
|
13 |
|
|
* new point to a line and the total distance of the line will be calculated
|
14 |
|
|
* @constructor
|
15 |
|
|
* @base EditButtonBase
|
16 |
|
|
* @author Imke Doerge AT geodan.nl
|
17 |
|
|
* @param widgetNode The widget from the Config XML file.
|
18 |
|
|
* @param model The parent model for this widget.
|
19 |
|
|
*/
|
20 |
|
|
function Measurement(widgetNode, model) {
|
21 |
|
|
EditButtonBase.apply(this, new Array(widgetNode, model));
|
22 |
|
|
|
23 |
|
|
/**
|
24 |
|
|
* Interactive EditLine control.
|
25 |
|
|
* @param objRef reference to this object.
|
26 |
|
|
* @return {OpenLayers.Control} class of the OL control.
|
27 |
|
|
*/
|
28 |
|
|
this.createControl = function(objRef) {
|
29 |
|
|
var Control = OpenLayers.Class(OpenLayers.Control.DrawFeature, {
|
30 |
|
|
// this is needed because all editing tools are of type
|
31 |
|
|
// OpenLayers.Control.DrawFeature
|
32 |
|
|
CLASS_NAME: 'mbMeasurement'
|
33 |
|
|
});
|
34 |
|
|
return Control;
|
35 |
|
|
}
|
36 |
|
|
|
37 |
|
|
this.instantiateControl = function(objRef, Control) {
|
38 |
|
|
var control = new Control(objRef.featureLayer, OpenLayers.Handler.Path,
|
39 |
|
|
{callbacks: {point: objRef.startAction}});
|
40 |
|
|
control.objRef = objRef;
|
41 |
|
|
control.activate = function() {
|
42 |
|
|
Control.prototype.activate.apply(this, arguments);
|
43 |
|
|
objRef.targetModel.setParam('showDistance', 0);
|
44 |
|
|
}
|
45 |
|
|
control.deactivate = function() {
|
46 |
|
|
Control.prototype.deactivate.apply(this, arguments);
|
47 |
|
|
objRef.targetModel.setParam('showDistance', null);
|
48 |
|
|
}
|
49 |
|
|
return control;
|
50 |
|
|
}
|
51 |
|
|
|
52 |
|
|
// override default cursor by user
|
53 |
|
|
// cursor can be changed by specifying a new cursor in config file
|
54 |
|
|
this.cursor = "crosshair";
|
55 |
|
|
|
56 |
|
|
var totalDistance=0;
|
57 |
|
|
var distance = 0;
|
58 |
|
|
var state = false;
|
59 |
|
|
var restart = false;
|
60 |
|
|
|
61 |
|
|
this.startAction = function(pointGeometry) {
|
62 |
|
|
var objRef = this.objRef;
|
63 |
|
|
objRef.pointGeometry = pointGeometry;
|
64 |
|
|
if (!objRef.targetModel.doc) {
|
65 |
|
|
objRef.targetModel.addListener("loadModel", objRef.doAction, objRef);
|
66 |
|
|
config.loadModel(objRef.targetModel.id, objRef.defaultModelUrl);
|
67 |
|
|
} else {
|
68 |
|
|
objRef.doAction(objRef);
|
69 |
|
|
}
|
70 |
|
|
}
|
71 |
|
|
/**
|
72 |
|
|
* Append a point to a line and calculate the distance between all
|
73 |
|
|
* points on the line. This will be called by the OpenLayers control
|
74 |
|
|
* for this widget in the context of the control.
|
75 |
|
|
* @param pointGeometry OpenLayers Geometry of the point to add
|
76 |
|
|
*/
|
77 |
|
|
this.doAction = function(objRef) {
|
78 |
|
|
pointGeometry = objRef.pointGeometry;
|
79 |
|
|
objRef.targetModel.removeListener("loadModel", objRef.doAction, objRef);
|
80 |
|
|
if (objRef.enabled) {
|
81 |
|
|
if(objRef.restart) {
|
82 |
|
|
objRef.model.setParam("clearMeasurementLine");
|
83 |
|
|
objRef.restart= false;
|
84 |
|
|
}
|
85 |
|
|
var point=[pointGeometry.x, pointGeometry.y];
|
86 |
|
|
var old=objRef.targetModel.getXpathValue(objRef.targetModel,objRef.featureXpath);
|
87 |
|
|
if(!old) old="";
|
88 |
|
|
sucess=objRef.targetModel.setXpathValue(objRef.targetModel,objRef.featureXpath,old+" "+point[0]+","+point[1]);
|
89 |
|
|
if(!sucess) {
|
90 |
|
|
alert(mbGetMessage("invalidFeatureXpathMeasurement", objRef.featureXpath));
|
91 |
|
|
}
|
92 |
|
|
|
93 |
|
|
var lineCoords = objRef.targetModel.getXpathValue(objRef.targetModel, objRef.featureXpath);
|
94 |
|
|
var coordArray = lineCoords.split(" ");
|
95 |
|
|
if (coordArray.length >= 3) {
|
96 |
|
|
var point_P = coordArray[coordArray.length-2];
|
97 |
|
|
var point_Q = coordArray[coordArray.length-1];
|
98 |
|
|
|
99 |
|
|
//Split point in x and y coordinate
|
100 |
|
|
var P =point_P.split(",");
|
101 |
|
|
var Q =point_Q.split(",");
|
102 |
|
|
|
103 |
|
|
//transform coordinates from lat/lon to x/y in meter
|
104 |
|
|
objRef.srs = srs.toUpperCase();
|
105 |
|
|
objRef.proj = new OpenLayers.Projection(objRef.srs);
|
106 |
|
|
|
107 |
|
|
if (!P || !Q ){
|
108 |
|
|
alert(mbGetMessage("projectionNotSupported"));
|
109 |
|
|
}
|
110 |
|
|
else {
|
111 |
|
|
//If projection is in meters use simple pythagoras
|
112 |
|
|
if(objRef.proj.getUnits() == "meters" || objRef.proj.getUnits() == "m") {
|
113 |
|
|
Xp=parseFloat(P[0]);
|
114 |
|
|
Yp=parseFloat(P[1]);
|
115 |
|
|
Xq=parseFloat(Q[0]);
|
116 |
|
|
Yq=parseFloat(Q[1]);
|
117 |
|
|
// calculate the distance between these two points via Pythagoras' theorem
|
118 |
|
|
distance=Math.sqrt(((Xp-Xq)*(Xp-Xq))+((Yp-Yq)*(Yp-Yq)))
|
119 |
|
|
if(distance==0) {
|
120 |
|
|
objRef.restart = true;
|
121 |
|
|
objRef.model.setParam("clearMouseLine");objRef.targetModel.setParam("mouseRenderer", false);
|
122 |
|
|
return;
|
123 |
|
|
}
|
124 |
|
|
totalDistance = Math.round(totalDistance + distance);
|
125 |
|
|
}
|
126 |
|
|
//If projection is in degrees use great circle formulae
|
127 |
|
|
//http://williams.best.vwh.net/avform.htm#GCF
|
128 |
|
|
else if(objRef.proj.getUnits() == "degrees" || objRef.proj.getUnits() == null) {
|
129 |
|
|
var deg2rad = Math.PI / 180.0
|
130 |
|
|
var centerOfEarth=new Array(0,0);
|
131 |
|
|
LonpRad=parseFloat(P[0]) * deg2rad;
|
132 |
|
|
LatpRad=parseFloat(P[1]) * deg2rad;
|
133 |
|
|
LonqRad=parseFloat(Q[0]) * deg2rad;
|
134 |
|
|
LatqRad=parseFloat(Q[1]) * deg2rad;
|
135 |
|
|
|
136 |
|
|
if((LonpRad>0 && LonqRad<0) || (LonpRad<0 && LonqRad>0)){
|
137 |
|
|
if(LonpRad<0){
|
138 |
|
|
radDistance=Math.acos(Math.sin(LatpRad)*Math.sin(LatqRad)+Math.cos(LatpRad)*Math.cos(LatqRad)*Math.cos(LonpRad));
|
139 |
|
|
radDistance+=Math.acos(Math.sin(LatpRad)*Math.sin(LatqRad)+Math.cos(LatpRad)*Math.cos(LatqRad)*Math.cos(LonqRad));
|
140 |
|
|
}
|
141 |
|
|
if(LonqRad<0){
|
142 |
|
|
radDistance=Math.acos(Math.sin(LatpRad)*Math.sin(LatqRad)+Math.cos(LatpRad)*Math.cos(LatqRad)*Math.cos(LonqRad));
|
143 |
|
|
radDistance+=Math.acos(Math.sin(LatpRad)*Math.sin(LatqRad)+Math.cos(LatpRad)*Math.cos(LatqRad)*Math.cos(LonpRad));
|
144 |
|
|
}
|
145 |
|
|
}
|
146 |
|
|
else{
|
147 |
|
|
|
148 |
|
|
radDistance=Math.acos(Math.sin(LatpRad)*Math.sin(LatqRad)+Math.cos(LatpRad)*Math.cos(LatqRad)*Math.cos(LonpRad-LonqRad));
|
149 |
|
|
|
150 |
|
|
}
|
151 |
|
|
//radDistance=Math.acos(Math.sin(Latp)*Math.sin(Latq)+Math.cos(Latp)*Math.cos(Latq)*Math.cos(Lonp-Lonq));
|
152 |
|
|
distance =radDistance * 6378137;
|
153 |
|
|
if(distance==0) {
|
154 |
|
|
objRef.restart = true;
|
155 |
|
|
objRef.model.setParam("clearMouseLine");objRef.targetModel.setParam("mouseRenderer", false);
|
156 |
|
|
return;
|
157 |
|
|
}
|
158 |
|
|
totalDistance = Math.round(totalDistance + distance);
|
159 |
|
|
}
|
160 |
|
|
else alert(mbGetMessage("cantCalculateDistance"));
|
161 |
|
|
}
|
162 |
|
|
}
|
163 |
|
|
objRef.targetModel.setParam("showDistance", totalDistance); // set parameter for the distance output
|
164 |
|
|
}
|
165 |
|
|
}
|
166 |
|
|
|
167 |
|
|
/**
|
168 |
|
|
* This will be called as defined in EditButtonBase.js. It is called
|
169 |
|
|
* when measurement is finished (ie. when user double-clicks on the map)
|
170 |
|
|
* @param objRef reference to this widget
|
171 |
|
|
* @param feature complete measurement path - currently unused.
|
172 |
|
|
*/
|
173 |
|
|
this.setFeature = function(objRef, feature) {
|
174 |
|
|
objRef.restart = true;
|
175 |
|
|
}
|
176 |
|
|
|
177 |
|
|
this.clearMeasurementLine = function(objRef){
|
178 |
|
|
if (totalDistance !=0) {
|
179 |
|
|
totalDistance=0;
|
180 |
|
|
sucess=objRef.targetModel.setXpathValue(objRef.targetModel,objRef.featureXpath,"");
|
181 |
|
|
if(!sucess){
|
182 |
|
|
alert(mbGetMessage("invalidFeatureXpathMeasurement", objRef.featureXpath));
|
183 |
|
|
}
|
184 |
|
|
objRef.targetModel.setParam("refresh");
|
185 |
|
|
}
|
186 |
|
|
}
|
187 |
|
|
//add a Listener to the model
|
188 |
|
|
this.model.addListener("clearMeasurementLine", this.clearMeasurementLine, this);
|
189 |
|
|
|
190 |
|
|
}
|