@@ -6,11 +6,13 @@ class Point extends Geometry
66{
77 protected $ lat ;
88 protected $ lng ;
9+ protected $ alt ;
910
10- public function __construct ($ lat , $ lng )
11+ public function __construct ($ lat , $ lng, $ alt = null )
1112 {
1213 $ this ->lat = (float )$ lat ;
1314 $ this ->lng = (float )$ lng ;
15+ $ this ->alt = isset ($ alt ) ? (float )$ alt : null ;
1416 }
1517
1618 public function getLat ()
@@ -33,28 +35,54 @@ public function setLng($lng)
3335 $ this ->lng = (float )$ lng ;
3436 }
3537
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+
3653 public function toPair ()
3754 {
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 ;
3960 }
40-
61+
4162 private static function stringifyFloat ($ float )
4263 {
4364 // normalized output among locales
4465 return rtrim (rtrim (sprintf ('%F ' , $ float ), '0 ' ), '. ' );
4566 }
46-
67+
4768 public static function fromPair ($ pair )
4869 {
4970 $ 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+ }
5177
52- return new static ((float )$ lat , (float )$ lng );
78+ return new static ((float )$ lat , (float )$ lng, isset ( $ alt ) ? ( float ) $ alt : null );
5379 }
5480
5581 public function toWKT ()
5682 {
57- return sprintf ('POINT(%s) ' , (string )$ this );
83+ $ wktType = 'POINT ' ;
84+ if ($ this ->is3d ()) $ wktType .= ' Z ' ;
85+ return sprintf ('%s(%s) ' , $ wktType , (string )$ this );
5886 }
5987
6088 public static function fromString ($ wktArgument )
@@ -74,6 +102,8 @@ public function __toString()
74102 */
75103 public function jsonSerialize ()
76104 {
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 );
78108 }
79109}
0 commit comments