Project

General

Profile

Download (17.3 KB) Statistics
| Branch: | Tag: | Revision:
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
 * @param bool $resizable
44
 *    only possible for openlayers_map
45
 * @param string $force_map_type
46
 *   Can be used to override the map_type setting stored in the settings variable CDM_MAP_DISTRIBUTION
47
 *   - 1: openlayers_map
48
 *   - 0: image_map
49
 *
50
 * @return array
51
 *    A drupal render array
52
 *
53
 * Similar compose function compose_distribution_map()
54
 *
55
 * @ingroup compose
56
 */
57
function compose_map($occurrence_query = NULL, $distribution_query = NULL,
58
                     $legend_format_query = NULL, array $event_listeners = array(), $resizable = false,
59
                     $force_map_type = NULL) {
60

    
61
    $map_settings = get_array_variable_merged(CDM_MAP_DISTRIBUTION, CDM_MAP_DISTRIBUTION_DEFAULT);
62

    
63
    if($force_map_type === NULL){
64
      $force_map_type = $map_settings['map_type'];
65
    }
66

    
67
    if ($force_map_type == 1) {
68
      _add_jquery_ui();
69
    $map_html = get_openlayers_map(
70
        $map_settings['bbox'],
71
        $occurrence_query,
72
        $distribution_query,
73
        $legend_format_query,
74
        $map_settings['caption'],
75
        $event_listeners,
76
        $resizable
77
    );
78
    }
79
     else {
80
      $map_height = round($map_settings['image_map']['width'] / (float)$map_settings['aspect_ratio']);
81
      $map_html = get_image_map(
82
        $map_settings['image_map']['width'],
83
        $map_height,
84
        $map_settings['bbox'],
85
        $occurrence_query,
86
        $distribution_query,
87
        $legend_format_query,
88
        $map_settings['caption']
89
      );
90
     }
91
  return markup_to_render_array($map_html);
92
}
93

    
94
/**
95
 * @param $map_settings
96
 *   The map settings array as retrieved by e.g. get_array_variable_merged(CDM_MAP_DISTRIBUTION, CDM_MAP_DISTRIBUTION_DEFAULT);
97
 * @param array $event_listeners
98
 *   An associative array of with OpenLayers.Map event names as key and corresponding js callbacks.
99
 *   In addition to the event names '#execute' as key is also allowed.
100
 *   Valid events are:
101
 *      - move
102
 *      - moveend
103
 *      - zoomend
104
 *      - changelayer
105
 *      - changebaselayer
106
 *      - #execute:
107
 *            force execution of the given callback after registration of the event handlers
108
 *   see http://dev.openlayers.org/apidocs/files/OpenLayers/Map-js.html#OpenLayers.Map.events for more
109
 * @param bool $resizable
110
 *   The map is made resizable when set to true
111
 */
112
function _add_js_openlayers_map($map_settings, array $event_listeners = array(), $resizable = false) {
113

    
114
  _add_js_openlayers();
115

    
116
  $edit_map_service = get_edit_map_service_settings();
117

    
118
  drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . '/js/map/openlayers_map.js');
119
  drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . '/js/map/openlayers_layers.js');
120

    
121
  $cdm_openlayers_options = array(
122
      'legendPosition'  => '3',
123
      'boundingBox' => $map_settings['bbox'],
124
      'aspectRatio' => $map_settings['aspect_ratio'],
125
      'distributionOpacity' => $map_settings['distribution_opacity'],
126
      'legendOpacity' => $map_settings['legend']['opacity'],
127
      'showLayerSwitcher' => $map_settings['openlayers']['show_layer_switcher']  ==  1,
128
      'displayOutsideMaxExtent' => $map_settings['openlayers']['display_outside_max_extent'] == 1,
129
      'resizable' => $resizable
130
//       'imgPath' => drupal_get_path('module', 'cdm_dataportal') . '/js/map/OpenLayers-2.13.1/img/' // path to the control icons
131
      // if no baseLayerNames or defaultBaseLayerName are not defined
132
      // the defaults in cdm_openlayers.js will be used
133
  );
134

    
135
  // --- setting the base layer options
