1
|
<?php
|
2
|
/**
|
3
|
* @file
|
4
|
* Functions for dealing maps
|
5
|
*
|
6
|
* @copyright
|
7
|
* (C) 2007-2013 EDIT
|
8
|
* European Distributed Institute of Taxonomy
|
9
|
* http://www.e-taxonomy.eu
|
10
|
*
|
11
|
* The contents of this module are subject to the Mozilla
|
12
|
* Public License Version 1.1.
|
13
|
* @see http://www.mozilla.org/MPL/MPL-1.1.html
|
14
|
*
|
15
|
* @author
|
16
|
* - Andreas Kohlbecker <a.kohlbecker@BGBM.org>
|
17
|
*/
|
18
|
|
19
|
/**
|
20
|
* Compose an render array for distribution and occurrence
|
21
|
* maps.
|
22
|
*
|
23
|
* The map can either be a plain image or a dynamic open layers map
|
24
|
* depending on the settings
|
25
|
*
|
26
|
* compose_hook() implementation
|
27
|
*
|
28
|
* @param string $occurrence_query
|
29
|
* @param string $distribution_query
|
30
|
* @param string $legend_format_query
|
31
|
* @param array $event_listeners
|
32
|
* An associative array of with OpenLayers.Map event names as key and corresponding js callbacks.
|
33
|
* In addition to the event names '#execute' as key is also allowed.
|
34
|
* Valid events are:
|
35
|
* - move
|
36
|
* - moveend
|
37
|
* - zoomend
|
38
|
* - changelayer
|
39
|
* - changebaselayer
|
40
|
* - #execute:
|
41
|
* force execution of the given callback after registration of the event handlers
|
42
|
* see http://dev.openlayers.org/apidocs/files/OpenLayers/Map-js.html#OpenLayers.Map.events for more
|
43
|
*
|
44
|
* @return array
|
45
|
* A drupal render array
|
46
|
*
|
47
|
* Similar compose function compose_distribution_map()
|
48
|
*
|
49
|
* @ingroup compose
|
50
|
*/
|
51
|
function compose_map($occurrence_query = NULL, $distribution_query = NULL, $legend_format_query = NULL, array $event_listeners = array()) {
|
52
|
|
53
|
$map_settings = get_array_variable_merged(CDM_MAP_DISTRIBUTION, CDM_MAP_DISTRIBUTION_DEFAULT);
|
54
|
if ($map_settings['map_type'] == 1) {
|
55
|
$map_html = get_openlayers_map(
|
56
|
$map_settings['width'],
|
57
|
$map_settings['height'],
|
58
|
$map_settings['bbox'],
|
59
|
$occurrence_query,
|
60
|
$distribution_query,
|
61
|
$legend_format_query,
|
62
|
$map_settings['caption'],
|
63
|
$event_listeners
|
64
|
);
|
65
|
}
|
66
|
else {
|
67
|
$map_html = get_image_map(
|
68
|
$map_settings['width'],
|
69
|
$map_settings['height'],
|
70
|
$map_settings['bbox'],
|
71
|
$occurrence_query,
|
72
|
$distribution_query,
|
73
|
$legend_format_query,
|
74
|
$map_settings['caption']
|
75
|
);
|
76
|
}
|
77
|
return markup_to_render_array($map_html, 0);
|
78
|
}
|
79
|
|
80
|
/**
|
81
|
* @param $map_settings
|
82
|
* The map settings array as retrieved by e.g. get_array_variable_merged(CDM_MAP_DISTRIBUTION, CDM_MAP_DISTRIBUTION_DEFAULT);
|
83
|
* @param array $event_listeners
|
84
|
* An associative array of with OpenLayers.Map event names as key and corresponding js callbacks.
|
85
|
* In addition to the event names '#execute' as key is also allowed.
|
86
|
* Valid events are:
|
87
|
* - move
|
88
|
* - moveend
|
89
|
* - zoomend
|
90
|
* - changelayer
|
91
|
* - changebaselayer
|
92
|
* - #execute:
|
93
|
* force execution of the given callback after registration of the event handlers
|
94
|
* see http://dev.openlayers.org/apidocs/files/OpenLayers/Map-js.html#OpenLayers.Map.events for more
|
95
|
*/
|
96
|
function _add_js_openlayers_map($map_settings, array $event_listeners = array()) {
|
97
|
|
98
|
_add_js_openlayers();
|
99
|
|
100
|
$edit_map_service = get_edit_map_service_settings();
|
101
|
|
102
|
$gmap_api_key = variable_get('gmap_api_key', 'ABQIAAAAFho6eHAcUOTHLmH9IYHAeBRi_j0U6kJrkFvY4-OX2XYmEAa76BTsyMmEq-tn6nFNtD2UdEGvfhvoCQ');
|
103
|
|
104
|
drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . '/js/map/openlayers_map.js');
|
105
|
drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . '/js/map/openlayers_layers.js');
|
106
|
|
107
|
$cdm_openlayers_options = array(
|
108
|
'legendPosition' => '3',
|
109
|
'boundingBox' => $map_settings['bbox'],
|
110
|
'distributionOpacity' => $map_settings['distribution_opacity'],
|
111
|
'legendOpacity' => $map_settings['legend']['opacity'],
|
112
|
'showLayerSwitcher' => $map_settings['openlayers']['show_layer_switcher'] == 1,
|
113
|
'displayOutsideMaxExtent' => $map_settings['openlayers']['display_outside_max_extent'] == 1
|
114
|
// if no baseLayerNames or defaultBaseLayerName are not defined
|
115
|
// the defaults in cdm_openlayers.js will be used
|
116
|
);
|
117
|
|
118
|
// --- setting the base layer options
|
119
|
if (is_array($map_settings['openlayers']['base_layers']) && count($map_settings['openlayers']['base_layers']) > 0) {
|
120
|
|
121
|
$layer_names = $map_settings['openlayers']['base_layers'];
|
122
|
|
123
|
if(isset($layer_names['PREFERRED'])){
|
124
|
$cdm_openlayers_options['defaultBaseLayerName'] = $layer_names['PREFERRED'];
|
125
|
unset($layer_names['PREFERRED']);
|
126
|
}
|
127
|
|
128
|
$cdm_openlayers_options['baseLayerNames'] = array_values($layer_names);
|
129
|
|
130
|
if (isset($layer_names['gmap']) || isset($layer_names['gsat']) || isset($layer_names['ghyb'])) {
|
131
|
// gmaps version 2 (needs api key)
|
132
|
drupal_add_js('http://maps.google.com/maps?file=api&v=2&key=' . $gmap_api_key . '', array('type' => 'external'));
|
133
|
// gmaps version 3 (does not need api key)
|
134
|
// drupal_add_js('http://maps.google.com/maps?file=api&v=3&sensor=false', array('type' => 'external'));
|
135
|
drupal_add_js('http://dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6.1', array('type' => 'external'));
|
136
|
}
|
137
|
|
138
|
}
|
139
|
|
140
|
// --- custom wms base layer
|
141
|
$map_settings['openlayers']['custom_wms_base_layer']['params'] = json_decode($map_settings['openlayers']['custom_wms_base_layer']['params']);
|
142
|
$cdm_openlayers_options['customWMSBaseLayerData'] = $map_settings['openlayers']['custom_wms_base_layer'];
|
143
|
|
144
|
// --- eventhandlers
|
145
|
$event_listeners_js = '';
|
146
|
$execute_handler = '';
|
147
|
foreach($event_listeners as $event=>$js_callback){
|
148
|
if($event == '#execute'){
|
149
|
$execute_handler = 'map_container.each(function(){' . $js_callback . '();});';
|
150
|
} else {
|
151
|
$event_listeners_js .= ($event_listeners_js ? ",\n": "\n") .'"' . $event . '": ' . $js_callback;
|
152
|
}
|
153
|
}
|
154
|
|
155
|
// // combine keys and values
|
156
|
// foreach($cdm_openlayers_options as $key=>&$val){
|
157
|
// $val = $key . ": " . $val;
|
158
|
// };
|
159
|
|
160
|
// window.onload - is executed when the document and images etc is fully loaded
|
161
|
// Query(document).ready - is executed much earlier, when the DOM is loaded
|
162
|
drupal_add_js("
|
163
|
jQuery(document).ready(function() {
|
164
|
window.onload = function () {
|
165
|
var map_container = jQuery('#openlayers_map').cdm_openlayers_map(
|
166
|
'" . $edit_map_service['base_uri'] . "',
|
167
|
'" . $edit_map_service['version'] . "',
|
168
|
" . json_encode($cdm_openlayers_options) . "
|
169
|
);
|
170
|
map_container.each(function(){
|
171
|
this.cdmOpenlayersMap.registerEvents({" . $event_listeners_js . "});
|
172
|
});
|
173
|
" . $execute_handler . "
|
174
|
};
|
175
|
});
|
176
|
", array('type' => 'inline'));
|
177
|
|
178
|
}
|
179
|
|
180
|
|
181
|
/**
|
182
|
* @todo Enter description here ...
|
183
|
*
|
184
|
* @param unknown_type $width
|
185
|
* @param unknown_type $bounding_box
|
186
|
* @param unknown_type $occurrenceQuery
|
187
|
* @param unknown_type $distributionQuery
|
188
|
* @param unknown_type $legendFormatQuery
|
189
|
* @param unknown_type $map_caption
|
190
|
* @param array $event_listeners
|
191
|
* An associative array of with OpenLayers.Map event names as key and corresponding js callbacks.
|
192
|
* In addition to the event names '#execute' as key is also allowed.
|
193
|
* Valid events are:
|
194
|
* - move
|
195
|
* - moveend
|
196
|
* - zoomend
|
197
|
* - changelayer
|
198
|
* - changebaselayer
|
199
|
* - #execute:
|
200
|
* force execution of the given callback after registration of the event handlers
|
201
|
* see http://dev.openlayers.org/apidocs/files/OpenLayers/Map-js.html#OpenLayers.Map.events for more
|
202
|
*
|
203
|
* @return String
|
204
|
* rendered html
|
205
|
*/
|
206
|
function get_openlayers_map($width, $height, $bounding_box = FALSE, $occurrenceQuery = FALSE, $distributionQuery = FALSE, $legendFormatQuery = FALSE, $map_caption = FALSE, array $event_listeners = array()) {
|
207
|
|
208
|
$map_settings = get_array_variable_merged(CDM_MAP_DISTRIBUTION, CDM_MAP_DISTRIBUTION_DEFAULT);
|
209
|
|
210
|
_add_js_openlayers_map($map_settings, $event_listeners);
|
211
|
|
212
|
$out = '<div id="openlayers">';
|
213
|
$out .= '<div id="openlayers_map" class="smallmap"';
|
214
|
if($width) {
|
215
|
if(!$height){
|
216
|
$height = $width / 2;
|
217
|
}
|
218
|
$out .= ' style="width: ' . $width . 'px; height:' . $height . 'px"';
|
219
|
}
|
220
|
|
221
|
// Additional query parameters as set in the data portal admin section.
|
222
|
$labels_on = $map_settings['show_labels'];
|
223
|
|
224
|
$openlayers_map_query_string = '&img=false&ms=' . $width
|
225
|
. ($bounding_box ? '&bbox=' . $bounding_box : '')
|
226
|
. ($labels_on ? '&label=' . $labels_on : '');
|
227
|
|
228
|
if ($occurrenceQuery) {
|
229
|
// @todo Fix $occurrenceQuery.
|
230
|
// $occurrenceQuery .= '&bbox=-180,-90,180,90';
|
231
|
$occurrenceQuery .= '&l=v%3Aatbi%2Ce_w_0'; // TODO why are we using v:atbi,e_w_0 as layer ???
|
232
|
// $occurrenceQuery .= '&l=v:e_w_0';
|
233
|
// TODO add to cdm service?
|
234
|
$occurrenceQuery .= '&legend=0';
|
235
|
|
236
|
$out .= ' occurrenceQuery="' . $occurrenceQuery . '&' . $openlayers_map_query_string . '"';
|
237
|
}
|
238
|
|
239
|
if ($distributionQuery) {
|
240
|
//HACK for testing (this must be done in js)
|
241
|
// $distributionQuery .= "&layer=em_tiny_jan2003&dest_projection_epsg=7777777";
|
242
|
$out .= ' distributionQuery="' . $distributionQuery . '&' . $openlayers_map_query_string . '"';
|
243
|
}
|
244
|
|
245
|
if ($legendFormatQuery) {
|
246
|
$out .= ' legendFormatQuery="' . $legendFormatQuery . '"';
|
247
|
}
|
248
|
|
249
|
$out .= '></div></div>';
|
250
|
|
251
|
// Showing map caption.
|
252
|
if ($map_caption) {
|
253
|
// FIXME: replace <br> by according css style.
|
254
|
$out .= '<div class="distribution_map_caption">' . $map_caption . '</div>' . '<br />';
|
255
|
$out .= '</div>';
|
256
|
}
|
257
|
return $out;
|
258
|
}
|
259
|
|
260
|
|
261
|
/**
|
262
|
* Composes the render array for a distribution map of the given taxon.
|
263
|
*
|
264
|
* The distribution map can either be a plain image or a dynamic open layers map
|
265
|
* depending on the settings.
|
266
|
*
|
267
|
* compose_hook() implementation
|
268
|
*
|
269
|
* @param $taxon
|
270
|
* The CDM Taxon instance to create the distribution map for.
|
271
|
* @return array
|
272
|
* A drupal render array
|
273
|
*
|
274
|
* Similar compose function compose_map()
|
275
|
*
|
276
|
* @ingroup compose
|
277
|
*/
|
278
|
function compose_distribution_map($taxon, $query_string) {
|
279
|
|
280
|
$out = '';
|
281
|
$settings = get_edit_map_service_settings();
|
282
|
|
283
|
$fontStyles = array(
|
284
|
0 => "plane",
|
285
|
1 => "italic",
|
286
|
);
|
287
|
|
288
|
// // Query cdm server for map service uri parameters.
|
289
|
// $query_string = cdm_ws_get(CDM_WS_GEOSERVICE_DISTRIBUTIONMAP, $taxon->uuid, queryString(cdm_distribution_filter_query()));
|
290
|
// $query_string = $query_string->String;
|
291
|
$out .= "<!-- map_data_parameters:" . print_r($query_string, TRUE) . " -->";
|
292
|
if (!$query_string) {
|
293
|
// The $query_string is empty if there are no distribution areas defined.
|
294
|
return;
|
295
|
}
|
296
|
|
297
|
/* ------ choose the display mode, either openlayers or static image ------ */
|
298
|
|
299
|
$map_settings = get_array_variable_merged(CDM_MAP_DISTRIBUTION, CDM_MAP_DISTRIBUTION_DEFAULT);
|
300
|
|
301
|
if ($map_settings['map_type'] == 1) {
|
302
|
|
303
|
/* =========== display distributions using the openlayers map viewer =========== */
|
304
|
|
305
|
$legendFormatQueryStr = "format=image" . urlencode('/') . "png"
|
306
|
. "&TRANSPARENT=TRUE"
|
307
|
. "&WIDTH=" . $map_settings['legend']['icon_width']
|
308
|
. "&HEIGHT=" . $map_settings['legend']['icon_height']
|
309
|
// TODO why is the layer=topp:tdwg_level_4 parameter needed at all here??
|
310
|
// AK: i think the tdwg_level_4 is used as place holder and will be replaced later on
|
311
|
// => search for "tdwg_level_4" in the code
|
312
|
. "&layer=topp" . urlencode(':') . "tdwg_level_4"
|
313
|
. "&LEGEND_OPTIONS=forceLabels" . urlencode(':') . "on"
|
314
|
. ";fontStyle" . urlencode(':') . $fontStyles[$map_settings['legend']['font_style']]
|
315
|
. ";fontSize" . urlencode(':') . $map_settings['legend']['font_size']
|
316
|
. "&SLD=";
|
317
|
|
318
|
$out .= get_openlayers_map(
|
319
|
$map_settings['width'],
|
320
|
$map_settings['height'],
|
321
|
$map_settings['bbox'],
|
322
|
NULL,
|
323
|
$query_string,
|
324
|
$legendFormatQueryStr,
|
325
|
$map_settings['caption']
|
326
|
);
|
327
|
}
|
328
|
else {
|
329
|
$legendFormatQueryStr = '';
|
330
|
$out = get_image_map(
|
331
|
$map_settings['width'],
|
332
|
$map_settings['height'],
|
333
|
$map_settings['bbox'],
|
334
|
NULL,
|
335
|
$query_string,
|
336
|
$legendFormatQueryStr,
|
337
|
$map_settings['caption']
|
338
|
);
|
339
|
}
|
340
|
return markup_to_render_array($out);
|
341
|
}
|
342
|
|
343
|
|
344
|
|
345
|
|
346
|
/**
|
347
|
* @todo Enter description here ...
|
348
|
*
|
349
|
* @param unknown_type $width
|
350
|
* @param unknown_type $bounding_box
|
351
|
* @param unknown_type $occurrenceQuery
|
352
|
* @param unknown_type $distributionQuery
|
353
|
* @param unknown_type $legendFormatQuery
|
354
|
* @param unknown_type $map_caption
|
355
|
*
|
356
|
* @return String
|
357
|
* rendered html
|
358
|
*/
|
359
|
function get_image_map($width, $height= NULL, $bounding_box = FALSE, $occurrenceQuery = FALSE, $distributionQuery = FALSE, $legendFormatQuery = FALSE, $map_caption = FALSE) {
|
360
|
|
361
|
$map_settings = get_array_variable_merged(CDM_MAP_DISTRIBUTION, CDM_MAP_DISTRIBUTION_DEFAULT);
|
362
|
|
363
|
$baselayer_name = $map_settings['image_map']['base_layer'];
|
364
|
if(empty($baselayer_name)){
|
365
|
$baselayer_name = "earth";
|
366
|
}
|
367
|
|
368
|
$query_string = '&image=true&recalculate=false&ms=' . $width . ($height ? ',' . $height : '')
|
369
|
// Additional query parameters as set in the data portal admin section.
|
370
|
. ($bounding_box ? '&bbox=' . $bounding_box : '')
|
371
|
. ($map_settings['show_labels'] ? '&label=' . $map_settings['show_labels'] : '');
|
372
|
|
373
|
if ($map_caption) {
|
374
|
$query_string .= '&mlp=3&mc_s=Georgia,15,blue&mc=' . $map_caption;
|
375
|
}
|
376
|
|
377
|
if (get_edit_map_service_version_number() >= 1.1) {
|
378
|
|
379
|
// Either occurrence or distribution - combined maps will be possible
|
380
|
// in the future.
|
381
|
if ($occurrenceQuery) {
|
382
|
// @todo Fix $occurrenceQuery.
|
383
|
$occurrenceQuery = str_replace("&image=false", "", $occurrenceQuery);
|
384
|
// $occurrenceQuery .= '&l=v%3Aatbi%2Ce_w_0';
|
385
|
|
386
|
// Will be replaced below.. HACK!!!
|
387
|
$occurrenceQuery .= '&l=' . $baselayer_name . '&as=';
|
388
|
|
389
|
$query_string .= "&" . $occurrenceQuery;
|
390
|
}
|
391
|
elseif ($distributionQuery) {
|
392
|
$query_string .= '&l=' . $baselayer_name . "&" .$distributionQuery;
|
393
|
}
|
394
|
|
395
|
// Apply Plain Image map settings special for version >= 1.1.
|
396
|
/*
|
397
|
example : title=a:Naturalized++non-invasive
|
398
|
&ad=cyprusdivs:bdcode:a:5&as=a:ff9900,,0.1,&l=tdwg4
|
399
|
&ms=500&bbox=32,34,35,36&img=true&legend=1&mlp=3
|
400
|
&mc_s=Georgia,15,blue&mc=&recalculate=false
|
401
|
|
402
|
http://edit.br.fgov.be/edit_wp5/v1/rest_gen.php?
|
403
|
l=background_gis:b,cyprusdivs&ad=cyprusdivs%3Abdcode%3Aa%3A8%2C4
|
404
|
&as=a%3A339966%2C%2C0.1%2C|b:0000ff,,
|
405
|
&bbox=32%2C34%2C35%2C36&img=true&legend=1&mc=&mc_s=Georgia%2C15%2Cblue
|
406
|
&mlp=3&ms=500&recalculate=false&title=a%3Aindigenous
|
407
|
*/
|
408
|
|
409
|
$map_service_script_name = "rest_gen.php";
|
410
|
|
411
|
$bgcolor_areaStyleId = "Y";
|
412
|
$baselayer_areaStyleId = "Z";
|
413
|
$bgcolor_layer = '';
|
414
|
$additional_area_styles = array();
|
415
|
|
416
|
// Background color:
|
417
|
if ($map_settings['image_map']['bg_color'] ) {
|
418
|
$bgcolor_layer = "background_gis:" . $bgcolor_areaStyleId;
|
419
|
$additional_area_styles[] = $bgcolor_areaStyleId . ":" . $map_settings['image_map']['bg_color'] . ",,";
|
420
|
}
|
421
|
|
422
|
// TODO HACK to replace the default base layer which currently is tdwg4 !!!
|
423
|
// only needed for distribution maps.
|
424
|
if (strpos($query_string, "?l=") !== FALSE) {
|
425
|
$layer_param_token = "?l=";
|
426
|
}
|
427
|
else {
|
428
|
$layer_param_token = "&l=";
|
429
|
}
|
430
|
if (strpos($query_string, "?as=") !== FALSE) {
|
431
|
$areystyle_param_token = "?as=";
|
432
|
}
|
433
|
else {
|
434
|
$areystyle_param_token = "&as=";
|
435
|
}
|
436
|
if ($map_settings['image_map']['base_layer']) {
|
437
|
$query_string = str_replace($layer_param_token .$baselayer_name, "$layer_param_token" . $map_settings['image_map']['base_layer'] . ":" . $baselayer_areaStyleId, $query_string);
|
438
|
}
|
439
|
else {
|
440
|
$query_string = str_replace($layer_param_token . $baselayer_name, $layer_param_token . $baselayer_name . ":" . $baselayer_areaStyleId . ",", $query_string);
|
441
|
}
|
442
|
|
443
|
if ($bgcolor_layer) {
|
444
|
$query_string = str_replace($layer_param_token, $layer_param_token . $bgcolor_layer . ",", $query_string);
|
445
|
}
|
446
|
|
447
|
if ($map_settings['image_map']['layer_style']) {
|
448
|
$additional_area_styles[] = $baselayer_areaStyleId . ":" . $map_settings['image_map']['layer_style'];
|
449
|
}
|
450
|
|
451
|
if(isset($map_settings['projection'])){
|
452
|
$query_string .= "&srs=" . $map_settings['projection'];
|
453
|
}
|
454
|
|
455
|
if(isset($map_settings['legend']['show']) && $map_settings['legend']['show']){
|
456
|
$query_string .= "&legend=1";
|
457
|
}
|
458
|
|
459
|
foreach ($additional_area_styles as $as) {
|
460
|
$query_string = str_replace($areystyle_param_token, $areystyle_param_token . $as . "|", $query_string);
|
461
|
}
|
462
|
|
463
|
}
|
464
|
else {
|
465
|
// Pre 1.1. version of map service.
|
466
|
if ($occurrenceQuery) {
|
467
|
|
468
|
$map_service_script_name = "point.php";
|
469
|
|
470
|
// Fix $occurrenceQuery.
|
471
|
$occurrenceQuery = str_replace("&image=false", "", $occurrenceQuery);
|
472
|
// $occurrenceQuery .= '&l=v%3Aatbi%2Ce_w_0';
|
473
|
$occurrenceQuery .= '&l=v:e_w_0';
|
474
|
$query_string .= "&" . $occurrenceQuery;
|
475
|
}
|
476
|
elseif ($distributionQuery) {
|
477
|
$query_string .= "&" . $distributionQuery;
|
478
|
$map_service_script_name = "areas.php";
|
479
|
}
|
480
|
}
|
481
|
|
482
|
$mapUri = url(get_edit_map_service_full_uri() . '/' . $map_service_script_name . '?' . $query_string);
|
483
|
$out = '<img class="distribution_map" src="' . $mapUri . '" alt="Map" />';
|
484
|
// Showing map caption.
|
485
|
if ($map_caption) {
|
486
|
// FIXME: replace <br> by according css style.
|
487
|
$out .= '<div class="distribution_map_caption">' . $map_caption . '</div>' . '<br />';
|
488
|
$out .= '</div>';
|
489
|
}
|
490
|
|
491
|
return $out;
|
492
|
}
|
493
|
|
494
|
|