Translated using Weblate (Portuguese)
[phpmyadmin.git] / gis_data_editor.php
blob4383ccd1f564e1db5bf262935c0898a6a5b603b8
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Editor for Geometry data types.
6 * @package PhpMyAdmin
7 */
8 declare(strict_types=1);
10 use PhpMyAdmin\Core;
11 use PhpMyAdmin\Gis\GisFactory;
12 use PhpMyAdmin\Gis\GisVisualization;
13 use PhpMyAdmin\Response;
14 use PhpMyAdmin\Template;
15 use PhpMyAdmin\Util;
17 if (! defined('ROOT_PATH')) {
18 define('ROOT_PATH', __DIR__ . DIRECTORY_SEPARATOR);
21 require_once ROOT_PATH . 'libraries/common.inc.php';
23 $template = new Template();
25 if (! isset($_POST['field'])) {
26 Util::checkParameters(['field']);
29 // Get data if any posted
30 $gis_data = [];
31 if (Core::isValid($_POST['gis_data'], 'array')) {
32 $gis_data = $_POST['gis_data'];
35 $gis_types = [
36 'POINT',
37 'MULTIPOINT',
38 'LINESTRING',
39 'MULTILINESTRING',
40 'POLYGON',
41 'MULTIPOLYGON',
42 'GEOMETRYCOLLECTION',
45 // Extract type from the initial call and make sure that it's a valid one.
46 // Extract from field's values if available, if not use the column type passed.
47 if (! isset($gis_data['gis_type'])) {
48 if (isset($_POST['type']) && $_POST['type'] != '') {
49 $gis_data['gis_type'] = mb_strtoupper($_POST['type']);
51 if (isset($_POST['value']) && trim($_POST['value']) != '') {
52 $start = (substr($_POST['value'], 0, 1) == "'") ? 1 : 0;
53 $gis_data['gis_type'] = mb_substr(
54 $_POST['value'],
55 $start,
56 mb_strpos($_POST['value'], "(") - $start
59 if (! isset($gis_data['gis_type'])
60 || (! in_array($gis_data['gis_type'], $gis_types))
61 ) {
62 $gis_data['gis_type'] = $gis_types[0];
65 $geom_type = $gis_data['gis_type'];
67 // Generate parameters from value passed.
68 $gis_obj = GisFactory::factory($geom_type);
69 if (isset($_POST['value'])) {
70 $gis_data = array_merge(
71 $gis_data,
72 $gis_obj->generateParams($_POST['value'])
76 // Generate Well Known Text
77 $srid = (isset($gis_data['srid']) && $gis_data['srid'] != '') ? $gis_data['srid'] : 0;
78 $wkt = $gis_obj->generateWkt($gis_data, 0);
79 $wkt_with_zero = $gis_obj->generateWkt($gis_data, 0, '0');
80 $result = "'" . $wkt . "'," . $srid;
82 // Generate SVG based visualization
83 $visualizationSettings = [
84 'width' => 450,
85 'height' => 300,
86 'spatialColumn' => 'wkt',
88 $data = [
90 'wkt' => $wkt_with_zero,
91 'srid' => $srid,
94 $visualization = GisVisualization::getByData($data, $visualizationSettings)
95 ->toImage('svg');
97 $open_layers = GisVisualization::getByData($data, $visualizationSettings)
98 ->asOl();
100 // If the call is to update the WKT and visualization make an AJAX response
101 if (isset($_POST['generate']) && $_POST['generate'] == true) {
102 $extra_data = [
103 'result' => $result,
104 'visualization' => $visualization,
105 'openLayers' => $open_layers,
107 $response = Response::getInstance();
108 $response->addJSON($extra_data);
109 exit;
112 $geom_count = 1;
113 if ($geom_type == 'GEOMETRYCOLLECTION') {
114 $geom_count = isset($gis_data[$geom_type]['geom_count'])
115 ? intval($gis_data[$geom_type]['geom_count']) : 1;
116 if (isset($gis_data[$geom_type]['add_geom'])) {
117 $geom_count++;
121 $templateOutput = $template->render('gis_data_editor_form', [
122 'width' => $visualizationSettings['width'],
123 'height' => $visualizationSettings['height'],
124 'pma_theme_image' => $GLOBALS['pmaThemeImage'],
125 'field' => $_POST['field'],
126 'input_name' => $_POST['input_name'],
127 'srid' => $srid,
128 'visualization' => $visualization,
129 'open_layers' => $open_layers,
130 'gis_types' => $gis_types,
131 'geom_type' => $geom_type,
132 'geom_count' => $geom_count,
133 'gis_data' => $gis_data,
134 'result' => $result,
136 Response::getInstance()->addJSON('gis_editor', $templateOutput);