136
  if (is_array($map_settings['openlayers']['base_layers']) && count($map_settings['openlayers']['base_layers']) > 0) {
137

    
138
    $layer_names = $map_settings['openlayers']['base_layers'];
139

    
140
    if(isset($layer_names['PREFERRED'])){
141
      $cdm_openlayers_options['defaultBaseLayerName'] = $layer_names['PREFERRED'];
142
      unset($layer_names['PREFERRED']);
143
    }
144

    
145
    $cdm_openlayers_options['baseLayerNames'] = array_values($layer_names);
146

    
147
  }
148

    
149
  // --- custom wms base layer
150
  $map_settings['openlayers']['custom_wms_base_layer']['params'] = json_decode($map_settings['openlayers']['custom_wms_base_layer']['params']);
151
  $cdm_openlayers_options['customWMSBaseLayerData'] = $map_settings['openlayers']['custom_wms_base_layer'];
152

    
153
  // --- eventhandlers
154
  $event_listeners_js = '';
155
  $execute_handler = '';
156
  foreach($event_listeners as $event=>$js_callback){
157
    if($event == '#execute'){
158
      $execute_handler = 'map_container.each(function(){' . $js_callback . '();});';
159
    } else {
160
      $event_listeners_js .= ($event_listeners_js ? ",\n": "\n") .'"' . $event . '": ' . $js_callback;
161
    }
162
  }
163

    
164
//   // combine keys and values
165
//   foreach($cdm_openlayers_options as $key=>&$val){
166
//     $val = $key . ": " . $val;
167
//   };
168

    
169
  // window.onload - is executed when the document and images etc is fully loaded
170
  // Query(document).ready - is executed much earlier, when the DOM is loaded
171
  drupal_add_js("
172
          jQuery(document).ready(function() {
173
                jQuery(window).load(function () {
174
                  var map_container = jQuery('#openlayers_map').cdm_openlayers_map(
175
                   '" . $edit_map_service['base_uri'] . "',
176
                   '" . $edit_map_service['version'] . "',
177
                   " .  json_encode($cdm_openlayers_options) . "
178
                );
179
                map_container.each(function(){
180
                        this.cdmOpenlayersMap.registerEvents({" . "
181
                        " . $event_listeners_js . "
182
                        });
183
                });
184
                " . $execute_handler . "
185
        });
186
      });
187
    ", array('type' => 'inline'));
188

    
189
}
190

    
191

    
192
/**
193
 * @todo Enter description here ...
194
 *
195
 * @param unknown_type $bounding_box
196
 * @param unknown_type $occurrenceQuery
197
 * @param unknown_type $distributionQuery
198
 * @param unknown_type $legendFormatQuery
199
 * @param unknown_type $map_caption
200
 * @param array $event_listeners
201
 *   An associative array of with OpenLayers.Map event names as key and corresponding js callbacks.
202
 *   In addition to the event names '#execute' as key is also allowed.
203
 *   Valid events are:
204
 *      - move
205
 *      - moveend
206
 *      - zoomend
207
 *      - changelayer
208
 *      - changebaselayer
209
 *      - #execute:
210
 *            force execution of the given callback after registration of the event handlers
211
 *   see http://dev.openlayers.org/apidocs/files/OpenLayers/Map-js.html#OpenLayers.Map.events for more
212
 *
213
 * @return String
214
 *    rendered html
215
 */
