Project

General

Profile

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