|
18 | 18 | use function mb_substr; |
19 | 19 | use function round; |
20 | 20 | use function sprintf; |
21 | | -use function str_contains; |
22 | 21 | use function trim; |
23 | 22 |
|
24 | 23 | /** |
@@ -63,20 +62,11 @@ public function scaleRow($spatial) |
63 | 62 |
|
64 | 63 | // Trim to remove leading 'MULTIPOLYGON(((' and trailing ')))' |
65 | 64 | $multipolygon = mb_substr($spatial, 15, -3); |
66 | | - // Separate each polygon |
67 | | - $polygons = explode(')),((', $multipolygon); |
68 | | - |
69 | | - foreach ($polygons as $polygon) { |
70 | | - // If the polygon doesn't have an inner ring, use polygon itself |
71 | | - if (! str_contains($polygon, '),(')) { |
72 | | - $ring = $polygon; |
73 | | - } else { |
74 | | - // Separate outer ring and use it to determine min-max |
75 | | - $parts = explode('),(', $polygon); |
76 | | - $ring = $parts[0]; |
77 | | - } |
| 65 | + $wkt_polygons = explode(')),((', $multipolygon); |
78 | 66 |
|
79 | | - $min_max = $this->setMinMax($ring, $min_max); |
| 67 | + foreach ($wkt_polygons as $wkt_polygon) { |
| 68 | + $wkt_outer_ring = explode('),(', $wkt_polygon)[0]; |
| 69 | + $min_max = $this->setMinMax($wkt_outer_ring, $min_max); |
80 | 70 | } |
81 | 71 |
|
82 | 72 | return $min_max; |
@@ -108,42 +98,31 @@ public function prepareRowAsPng( |
108 | 98 | // Separate each polygon |
109 | 99 | $polygons = explode(')),((', $multipolygon); |
110 | 100 |
|
111 | | - $first_poly = true; |
112 | 101 | foreach ($polygons as $polygon) { |
113 | | - // If the polygon doesn't have an inner polygon |
114 | | - if (! str_contains($polygon, '),(')) { |
115 | | - $points_arr = $this->extractPoints($polygon, $scale_data, true); |
116 | | - } else { |
117 | | - // Separate outer and inner polygons |
118 | | - $parts = explode('),(', $polygon); |
119 | | - $outer = $parts[0]; |
120 | | - $inner = array_slice($parts, 1); |
121 | | - |
122 | | - $points_arr = $this->extractPoints($outer, $scale_data, true); |
123 | | - |
124 | | - foreach ($inner as $inner_poly) { |
125 | | - $points_arr = array_merge( |
126 | | - $points_arr, |
127 | | - $this->extractPoints($inner_poly, $scale_data, true) |
128 | | - ); |
129 | | - } |
| 102 | + $wkt_rings = explode('),(', $polygon); |
| 103 | + |
| 104 | + $points_arr = []; |
| 105 | + |
| 106 | + foreach ($wkt_rings as $wkt_ring) { |
| 107 | + $ring = $this->extractPoints($wkt_ring, $scale_data, true); |
| 108 | + $points_arr = array_merge($points_arr, $ring); |
130 | 109 | } |
131 | 110 |
|
132 | 111 | // draw polygon |
133 | 112 | $image->filledPolygon($points_arr, $fill_color); |
134 | 113 | // mark label point if applicable |
135 | | - if ($label !== '' && $first_poly) { |
136 | | - $label_point = [ |
137 | | - $points_arr[2], |
138 | | - $points_arr[3], |
139 | | - ]; |
| 114 | + if (isset($label_point)) { |
| 115 | + continue; |
140 | 116 | } |
141 | 117 |
|
142 | | - $first_poly = false; |
| 118 | + $label_point = [ |
| 119 | + $points_arr[2], |
| 120 | + $points_arr[3], |
| 121 | + ]; |
143 | 122 | } |
144 | 123 |
|
145 | 124 | // print label if applicable |
146 | | - if (isset($label_point)) { |
| 125 | + if ($label !== '' && isset($label_point)) { |
147 | 126 | $image->string( |
148 | 127 | 1, |
149 | 128 | (int) round($label_point[0]), |
@@ -174,44 +153,32 @@ public function prepareRowAsPdf($spatial, ?string $label, array $color, array $s |
174 | 153 | // Trim to remove leading 'MULTIPOLYGON(((' and trailing ')))' |
175 | 154 | $multipolygon = mb_substr($spatial, 15, -3); |
176 | 155 | // Separate each polygon |
177 | | - $polygons = explode(')),((', $multipolygon); |
| 156 | + $wkt_polygons = explode(')),((', $multipolygon); |
178 | 157 |
|
179 | | - $first_poly = true; |
180 | | - foreach ($polygons as $polygon) { |
181 | | - // If the polygon doesn't have an inner polygon |
182 | | - if (! str_contains($polygon, '),(')) { |
183 | | - $points_arr = $this->extractPoints($polygon, $scale_data, true); |
184 | | - } else { |
185 | | - // Separate outer and inner polygons |
186 | | - $parts = explode('),(', $polygon); |
187 | | - $outer = $parts[0]; |
188 | | - $inner = array_slice($parts, 1); |
189 | | - |
190 | | - $points_arr = $this->extractPoints($outer, $scale_data, true); |
191 | | - |
192 | | - foreach ($inner as $inner_poly) { |
193 | | - $points_arr = array_merge( |
194 | | - $points_arr, |
195 | | - $this->extractPoints($inner_poly, $scale_data, true) |
196 | | - ); |
197 | | - } |
| 158 | + foreach ($wkt_polygons as $wkt_polygon) { |
| 159 | + $wkt_rings = explode('),(', $wkt_polygon); |
| 160 | + $points_arr = []; |
| 161 | + |
| 162 | + foreach ($wkt_rings as $wkt_ring) { |
| 163 | + $ring = $this->extractPoints($wkt_ring, $scale_data, true); |
| 164 | + $points_arr = array_merge($points_arr, $ring); |
198 | 165 | } |
199 | 166 |
|
200 | 167 | // draw polygon |
201 | 168 | $pdf->Polygon($points_arr, 'F*', [], $color, true); |
202 | 169 | // mark label point if applicable |
203 | | - if ($label !== '' && $first_poly) { |
204 | | - $label_point = [ |
205 | | - $points_arr[2], |
206 | | - $points_arr[3], |
207 | | - ]; |
| 170 | + if (isset($label_point)) { |
| 171 | + continue; |
208 | 172 | } |
209 | 173 |
|
210 | | - $first_poly = false; |
| 174 | + $label_point = [ |
| 175 | + $points_arr[2], |
| 176 | + $points_arr[3], |
| 177 | + ]; |
211 | 178 | } |
212 | 179 |
|
213 | 180 | // print label if applicable |
214 | | - if (isset($label_point)) { |
| 181 | + if ($label !== '' && isset($label_point)) { |
215 | 182 | $pdf->setXY($label_point[0], $label_point[1]); |
216 | 183 | $pdf->setFontSize(5); |
217 | 184 | $pdf->Cell(0, 0, $label); |
@@ -247,25 +214,14 @@ public function prepareRowAsSvg($spatial, $label, array $color, array $scale_dat |
247 | 214 | // Trim to remove leading 'MULTIPOLYGON(((' and trailing ')))' |
248 | 215 | $multipolygon = mb_substr($spatial, 15, -3); |
249 | 216 | // Separate each polygon |
250 | | - $polygons = explode(')),((', $multipolygon); |
| 217 | + $wkt_polygons = explode(')),((', $multipolygon); |
251 | 218 |
|
252 | | - foreach ($polygons as $polygon) { |
| 219 | + foreach ($wkt_polygons as $wkt_polygon) { |
253 | 220 | $row .= '<path d="'; |
254 | 221 |
|
255 | | - // If the polygon doesn't have an inner polygon |
256 | | - if (! str_contains($polygon, '),(')) { |
257 | | - $row .= $this->drawPath($polygon, $scale_data); |
258 | | - } else { |
259 | | - // Separate outer and inner polygons |
260 | | - $parts = explode('),(', $polygon); |
261 | | - $outer = $parts[0]; |
262 | | - $inner = array_slice($parts, 1); |
263 | | - |
264 | | - $row .= $this->drawPath($outer, $scale_data); |
265 | | - |
266 | | - foreach ($inner as $inner_poly) { |
267 | | - $row .= $this->drawPath($inner_poly, $scale_data); |
268 | | - } |
| 222 | + $wkt_rings = explode('),(', $wkt_polygon); |
| 223 | + foreach ($wkt_rings as $wkt_ring) { |
| 224 | + $row .= $this->drawPath($wkt_ring, $scale_data); |
269 | 225 | } |
270 | 226 |
|
271 | 227 | $polygon_options['id'] = $label . $this->getRandomId(); |
@@ -519,39 +475,26 @@ public function generateParams($value, $index = -1) |
519 | 475 | // Trim to remove leading 'MULTIPOLYGON(((' and trailing ')))' |
520 | 476 | $multipolygon = mb_substr($wkt, 15, -3); |
521 | 477 | // Separate each polygon |
522 | | - $polygons = explode(')),((', $multipolygon); |
| 478 | + $wkt_polygons = explode(')),((', $multipolygon); |
523 | 479 |
|
524 | 480 | $param_row =& $params[$index]['MULTIPOLYGON']; |
525 | | - $param_row['no_of_polygons'] = count($polygons); |
| 481 | + $param_row['no_of_polygons'] = count($wkt_polygons); |
526 | 482 |
|
527 | 483 | $k = 0; |
528 | | - foreach ($polygons as $polygon) { |
529 | | - // If the polygon doesn't have an inner polygon |
530 | | - if (! str_contains($polygon, '),(')) { |
531 | | - $param_row[$k]['no_of_lines'] = 1; |
532 | | - $points_arr = $this->extractPoints($polygon, null); |
| 484 | + foreach ($wkt_polygons as $wkt_polygon) { |
| 485 | + $wkt_rings = explode('),(', $wkt_polygon); |
| 486 | + $param_row[$k]['no_of_lines'] = count($wkt_rings); |
| 487 | + $j = 0; |
| 488 | + foreach ($wkt_rings as $wkt_ring) { |
| 489 | + $points_arr = $this->extractPoints($wkt_ring, null); |
533 | 490 | $no_of_points = count($points_arr); |
534 | | - $param_row[$k][0]['no_of_points'] = $no_of_points; |
| 491 | + $param_row[$k][$j]['no_of_points'] = $no_of_points; |
535 | 492 | for ($i = 0; $i < $no_of_points; $i++) { |
536 | | - $param_row[$k][0][$i]['x'] = $points_arr[$i][0]; |
537 | | - $param_row[$k][0][$i]['y'] = $points_arr[$i][1]; |
| 493 | + $param_row[$k][$j][$i]['x'] = $points_arr[$i][0]; |
| 494 | + $param_row[$k][$j][$i]['y'] = $points_arr[$i][1]; |
538 | 495 | } |
539 | | - } else { |
540 | | - // Separate outer and inner polygons |
541 | | - $parts = explode('),(', $polygon); |
542 | | - $param_row[$k]['no_of_lines'] = count($parts); |
543 | | - $j = 0; |
544 | | - foreach ($parts as $ring) { |
545 | | - $points_arr = $this->extractPoints($ring, null); |
546 | | - $no_of_points = count($points_arr); |
547 | | - $param_row[$k][$j]['no_of_points'] = $no_of_points; |
548 | | - for ($i = 0; $i < $no_of_points; $i++) { |
549 | | - $param_row[$k][$j][$i]['x'] = $points_arr[$i][0]; |
550 | | - $param_row[$k][$j][$i]['y'] = $points_arr[$i][1]; |
551 | | - } |
552 | 496 |
|
553 | | - $j++; |
554 | | - } |
| 497 | + $j++; |
555 | 498 | } |
556 | 499 |
|
557 | 500 | $k++; |
|
0 commit comments