Revision 4307
Added by ben leinfelder over 15 years ago
Extent.js | ||
---|---|---|
1 | 1 |
/* |
2 | 2 |
Author: Mike Adair mike.adairATccrs.nrcan.gc.ca |
3 | 3 |
License: LGPL as per: http://www.gnu.org/copyleft/lesser.html |
4 |
$Id$ |
|
4 |
$Id: Extent.js 3067 2007-08-03 09:25:59Z jseb.baklouti $
|
|
5 | 5 |
*/ |
6 | 6 |
|
7 | 7 |
|
... | ... | |
9 | 9 |
var degToMeter = Rearth*2*Math.PI/360; |
10 | 10 |
//var mbScaleFactor = 72 * 39.3701; //PixelsPerInch*InchesPerMapUnit; magic numbers |
11 | 11 |
//need to determine magic number for lat/lon |
12 |
var mbScaleFactor = 3571.428 //magic number, for Geoserver SLD compatibility |
|
12 |
var mbScaleFactor = 3571.428; //magic number, for Geoserver SLD compatibility
|
|
13 | 13 |
// 1/0.00028 (0.28 mm "is a common actual size for |
14 | 14 |
// contemporary display" as written in the SLD specification ... |
15 | 15 |
|
... | ... | |
37 | 37 |
*/ |
38 | 38 |
function Extent( model, initialRes ) { |
39 | 39 |
this.model = model; |
40 |
this.id = model.id + "_MbExtent" + mbIds.getId(); |
|
40 | 41 |
this.size = new Array(); |
41 | 42 |
this.res = new Array(); |
42 | 43 |
this.zoomBy = 4; |
43 |
this.id = model.id + "_MbExtent" + mbIds.getId(); |
|
44 |
|
|
44 |
|
|
45 | 45 |
/** |
46 |
* Returns the bounding box as stored in the model |
|
47 |
* @return array with the bounding box |
|
48 |
*/ |
|
49 |
this.getBbox = function() { |
|
50 |
var bbox = this.model.getBoundingBox(); |
|
51 |
return bbox; |
|
52 |
} |
|
53 |
|
|
54 |
/** |
|
55 |
* Recalculates a given bbox and stores a proper aspect one in the model |
|
56 |
* @param bbox an array with a bounding box |
|
57 |
*/ |
|
58 |
this.setBbox = function(bbox){ |
|
59 |
size = this.getSize(); |
|
60 |
res = Math.max((bbox[2] - bbox[0])/size[0], (bbox[3] - bbox[1])/size[1]); |
|
61 |
scale=this.getFixedScale(res); |
|
62 |
center = new Array((bbox[1]-bbox[3])/2,(bbox[0]-bbox[2])/2);//center=horizontal,vertical |
|
63 |
half = new Array(size[0]/2,size[1]/2); |
|
64 |
bbox = new Array(center[0]-half[0]*scale, center[1]-half[1]*scale, center[0]+half[0]*scale,center[1]+half[1]*scale); |
|
65 |
this.model.setBoundingBox(bbox); |
|
66 |
} |
|
67 |
|
|
68 |
/** |
|
69 |
* Returns the window size as stored in the model |
|
70 |
* @return array with the window size |
|
71 |
*/ |
|
72 |
this.getSize = function() { |
|
73 |
size= new Array(); |
|
74 |
size[0] = this.model.getWindowWidth(); |
|
75 |
size[1] = this.model.getWindowHeight(); |
|
76 |
return size; |
|
77 |
} |
|
78 |
|
|
79 |
/** |
|
80 |
* Stores a given window size in the model. |
|
81 |
* Can be used in the future for dynamic window resizing |
|
82 |
* @param size an array with a window size |
|
83 |
*/ |
|
84 |
this.setSize = function(size){ |
|
85 |
this.model.setWindowWidth(size[0]); |
|
86 |
this.model.setWindowHeight(size[1]); |
|
87 |
} |
|
88 |
|
|
89 |
/** |
|
90 |
* When given a res, it recalculates it to match the zoomlevels, when present and returns a fixed scale. |
|
91 |
* When no res is given it returns the maximum resolution |
|
92 |
* @param res optional resolution to be checked |
|
93 |
* @return fixedScale the resolution to display the map with |
|
94 |
*/ |
|
95 |
this.getFixedScale = function(res) { |
|
96 |
if (this.zoomLevels){ |
|
97 |
if (!res) { |
|
98 |
this.setResolution( new Array(this.model.getWindowWidth(), this.model.getWindowHeight() ) ); |
|
99 |
res = Math.max(this.res[0],this.res[1]); |
|
100 |
|
|
101 |
} |
|
102 |
var sortstring = "function sort(a,b){return b-a}"; |
|
103 |
var evalsort= eval(sortstring); |
|
104 |
var zoomLevels = this.zoomLevels.sort(evalsort); |
|
105 |
var i=0; |
|
106 |
while(zoomLevels[i] >= res){ |
|
107 |
i++; |
|
108 |
} |
|
109 |
if(i==0) { |
|
110 |
i=1; |
|
111 |
} |
|
112 |
this.fixedScale = zoomLevels[i-1]; |
|
113 |
} |
|
114 |
else this.fixedScale = Math.max(this.res[0],this.res[1]); |
|
115 |
return this.fixedScale; |
|
116 |
|
|
117 |
} |
|
118 |
|
|
119 |
/* |
|
120 |
* Sets the zoomLevels in the extent |
|
121 |
* @param enabled boolean to enable or disable zoomLevels support |
|
122 |
* @param zoomLevels an array containing a fixed set of zoomLevels |
|
123 |
*/ |
|
124 |
this.setZoomLevels = function(enabled,zoomLevels){ |
|
125 |
if(enabled) { |
|
126 |
this.zoomLevels = zoomLevels; |
|
127 |
} |
|
128 |
else this.zoomLevels = null; |
|
129 |
} |
|
130 |
|
|
131 |
/* |
|
132 |
* Recalculates the lr and ul to a proper aspect. It also takes into account zoomLevels when present. |
|
133 |
*/ |
|
134 |
this.checkBbox = function() { |
|
135 |
var center = this.getCenter(); |
|
136 |
var half = new Array(this.size[0]/2, this.size[1]/2); |
|
137 |
var res = this.getFixedScale(); |
|
138 |
this.lr = new Array(center[0]+half[0]*res, center[1]-half[1]*res); |
|
139 |
this.ul = new Array(center[0]-half[0]*res, center[1]+half[1]*res); |
|
140 |
} |
|
141 |
/** |
|
46 | 142 |
* Returns the XY center of this extent |
47 | 143 |
* @return array of XY for th center of the extent |
48 | 144 |
*/ |
... | ... | |
56 | 152 |
* @return point array of XY coordinates |
57 | 153 |
*/ |
58 | 154 |
this.getXY = function(pl) { |
59 |
var x = this.ul[0]+pl[0]*this.res[0]; |
|
60 |
var y = this.ul[1]- pl[1]*this.res[1]; |
|
61 |
return new Array(x,y); |
|
155 |
//switch(this.model.getSRS()) { |
|
156 |
// case "EPSG:GMAPS": //@TODO Cleanup this hack |
|
157 |
// gmap=this.model.getParam("gmap"); |
|
158 |
// if(gmap){ |
|
159 |
// p=new GPoint(pl[0],pl[1]); |
|
160 |
// latlng=gmap.fromDivPixelToLatLng(p); |
|
161 |
// latlng=new Array(latlng.lng(),latlng.lat()); |
|
162 |
// } |
|
163 |
// else alert("Extent: gmap not defined"); |
|
164 |
// break; |
|
165 |
// default: |
|
166 |
// latlng=new Array(this.ul[0]+pl[0]*this.res[0],this.ul[1]- pl[1]*this.res[1]); |
|
167 |
// break; |
|
168 |
//} |
|
169 |
latlng=new Array(this.ul[0]+pl[0]*this.res[0],this.ul[1]- pl[1]*this.res[1]); |
|
170 |
return latlng; |
|
62 | 171 |
} |
63 | 172 |
|
64 | 173 |
/** |
... | ... | |
67 | 176 |
* @return point array of pxiel/line coordinates w.r.t. top left corner |
68 | 177 |
*/ |
69 | 178 |
this.getPL = function(xy) { |
179 |
//switch(this.model.getSRS()) { |
|
180 |
// case "EPSG:GMAPS": //@TODO Cleanup this hack |
|
181 |
// return xy; |
|
182 |
//} |
|
183 |
|
|
70 | 184 |
var p = Math.floor( (xy[0]-this.ul[0])/this.res[0] ); |
71 | 185 |
var l = Math.floor( (this.ul[1]-xy[1])/this.res[1] ); |
72 | 186 |
return new Array(p,l); |
... | ... | |
96 | 210 |
newres= nRmax ; |
97 | 211 |
} |
98 | 212 |
*/ |
213 |
if (this.zoomLevels) { |
|
214 |
newres=this.getFixedScale(newres); |
|
215 |
} |
|
99 | 216 |
this.lr = new Array(center[0]+half[0]*newres, center[1]-half[1]*newres); |
100 | 217 |
this.ul = new Array(center[0]-half[0]*newres, center[1]+half[1]*newres); |
101 | 218 |
|
... | ... | |
166 | 283 |
this.getScale = function() { |
167 | 284 |
var pixRes = null; |
168 | 285 |
switch(this.model.getSRS()) { |
286 |
case "EPSG:GMAPS": |
|
287 |
break; |
|
169 | 288 |
case "EPSG:4326": //all projection codes in degrees |
170 | 289 |
case "EPSG:4269": |
171 | 290 |
pixRes = this.res[0]*degToMeter; |
... | ... | |
228 | 347 |
} |
229 | 348 |
*/ |
230 | 349 |
extent.setResolution( new Array(extent.model.getWindowWidth(), extent.model.getWindowHeight() ) ); |
350 |
extent.checkBbox(); |
|
231 | 351 |
} |
232 | 352 |
if ( initialRes ) this.init(this, initialRes); |
233 | 353 |
|
Also available in: Unified diff
upgrade to MapBuilder 1.5rc2 - includes support for Firefox 3 compatibility (yes, it is also EOLed)