2
2
3
3
class Waymark_GeoJSON {
4
4
5
- static public function string_to_feature_collection ($ string = '' ) {
6
- if (is_string ($ string )) {
7
- return json_decode ($ string , true , 512 , JSON_OBJECT_AS_ARRAY );
5
+ static public function string_to_feature_collection ($ string = '' ) {
6
+ if (is_string ($ string )) {
7
+ return json_decode ($ string , true , 512 , JSON_OBJECT_AS_ARRAY );
8
8
}
9
9
10
- return false ;
11
- }
10
+ return false ;
11
+ }
12
12
13
- static public function get_feature_count ($ FeatureCollection = []) {
14
- if (is_string ($ FeatureCollection )) {
15
- $ FeatureCollection = self ::string_to_feature_collection ($ FeatureCollection );
13
+ static public function get_feature_count ($ FeatureCollection = []) {
14
+ if (is_string ($ FeatureCollection )) {
15
+ $ FeatureCollection = self ::string_to_feature_collection ($ FeatureCollection );
16
16
}
17
-
18
- if ($ FeatureCollection && isset ($ FeatureCollection ['features ' ]) && is_array ($ FeatureCollection ['features ' ])) {
17
+
18
+ if ($ FeatureCollection && isset ($ FeatureCollection ['features ' ]) && is_array ($ FeatureCollection ['features ' ])) {
19
19
20
20
return sizeof ($ FeatureCollection ['features ' ]);
21
- }
21
+ }
22
22
23
- return false ;
24
- }
23
+ return false ;
24
+ }
25
25
26
- static public function update_feature_property ($ FeatureCollection = [], $ property_key = null , $ property_value = null ) {
27
- if (is_string ($ FeatureCollection )) {
28
- $ FeatureCollection = self ::string_to_feature_collection ($ FeatureCollection );
26
+ static public function update_feature_property ($ FeatureCollection = [], $ property_key = null , $ property_value = null ) {
27
+ if (is_string ($ FeatureCollection )) {
28
+ $ FeatureCollection = self ::string_to_feature_collection ($ FeatureCollection );
29
29
}
30
30
31
31
//Feature Collection
32
- if ($ FeatureCollection && isset ($ FeatureCollection ['features ' ])) {
32
+ if ($ FeatureCollection && isset ($ FeatureCollection ['features ' ])) {
33
33
34
34
//Each Feature
35
- foreach ($ FeatureCollection ['features ' ] as &$ Feature ) {
36
- if (! isset ($ Feature ['properties ' ]) || ! is_array ($ Feature ['properties ' ])) {
35
+ foreach ($ FeatureCollection ['features ' ] as &$ Feature ) {
36
+ if (! isset ($ Feature ['properties ' ]) || !is_array ($ Feature ['properties ' ])) {
37
37
$ Feature ['properties ' ] = [];
38
38
}
39
-
39
+
40
40
$ Feature ['properties ' ][$ property_key ] = $ property_value ;
41
41
}
42
- }
42
+ }
43
+
44
+ return $ FeatureCollection ;
45
+ }
43
46
44
- return $ FeatureCollection ;
45
- }
46
-
47
47
static public function clean_feature_descriptions ($ FeatureCollection = []) {
48
48
//Feature Collection
49
- if ($ FeatureCollection && isset ($ FeatureCollection ['features ' ])) {
49
+ if ($ FeatureCollection && isset ($ FeatureCollection ['features ' ])) {
50
50
51
51
//Each Feature
52
- foreach ($ FeatureCollection ['features ' ] as &$ Feature ) {
53
- if (isset ($ Feature ['properties ' ]['description ' ])) {
54
- $ bad = ['" ' ];
52
+ foreach ($ FeatureCollection ['features ' ] as &$ Feature ) {
53
+ if (isset ($ Feature ['properties ' ]['description ' ])) {
54
+ $ bad = ['" ' ];
55
55
$ good = ['\" ' ];
56
56
$ Feature ['properties ' ]['description ' ] = str_replace ($ bad , $ good , $ Feature ['properties ' ]['description ' ]);
57
57
}
58
58
}
59
- }
59
+ }
60
60
61
61
return $ FeatureCollection ;
62
62
}
63
+
64
+ static public function features_by_overlay_type (Array $ FeatureCollection = []) {
65
+
66
+ // Waymark_Helper::debug($FeatureCollection);
67
+
68
+ $ overlays = array (
69
+ 'markers ' => array (),
70
+ 'lines ' => array (),
71
+ 'shapes ' => array (),
72
+ );
73
+
74
+ foreach ($ FeatureCollection ['features ' ] as $ feature ) {
75
+
76
+ // Waymark_Helper::debug($feature);
77
+
78
+ //Ensure feature properties has type
79
+ if (!isset ($ feature ['properties ' ]['type ' ])) {
80
+ continue ;
81
+ }
82
+
83
+ if (isset ($ feature ['geometry ' ]['type ' ])) {
84
+ switch ($ feature ['geometry ' ]['type ' ]) {
85
+ case 'Point ' :
86
+ //Circle
87
+ if (isset ($ feature ['properties ' ]['radius ' ])) {
88
+ $ type = 'shapes ' ;
89
+ //Marker
90
+ } else {
91
+ $ type = 'markers ' ;
92
+ }
93
+
94
+ break ;
95
+
96
+ case 'LineString ' :
97
+ case 'MultiLineString ' :
98
+ $ type = 'lines ' ;
99
+
100
+ break ;
101
+ case 'Polygon ' :
102
+ $ type = 'shapes ' ;
103
+
104
+ break ;
105
+ }
106
+ }
107
+
108
+ //Add to Array if we have a type
109
+ if (isset ($ type )) {
110
+ $ overlays [$ type ][$ feature ['properties ' ]['type ' ]][] = $ feature ;
111
+ }
112
+ }
113
+
114
+ // Order markers/lines/shapes by type
115
+ foreach ($ overlays as &$ overlay ) {
116
+ ksort ($ overlay );
117
+ }
118
+
119
+ return $ overlays ;
120
+ }
63
121
}
0 commit comments