1
|
<?php
|
2
|
|
3
|
/*
|
4
|
* An attempt at automatic map generation using:
|
5
|
* - ogr2ogr - which will combine shape files
|
6
|
* - Google map creator - which will create the google maps from a single shape file.
|
7
|
*
|
8
|
* This script returns JavaScript, therefore it can be embedded on ANY page!
|
9
|
*/
|
10
|
|
11
|
// We take as arguments to this script the two letter country codes of countries
|
12
|
// we wish to include within out map. This means that we can easily expand this
|
13
|
// to include any world countries (not just African ones), althoughh this may require
|
14
|
// three letters!
|
15
|
|
16
|
// Following temp filename is used in the configuration section, and therefore needs to come
|
17
|
// before it.
|
18
|
$tmp_file_name = tempnam("/tmp","googlemaps");
|
19
|
$tmp_file_name = substr($tmp_file_name,5);
|
20
|
$tmp_file = "/tmp/".$tmp_file_name;
|
21
|
|
22
|
// Configuration -----------------------------------------------------------------------
|
23
|
$earth_shape_file = dirname(__FILE__)."/country-earth.shp";
|
24
|
$maps_extensions = array("dbf","shp","shx","prj","xml"); // Not too sure which of these IS needed.
|
25
|
$ogr2ogr_path = "/usr/share/fwtools/bin_safe/ogr2ogr";
|
26
|
$gmap_creator_path = "/var/www/html/sites/all/gmap/gmapcreator.jar";
|
27
|
if (!isset($_GET['apikey'])||$_GET['apikey']==""){
|
28
|
?>alert('EDIT GMap colourer: You must supply an API key');<?php
|
29
|
exit;
|
30
|
}
|
31
|
$google_api_key = $_GET['apikey'];
|
32
|
$gmap_settings_xml = "<gmapcreator>
|
33
|
<apikey>".$google_api_key."</apikey>
|
34
|
<apiversion>2</apiversion>
|
35
|
<maptitle>Test Map</maptitle>
|
36
|
<shpfilename>".$tmp_file_name.".shp</shpfilename>
|
37
|
<outlinesenabled>true</outlinesenabled>
|
38
|
<maxzoomlevel>5</maxzoomlevel>
|
39
|
<datafield>IDR_ID</datafield>
|
40
|
<limitarea>false</limitarea>
|
41
|
<mapboundswgs84>
|
42
|
<minlat>-55.72056961021754</minlat>
|
43
|
<minlon>-180.0</minlon>
|
44
|
<maxlat>83.62303161612134</maxlat>
|
45
|
<maxlon>179.99981689453125</maxlon>
|
46
|
</mapboundswgs84>
|
47
|
<mapviewenvelope>
|
48
|
<minx>-2.1397475258323826E7</minx>
|
49
|
<maxx>2.139745487511628E7</maxx>
|
50
|
<miny>-9944658.00979416</miny>
|
51
|
<maxy>2.0852293210672483E7</maxy>
|
52
|
</mapviewenvelope>
|
53
|
<colourscale>
|
54
|
<isdiscrete>true</isdiscrete>
|
55
|
<colourThresholds>
|
56
|
<colourThreshold>
|
57
|
<colour>ff0000ff</colour>
|
58
|
<threshold>0.0</threshold>
|
59
|
<description>First Colour</description>
|
60
|
</colourThreshold>
|
61
|
</colourThresholds>
|
62
|
</colourscale>
|
63
|
</gmapcreator>
|
64
|
";
|
65
|
if (!isset($_GET['countries'])){
|
66
|
|
67
|
// The countries have not been sent! Error.
|
68
|
?>alert('EDIT GMap colourer: You haven\'t supplied any countries');<?php
|
69
|
exit;
|
70
|
}
|
71
|
|
72
|
// Get the countries.
|
73
|
$countries = split(",",$_GET['countries']);
|
74
|
|
75
|
// Extract each country from the "Earth shape" and add it to the previous.
|
76
|
// If this is the first country, there is no adding!)
|
77
|
for($i=0;$i<count($countries);$i++){
|
78
|
$exec = $ogr2ogr_path . ' ' . $tmp_file . $i . '.shp -fid ' . ($countries[$i]-1) . ' ' . $earth_shape_file;
|
79
|
exec($exec);
|
80
|
if ($i==0){
|
81
|
$exec = $ogr2ogr_path . ' ' . $tmp_file . '.shp ' . $tmp_file . $i . '.shp';
|
82
|
exec($exec);
|
83
|
}
|
84
|
else{
|
85
|
$exec = $ogr2ogr_path . ' -update -append ' . $tmp_file.'.shp ' . $tmp_file . $i . '.shp -nln ' . $tmp_file_name;
|
86
|
exec($exec);
|
87
|
}
|
88
|
}
|
89
|
// Once this is done, we can have a go at creating the GoogleMap from the command line.
|
90
|
// Firstly, create the settings file
|
91
|
file_put_contents($tmp_file.".xml",$gmap_settings_xml);
|
92
|
// Next copy the projection file
|
93
|
copy($maps_directory."projection.prj",$tmp_file.".prj");
|
94
|
// Mv the temp files to the semi temp folder, don't forget to delete them afterwards!
|
95
|
foreach($maps_extensions as $map_extension){
|
96
|
@copy($tmp_file.".".$map_extension,"/var/www/html/sites/all/gmap".$tmp_file.".".$map_extension);
|
97
|
}
|
98
|
$exec = "cd /var/www/html/sites/all/gmap/tmp && /usr/bin/java -jar ".$gmap_creator_path." -dfile=".$tmp_file_name.".xml";
|
99
|
exec($exec);
|
100
|
?>
|
101
|
var centreLat=3.477775573709623;
|
102
|
var centreLon=10.218469619750977;
|
103
|
var n_buttonText="Map"; //Text that shows up on the button for the custom layer (n=normal, s=sat)
|
104
|
var s_buttonText="Satellite";
|
105
|
var mapBounds=new GLatLngBounds(new GLatLng(-90,-180),new GLatLng(90,180));
|
106
|
var map; //the GMap2 itself
|
107
|
var opacity=0.75;
|
108
|
|
109
|
|
110
|
function customGetTileURL(a,b) {
|
111
|
//converts tile x,y into keyhole string
|
112
|
if (b>5) { return "/sites/all/gmap/tmp/<?php echo $tmp_file_name; ?>-tiles/blank-tile.png"; };
|
113
|
|
114
|
var c=Math.pow(2,b);
|
115
|
var x=360/c*a.x-180;
|
116
|
var y=180-360/c*a.y;
|
117
|
var x2=x+360/c;
|
118
|
var y2=y-360/c;
|
119
|
var lon=x; //Math.toRadians(x); //would be lon=x+lon0, but lon0=0 degrees
|
120
|
var lat=(2.0*Math.atan(Math.exp(y/180*Math.PI))-Math.PI/2.0)*180/Math.PI; //in degrees
|
121
|
var lon2=x2;
|
122
|
var lat2=(2.0*Math.atan(Math.exp(y2/180*Math.PI))-Math.PI/2.0)*180/Math.PI; //in degrees
|
123
|
var tileBounds=new GLatLngBounds(new GLatLng(lat2,lon),new GLatLng(lat,lon2));
|
124
|
|
125
|
if (!tileBounds.intersects(mapBounds)) { return "/sites/all/gmap/tmp/<?php echo $tmp_file_name; ?>-tiles/blank-tile.png"; };
|
126
|
var d=a.x;
|
127
|
var e=a.y;
|
128
|
var f="t";
|
129
|
for(var g=0;g<b;g++){
|
130
|
c=c/2;
|
131
|
if(e<c){
|
132
|
if(d<c){f+="q"}
|
133
|
else{f+="r";d-=c}
|
134
|
}
|
135
|
else{
|
136
|
if(d<c){f+="t";e-=c}
|
137
|
else{f+="s";d-=c;e-=c}
|
138
|
}
|
139
|
}
|
140
|
return "/sites/all/gmap/tmp/<?php echo $tmp_file_name; ?>-tiles/"+f+".png"
|
141
|
}
|
142
|
|
143
|
function changeOpacity(op) {
|
144
|
//this works as long as there are at least two map types
|
145
|
var current=map.getCurrentMapType();
|
146
|
if (current==map.getMapTypes()[0])
|
147
|
map.setMapType(map.getMapTypes()[1]);
|
148
|
else
|
149
|
map.setMapType(map.getMapTypes()[0]);
|
150
|
opacity=op;
|
151
|
map.setMapType(current); //was map.getMapTypes()[1]
|
152
|
}
|
153
|
|
154
|
|
155
|
function getWindowHeight() {
|
156
|
if (window.self&&self.innerHeight) {
|
157
|
return self.innerHeight;
|
158
|
}
|
159
|
if (document.documentElement&&document.documentElement.clientHeight) {
|
160
|
return document.documentElement.clientHeight;
|
161
|
}
|
162
|
return 0;
|
163
|
}
|
164
|
|
165
|
function resizeMapDiv() {
|
166
|
//Resize the height of the div containing the map.
|
167
|
//Do not call any map methods here as the resize is called before the map is created.
|
168
|
var d=document.getElementById("map");
|
169
|
var offsetTop=0;
|
170
|
for (var elem=d; elem!=null; elem=elem.offsetParent) {
|
171
|
offsetTop+=elem.offsetTop;
|
172
|
}
|
173
|
var height=getWindowHeight()-offsetTop-16;
|
174
|
if (height>=0) {
|
175
|
d.style.height=height+"px";
|
176
|
}
|
177
|
}
|
178
|
|
179
|
|
180
|
function load() {
|
181
|
if (GBrowserIsCompatible()) {
|
182
|
resizeMapDiv();
|
183
|
var copyright = new GCopyright(1,
|
184
|
new GLatLngBounds(new GLatLng(-90, -180),
|
185
|
new GLatLng(90, 180)),
|
186
|
0,
|
187
|
"<a href=\"http://www.casa.ucl.ac.uk\">CASA</a>");
|
188
|
var copyrightCollection = new GCopyrightCollection("GMapCreator");
|
189
|
copyrightCollection.addCopyright(copyright);
|
190
|
|
191
|
//create a custom G_NORMAL_MAP layer
|
192
|
var n_tileLayers = [ G_NORMAL_MAP.getTileLayers()[0], new GTileLayer(copyrightCollection , 0, 17)];
|
193
|
n_tileLayers[1].getTileUrl = customGetTileURL;
|
194
|
n_tileLayers[1].isPng = function() { return false; };
|
195
|
n_tileLayers[1].getOpacity = function() { return opacity; };
|
196
|
var n_customMap = new GMapType(n_tileLayers, new GMercatorProjection(6), n_buttonText,
|
197
|
{maxResolution:5, minResolution:0, errorMessage:"Data not available"});
|
198
|
|
199
|
//create a custom G_SATELLITE_MAP layer
|
200
|
var s_tileLayers = [ G_SATELLITE_MAP.getTileLayers()[0], new GTileLayer(copyrightCollection , 0, 17)];
|
201
|
s_tileLayers[1].getTileUrl = customGetTileURL;
|
202
|
s_tileLayers[1].isPng = function() { return false; };
|
203
|
s_tileLayers[1].getOpacity = function() { return opacity; };
|
204
|
var s_customMap = new GMapType(s_tileLayers, new GMercatorProjection(6), s_buttonText,
|
205
|
{maxResolution:5, minResolution:0, errorMessage:"Data not available"});
|
206
|
|
207
|
//Now create the custom map. Would normally be G_NORMAL_MAP,G_SATELLITE_MAP,G_HYBRID_MAP
|
208
|
map = new GMap2(document.getElementById("map"),{mapTypes:[n_customMap,s_customMap]});
|
209
|
map.addControl(new GLargeMapControl());
|
210
|
map.addControl(new GMapTypeControl());
|
211
|
map.enableContinuousZoom();
|
212
|
map.setCenter(new GLatLng(centreLat, centreLon), 2, n_customMap);
|
213
|
map.enableDoubleClickZoom();
|
214
|
/*
|
215
|
var point = new GLatLng(0,0);
|
216
|
var marker = new GMarker(point);
|
217
|
GEvent.addListener(marker, "click", function() {
|
218
|
marker.openInfoWindowHtml("More stuff");
|
219
|
});
|
220
|
map.addOverlay(marker);
|
221
|
*/
|
222
|
}
|
223
|
}
|