You are here

function kml_format_placemark in KML 6

Same name and namespace in other branches
  1. 5 kml.module \kml_format_placemark()

Format a single KML Placemark (based on format_rss_item()).

Arbitrary elements may be added using the $args associative array.

1 call to kml_format_placemark()
_kml_format_feed in ./kml.module
A generic function for generating KML (Keyhole Markup Language for Google Earth) feeds from a set of nodes.

File

./kml.module, line 836
KML Module

Code

function kml_format_placemark($item, $link, $alt = 0, $args = array()) {
  $title = $item->title;
  $output = "   <Placemark>\n" . '    <name>' . check_plain($title) . "</name>\n" . "    <description><![CDATA[\n" . theme('kml_placemark_description', $item, $link) . "\n]]></description>\n" . '    <styleUrl>#myStyleMap_' . $item->type . '</styleUrl>' . "\n";
  $time_type = variable_get('kml_timetype', 'created');
  if ($time_type != 'none') {
    if ($time_type == 'created') {
      $timestamp = date("Y-m-d\\TH:i:s\\Z", $item->created);
    }
    else {
      if ($time_type == 'changed') {
        $timestamp = date("Y-m-d\\TH:i:s\\Z", $item->changed);
      }
    }
    if ($timestamp) {
      $output .= "    <TimeStamp><when>" . $timestamp . "</when></TimeStamp>\n";
    }
  }

  // Strip any geometry information from args
  $geometrys = array();
  $force_multi = FALSE;

  // Handle Placemark on it's own.
  if ($args['Placemark']) {
    $extra_places = $args['Placemark'];
    unset($args['Placemark']);
  }

  // Handle MultiGeometry on it's own.
  if ($args['MultiGeometry']) {
    $geometrys[] = $args['MultiGeometry'] . "\n";
    unset($args['MultiGeometry']);
    $force_multi = TRUE;
  }
  $geo_types = array(
    'Point',
    'LineString',
    'LinearRing',
    'Polygon',
    'Model',
  );
  foreach ($geo_types as $geo_type) {
    if ($args[$geo_type]) {
      $geometrys[] = "<{$geo_type}>" . $args[$geo_type] . "</{$geo_type}>\n";
      unset($args[$geo_type]);
    }
  }

  // Coordinate information
  $lat = $item->location['latitude'];
  $long = $item->location['longitude'];
  if ($lat && $long) {
    $point = "    <Point>\n";
    $pointelements = array(
      'altitudeMode',
      'extrude',
    );

    // elements that extend Point
    foreach ($pointelements as $pointelement) {
      if ($args[$pointelement]) {
        $point .= "     <{$pointelement}>" . $args[$pointelement] . "</{$pointelement}>\n";
        unset($args[$pointelement]);
      }
    }
    $point .= '     <coordinates>' . $long . ',' . $lat . ',' . $alt . "</coordinates>\n";
    $point .= "    </Point>\n";
    $geometrys[] = $point;
  }
  if ($force_multi or count($geometrys) > 1) {
    $output .= "    <MultiGeometry>\n";
    while ($geometrys) {
      $output .= '      ' . array_shift($geometrys);
    }
    $output .= "    </MultiGeometry>\n";
  }
  else {
    if (count($geometrys)) {
      $output .= array_shift($geometrys);
    }
  }

  // Address information. Needs city and country at a minimum.
  // Includes <address> element as well as <AddressDetails> based on xAL format
  // see http://www.oasis-open.org/committees/ciq/Downloads/xNAL/xAL/Versions/xALv2_0/
  if ($item->location['city'] && $item->location['country']) {
    if ($item->location['name']) {
      $address['name'] = check_plain($item->location['name']);
    }
    if ($item->location['street']) {
      $address['street'] = check_plain($item->location['street']);
    }
    if ($item->location['additional']) {
      $address['additional'] = check_plain($item->location['additional']);
    }
    if ($item->location['city']) {
      $address['city'] = check_plain($item->location['city']);
    }
    if ($item->location['province']) {
      $address['province'] = check_plain($item->location['province']);
    }
    if ($item->location['postal_code']) {
      $address['postal_code'] = check_plain($item->location['postal_code']);
    }
    if ($item->location['country']) {
      if (module_exists('location')) {

        // look up country name if location module is enabled
        $countries = location_get_iso3166_list();
        $address['country'] = $countries[$item->location['country']];
      }
      else {
        $address['country'] = check_plain($item->location['country']);
      }
    }

    // Single line address
    $output .= '    <address>' . implode(', ', $address) . "</address>\n";
    $output .= "    <AddressDetails>\n";
    $output .= "     <Country>\n";
    $output .= "      <AddressLine>" . $address['country'] . "</AddressLine>\n";
    if ($address['province']) {
      $output .= "      <AdministrativeArea>\n";
      $output .= "       <AddressLine>" . $address['province'] . "</AddressLine>\n";
    }
    $output .= "       <Locality>\n";
    $output .= "        <AddressLine>" . $address['city'] . "</AddressLine>\n";
    if ($address['street']) {
      $output .= "        <Thoroughfare>\n";
      $output .= "         <AddressLine>" . $address['street'] . "</AddressLine>\n";
      $output .= "        </Thoroughfare>\n";
    }
    if ($address['postal_code']) {
      $output .= "        <PostalCode>\n";
      $output .= "         <AddressLine>" . $address['postal_code'] . "</AddressLine>\n";
      $output .= "        </PostalCode>\n";
    }
    $output .= "       </Locality>\n";
    if ($address['province']) {
      $output .= "      </AdministrativeArea>\n";
    }
    $output .= "     </Country>\n";
    $output .= "    </AddressDetails>\n";
  }
  if ($args) {

    // TODO: needs better way of structuring this data so we can embed elements and their attributes
    foreach ($args as $key => $value) {
      if (is_array($value)) {
        if ($value['key']) {
          $output .= '    <' . $value['key'];
          if (is_array($value['attributes'])) {
            $output .= drupal_attributes($value['attributes']);
          }
          if ($value['value']) {
            $output .= '>' . $value['value'] . '</' . $value['key'] . ">\n";
          }
          else {
            $output .= " />\n";
          }
        }
      }
      else {
        $output .= '    <' . $key . '>' . check_plain($value) . "</{$key}>\n";
      }
    }
  }
  $output .= "   </Placemark>\n";

  // Add in extra_places if set
  if (isset($extra_places)) {
    if (is_array($extra_places)) {
      foreach ($extra_places as $placemark) {
        $output .= "   <Placemark>\n{$placemark}\n</Placemark>\n";
      }
    }
    else {
      $output .= "   <Placemark>\n{$extra_places}\n</Placemark>\n";
    }
  }
  return $output;
}