Project

General

Profile

1
/*
2
Author:       Mike Adair mike.adairATccrs.nrcan.gc.ca
3
License:      LGPL as per: http://www.gnu.org/copyleft/lesser.html
4

    
5
$Id: CursorTrack.js 3889 2008-02-27 18:29:43Z ahocevar $
6
*/
7

    
8
// Ensure this object's dependancies are loaded.
9
mapbuilder.loadScript(baseDir+"/widget/WidgetBaseXSL.js");
10

    
11
/**
12
 * Widget to display the mouse coordinates when it is over a MapContainer widget.
13
 *
14
 * @constructor
15
 * @base WidgetBaseXSL
16
 * @param widgetNode This widget's object node from the configuration document.
17
 * @param model The model that this widget is a view of.
18
 */
19
function CursorTrack(widgetNode, model) {
20
  WidgetBaseXSL.apply(this,new Array(widgetNode, model));
21

    
22
  //by default, display coords in latlon; if false show map XY
23
  this.showPx = Mapbuilder.parseBoolean(this.getProperty("mb:showPx", false)); // pixel coordinates
24
  this.showXY = Mapbuilder.parseBoolean(this.getProperty("mb:showXY", false)); // XY Coordinates
25
  this.showLatLong = Mapbuilder.parseBoolean(this.getProperty("mb:showLatLong", true)); // Standard lat long
26
  this.showDMS = Mapbuilder.parseBoolean(this.getProperty("mb:showDMS", false)); // Lat/long in DD MM SS.S format
27
  this.showDM = Mapbuilder.parseBoolean(this.getProperty("mb:showDM", false)); // Lat/long in DD MM.MMMM format
28
  this.showMGRS = Mapbuilder.parseBoolean(this.getProperty("mb:showMGRS", false)); // Military Grid Reference System
29
  if( this.showMGRS ) {
30
    // load this here so it is not required for everyone
31
    mapbuilder.loadScript(baseDir+"/util/MGRS.js");
32
  }
33

    
34
  this.precision = this.getProperty("mb:precision", 2);
35

    
36
  //set some properties for the form output
37
  this.formName = "CursorTrackForm_" + mbIds.getId();
38
  this.stylesheet.setParameter("formName", this.formName);
39

    
40

    
41
  /**
42
   * Add mouse event listeners to the map object in the model.
43
   * @param objRef Pointer to this CursorTrack object.
44
   */
45
  this.init = function(objRef) {
46
    objRef.proj = new OpenLayers.Projection( objRef.model.getSRS() );
47

    
48
    objRef.model.map.events.register('mousemove', objRef, objRef.mousemoveHandler);
49
    objRef.model.map.events.register('mouseout',  objRef, objRef.mouseoutHandler);
50

    
51
    if( this.showMGRS )
52
      this.MGRS = new MGRS();
53
  }
54
  this.model.addListener("loadModel", this.init, this );
55
  
56
  this.clear = function(objRef) {
57
    if (objRef.model.map && objRef.model.map.events) {
58
      objRef.model.map.events.unregister('mousemove', objRef, objRef.mousemoveHandler);
59
      objRef.model.map.events.unregister('mouseout',  objRef, objRef.mouseoutHandler);
60
    }
61
  }
62
  this.model.addListener("newModel", this.clear, this);
63

    
64
  /**
65
   * OpenLayers mousemove event listener.
66
   * @param evt OpenLayers mouse event
67
   */
68
  this.mousemoveHandler = function(evt) {
69
    var coordForm = document.getElementById(this.formName);
70
    if (!evt) return;
71

    
72
    // capture XY coordinates
73
    var evXY = this.model.map.getLonLatFromPixel(evt.xy);
74

    
75
    ///CSCS
76
    var pt=new OpenLayers.Geometry.Point(evXY.lon, evXY.lat)
77
    pt.transform(this.proj, new OpenLayers.Projection("EPSG:4326"));
78
    var evLonLat = new OpenLayers.LonLat(pt.x,pt.y);
79

    
80

    
81
    if( this.showPx ) {
82
      if( coordForm.px )
83
        coordForm.px.value = evt.xy.x;
84
      if( coordForm.py )
85
        coordForm.py.value = evt.xy.y;
86
    }
87

    
88
    if( this.showXY ) {
89
      if( coordForm.x )
90
        coordForm.x.value = evXY.lon.toFixed(this.precision);;
91
      if( coordForm.y )
92
        coordForm.y.value = evXY.lat.toFixed(this.precision);;
93
    }
94

    
95
    if( this.showLatLong ) {
96
      if( coordForm.longitude )
97
        coordForm.longitude.value = evLonLat.lon.toFixed(this.precision);
98
      if( coordForm.latitude )
99
        coordForm.latitude.value = evLonLat.lat.toFixed(this.precision);
100
    }
101

    
102
    if( this.showDMS ) {
103
      var longitude = this.convertDMS(evLonLat.lon, 'LON');
104
      if( coordForm.longdeg )
105
        coordForm.longdeg.value = longitude[0];
106
      if( coordForm.longmin )
107
        coordForm.longmin.value = longitude[1];
108
      if( coordForm.longsec )
109
        coordForm.longsec.value = longitude[2];
110
     if( coordForm.longH )
111
        coordForm.longH.value = longitude[3];
112

    
113
      var latitude = this.convertDMS(evLonLat.lat, 'LAT');
114
      if( coordForm.latdeg )
115
        coordForm.latdeg.value = latitude[0];
116
      if( coordForm.latmin )
117
        coordForm.latmin.value = latitude[1];
118
      if( coordForm.latsec )
119
        coordForm.latsec.value = latitude[2];
120
      if( coordForm.latH )
121
        coordForm.latH.value = latitude[3];
122
    }
123

    
124
    if( this.showDM ) {
125
      var longitude = this.convertDM(evLonLat.lon, 'LON');
126
      if( coordForm.longDMdeg )
127
        coordForm.longDMdeg.value = longitude[0];
128
      if( coordForm.longDMmin )
129
        coordForm.longDMmin.value = longitude[1];
130
      if( coordForm.longDMH )
131
        coordForm.longDMH.value = longitude[2];
132

    
133
      var latitude = this.convertDM(evLonLat.lat, 'LAT');
134
      if( coordForm.latDMdeg )
135
        coordForm.latDMdeg.value = latitude[0];
136
      if( coordForm.latDMmin )
137
        coordForm.latDMmin.value = latitude[1];
138
      if( coordForm.latDMH )
139
        coordForm.latDMH.value = latitude[2];
140
    }
141

    
142
    if( this.showMGRS ) {
143
      if( !this.MGRS )
144
        this.MGRS = new MGRS();
145
      coordForm.mgrs.value = this.MGRS.convert(evLonLat.lat,evLonLat.lon) ;
146
    }
147
  }
148

    
149
  /**
150
   * OpenLayers mouseout event listener.
151
   * @param evt OpenLayers mouse event
152
   */
153
  this.mouseoutHandler = function(evt) {
154
    var coordForm = document.getElementById(this.formName);
155

    
156
    if( this.showPx ) {
157
      if( coordForm.px)
158
        coordForm.px.value = "";
159
      if( coordForm.py)
160
        coordForm.py.value = "";
161
    }
162

    
163
    if( this.showXY ) {
164
      if( coordForm.x)
165
        coordForm.x.value = "";
166
      if( coordForm.y)
167
        coordForm.y.value = "";
168
    }
169

    
170
    if( this.showLatLong ) {
171
      if( coordForm.longitude )
172
        coordForm.longitude.value = "";
173
      if( coordForm.latitude )
174
        coordForm.latitude.value = "";
175
    }
176

    
177
    if( this.showDMS ) {
178
      if( coordForm.longdeg )
179
        coordForm.longdeg.value = "";
180
      if( coordForm.longmin )
181
        coordForm.longmin.value = "";
182
      if( coordForm.longsec )
183
        coordForm.longsec.value = "";
184
      if( coordForm.longH )
185
        coordForm.longH.value = "";
186

    
187
      if( coordForm.latdeg )
188
        coordForm.latdeg.value = "";
189
      if( coordForm.latmin )
190
        coordForm.latmin.value = "";
191
      if( coordForm.latsec )
192
        coordForm.latsec.value = "";
193
      if( coordForm.latH )
194
        coordForm.latH.value = "";
195
    }
196

    
197
    if( this.showDM ) {
198
      if( coordForm.longDMdeg )
199
        coordForm.longDMdeg.value = "";
200
      if( coordForm.longDMmin )
201
        coordForm.longDMmin.value = "";
202
      if( coordForm.longDMH )
203
        coordForm.longDMH.value = "";
204

    
205
      if( coordForm.latDMdeg )
206
        coordForm.latDMdeg.value = "";
207
      if( coordForm.latDMmin )
208
        coordForm.latDMmin.value = "";
209
      if( coordForm.latDMH )
210
        coordForm.latDMH.value = "";
211
    }
212

    
213
    if( this.showMGRS ) {
214
      if( coordForm.mgrs )
215
        coordForm.mgrs.value = "";
216
    }
217
  }
218

    
219
  /**
220
   * Decimal to DMS conversion
221
   */
222
  this.convertDMS = function(coordinate, type) {
223
    var coords = new Array();
224

    
225
    abscoordinate = Math.abs(coordinate)
226
    coordinatedegrees = Math.floor(abscoordinate);
227

    
228
    coordinateminutes = (abscoordinate - coordinatedegrees)/(1/60);
229
    tempcoordinateminutes = coordinateminutes;
230
    coordinateminutes = Math.floor(coordinateminutes);
231
    coordinateseconds = (tempcoordinateminutes - coordinateminutes)/(1/60);
232
    coordinateseconds =  Math.round(coordinateseconds*10);
233
    coordinateseconds /= 10;
234

    
235
    if( coordinatedegrees < 10 )
236
      coordinatedegrees = "0" + coordinatedegrees;
237

    
238
    if( coordinateminutes < 10 )
239
      coordinateminutes = "0" + coordinateminutes;
240

    
241
    if( coordinateseconds < 10 )
242
      coordinateseconds = "0" + coordinateseconds;
243

    
244
    coords[0] = coordinatedegrees;
245
    coords[1] = coordinateminutes;
246
    coords[2] = coordinateseconds;
247
    coords[3] = this.getHemi(coordinate, type);
248

    
249
    return coords;
250
  }
251

    
252
  /**
253
   * Decimal to DM (degrees plus decimal minutes) conversion
254
   */
255
  this.convertDM = function(coordinate, type) {
256
    var coords = new Array();
257

    
258
    abscoordinate = Math.abs(coordinate)
259
    coordinatedegrees = Math.floor(abscoordinate);
260

    
261
    coordinateminutes = (abscoordinate - coordinatedegrees)*60;
262
    coordinateminutes = Math.round(coordinateminutes*1000);
263
    coordinateminutes /= 1000;
264

    
265
    if( coordinatedegrees < 10 )
266
      coordinatedegrees = "0" + coordinatedegrees;
267

    
268
    if( coordinateminutes < 10 )
269
      coordinateminutes = "0" + coordinateminutes;
270

    
271
    coords[0] = coordinatedegrees;
272
    coords[1] = coordinateminutes;
273
    coords[2] = this.getHemi(coordinate, type);
274

    
275
    return coords;
276
  }
277

    
278
  /**
279
   * Return the hemisphere abbreviation for this coordinate.
280
   */
281
  this.getHemi = function(coordinate, type) {
282
    var coordinatehemi = "";
283
    if (type == 'LAT') {
284
      if (coordinate >= 0) {
285
        coordinatehemi = "N";
286
      }
287
      else {
288
        coordinatehemi = "S";
289
      }
290
    }
291
    else if (type == 'LON') {
292
      if (coordinate >= 0) {
293
        coordinatehemi = "E";
294
      } else {
295
        coordinatehemi = "W";
296
      }
297
    }
298

    
299
    return coordinatehemi;
300
  }
301
}
(20-20/145)