mirror of
https://github.com/farmOS/farmOS.git
synced 2024-02-23 11:37:38 +01:00
Preview polygons in the map before generating them.
This commit is contained in:
parent
b7016ddc76
commit
7dd1fb95e4
|
@ -44,6 +44,13 @@ function farm_area_generate_menu() {
|
|||
'type' => MENU_LOCAL_TASK,
|
||||
);
|
||||
|
||||
// Area generator callback.
|
||||
$items['farm/areas/generate/callback'] = array(
|
||||
'page callback' => 'farm_area_generate_callback',
|
||||
'access arguments' => array('use farm area generator'),
|
||||
'type' => MENU_CALLBACK,
|
||||
);
|
||||
|
||||
return $items;
|
||||
}
|
||||
|
||||
|
@ -66,6 +73,15 @@ function farm_area_generate_form($form, &$form_state) {
|
|||
// Add javascript.
|
||||
drupal_add_js(drupal_get_path('module', 'farm_area_generate') . '/js/farm_area_generate.js');
|
||||
|
||||
// Hidden field for generated area WKT. If the areas were generated via the
|
||||
// "Preview" button, WKT will be stored in $form_state['storage']['wkt'].
|
||||
$form['wkt'] = array(
|
||||
'#type' => 'hidden',
|
||||
'#value' => !empty($form_state['storage']['wkt']) ? $form_state['storage']['wkt'] : '',
|
||||
'#prefix' => '<div id="generated-wkt">',
|
||||
'#suffix' => '</div>',
|
||||
);
|
||||
|
||||
// Area select.
|
||||
$form['area'] = array(
|
||||
'#type' => 'textfield',
|
||||
|
@ -108,6 +124,14 @@ function farm_area_generate_form($form, &$form_state) {
|
|||
drupal_set_message('This area generator tool requires the GEOS libary: ' . l('http://trac.osgeo.org/geos', 'http://trac.osgeo.org/geos'), 'warning');
|
||||
}
|
||||
else {
|
||||
$form['actions']['preview'] = array(
|
||||
'#type' => 'submit',
|
||||
'#value' => t('Preview'),
|
||||
'#submit' => array('farm_area_generate_form_preview'),
|
||||
'#ajax' => array(
|
||||
'callback' => 'farm_area_generate_form_ajax',
|
||||
),
|
||||
);
|
||||
$form['actions']['submit'] = array(
|
||||
'#type' => 'submit',
|
||||
'#value' => t('Generate'),
|
||||
|
@ -124,6 +148,31 @@ function farm_area_generate_form($form, &$form_state) {
|
|||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ajax callback for farm_area_generate_form().
|
||||
*/
|
||||
function farm_area_generate_form_ajax($form, $form_state) {
|
||||
|
||||
// Get the "wkt" form element and CSS selector.
|
||||
$element = $form['wkt'];
|
||||
$selector = '#generated-wkt';
|
||||
|
||||
// Assemble commands...
|
||||
$commands = array();
|
||||
|
||||
// Replace the hidden field.
|
||||
$commands[] = ajax_command_replace($selector, render($element));
|
||||
|
||||
// Execute Javascript to add WKT to the map.
|
||||
$commands[] = array('command' => 'farmAreaGeneratePreview');
|
||||
|
||||
// Display status messages.
|
||||
$commands[] = ajax_command_prepend($selector, theme('status_messages'));
|
||||
|
||||
// Return ajax commands.
|
||||
return array('#type' => 'ajax', '#commands' => $commands);
|
||||
}
|
||||
|
||||
/**
|
||||
* Area generator form validate.
|
||||
*/
|
||||
|
@ -187,6 +236,34 @@ function farm_area_generate_form_validate(&$form, &$form_state) {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Farm area generator form preview.
|
||||
*/
|
||||
function farm_area_generate_form_preview(&$form, &$form_state) {
|
||||
|
||||
// If the area and polygon was not stored in validation, bail.
|
||||
if (empty($form_state['storage']['area']) || empty($form_state['storage']['polygon'])) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Get all the necessary variables from the form state.
|
||||
$polygon = $form_state['storage']['polygon'];
|
||||
$count = $form_state['values']['count'];
|
||||
$orientation = $form_state['values']['orientation'];
|
||||
|
||||
// Generate child area geometries.
|
||||
$geometries = farm_area_generate_geometries($polygon, $count, $orientation);
|
||||
if (!empty($geometries)) {
|
||||
$collection = new GeometryCollection($geometries);
|
||||
$form_state['storage']['wkt'] = $collection->asText();
|
||||
}
|
||||
|
||||
// Rebuild the form if WKT was generated.
|
||||
if (!empty($form_state['storage']['wkt'])) {
|
||||
$form_state['rebuild'] = TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Farm area generate form submit.
|
||||
*/
|
||||
|
|
|
@ -2,6 +2,9 @@
|
|||
Drupal.behaviors.farm_area_generate = {
|
||||
attach: function (context, settings) {
|
||||
|
||||
// Initialize a setting for storing the temporary preview layer.
|
||||
Drupal.settings.farm_area_generate_preview_layer = Drupal.settings.farm_area_generate_preview_layer || {};
|
||||
|
||||
// When an Openlayers popup is displayed, copy the area to the area
|
||||
// generator form field.
|
||||
$(document).on('openlayers.Component:Popup', function (event, options) {
|
||||
|
@ -12,4 +15,44 @@
|
|||
});
|
||||
}
|
||||
};
|
||||
|
||||
Drupal.farm_area_generate = {
|
||||
|
||||
// Function for adding WKT as a layer to the map.
|
||||
preview: function (wkt) {
|
||||
|
||||
// Load the Openlayers map.
|
||||
var map_id = $('.openlayers-map').attr('id');
|
||||
var map = Drupal.openlayers.getMapById(map_id);
|
||||
|
||||
// If a bed layer was already added, remove it.
|
||||
if (Drupal.settings.farm_area_generate_preview_layer) {
|
||||
map.map.removeLayer(Drupal.settings.farm_area_generate_preview_layer);
|
||||
}
|
||||
|
||||
// Generate a new layer from WKT.
|
||||
var format = new ol.format.WKT();
|
||||
var feature = format.readFeature(wkt, {
|
||||
dataProjection: 'EPSG:4326',
|
||||
featureProjection: 'EPSG:3857'
|
||||
});
|
||||
Drupal.settings.farm_area_generate_preview_layer = new ol.layer.Vector({
|
||||
source: new ol.source.Vector({
|
||||
features: [feature]
|
||||
})
|
||||
});
|
||||
|
||||
// Add layer to the map.
|
||||
map.map.addLayer(Drupal.settings.farm_area_generate_preview_layer);
|
||||
}
|
||||
};
|
||||
|
||||
// Define a Drupal ajax command for loading the wkt from the hidden input
|
||||
// field and previewing it on the map.
|
||||
Drupal.ajax.prototype.commands.farmAreaGeneratePreview = function() {
|
||||
var wkt = $('#generated-wkt input[name="wkt"]').val();
|
||||
if (wkt) {
|
||||
Drupal.farm_area_generate.preview(wkt);
|
||||
}
|
||||
}
|
||||
}(jQuery));
|
||||
|
|
Loading…
Reference in a new issue