api de gestion de ticket, basé sur php-crud-api. Le but est de décorrélé les outils de gestion des données, afin
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Geometry.php 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace Tqdev\PhpCrudApi\GeoJson;
  3. class Geometry implements \JsonSerializable
  4. {
  5. private $type;
  6. private $geometry;
  7. public static $types = [
  8. "Point",
  9. "MultiPoint",
  10. "LineString",
  11. "MultiLineString",
  12. "Polygon",
  13. "MultiPolygon",
  14. //"GeometryCollection",
  15. ];
  16. public function __construct(string $type, array $coordinates)
  17. {
  18. $this->type = $type;
  19. $this->coordinates = $coordinates;
  20. }
  21. public static function fromWkt(string $wkt): Geometry
  22. {
  23. $bracket = strpos($wkt, '(');
  24. $type = strtoupper(trim(substr($wkt, 0, $bracket)));
  25. $supported = false;
  26. foreach (Geometry::$types as $typeName) {
  27. if (strtoupper($typeName) == $type) {
  28. $type = $typeName;
  29. $supported = true;
  30. }
  31. }
  32. if (!$supported) {
  33. throw new \Exception('Geometry type not supported: ' . $type);
  34. }
  35. $coordinates = substr($wkt, $bracket);
  36. if (substr($type, -5) != 'Point' || ($type == 'MultiPoint' && $coordinates[1] != '(')) {
  37. $coordinates = preg_replace('|([0-9\-\.]+ )+([0-9\-\.]+)|', '[\1\2]', $coordinates);
  38. }
  39. $coordinates = str_replace(['(', ')', ', ', ' '], ['[', ']', ',', ','], $coordinates);
  40. $json = $coordinates;
  41. $coordinates = json_decode($coordinates);
  42. if (!$coordinates) {
  43. throw new \Exception('Could not decode WKT: ' . $wkt);
  44. }
  45. return new Geometry($type, $coordinates);
  46. }
  47. public function serialize()
  48. {
  49. return [
  50. 'type' => $this->type,
  51. 'coordinates' => $this->coordinates,
  52. ];
  53. }
  54. public function jsonSerialize()
  55. {
  56. return $this->serialize();
  57. }
  58. }