Project

General

Profile

Download (13.7 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
 *
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';
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
    $out .= ' distributionQuery="' . $distributionQuery . '&' . $openlayers_map_query_string . '"';
241
  }
242

    
243
  if ($legendFormatQuery) {
244
    $out .= ' legendFormatQuery="' . $legendFormatQuery . '"';
245
  }
246

    
247
  $out .= '></div></div>';
248

    
249
  // Showing map caption.
250
  if ($map_caption) {
251
    // FIXME: replace <br> by according css style.
252
    $out .= '<div class="distribution_map_caption">' . $map_caption . '</div>' . '<br />';
253
    $out .= '</div>';
254
  }
255
  return $out;
256
}
257

    
258

    
259

    
260
/**
261
 * @todo Enter description here ...
262
 *
263
 * @param unknown_type $width
264
 * @param unknown_type $bounding_box
265
 * @param unknown_type $occurrenceQuery
266
 * @param unknown_type $distributionQuery
267
 * @param unknown_type $legendFormatQuery
268
 * @param unknown_type $map_caption
269
 *
270
* @return String
271
 *    rendered html
272
 */
273
function get_image_map($width, $height= NULL, $bounding_box = FALSE, $occurrenceQuery = FALSE, $distributionQuery = FALSE, $legendFormatQuery = FALSE, $map_caption = FALSE) {
274

    
275
  $map_settings = get_array_variable_merged(CDM_MAP_DISTRIBUTION, CDM_MAP_DISTRIBUTION_DEFAULT);
276

    
277
  $query_string = '&image=true&recalculate=false&legend=1&ms=' . $width . ($height ? ',' . $height : '')
278
  // Additional query parameters as set in the data portal admin section.
279
  . ($bounding_box ? '&bbox=' . $bounding_box : '')
280
  . ($map_settings['show_labels'] ? '&label=' . $map_settings['show_labels'] : '');
281

    
282
  if ($map_caption) {
283
    $query_string .= '&mlp=3&mc_s=Georgia,15,blue&mc=' . $map_caption;
284
  }
285

    
286
  if (get_edit_map_service_version_number() >= 1.1) {
287

    
288
    // Either occurrence or distribution - combined maps will be possible
289
    // in the future.
290
    if ($occurrenceQuery) {
291
      // @todo Fix $occurrenceQuery.
292
      $occurrenceQuery = str_replace("&image=false", "", $occurrenceQuery);
293
      // $occurrenceQuery .= '&l=v%3Aatbi%2Ce_w_0';
294
      // Will be replaced below.. HACK!!!
295
      $occurrenceQuery .= '&l=tdwg4&as=';
296

    
297
      $query_string .= "&" . $occurrenceQuery;
298
    }
299
    elseif ($distributionQuery) {
300
      $query_string .= "&" . $distributionQuery;
301
    }
302

    
303
    // Apply Plain Image map settings special for version >= 1.1.
304
    /*
305
    example : title=a:Naturalized++non-invasive
306
    &ad=cyprusdivs:bdcode:a:5&as=a:ff9900,,0.1,&l=tdwg4
307
    &ms=500&bbox=32,34,35,36&img=true&legend=1&mlp=3
308
    &mc_s=Georgia,15,blue&mc=&recalculate=false
309

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

    
317
    $map_service_script_name = "rest_gen.php";
318

    
319
    $bgcolor_areaStyleId = "y";
320
    $baselayer_areaStyleId = "z";
321
    $bgcolor_layer = '';
322
    $additional_area_styles = array();
323

    
324
    // Background color:
325
    if ($map_settings['image_map']['bg_color'] ) {
326
      $bgcolor_layer = "background_gis:" . $bgcolor_areaStyleId;
327
      $additional_area_styles[] = $bgcolor_areaStyleId . ":" . $map_settings['image_map']['bg_color'] . ",,";
328
    }
329

    
330
    // TODO HACK to replace the default base layer which currently is tdwg4 !!!
331
    // only needed for distribution maps.
332
    if (strpos($query_string, "?l=") !== FALSE) {
333
      $layer_param_token = "?l=";
334
    }
335
    else {
336
      $layer_param_token = "&l=";
337
    }
338
    if (strpos($query_string, "?as=") !== FALSE) {
339
      $areystyle_param_token = "?as=";
340
    }
341
    else {
342
      $areystyle_param_token = "&as=";
343
    }
344
    if ($map_settings['image_map']['base_layer']) {
345
      $query_string = str_replace($layer_param_token . "tdwg4", "$layer_param_token" . $map_settings['image_map']['base_layer'] . ":" . $baselayer_areaStyleId, $query_string);
346
    }
347
    else {
348
      $query_string = str_replace($layer_param_token . "tdwg4", $layer_param_token . "tdwg4:" . $baselayer_areaStyleId . ",", $query_string);
349
    }
350

    
351
    if ($bgcolor_layer) {
352
      $query_string = str_replace($layer_param_token, $layer_param_token . $bgcolor_layer . ",", $query_string);
353
    }
354

    
355
    if ($map_settings['image_map']['layer_style']) {
356
      $additional_area_styles[] = $baselayer_areaStyleId . ":" . $map_settings['image_map']['layer_style'];
357
    }
358

    
359
    foreach ($additional_area_styles as $as) {
360
      $query_string = str_replace($areystyle_param_token, $areystyle_param_token . $as . "|", $query_string);
361
    }
362

    
363
  }
364
  else {
365
    // Pre 1.1. version of map service.
366
    if ($occurrenceQuery) {
367

    
368
      $map_service_script_name = "point.php";
369

    
370
      // Fix $occurrenceQuery.
371
      $occurrenceQuery = str_replace("&image=false", "", $occurrenceQuery);
372
      // $occurrenceQuery .= '&l=v%3Aatbi%2Ce_w_0';
373
      $occurrenceQuery .= '&l=v:e_w_0';
374
      $query_string .= "&" . $occurrenceQuery;
375
    }
376
    elseif ($distributionQuery) {
377
      $query_string .= "&" . $distributionQuery;
378
      $map_service_script_name = "areas.php";
379
    }
380
  }
381

    
382
  $mapUri = url(get_edit_map_service_full_uri() . '/' . $map_service_script_name . '?' .  $query_string);
383
  $out = '<img class="distribution_map" src="' . $mapUri . '" alt="Map" />';
384
  // Showing map caption.
385
  if ($map_caption) {
386
    // FIXME: replace <br> by according css style.
387
    $out .= '<div class="distribution_map_caption">' . $map_caption . '</div>' . '<br />';
388
    $out .= '</div>';
389
  }
390

    
391
  return $out;
392
}
393

    
394

    
(3-3/7)