1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
|
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Utils;
use function array_map;
use function bin2hex;
use function mb_strtolower;
use function preg_match;
use function trim;
final class Gis
{
/**
* Converts GIS data to Well Known Text format
*
* @param string $data GIS data
* @param bool $includeSRID Add SRID to the WKT
*
* @return string GIS data in Well Know Text format
*/
public static function convertToWellKnownText($data, $includeSRID = false): string
{
global $dbi;
// Convert to WKT format
$hex = bin2hex($data);
$spatialAsText = 'ASTEXT';
$spatialSrid = 'SRID';
$axisOrder = '';
$mysqlVersionInt = $dbi->getVersion();
if ($mysqlVersionInt >= 50600) {
$spatialAsText = 'ST_ASTEXT';
$spatialSrid = 'ST_SRID';
}
if ($mysqlVersionInt >= 80001 && ! $dbi->isMariaDb()) {
$axisOrder = ', \'axis-order=long-lat\'';
}
$wktsql = 'SELECT ' . $spatialAsText . "(x'" . $hex . "'" . $axisOrder . ')';
if ($includeSRID) {
$wktsql .= ', ' . $spatialSrid . "(x'" . $hex . "')";
}
$wktresult = $dbi->tryQuery($wktsql);
$wktarr = [];
if ($wktresult) {
$wktarr = $wktresult->fetchRow();
}
$wktval = $wktarr[0] ?? '';
if ($includeSRID) {
$srid = $wktarr[1] ?? null;
$wktval = "'" . $wktval . "'," . $srid;
}
return $wktval;
}
/**
* Return GIS data types
*
* @param bool $upperCase whether to return values in upper case
*
* @return string[] GIS data types
*/
public static function getDataTypes($upperCase = false): array
{
$gisDataTypes = [
'geometry',
'point',
'linestring',
'polygon',
'multipoint',
'multilinestring',
'multipolygon',
'geometrycollection',
];
if ($upperCase) {
$gisDataTypes = array_map('mb_strtoupper', $gisDataTypes);
}
return $gisDataTypes;
}
/**
* Generates GIS data based on the string passed.
*
* @param string $gisString GIS string
* @param int $mysqlVersion The mysql version as int
*
* @return string GIS data enclosed in 'ST_GeomFromText' or 'GeomFromText' function
*/
public static function createData($gisString, $mysqlVersion)
{
$geomFromText = $mysqlVersion >= 50600 ? 'ST_GeomFromText' : 'GeomFromText';
$gisString = trim($gisString);
$geomTypes = '(POINT|MULTIPOINT|LINESTRING|MULTILINESTRING|POLYGON|MULTIPOLYGON|GEOMETRYCOLLECTION)';
if (preg_match("/^'" . $geomTypes . "\(.*\)',[0-9]*$/i", $gisString)) {
return $geomFromText . '(' . $gisString . ')';
}
if (preg_match('/^' . $geomTypes . '\(.*\)$/i', $gisString)) {
return $geomFromText . "('" . $gisString . "')";
}
return $gisString;
}
/**
* Returns the names and details of the functions
* that can be applied on geometry data types.
*
* @param string $geomType if provided the output is limited to the functions
* that are applicable to the provided geometry type.
* @param bool $binary if set to false functions that take two geometries
* as arguments will not be included.
* @param bool $display if set to true separators will be added to the
* output array.
*
* @return array<int|string,array<string,int|string>> names and details of the functions that can be applied on
* geometry data types.
*/
public static function getFunctions(
$geomType = null,
$binary = true,
$display = false
): array {
global $dbi;
$funcs = [];
if ($display) {
$funcs[] = ['display' => ' '];
}
// Unary functions common to all geometry types
$funcs['Dimension'] = [
'params' => 1,
'type' => 'int',
];
$funcs['Envelope'] = [
'params' => 1,
'type' => 'Polygon',
];
$funcs['GeometryType'] = [
'params' => 1,
'type' => 'text',
];
$funcs['SRID'] = [
'params' => 1,
'type' => 'int',
];
$funcs['IsEmpty'] = [
'params' => 1,
'type' => 'int',
];
$funcs['IsSimple'] = [
'params' => 1,
'type' => 'int',
];
$geomType = mb_strtolower(trim((string) $geomType));
if ($display && $geomType !== 'geometry' && $geomType !== 'multipoint') {
$funcs[] = ['display' => '--------'];
}
$spatialPrefix = '';
if ($dbi->getVersion() >= 50601) {
// If MySQL version is greater than or equal 5.6.1,
// use the ST_ prefix.
$spatialPrefix = 'ST_';
}
// Unary functions that are specific to each geometry type
if ($geomType === 'point') {
$funcs[$spatialPrefix . 'X'] = [
'params' => 1,
'type' => 'float',
];
$funcs[$spatialPrefix . 'Y'] = [
'params' => 1,
'type' => 'float',
];
} elseif ($geomType === 'linestring') {
$funcs['EndPoint'] = [
'params' => 1,
'type' => 'point',
];
$funcs['GLength'] = [
'params' => 1,
'type' => 'float',
];
$funcs['NumPoints'] = [
'params' => 1,
'type' => 'int',
];
$funcs['StartPoint'] = [
'params' => 1,
'type' => 'point',
];
$funcs['IsRing'] = [
'params' => 1,
'type' => 'int',
];
} elseif ($geomType === 'multilinestring') {
$funcs['GLength'] = [
'params' => 1,
'type' => 'float',
];
$funcs['IsClosed'] = [
'params' => 1,
'type' => 'int',
];
} elseif ($geomType === 'polygon') {
$funcs['Area'] = [
'params' => 1,
'type' => 'float',
];
$funcs['ExteriorRing'] = [
'params' => 1,
'type' => 'linestring',
];
$funcs['NumInteriorRings'] = [
'params' => 1,
'type' => 'int',
];
} elseif ($geomType === 'multipolygon') {
$funcs['Area'] = [
'params' => 1,
'type' => 'float',
];
$funcs['Centroid'] = [
'params' => 1,
'type' => 'point',
];
// Not yet implemented in MySQL
//$funcs['PointOnSurface'] = array('params' => 1, 'type' => 'point');
} elseif ($geomType === 'geometrycollection') {
$funcs['NumGeometries'] = [
'params' => 1,
'type' => 'int',
];
}
// If we are asked for binary functions as well
if ($binary) {
// section separator
if ($display) {
$funcs[] = ['display' => '--------'];
}
$funcs[$spatialPrefix . 'Crosses'] = [
'params' => 2,
'type' => 'int',
];
$funcs[$spatialPrefix . 'Contains'] = [
'params' => 2,
'type' => 'int',
];
$funcs[$spatialPrefix . 'Disjoint'] = [
'params' => 2,
'type' => 'int',
];
$funcs[$spatialPrefix . 'Equals'] = [
'params' => 2,
'type' => 'int',
];
$funcs[$spatialPrefix . 'Intersects'] = [
'params' => 2,
'type' => 'int',
];
$funcs[$spatialPrefix . 'Overlaps'] = [
'params' => 2,
'type' => 'int',
];
$funcs[$spatialPrefix . 'Touches'] = [
'params' => 2,
'type' => 'int',
];
$funcs[$spatialPrefix . 'Within'] = [
'params' => 2,
'type' => 'int',
];
if ($display) {
$funcs[] = ['display' => '--------'];
}
// Minimum bounding rectangle functions
$funcs['MBRContains'] = [
'params' => 2,
'type' => 'int',
];
$funcs['MBRDisjoint'] = [
'params' => 2,
'type' => 'int',
];
$funcs['MBREquals'] = [
'params' => 2,
'type' => 'int',
];
$funcs['MBRIntersects'] = [
'params' => 2,
'type' => 'int',
];
$funcs['MBROverlaps'] = [
'params' => 2,
'type' => 'int',
];
$funcs['MBRTouches'] = [
'params' => 2,
'type' => 'int',
];
$funcs['MBRWithin'] = [
'params' => 2,
'type' => 'int',
];
}
return $funcs;
}
}
|