You are here

function farm_map_kml_parse_geometries in farmOS 7

Helper function for converting KML to a set of geometries.

Parameters

string $kml: The KML string to parse.

Return value

array Returns an array of geometries, with name, description, and WKT for each.

1 call to farm_map_kml_parse_geometries()
farm_area_import_form in modules/farm/farm_area/farm_area_import/farm_area_import.module
Area import form.

File

modules/farm/farm_map/farm_map_kml/farm_map_kml.module, line 143
Farm map KML.

Code

function farm_map_kml_parse_geometries($kml) {

  // Start an array to hold the geometries.
  $geometries = array();

  // Load the GeoPHP library.
  geophp_load();

  // Parse the KML into an XML object.
  $xml = simplexml_load_string($kml);

  // Determine the root element. Sometimes it is "Document".
  $root = $xml;
  if (isset($xml->Document)) {
    $root = $xml->Document;
  }

  // If the KML file is organized into folders, iterate through them.
  if (isset($root->Folder)) {
    $folders = $folders = $root->Folder;
    foreach ($folders as $folder) {

      // Iterate through the KML Placemarks and parse their geometry info.
      $placemarks = $folder->Placemark;
      foreach ($placemarks as $placemark) {
        $geometries[] = farm_map_kml_parse_placemark($placemark);
      }
    }
  }
  else {
    $placemarks = $root->Placemark;
    foreach ($placemarks as $placemark) {
      $geometries[] = farm_map_kml_parse_placemark($placemark);
    }
  }

  // Return the geometries.
  return $geometries;
}