Add Google Map of voters
[selectricity-live] / public / javascripts / wms-gs.js
1 /*
2  * Call generic wms service for GoogleMaps v2
3  * John Deck, UC Berkeley
4  * Inspiration & Code from:
5  *      Mike Williams http://www.econym.demon.co.uk/googlemaps2/ V2 Reference & custommap code
6  *      Brian Flood http://www.spatialdatalogic.com/cs/blogs/brian_flood/archive/2005/07/11/39.aspx V1 WMS code
7  *      Kyle Mulka http://blog.kylemulka.com/?p=287  V1 WMS code modifications
8  *      http://search.cpan.org/src/RRWO/GPS-Lowrance-0.31/lib/Geo/Coordinates/MercatorMeters.pm
9  *
10  * Modified by Chris Holmes, TOPP to work by default with GeoServer.
11  *
12  * Bundled with YM4R with John Deck's permission.
13  * Slightly modified to fit YM4R.
14  * See johndeck.blogspot.com for the original version and for examples and instructions of how to use it.
15  */
16
17 var WGS84_SEMI_MAJOR_AXIS = 6378137.0; //equatorial radius
18 var WGS84_ECCENTRICITY = 0.0818191913108718138;
19 var DEG2RAD=0.0174532922519943;
20 var PI=3.14159267;
21
22 function dd2MercMetersLng(p_lng) { 
23         return WGS84_SEMI_MAJOR_AXIS * (p_lng*DEG2RAD); 
24 }
25
26 function dd2MercMetersLat(p_lat) {
27         var lat_rad = p_lat * DEG2RAD;
28         return WGS84_SEMI_MAJOR_AXIS * Math.log(Math.tan((lat_rad + PI / 2) / 2) * Math.pow( ((1 - WGS84_ECCENTRICITY * Math.sin(lat_rad)) / (1 + WGS84_ECCENTRICITY * Math.sin(lat_rad))), (WGS84_ECCENTRICITY/2)));
29 }
30
31 function addWMSPropertiesToLayer(tile_layer,base_url,layers,styles,format,merc_proj,use_geo){
32    tile_layer.format = format;
33    tile_layer.baseURL = base_url;
34    tile_layer.styles = styles;
35    tile_layer.layers = layers;
36    tile_layer.mercatorEpsg = merc_proj;
37    tile_layer.useGeographic = use_geo;
38    return tile_layer;
39 }
40
41 getTileUrlForWMS=function(a,b,c) {
42         var lULP = new GPoint(a.x*256,(a.y+1)*256);
43         var lLRP = new GPoint((a.x+1)*256,a.y*256);
44         var lUL = G_NORMAL_MAP.getProjection().fromPixelToLatLng(lULP,b,c);
45         var lLR = G_NORMAL_MAP.getProjection().fromPixelToLatLng(lLRP,b,c);
46          
47         if (this.useGeographic){
48            var lBbox=lUL.x+","+lUL.y+","+lLR.x+","+lLR.y;
49            var lSRS="EPSG:4326";
50         }else{
51            var lBbox=dd2MercMetersLng(lUL.x)+","+dd2MercMetersLat(lUL.y)+","+dd2MercMetersLng(lLR.x)+","+dd2MercMetersLat(lLR.y);
52            var lSRS="EPSG:" + this.mercatorEpsg;
53         }
54         var lURL=this.baseURL;
55         lURL+="?REQUEST=GetMap";
56         lURL+="&SERVICE=WMS";
57         lURL+="&VERSION=1.1.1";
58         lURL+="&LAYERS="+this.layers;
59         lURL+="&STYLES="+this.styles; 
60         lURL+="&FORMAT=image/"+this.format;
61         lURL+="&BGCOLOR=0xFFFFFF";
62         lURL+="&TRANSPARENT=TRUE";
63         lURL+="&SRS="+lSRS;
64         lURL+="&BBOX="+lBbox;
65         lURL+="&WIDTH=256";
66         lURL+="&HEIGHT=256";
67         lURL+="&reaspect=false";
68         return lURL;
69 }

Benjamin Mako Hill || Want to submit a patch?