Project

General

Profile

Download (19.1 KB) Statistics
| Branch: | Tag: | Revision:
1 3eb8fbfb Andreas Kohlbecker
<?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 d2fd2a4c Andreas Kohlbecker
/**
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 653e9c6b Andreas Kohlbecker
 * @param $map_id
29 d2fd2a4c Andreas Kohlbecker
 * @param string $occurrence_query
30
 * @param string $distribution_query
31
 * @param string $legend_format_query
32
 * @param array $event_listeners
33
 *   An associative array of with OpenLayers.Map event names as key and corresponding js callbacks.
34
 *   In addition to the event names '#execute' as key is also allowed.
35
 *   Valid events are:
36
 *      - move
37
 *      - moveend
38
 *      - zoomend
39
 *      - changelayer
40
 *      - changebaselayer
41
 *      - #execute:
42
 *            force execution of the given callback after registration of the event handlers
43
 *   see http://dev.openlayers.org/apidocs/files/OpenLayers/Map-js.html#OpenLayers.Map.events for more
44 087bb473 Andreas Kohlbecker
 * @param bool $resizable
45
 *    only possible for openlayers_map
46 6f718d1b Andreas Kohlbecker
 * @param string $force_map_type
47
 *   Can be used to override the map_type setting stored in the settings variable CDM_MAP_DISTRIBUTION
48
 *   - 1: openlayers_map
49
 *   - 0: image_map
50 653e9c6b Andreas Kohlbecker
 * @return array A drupal render array
51
 * A drupal render array
52 d2fd2a4c Andreas Kohlbecker
 * @ingroup compose
53
 */
54 653e9c6b Andreas Kohlbecker
function compose_map($map_id, $occurrence_query = NULL, $distribution_query = NULL, $legend_format_query = NULL, array $event_listeners = array(), $resizable = false, $force_map_type = NULL) {
55 d2fd2a4c Andreas Kohlbecker
56 62cd8253 Patric Plitzner
    $map_settings = get_array_variable_merged(CDM_MAP_DISTRIBUTION, CDM_MAP_DISTRIBUTION_DEFAULT);
57 50791e51 Andreas Kohlbecker
58 6f718d1b Andreas Kohlbecker
    if($force_map_type === NULL){
59
      $force_map_type = $map_settings['map_type'];
60 ff965d76 Andreas Kohlbecker
    }
61 50791e51 Andreas Kohlbecker
62 6f718d1b Andreas Kohlbecker
    if ($force_map_type == 1) {
63 087bb473 Andreas Kohlbecker
      _add_jquery_ui();
64 653e9c6b Andreas Kohlbecker
      $map_html = cdm_map_openlayers(
65
        $map_id,
66 d2fd2a4c Andreas Kohlbecker
        $occurrence_query,
67
        $distribution_query,
68
        $legend_format_query,
69 cc3c9807 Andreas Kohlbecker
        $map_settings['caption'],
70 087bb473 Andreas Kohlbecker
        $event_listeners,
71
        $resizable
72 653e9c6b Andreas Kohlbecker
      );
73 62cd8253 Patric Plitzner
    }
74
     else {
75 50791e51 Andreas Kohlbecker
      $map_height = round($map_settings['image_map']['width'] / (float)$map_settings['aspect_ratio']);
76 653e9c6b Andreas Kohlbecker
      $map_html = cdm_map_plain_image(
77 b4503332 Andreas Kohlbecker
        $map_settings['image_map']['width'],
78 50791e51 Andreas Kohlbecker
        $map_height,
79 d2fd2a4c Andreas Kohlbecker
        $occurrence_query,
80
        $distribution_query,
81
        $legend_format_query,
82 cc3c9807 Andreas Kohlbecker
        $map_settings['caption']
83 50791e51 Andreas Kohlbecker
      );
84 62cd8253 Patric Plitzner
     }
85 50791e51 Andreas Kohlbecker
  return markup_to_render_array($map_html);
86 d2fd2a4c Andreas Kohlbecker
}
87 3eb8fbfb Andreas Kohlbecker
88
/**
89 653e9c6b Andreas Kohlbecker
 * Adds the javascript for a openlayers map to the page as well as all javascript libs.
90
 *
91
 *
92 669f08ee Andreas Kohlbecker
 * @param $map_id
93 2bbc28c8 Andreas Kohlbecker
 * @param $map_settings
94
 *   The map settings array as retrieved by e.g. get_array_variable_merged(CDM_MAP_DISTRIBUTION, CDM_MAP_DISTRIBUTION_DEFAULT);
95 d2fd2a4c Andreas Kohlbecker
 * @param array $event_listeners
96
 *   An associative array of with OpenLayers.Map event names as key and corresponding js callbacks.
97
 *   In addition to the event names '#execute' as key is also allowed.
98
 *   Valid events are:
99
 *      - move
100
 *      - moveend
101
 *      - zoomend
102
 *      - changelayer
103
 *      - changebaselayer
104
 *      - #execute:
105
 *            force execution of the given callback after registration of the event handlers
106
 *   see http://dev.openlayers.org/apidocs/files/OpenLayers/Map-js.html#OpenLayers.Map.events for more
107 087bb473 Andreas Kohlbecker
 * @param bool $resizable
108
 *   The map is made resizable when set to true
109 3eb8fbfb Andreas Kohlbecker
 */
