View source
<?php
namespace Drupal\geofield;
class WktGenerator implements WktGeneratorInterface {
protected function ddGenerate($min, $max, $int = FALSE) {
$func = 'rand';
if (function_exists('mt_rand')) {
$func = 'mt_rand';
}
$number = $func($min, $max);
if ($int || $number === $min || $number === $max) {
return $number;
}
$decimals = $func(1, pow(10, 5)) / pow(10, 5);
return round($number + $decimals, 5);
}
public function wktGenerateGeometry() {
$types = [
GEOFIELD_TYPE_POINT,
GEOFIELD_TYPE_MULTIPOINT,
GEOFIELD_TYPE_LINESTRING,
GEOFIELD_TYPE_MULTILINESTRING,
GEOFIELD_TYPE_POLYGON,
GEOFIELD_TYPE_MULTIPOLYGON,
];
shuffle($types);
$type = $types[0];
$func = 'WktGenerate' . ucfirst($type);
if (method_exists($this, $func)) {
return $this
->{$func}();
}
return 'POINT (0 0)';
}
protected function randomPoint() {
$lon = $this
->ddGenerate(-180, 180);
$lat = $this
->ddGenerate(-84, 84);
return [
$lon,
$lat,
];
}
protected function buildWkt($type, $value) {
return strtoupper($type) . ' (' . $value . ')';
}
protected function buildMultiCoordinates(array $coordinates) {
return '(' . implode('), (', $coordinates) . ')';
}
protected function buildPoint(array $point) {
return implode(' ', $point);
}
public function wktGeneratePoint(array $point = NULL) {
$point = $point ? $point : $this
->randomPoint();
return $this
->wktBuildPoint($point);
}
public function wktBuildPoint(array $point) {
return $this
->buildWkt(GEOFIELD_TYPE_POINT, $this
->buildPoint($point));
}
protected function generateMultipoint() {
$num = $this
->ddGenerate(1, 5, TRUE);
$start = $this
->randomPoint();
$points[] = $this
->buildPoint($start);
for ($i = 0; $i < $num; $i += 1) {
$diff = $this
->randomPoint();
$start[0] += $diff[0] / 100;
$start[1] += $diff[1] / 100;
$points[] = $this
->buildPoint($start);
}
return $this
->buildMultiCoordinates($points);
}
public function wktGenerateMultipoint() {
return $this
->buildWkt(GEOFIELD_TYPE_MULTIPOINT, $this
->generateMultipoint());
}
protected function generateLinestring(array $start = NULL, $segments = NULL) {
$start = $start ? $start : $this
->randomPoint();
$segments = $segments ? $segments : $this
->ddGenerate(2, 5, TRUE);
$points[] = [
$start[0],
$start[1],
];
for ($i = 1; $i < $segments; $i += 1) {
$diff = $this
->randomPoint();
$start[0] += $diff[0] / 100;
$start[1] += $diff[1] / 100;
$points[] = [
$start[0],
$start[1],
];
}
return $points;
}
public function wktGenerateLinestring(array $start = NULL, $segments = NULL) {
return $this
->wktBuildLinestring($this
->generateLinestring($start, $segments));
}
protected function buildLinestring(array $points) {
$components = [];
foreach ($points as $point) {
$components[] = $this
->buildPoint($point);
}
return implode(", ", $components);
}
public function wktBuildLinestring(array $points) {
return $this
->buildWkt(GEOFIELD_TYPE_LINESTRING, $this
->buildLinestring($points));
}
protected function generateMultilinestring() {
$start = $this
->randomPoint();
$num = $this
->ddGenerate(1, 3, TRUE);
$lines[] = $this
->buildLinestring($this
->generateLinestring($start));
for ($i = 0; $i < $num; $i += 1) {
$diff = $this
->randomPoint();
$start[0] += $diff[0] / 100;
$start[1] += $diff[1] / 100;
$lines[] = $this
->buildLinestring($this
->generateLinestring($start));
}
return $this
->buildMultiCoordinates($lines);
}
public function wktGenerateMultilinestring() {
return $this
->buildWkt(GEOFIELD_TYPE_MULTILINESTRING, $this
->generateMultilinestring());
}
protected function generatePolygon(array $start = NULL, $segments = NULL) {
$start = $start ? $start : $this
->randomPoint();
$segments = $segments ? $segments : $this
->ddGenerate(2, 4, TRUE);
$poly = $this
->generateLinestring($start, $segments);
$poly[] = $start;
return $poly;
}
public function wktGeneratePolygon(array $start = NULL, $segments = NULL) {
return $this
->wktBuildPolygon($this
->generatePolygon($start, $segments));
}
protected function buildPolygon(array $points) {
$components = [];
foreach ($points as $point) {
$components[] = $this
->buildPoint($point);
}
return '(' . implode(", ", $components) . ')';
}
public function wktBuildPolygon(array $points) {
return $this
->buildWkt(GEOFIELD_TYPE_POLYGON, $this
->buildPolygon($points));
}
protected function generateMultipolygon() {
$start = $this
->randomPoint();
$num = $this
->ddGenerate(1, 5, TRUE);
$segments = $this
->ddGenerate(2, 3, TRUE);
$poly[] = $this
->buildPolygon($this
->generatePolygon($start, $segments));
for ($i = 0; $i < $num; $i += 1) {
$diff = $this
->randomPoint();
$start[0] += $diff[0] / 100;
$start[1] += $diff[1] / 100;
$poly[] = $this
->buildPolygon($this
->generatePolygon($start, $segments));
}
return $this
->buildMultiCoordinates($poly);
}
public function wktGenerateMultipolygon() {
return $this
->buildWkt(GEOFIELD_TYPE_MULTIPOLYGON, $this
->generateMultipolygon());
}
protected function buildMultipolygon(array $rings) {
$poly = [];
foreach ($rings as $ring) {
$poly[] = $this
->buildPolygon($ring);
}
return $this
->buildMultiCoordinates($poly);
}
public function wktBuildMultipolygon(array $rings) {
return $this
->buildWkt(GEOFIELD_TYPE_MULTIPOLYGON, $this
->buildMultipolygon($rings));
}
}