216
function get_openlayers_map($bounding_box = FALSE, $occurrenceQuery = FALSE, $distributionQuery = FALSE,
217
                            $legendFormatQuery = FALSE, $map_caption = FALSE, array $event_listeners = array(),
218
                            $resizable = false) {
219

    
220
  $map_settings = get_array_variable_merged(CDM_MAP_DISTRIBUTION, CDM_MAP_DISTRIBUTION_DEFAULT);
221

    
222
  _add_js_openlayers_map($map_settings, $event_listeners, $resizable);
223

    
224
  $out = '<div id="openlayers_container" class="openlayers_width ui-widget-content" style="width: 100%;">';
225
  $out .= '<div id="openlayers_map" class="smallmap" style="width:100%; height:100%; margin: 10px;"';
226

    
227
  // Additional query parameters as set in the data portal admin section.
228
  $labels_on = $map_settings['show_labels'];
229

    
230
  // need to set the ms parameter to some value in order to satisfy the
231
  // map service even if this value should not be required:
232
  $width = 512;
233

    
234
  $openlayers_map_query_string = '&img=false&ms=' . $width
235
  . ($bounding_box ? '&bbox=' . $bounding_box : '')
236
  . ($labels_on ? '&label=' . $labels_on : '');
237

    
238
  if ($occurrenceQuery) {
239
    // @todo Fix $occurrenceQuery.
240
    //     $occurrenceQuery .= '&bbox=-180,-90,180,90';
241
    $occurrenceQuery .= '&l=v%3Aatbi%2Ce_w_0'; // TODO why are we using v:atbi,e_w_0 as layer ???
242
    // $occurrenceQuery .= '&l=v:e_w_0';
243
    // TODO add to cdm service?
244
    $occurrenceQuery .= '&legend=0';
245

    
246
    $out .= ' occurrenceQuery="' . $occurrenceQuery . '&' . $openlayers_map_query_string . '"';
247
  }
248

    
249
  if ($distributionQuery) {
250
    //HACK for testing (this must be done in js)
251
//     $distributionQuery .= "&layer=em_tiny_jan2003&dest_projection_epsg=7777777";
252
    $out .= ' distributionQuery="' . $distributionQuery . '&' . $openlayers_map_query_string . '"';
253
  }
254

    
255
  if ($legendFormatQuery) {
256
    $out .= ' legendFormatQuery="' . $legendFormatQuery . '"';
257
  }
258

    
259
  $out .= '></div></div>';
260

    
261
  // Showing map caption.
262
  if ($map_caption) {
263
    // FIXME: replace <br> by according css style.
264
    $out .= '<div class="distribution_map_caption">' . $map_caption . '</div>' . '<br />';
265
    $out .= '</div>';
266
  }
267
  return $out;
268
}
269

    
270

    
271
/**
272
 * Composes the render array for a distribution map of the given taxon.
273
 *
274
 * The distribution map can either be a plain image or a dynamic open layers map
275
 * depending on the settings.
276
 *
277
 * compose_hook() implementation
278
 *
279
 * @param $taxon
280
 *   The CDM Taxon instance to create the distribution map for.
281
 * @return array
282
 *    A drupal render array
283
 *
284
 * Similar compose function compose_map()
285
 *
286
 * @ingroup compose
287
 */
288
function compose_distribution_map($taxon, $query_string) {
289

    
290
  $out = '';
291
  $settings = get_edit_map_service_settings();
292

    
293
  $fontStyles = array(
294
      0 => "plane",
295
      1 => "italic",
296
  );
297

    
298
//   // Query cdm server for map service uri parameters.
299
//   $query_string = cdm_ws_get(CDM_WS_GEOSERVICE_DISTRIBUTIONMAP, $taxon->uuid, queryString(cdm_distribution_filter_query()));
300
//   $query_string = $query_string->String;
301
  $out .= "<!-- map_data_parameters:" . print_r($query_string, TRUE) . " -->";
302
  if (!$query_string) {
303
    // The $query_string is empty if there are no distribution areas defined.
304
    return;
305
  }
306

    
307
  /* ------ choose the display mode, either openlayers or static image ------ */
308

    
309
  $map_settings = get_array_variable_merged(CDM_MAP_DISTRIBUTION, CDM_MAP_DISTRIBUTION_DEFAULT);
310

    
311
  if ($map_settings['map_type'] == 1) {
312

    
313
    /* =========== display distributions using the openlayers map viewer =========== */
314

    
315
    $legendFormatQueryStr = "format=image" . urlencode('/') . "png"
316
      . "&TRANSPARENT=TRUE"
317
      . "&WIDTH=" . $map_settings['legend']['icon_width']
318
      . "&HEIGHT=" . $map_settings['legend']['icon_height']
319
      // TODO why is the layer=topp:tdwg_level_4 parameter needed at all here??
320
      // AK: i think the tdwg_level_4 is used as place holder and will be replaced later on
321
      // => search for "tdwg_level_4" in the code
322
      . "&layer=topp" . urlencode(':') . "tdwg_level_4"
323
      . "&LEGEND_OPTIONS=forceLabels" . urlencode(':') . "on"
324
      . ";fontStyle" . urlencode(':') . $fontStyles[$map_settings['legend']['font_style']]
325
      . ";fontSize" . urlencode(':') .  $map_settings['legend']['font_size']
326
      . "&SLD=";
327

    
328
    /*$out .= get_openlayers_map(
329
        $map_settings['bbox'],
330
        NULL,
331
        $query_string,
332
        $legendFormatQueryStr,
333
        $map_settings['caption']
334
    );
335
    */
336
  }
337
  else {
338
    $legendFormatQueryStr = '';
339
    /*
340
        get_image_map(
341
            $map_settings['image_map']['width'],
342
            $map_settings['image_map']['height'],
343
            $map_settings['bbox'],
344
            NULL,
345
            $query_string,
346
            $legendFormatQueryStr,
347
            $map_settings['caption']
348
        );
349
    */
350
  }
351
  $out = compose_map(NULL, $query_string, $legendFormatQueryStr);
352

    
353
  return $out;
354
}
355

    
356

    
357

    
358

    
359
/**
360
 * @todo Enter description here ...
361
 *
362
 * @param unknown_type $width
363
 * @param unknown_type $bounding_box
364
 * @param unknown_type $occurrenceQuery
365
 * @param unknown_type $distributionQuery
366
 * @param unknown_type $legendFormatQuery
367
 * @param unknown_type $map_caption
368
 *
369
* @return String
370
 *    rendered html
371
 */