110 669f08ee Andreas Kohlbecker
function _add_js_openlayers_map($map_id, $map_settings, array $event_listeners = array(), $resizable = false) {
111 3eb8fbfb Andreas Kohlbecker
112 5053f04f Andreas Kohlbecker
  font_awesome_icon_markup(); // no icon specified, only used to add the font and styles
113
114 3eb8fbfb Andreas Kohlbecker
  _add_js_openlayers();
115
116 2bbc28c8 Andreas Kohlbecker
  $edit_map_service = get_edit_map_service_settings();
117
118 4c21ffdb Andreas Kohlbecker
  drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . '/js/map/openlayers_map.js',
119
    array(
120
      'type' => 'file',
121
      'group' => JS_DEFAULT, //  module-layer JavaScript.
122
      'weight' => 0,
123
      'cache' => TRUE,
124
      'preprocess' => FALSE
125
  ));
126
  drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . '/js/map/openlayers_layers.js',
127
    array(
128
      'type' => 'file',
129
      'group' => JS_DEFAULT,
130
       'weight' => 1, // after openlayers_map.js
131
      'cache' => TRUE,
132
      'preprocess' => FALSE
133
  ));
134 3eb8fbfb Andreas Kohlbecker
135 2bbc28c8 Andreas Kohlbecker
  $cdm_openlayers_options = array(
136
      'legendPosition'  => '3',
137 d5661a4c Andreas Kohlbecker
      'boundingBox' => $map_settings['bbox'],
138 46de7f4c Andreas Kohlbecker
      'maxZoom' => $map_settings['maxZoom'],
139 b4503332 Andreas Kohlbecker
      'aspectRatio' => $map_settings['aspect_ratio'],
140 d5661a4c Andreas Kohlbecker
      'distributionOpacity' => $map_settings['distribution_opacity'],
141
      'legendOpacity' => $map_settings['legend']['opacity'],
142 2bbc28c8 Andreas Kohlbecker
      'showLayerSwitcher' => $map_settings['openlayers']['show_layer_switcher']  ==  1,
143 10f6aa4b Andreas Kohlbecker
      'displayOutsideMaxExtent' => $map_settings['openlayers']['display_outside_max_extent'] == 1,
144 087bb473 Andreas Kohlbecker
      'resizable' => $resizable
145 10f6aa4b Andreas Kohlbecker
//       'imgPath' => drupal_get_path('module', 'cdm_dataportal') . '/js/map/OpenLayers-2.13.1/img/' // path to the control icons
146 2bbc28c8 Andreas Kohlbecker
      // if no baseLayerNames or defaultBaseLayerName are not defined
147
      // the defaults in cdm_openlayers.js will be used
148
  );
