@@ -6,11 +6,13 @@ class Point extends Geometry
6
6
{
7
7
protected $ lat ;
8
8
protected $ lng ;
9
+ protected $ alt ;
9
10
10
- public function __construct ($ lat , $ lng )
11
+ public function __construct ($ lat , $ lng, $ alt = null )
11
12
{
12
13
$ this ->lat = (float )$ lat ;
13
14
$ this ->lng = (float )$ lng ;
15
+ $ this ->alt = isset ($ alt ) ? (float )$ alt : null ;
14
16
}
15
17
16
18
public function getLat ()
@@ -33,28 +35,54 @@ public function setLng($lng)
33
35
$ this ->lng = (float )$ lng ;
34
36
}
35
37
38
+ public function getAlt ()
39
+ {
40
+ return $ this ->alt ;
41
+ }
42
+
43
+ public function setAlt ($ alt )
44
+ {
45
+ $ this ->alt = (float )$ alt ;
46
+ }
47
+
48
+ public function is3d ()
49
+ {
50
+ return isset ($ this ->alt );
51
+ }
52
+
36
53
public function toPair ()
37
54
{
38
- return self ::stringifyFloat ($ this ->getLng ()) . ' ' . self ::stringifyFloat ($ this ->getLat ());
55
+ $ pair = self ::stringifyFloat ($ this ->getLng ()) . ' ' . self ::stringifyFloat ($ this ->getLat ());
56
+ if ($ this ->is3d ()) {
57
+ $ pair .= ' ' . self ::stringifyFloat ($ this ->getAlt ());
58
+ }
59
+ return $ pair ;
39
60
}
40
-
61
+
41
62
private static function stringifyFloat ($ float )
42
63
{
43
64
// normalized output among locales
44
65
return rtrim (rtrim (sprintf ('%F ' , $ float ), '0 ' ), '. ' );
45
66
}
46
-
67
+
47
68
public static function fromPair ($ pair )
48
69
{
49
70
$ pair = preg_replace ('/^[a-zA-Z\(\)]+/ ' , '' , trim ($ pair ));
50
- list ($ lng , $ lat ) = explode (' ' , trim ($ pair ));
71
+ $ splits = explode (' ' , trim ($ pair ));
72
+ $ lng = $ splits [0 ];
73
+ $ lat = $ splits [1 ];
74
+ if (count ($ splits ) > 2 ) {
75
+ $ alt = $ splits [2 ];
76
+ }
51
77
52
- return new static ((float )$ lat , (float )$ lng );
78
+ return new static ((float )$ lat , (float )$ lng, isset ( $ alt ) ? ( float ) $ alt : null );
53
79
}
54
80
55
81
public function toWKT ()
56
82
{
57
- return sprintf ('POINT(%s) ' , (string )$ this );
83
+ $ wktType = 'POINT ' ;
84
+ if ($ this ->is3d ()) $ wktType .= ' Z ' ;
85
+ return sprintf ('%s(%s) ' , $ wktType , (string )$ this );
58
86
}
59
87
60
88
public static function fromString ($ wktArgument )
@@ -74,6 +102,8 @@ public function __toString()
74
102
*/
75
103
public function jsonSerialize ()
76
104
{
77
- return new \GeoJson \Geometry \Point ([$ this ->getLng (), $ this ->getLat ()]);
105
+ $ position = [$ this ->getLng (), $ this ->getLat ()];
106
+ if ($ this ->is3d ()) $ position [] = $ this ->getAlt ();
107
+ return new \GeoJson \Geometry \Point ($ position );
78
108
}
79
109
}
0 commit comments