372
function get_image_map($width, $height= NULL, $bounding_box = FALSE, $occurrenceQuery = FALSE, $distributionQuery = FALSE, $legendFormatQuery = FALSE, $map_caption = FALSE) {
373

    
374
  $map_settings = get_array_variable_merged(CDM_MAP_DISTRIBUTION, CDM_MAP_DISTRIBUTION_DEFAULT);
375

    
376
  $baselayer_name = $map_settings['image_map']['base_layer'];
377
  if(empty($baselayer_name)){
378
    $baselayer_name = "earth";
379
  }
380

    
381
  $query_string = '&img=true&recalculate=false&ms=' . $width . ($height ? ',' . $height : '')
382
  // Additional query parameters as set in the data portal admin section.
383
  . ($bounding_box ? '&bbox=' . $bounding_box : '')
384
  . ($map_settings['show_labels'] ? '&label=' . $map_settings['show_labels'] : '');
385

    
386
  if ($map_caption) {
387
    $query_string .= '&mlp=3&mc_s=Georgia,15,blue&mc=' . $map_caption;
388
  }
389

    
390
  if (get_edit_map_service_version_number() >= 1.1) {
391

    
392
    // Either occurrence or distribution - combined maps will be possible
393
    // in the future.
394
    if ($occurrenceQuery) {
395
      // @todo Fix $occurrenceQuery.
396
      $occurrenceQuery = str_replace("&image=false", "", $occurrenceQuery);
397
      // $occurrenceQuery .= '&l=v%3Aatbi%2Ce_w_0';
398

    
399
      // Will be replaced below.. HACK!!!
400
      $occurrenceQuery .= '&l=' . $baselayer_name . '&as=';
401

    
402
      $query_string .= "&" . $occurrenceQuery;
403
    }
404
    elseif ($distributionQuery) {
405
      $query_string .= '&l=' . $baselayer_name . "&" .$distributionQuery;
406
    }
407

    
408
    // Apply Plain Image map settings special for version >= 1.1.
409
    /*
410
    example : title=a:Naturalized++non-invasive
411
    &ad=cyprusdivs:bdcode:a:5&as=a:ff9900,,0.1,&l=tdwg4
412
    &ms=500&bbox=32,34,35,36&img=true&legend=1&mlp=3
413
    &mc_s=Georgia,15,blue&mc=&recalculate=false
414

    
415
    http://edit.br.fgov.be/edit_wp5/v1/rest_gen.php?
416
    l=background_gis:b,cyprusdivs&ad=cyprusdivs%3Abdcode%3Aa%3A8%2C4
417
    &as=a%3A339966%2C%2C0.1%2C|b:0000ff,,
418
    &bbox=32%2C34%2C35%2C36&img=true&legend=1&mc=&mc_s=Georgia%2C15%2Cblue
419
    &mlp=3&ms=500&recalculate=false&title=a%3Aindigenous
420
    */
421

    
422
    $map_service_script_name = "rest_gen.php";
423

    
424
    $bgcolor_areaStyleId = "Y";
425
    $baselayer_areaStyleId = "Z";
426
    $bgcolor_layer = '';
427
    $additional_area_styles = array();
428

    
429
    // Background color:
430
    if ($map_settings['image_map']['bg_color'] ) {
431
      $bgcolor_layer = "background_gis:" . $bgcolor_areaStyleId;
432
      $additional_area_styles[] = $bgcolor_areaStyleId . ":" . $map_settings['image_map']['bg_color'] . ",,";
433
    }
434

    
435
    // TODO HACK to replace the default base layer which currently is tdwg4 !!!
436
    // only needed for distribution maps.
437
    if (strpos($query_string, "?l=") !== FALSE) {
438
      $layer_param_token = "?l=";
439
    }
440
    else {
441
      $layer_param_token = "&l=";
442
    }
443
    if (strpos($query_string, "?as=") !== FALSE) {
444
      $areystyle_param_token = "?as=";
445
    }
446
    else {
447
      $areystyle_param_token = "&as=";
448
    }
449
    if ($map_settings['image_map']['base_layer']) {
450
      $query_string = str_replace($layer_param_token .$baselayer_name, "$layer_param_token" . $map_settings['image_map']['base_layer'] . ":" . $baselayer_areaStyleId, $query_string);
451
    }
452
    else {
453
      $query_string = str_replace($layer_param_token . $baselayer_name, $layer_param_token . $baselayer_name . ":" . $baselayer_areaStyleId . ",", $query_string);
454
    }
455

    
456
    if ($bgcolor_layer) {
457
      $query_string = str_replace($layer_param_token, $layer_param_token . $bgcolor_layer . ",", $query_string);
458
    }
459

    
460
    if ($map_settings['image_map']['layer_style']) {
461
      $additional_area_styles[] = $baselayer_areaStyleId . ":" . $map_settings['image_map']['layer_style'];
462
    }
463

    
464
    if(isset($map_settings['projection'])){
465
      $query_string .= "&srs=" . $map_settings['projection'];
466
    }
467

    
468
    if(isset($map_settings['legend']['show']) && $map_settings['legend']['show']){
469
      $query_string .= "&legend=1";
470
    }
471

    
472
    foreach ($additional_area_styles as $as) {
473
      $query_string = str_replace($areystyle_param_token, $areystyle_param_token . $as . "|", $query_string);
474
    }
475

    
476
  }
477
  else {
478
    // Pre 1.1. version of map service.
479
    if ($occurrenceQuery) {
480

    
481
      $map_service_script_name = "point.php";
482

    
483
      // Fix $occurrenceQuery.
484
      $occurrenceQuery = str_replace("&image=false", "", $occurrenceQuery);
485
      // $occurrenceQuery .= '&l=v%3Aatbi%2Ce_w_0';
486
      $occurrenceQuery .= '&l=v:e_w_0';
487
      $query_string .= "&" . $occurrenceQuery;
488
    }
489
    elseif ($distributionQuery) {
490
      $query_string .= "&" . $distributionQuery;
491
      $map_service_script_name = "areas.php";
492
    }
493
  }
494

    
495
  $mapUri = url(get_edit_map_service_full_uri() . '/' . $map_service_script_name . '?' .  $query_string);
496
  $out = '<img class="distribution_map" src="' . $mapUri . '" alt="Map" />';
497
  // Showing map caption.
498
  if ($map_caption) {
499
    // FIXME: replace <br> by according css style.
500
    $out .= '<div class="distribution_map_caption">' . $map_caption . '</div>' . '<br />';
501
    $out .= '</div>';
502
  }
503

    
504
  return $out;
505
}
506

    
507

    
(3-3/8)