149
150
  // --- setting the base layer options
151
  if (is_array($map_settings['openlayers']['base_layers']) && count($map_settings['openlayers']['base_layers']) > 0) {
152
153 cb961f9d Andreas Kohlbecker
    $base_layer_names = $map_settings['openlayers']['base_layers'];
154 2bbc28c8 Andreas Kohlbecker
155 cb961f9d Andreas Kohlbecker
    foreach($base_layer_names as $name){
156 2b83cc8b Andreas Kohlbecker
      if(str_beginsWith($name, 'g')){
157
        if( isset($map_settings['openlayers']['google_maps_api_key']) && strlen($map_settings['openlayers']['google_maps_api_key']) == 39) {
158
          // google layer detected
159
          drupal_add_js("https://maps.googleapis.com/maps/api/js?key=" . $map_settings['openlayers']['google_maps_api_key'] . "&callback=initMap", 'external');
160
        } else {
161
          drupal_set_message('A Google Maps layer is configured but the API key is either missing or invalid. 
162
          Please set your Google Maps API key in the '  . l('Geo & Map Settings', 'admin/config/cdm_dataportal/settings/geo') .'.', 'warning');
163
        }
164
      }
165
    }
166
167 cb961f9d Andreas Kohlbecker
    // get default layer and remove the 'PREFERRED' from the list to avoid duplicate layers
168
    $preferred_baseLayer = $base_layer_names['PREFERRED'];
169
    unset($base_layer_names['PREFERRED']);
170 a5ab2641 Andreas Kohlbecker
171 cb961f9d Andreas Kohlbecker
    $cdm_openlayers_options['baseLayerNames'] = array_values($base_layer_names);
172
173
    if($preferred_baseLayer){
174
      $cdm_openlayers_options['defaultBaseLayerName'] = $preferred_baseLayer;
175
      if(array_search($preferred_baseLayer, $cdm_openlayers_options['baseLayerNames']) === false){
176 d5f29731 Andreas Kohlbecker
        // the default layer must also  be in the list of base layers
177 cb961f9d Andreas Kohlbecker
        $cdm_openlayers_options['baseLayerNames'][] = $preferred_baseLayer;
178 a5ab2641 Andreas Kohlbecker
      }
179 3eb8fbfb Andreas Kohlbecker
    }
180 2bbc28c8 Andreas Kohlbecker
181 3eb8fbfb Andreas Kohlbecker
  }
182
183 2bbc28c8 Andreas Kohlbecker
  // --- custom wms base layer
184
  $map_settings['openlayers']['custom_wms_base_layer']['params'] = json_decode($map_settings['openlayers']['custom_wms_base_layer']['params']);
185
  $cdm_openlayers_options['customWMSBaseLayerData'] = $map_settings['openlayers']['custom_wms_base_layer'];
186
187 cb961f9d Andreas Kohlbecker
188
  // --- wms_overlay_layer
189 5415eac2 Andreas Kohlbecker
  if(isset($map_settings['openlayers']['wms_overlay_layer']) && isset($map_settings['openlayers']['wms_overlay_layer']['is_enabled']) && $map_settings['openlayers']['wms_overlay_layer']['is_enabled']) {
190 cb961f9d Andreas Kohlbecker
    $map_settings['openlayers']['wms_overlay_layer']['params'] = json_decode($map_settings['openlayers']['wms_overlay_layer']['params']);
191
    $cdm_openlayers_options['wmsOverlayLayerData'] = $map_settings['openlayers']['wms_overlay_layer'];
192
  }
193
194 2bbc28c8 Andreas Kohlbecker
  // --- eventhandlers
195 d2fd2a4c Andreas Kohlbecker
  $event_listeners_js = '';
196
  $execute_handler = '';
197
  foreach($event_listeners as $event=>$js_callback){
198
    if($event == '#execute'){
199
      $execute_handler = 'map_container.each(function(){' . $js_callback . '();});';
200
    } else {
201
      $event_listeners_js .= ($event_listeners_js ? ",\n": "\n") .'"' . $event . '": ' . $js_callback;
202
    }
203
  }
204
205 0b554b40 Andreas Kohlbecker
  $mapserver_base_uri = $edit_map_service['base_uri'];
206
  $is_https = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on';
207
  $http_protocol = $is_https ? 'https' : 'http';
208
  $mapserver_base_uri = preg_replace('/^https?:/', $http_protocol . ':', $mapserver_base_uri);
209 2bbc28c8 Andreas Kohlbecker
210 3eb8fbfb Andreas Kohlbecker
  // window.onload - is executed when the document and images etc is fully loaded
211
  // Query(document).ready - is executed much earlier, when the DOM is loaded
212
  drupal_add_js("
213
          jQuery(document).ready(function() {
214 087bb473 Andreas Kohlbecker
                jQuery(window).load(function () {
215 669f08ee Andreas Kohlbecker
                  var map_container = jQuery('#openlayers-map-" . $map_id . "').cdm_openlayers_map(
216 0b554b40 Andreas Kohlbecker
                   '" . $mapserver_base_uri . "',
217 3eb8fbfb Andreas Kohlbecker
                   '" . $edit_map_service['version'] . "',
218 2bbc28c8 Andreas Kohlbecker
                   " .  json_encode($cdm_openlayers_options) . "
219 3eb8fbfb Andreas Kohlbecker
                );
220 d2fd2a4c Andreas Kohlbecker
                map_container.each(function(){
221 087bb473 Andreas Kohlbecker
                        this.cdmOpenlayersMap.registerEvents({" . "
222
                        " . $event_listeners_js . "
223
                        });
224 d2fd2a4c Andreas Kohlbecker
                });
225
                " . $execute_handler . "
226 087bb473 Andreas Kohlbecker
        });
227 3eb8fbfb Andreas Kohlbecker
      });
228
    ", array('type' => 'inline'));
229
230
}
231
232
233
/**
234 653e9c6b Andreas Kohlbecker
 * Creates markup for an openlayers based dynamic map.
235 3eb8fbfb Andreas Kohlbecker
 *
236 f4675ec1 Andreas Kohlbecker
 * @param string $bounding_box
237
 * @param string $occurrenceQuery
238
 * @param string $distributionQuery
239
 * @param string $legendFormatQuery
240
 * @param string $map_caption
241 d2fd2a4c Andreas Kohlbecker
 * @param array $event_listeners
242
 *   An associative array of with OpenLayers.Map event names as key and corresponding js callbacks.
243
 *   In addition to the event names '#execute' as key is also allowed.
244
 *   Valid events are:
245
 *      - move
246
 *      - moveend
247
 *      - zoomend
248
 *      - changelayer
249
 *      - changebaselayer
250
 *      - #execute:
251
 *            force execution of the given callback after registration of the event handlers
252
 *   see http://dev.openlayers.org/apidocs/files/OpenLayers/Map-js.html#OpenLayers.Map.events for more
253 3eb8fbfb Andreas Kohlbecker
 *
254 d2fd2a4c Andreas Kohlbecker
 * @return String
255 653e9c6b Andreas Kohlbecker
 *    The markup for the map
256 3eb8fbfb Andreas Kohlbecker
 */
257 653e9c6b Andreas Kohlbecker
function cdm_map_openlayers($map_id, $occurrenceQuery = FALSE, $distributionQuery = FALSE,
258 087bb473 Andreas Kohlbecker
                            $legendFormatQuery = FALSE, $map_caption = FALSE, array $event_listeners = array(),
259
                            $resizable = false) {
260 3eb8fbfb Andreas Kohlbecker
261 cc3c9807 Andreas Kohlbecker
  $map_settings = get_array_variable_merged(CDM_MAP_DISTRIBUTION, CDM_MAP_DISTRIBUTION_DEFAULT);
262
263 653e9c6b Andreas Kohlbecker
  if($map_id == NULL){
264 669f08ee Andreas Kohlbecker
    $map_id = sha1($occurrenceQuery . $distributionQuery);
265 653e9c6b Andreas Kohlbecker
  }
266
267 669f08ee Andreas Kohlbecker
  _add_js_openlayers_map($map_id, $map_settings, $event_listeners, $resizable);
268 2bbc28c8 Andreas Kohlbecker
269 669f08ee Andreas Kohlbecker
  $out = '<div id=""openlayers-container-' . $map_id . '" class="openlayers-container openlayers_width ui-widget-content" style="width: 100%;">';
270
  $out .= '<div id="openlayers-map-' . $map_id . '" class="smallmap" style="width:100%; height:100%; margin: 10px;"';
271 3eb8fbfb Andreas Kohlbecker
272
  // Additional query parameters as set in the data portal admin section.
273 cc3c9807 Andreas Kohlbecker
  $labels_on = $map_settings['show_labels'];
274 3eb8fbfb Andreas Kohlbecker
275 b4503332 Andreas Kohlbecker
  // need to set the ms parameter to some value in order to satisfy the
276
  // map service even if this value should not be required:
277
  $width = 512;
278
279 3eb8fbfb Andreas Kohlbecker
  $openlayers_map_query_string = '&img=false&ms=' . $width
280
  . ($labels_on ? '&label=' . $labels_on : '');
281
282
  if ($occurrenceQuery) {
283
    // @todo Fix $occurrenceQuery.
284
    //     $occurrenceQuery .= '&bbox=-180,-90,180,90';
285 8faca2e1 Andreas Kohlbecker
    $occurrenceQuery .= '&l=v%3Aatbi%2Ce_w_0'; // TODO why are we using v:atbi,e_w_0 as layer ???
286 3eb8fbfb Andreas Kohlbecker
    // $occurrenceQuery .= '&l=v:e_w_0';
287
    // TODO add to cdm service?
288
    $occurrenceQuery .= '&legend=0';
289
290
    $out .= ' occurrenceQuery="' . $occurrenceQuery . '&' . $openlayers_map_query_string . '"';
291
  }
292
293
  if ($distributionQuery) {
294 4bef194f Andreas Kohlbecker
    //HACK for testing (this must be done in js)
295
//     $distributionQuery .= "&layer=em_tiny_jan2003&dest_projection_epsg=7777777";
296 3eb8fbfb Andreas Kohlbecker
    $out .= ' distributionQuery="' . $distributionQuery . '&' . $openlayers_map_query_string . '"';
297
  }
298
299
  if ($legendFormatQuery) {
300
    $out .= ' legendFormatQuery="' . $legendFormatQuery . '"';
301
  }
302
303
  $out .= '></div></div>';
304
305
  // Showing map caption.
306
  if ($map_caption) {
307 24b3a0f0 Andreas Kohlbecker
    $out .= '<div class="distribution_map_caption">' . $map_caption . '</div>';
308 3eb8fbfb Andreas Kohlbecker
  }
309
  return $out;
310
}
311
312
313 aa63dfb4 Andreas Kohlbecker
/**
314 653e9c6b Andreas Kohlbecker
 * Composes the render array for a distribution map using the given distribution query string.
315 aa63dfb4 Andreas Kohlbecker
 *
316
 * The distribution map can either be a plain image or a dynamic open layers map
317
 * depending on the settings.
318
 *
319
 * compose_hook() implementation
320
 *
321 653e9c6b Andreas Kohlbecker
 * @param string $query_string
322
 *    An EDIT map services distribution query string
323
 *
324 aa63dfb4 Andreas Kohlbecker
 * @return array
325
 *    A drupal render array
326
 *
327
 * Similar compose function compose_map()
328
 *
329
 * @ingroup compose
330
 */
331 653e9c6b Andreas Kohlbecker
function compose_distribution_map($query_string) {
332 aa63dfb4 Andreas Kohlbecker
333
  $fontStyles = array(
334
      0 => "plane",
335
      1 => "italic",
336
  );
337
338
  if (!$query_string) {
339
    // The $query_string is empty if there are no distribution areas defined.
340 0b554b40 Andreas Kohlbecker
    return null;
341 aa63dfb4 Andreas Kohlbecker
  }
342
343
  /* ------ choose the display mode, either openlayers or static image ------ */
344
345
  $map_settings = get_array_variable_merged(CDM_MAP_DISTRIBUTION, CDM_MAP_DISTRIBUTION_DEFAULT);
346
347
  if ($map_settings['map_type'] == 1) {
348
349
    /* =========== display distributions using the openlayers map viewer =========== */
350
351
    $legendFormatQueryStr = "format=image" . urlencode('/') . "png"
352 44d445c0 Andreas Kohlbecker
      . "&TRANSPARENT=TRUE"
353
      . "&WIDTH=" . $map_settings['legend']['icon_width']
354
      . "&HEIGHT=" . $map_settings['legend']['icon_height']
355
      // TODO why is the layer=topp:tdwg_level_4 parameter needed at all here??
356
      // AK: i think the tdwg_level_4 is used as place holder and will be replaced later on
357
      // => search for "tdwg_level_4" in the code
358
      . "&layer=topp" . urlencode(':') . "tdwg_level_4"
359
      . "&LEGEND_OPTIONS=forceLabels" . urlencode(':') . "on"
360
      . ";fontStyle" . urlencode(':') . $fontStyles[$map_settings['legend']['font_style']]
361
      . ";fontSize" . urlencode(':') .  $map_settings['legend']['font_size']
362
      . "&SLD=";
363 aa63dfb4 Andreas Kohlbecker
364 653e9c6b Andreas Kohlbecker
    /*$out .= cdm_map_openlayers(
365 aa63dfb4 Andreas Kohlbecker
        $map_settings['bbox'],
366
        NULL,
367
        $query_string,
368
        $legendFormatQueryStr,
369
        $map_settings['caption']
370
    );
371 50791e51 Andreas Kohlbecker
    */
372 aa63dfb4 Andreas Kohlbecker
  }
373
  else {
374
    $legendFormatQueryStr = '';
375 50791e51 Andreas Kohlbecker
    /*
376 653e9c6b Andreas Kohlbecker
        cdm_map_plain_image(
377 50791e51 Andreas Kohlbecker
            $map_settings['image_map']['width'],
378
            $map_settings['image_map']['height'],
379
            $map_settings['bbox'],
380
            NULL,
381
            $query_string,
382
            $legendFormatQueryStr,
383
            $map_settings['caption']
384
        );
385
    */
386 aa63dfb4 Andreas Kohlbecker
  }
387 4c21ffdb Andreas Kohlbecker
  $out = compose_map('distribution', NULL, $query_string, $legendFormatQueryStr);
388 50791e51 Andreas Kohlbecker
389
  return $out;
390 aa63dfb4 Andreas Kohlbecker
}
391
392
393
394 3eb8fbfb Andreas Kohlbecker
395
/**
396 653e9c6b Andreas Kohlbecker
 * Composes the markup for a plain image map.
397 3eb8fbfb Andreas Kohlbecker
 *
398 653e9c6b Andreas Kohlbecker
 * @param int $width
399
 * @param string $occurrenceQuery
400
 * @param string $distributionQuery
401
 * @param string $legendFormatQuery
402
 * @param string $map_caption
403 3eb8fbfb Andreas Kohlbecker
 *
404 d2fd2a4c Andreas Kohlbecker
* @return String
405
 *    rendered html
406 3eb8fbfb Andreas Kohlbecker
 */
407 653e9c6b Andreas Kohlbecker
function cdm_map_plain_image($width, $height= NULL, $occurrenceQuery = FALSE, $distributionQuery = FALSE,
408
                             $legendFormatQuery = FALSE, $map_caption = FALSE) {
409 3eb8fbfb Andreas Kohlbecker
410 cc3c9807 Andreas Kohlbecker
  $map_settings = get_array_variable_merged(CDM_MAP_DISTRIBUTION, CDM_MAP_DISTRIBUTION_DEFAULT);
411 3eb8fbfb Andreas Kohlbecker
412 4bef194f Andreas Kohlbecker
  $baselayer_name = $map_settings['image_map']['base_layer'];
413
  if(empty($baselayer_name)){
414
    $baselayer_name = "earth";
415
  }
416
417 96835579 Andreas Kohlbecker
  $query_string = '&img=true&recalculate=false&ms=' . $width . ($height ? ',' . $height : '')
418 cc3c9807 Andreas Kohlbecker
  // Additional query parameters as set in the data portal admin section.
419 753d936d Andreas Kohlbecker
  . ($map_settings['bbox'] ? '&bbox=' . $map_settings['bbox'] : '')
420 cc3c9807 Andreas Kohlbecker
  . ($map_settings['show_labels'] ? '&label=' . $map_settings['show_labels'] : '');
421 3eb8fbfb Andreas Kohlbecker
422
  if ($map_caption) {
423
    $query_string .= '&mlp=3&mc_s=Georgia,15,blue&mc=' . $map_caption;
424
  }
425
426
  if (get_edit_map_service_version_number() >= 1.1) {
427
428
    // Either occurrence or distribution - combined maps will be possible
429
    // in the future.
430
    if ($occurrenceQuery) {
431
      // @todo Fix $occurrenceQuery.
432
      $occurrenceQuery = str_replace("&image=false", "", $occurrenceQuery);
433
      // $occurrenceQuery .= '&l=v%3Aatbi%2Ce_w_0';
434 4bef194f Andreas Kohlbecker
435 3eb8fbfb Andreas Kohlbecker
      // Will be replaced below.. HACK!!!
436 4bef194f Andreas Kohlbecker
      $occurrenceQuery .= '&l=' . $baselayer_name . '&as=';
437 3eb8fbfb Andreas Kohlbecker
438
      $query_string .= "&" . $occurrenceQuery;
439
    }
440
    elseif ($distributionQuery) {
441 4bef194f Andreas Kohlbecker
      $query_string .= '&l=' . $baselayer_name . "&" .$distributionQuery;
442 3eb8fbfb Andreas Kohlbecker
    }
443
444
    // Apply Plain Image map settings special for version >= 1.1.
445
    /*
446
    example : title=a:Naturalized++non-invasive
447
    &ad=cyprusdivs:bdcode:a:5&as=a:ff9900,,0.1,&l=tdwg4
448
    &ms=500&bbox=32,34,35,36&img=true&legend=1&mlp=3
449
    &mc_s=Georgia,15,blue&mc=&recalculate=false
450
451
    http://edit.br.fgov.be/edit_wp5/v1/rest_gen.php?
452
    l=background_gis:b,cyprusdivs&ad=cyprusdivs%3Abdcode%3Aa%3A8%2C4
453
    &as=a%3A339966%2C%2C0.1%2C|b:0000ff,,
454
    &bbox=32%2C34%2C35%2C36&img=true&legend=1&mc=&mc_s=Georgia%2C15%2Cblue
455
    &mlp=3&ms=500&recalculate=false&title=a%3Aindigenous
456
    */
457
458
    $map_service_script_name = "rest_gen.php";
459
460 4bef194f Andreas Kohlbecker
    $bgcolor_areaStyleId = "Y";
461
    $baselayer_areaStyleId = "Z";
462 3eb8fbfb Andreas Kohlbecker
    $bgcolor_layer = '';
463
    $additional_area_styles = array();
464
465
    // Background color:
466 cc3c9807 Andreas Kohlbecker
    if ($map_settings['image_map']['bg_color'] ) {
467 3eb8fbfb Andreas Kohlbecker
      $bgcolor_layer = "background_gis:" . $bgcolor_areaStyleId;
468 cc3c9807 Andreas Kohlbecker
      $additional_area_styles[] = $bgcolor_areaStyleId . ":" . $map_settings['image_map']['bg_color'] . ",,";
469 3eb8fbfb Andreas Kohlbecker
    }
470
471
    // TODO HACK to replace the default base layer which currently is tdwg4 !!!
472
    // only needed for distribution maps.
473
    if (strpos($query_string, "?l=") !== FALSE) {
474
      $layer_param_token = "?l=";
475
    }
476
    else {
477
      $layer_param_token = "&l=";
478
    }
479
    if (strpos($query_string, "?as=") !== FALSE) {
480
      $areystyle_param_token = "?as=";
481
    }
482
    else {
483
      $areystyle_param_token = "&as=";
484
    }
485 cc3c9807 Andreas Kohlbecker
    if ($map_settings['image_map']['base_layer']) {
486 4bef194f Andreas Kohlbecker
      $query_string = str_replace($layer_param_token .$baselayer_name, "$layer_param_token" . $map_settings['image_map']['base_layer'] . ":" . $baselayer_areaStyleId, $query_string);
487 3eb8fbfb Andreas Kohlbecker
    }
488
    else {
489 4bef194f Andreas Kohlbecker
      $query_string = str_replace($layer_param_token . $baselayer_name, $layer_param_token . $baselayer_name . ":" . $baselayer_areaStyleId . ",", $query_string);
490 3eb8fbfb Andreas Kohlbecker
    }
491
492
    if ($bgcolor_layer) {
493
      $query_string = str_replace($layer_param_token, $layer_param_token . $bgcolor_layer . ",", $query_string);
494
    }
495
496 cc3c9807 Andreas Kohlbecker
    if ($map_settings['image_map']['layer_style']) {
497
      $additional_area_styles[] = $baselayer_areaStyleId . ":" . $map_settings['image_map']['layer_style'];
498 3eb8fbfb Andreas Kohlbecker
    }
499
500 4bef194f Andreas Kohlbecker
    if(isset($map_settings['projection'])){
501
      $query_string .= "&srs=" . $map_settings['projection'];
502
    }
503
504
    if(isset($map_settings['legend']['show']) && $map_settings['legend']['show']){
505
      $query_string .= "&legend=1";
506
    }
507
508 3eb8fbfb Andreas Kohlbecker
    foreach ($additional_area_styles as $as) {
509
      $query_string = str_replace($areystyle_param_token, $areystyle_param_token . $as . "|", $query_string);
510
    }
511
512
  }
513
  else {
514
    // Pre 1.1. version of map service.
515
    if ($occurrenceQuery) {
516
517
      $map_service_script_name = "point.php";
518
519
      // Fix $occurrenceQuery.
520
      $occurrenceQuery = str_replace("&image=false", "", $occurrenceQuery);
521
      // $occurrenceQuery .= '&l=v%3Aatbi%2Ce_w_0';
522
      $occurrenceQuery .= '&l=v:e_w_0';
523
      $query_string .= "&" . $occurrenceQuery;
524
    }
525
    elseif ($distributionQuery) {
526
      $query_string .= "&" . $distributionQuery;
527
      $map_service_script_name = "areas.php";
528
    }
529
  }
530
531
  $mapUri = url(get_edit_map_service_full_uri() . '/' . $map_service_script_name . '?' .  $query_string);
532
  $out = '<img class="distribution_map" src="' . $mapUri . '" alt="Map" />';
533
  // Showing map caption.
534
  if ($map_caption) {
535 24b3a0f0 Andreas Kohlbecker
    $out .= '<div class="distribution_map_caption">' . $map_caption . '</div>';
536 3eb8fbfb Andreas Kohlbecker
  }
537
538
  return $out;
539
}
540