Initial commit of new farm_map_kml module - provides a KML export action for farmOS geometry fields.

This commit is contained in:
Michael Stenta 2018-05-30 12:41:21 -04:00
parent f43c0699a1
commit f2d5c7c336
5 changed files with 190 additions and 0 deletions

View File

@ -0,0 +1,6 @@
name = Farm Map KML
description = Provides KML features for farm maps.
core = 7.x
package = farmOS
dependencies[] = farm_map
dependencies[] = geophp

View File

@ -0,0 +1,123 @@
<?php
/**
* @file
* Farm map KML.
*/
/**
* Implements hook_theme().
*/
function farm_map_kml_theme($existing, $type, $theme, $path) {
return array(
'farm_map_kml' => array(
'variables' => array(
'content' => NULL,
'placemarks' => array(),
),
'template' => 'farm_map_kml',
'file' => 'farm_map_kml.theme.inc',
),
'farm_map_kml_placemark' => array(
'variables' => array(
'pid' => NULL,
'name' => NULL,
'description' => NULL,
'geometry' => NULL,
'kml' => NULL,
),
'template' => 'farm_map_kml_placemark',
'file' => 'farm_map_kml.theme.inc',
),
);
}
/**
* Implements hook_action_info().
*/
function farm_map_kml_action_info() {
return array(
'farm_map_kml_action' => array(
'type' => 'entity',
'label' => t('KML'),
'triggers' => array('any'),
'configurable' => FALSE,
'aggregate' => TRUE,
),
);
}
/**
* Action function for farm_map_kml_action.
*
* Creates a KML file containing shapes from selected entities.
*
* @param array $entities
* An array of entities.
* @param array $context
* Array with parameters for this action.
*/
function farm_map_kml_action(array $entities, $context = array()) {
// Iterate through the entities to generate placemarks.
$placemarks = array();
foreach ($entities as $entity) {
// Get the geometry. If it is empty, skip the entity.
if (empty($entity->field_farm_geofield[LANGUAGE_NONE][0]['geom'])) {
continue;
}
$geometry = $entity->field_farm_geofield[LANGUAGE_NONE][0]['geom'];
// Get the entity id and label.
list($id, $rid, $bundle) = entity_extract_ids($context['entity_type'], $entity);
$label = entity_label($context['entity_type'], $entity);
// Create a placemark.
$placemark = array(
'pid' => $id,
'name' => $label,
'geometry' => $geometry,
);
// If this is an area entity (taxonomy_term), add the description.
if ($context['entity_type'] == 'taxonomy_term' && $entity->vocabulary_machine_name == 'farm_areas') {
if (!empty($entity->description)) {
$placemark['description'] = $entity->description;
}
}
// Add the placemark to the list.
$placemarks[] = $placemark;
}
// If there are no placemarks, bail with a warning.
if (empty($placemarks)) {
drupal_set_message(t('No placemarks were found.'), 'warning');
return;
}
// Create KML output.
$kml = theme('farm_map_kml', array('placemarks' => $placemarks));
// Ensure that a directory exists to store the KML file in.
$directory = 'public://kml';
file_prepare_directory($directory, FILE_CREATE_DIRECTORY);
// Create the temporary KML file.
$filename = 'kml_export-' . date('c') . '.kml';
$destination = $directory . '/' . $filename;
$file = file_save_data($kml, $destination);
// Make the file temporary.
$file->status = 0;
file_save($file);
// Show a link to the file.
$message = 'KML file created: <a href="!path">%filename</a>';
$args = array(
'!path' => file_create_url($file->uri),
'%filename' => $file->filename,
);
drupal_set_message(t($message, $args));
}

View File

@ -0,0 +1,49 @@
<?php
/**
* @file
* Farm map theme functions.
*/
/**
* Template preprocessor for farm map KML output.
*/
function template_preprocess_farm_map_kml(&$vars) {
// If content is not set, initialize it as an empty string.
if (!isset($vars['content'])) {
$vars['content'] = '';
}
// Add placemarks to the content.
foreach ($vars['placemarks'] as $placemark) {
$vars['content'] .= theme('farm_map_kml_placemark', $placemark);
}
}
/**
* Template preprocessor for farm map KML placemark output.
*/
function template_preprocess_farm_map_kml_placemark(&$vars) {
// If KML is not set, initialize it as an empty string.
if (!isset($vars['kml'])) {
$vars['kml'] = '';
}
// If there is a geometry, convert it to KML and append it.
if (!empty($vars['geometry'])) {
// Load GeoPHP.
geophp_load();
// Create a geometry object.
$geometry = geoPHP::load($vars['geometry'], 'wkt');
// Convert the geometry to KML.
$kml = $geometry->out('kml');
// Append the KML to the output.
$vars['kml'] .= $kml . "\n";
}
}

View File

@ -0,0 +1,7 @@
<?php print '<?xml version="1.0" encoding="UTF-8"?>'; ?>
<kml xmlns = "http://earth.google.com/kml/2.1">
<Document>
<?php if (!empty($content)) print $content; ?>
</Document>
</kml>

View File

@ -0,0 +1,5 @@
<Placemark id="<?php print $pid; ?>">
<name><?php print $name; ?></name>
<?php if (!empty($description)): ?><description><?php print $description; ?></description><?php endif; ?>
<?php if (!empty($kml)) print $kml; ?>
</Placemark>