Skip to content

Commit 09dba8e

Browse files
author
Brandonian
committed
Better support for Polygons
1 parent 31d34de commit 09dba8e

File tree

1 file changed

+48
-12
lines changed

1 file changed

+48
-12
lines changed

shpParser.php

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,51 @@ private function loadPolyLineRecord() {
174174
),
175175
);
176176

177+
$geometries = $this->processLineStrings();
178+
179+
$return['numGeometries'] = $geometries['numParts'];
180+
if ($geometries['numParts'] > 1) {
181+
$return['wkt'] = 'MULTILINESTRING(' . implode(', ', $geometries['geometries']) . ')';
182+
}
183+
else {
184+
$return['wkt'] = 'LINESTRING(' . implode(', ', $geometries['geometries']) . ')';
185+
}
186+
187+
return $return;
188+
}
189+
190+
private function loadPolygonRecord() {
191+
$return = array(
192+
'bbox' => array(
193+
'xmin' => $this->loadData("d"),
194+
'ymin' => $this->loadData("d"),
195+
'xmax' => $this->loadData("d"),
196+
'ymax' => $this->loadData("d"),
197+
),
198+
);
199+
200+
$geometries = $this->processLineStrings();
201+
202+
$return['numGeometries'] = $geometries['numParts'];
203+
if ($geometries['numParts'] > 1) {
204+
$return['wkt'] = 'MULTIPOLYGON(' . implode(', ', $geometries['geometries']) . ')';
205+
}
206+
else {
207+
$return['wkt'] = 'POLYGON(' . implode(', ', $geometries['geometries']) . ')';
208+
}
209+
210+
return $return;
211+
}
212+
213+
/**
214+
* Process function for loadPolyLineRecord and loadPolygonRecord.
215+
* Returns geometries array.
216+
*/
217+
218+
private function processLineStrings() {
177219
$numParts = $this->loadData("V");
178220
$numPoints = $this->loadData("V");
221+
$geometries = array();
179222

180223
$parts = array();
181224
for ($i = 0; $i < $numParts; $i++) {
@@ -190,32 +233,25 @@ private function loadPolyLineRecord() {
190233
}
191234

192235
if ($numParts == 1) {
193-
$lines = array();
194236
for ($i = 0; $i < $numPoints; $i++) {
195-
$lines[] = sprintf('%f %f', $points[$i]['x'], $points[$i]['y']);
237+
$geometries[] = sprintf('%f %f', $points[$i]['x'], $points[$i]['y']);
196238
}
197239

198-
$return['wkt'] = 'LINESTRING (' . implode(', ', $lines) . ')';
199240
}
200241
else {
201-
$geometries = array();
202242
for ($i = 0; $i < $numParts; $i++) {
203243
$my_points = array();
204244
for ($j = $parts[$i]; $j < $parts[$i + 1]; $j++) {
205245
$my_points[] = sprintf('%f %f', $points[$j]['x'], $points[$j]['y']);
206246
}
207247
$geometries[] = '(' . implode(', ', $my_points) . ')';
208248
}
209-
$return['wkt'] = 'MULTILINESTRING (' . implode(', ', $geometries) . ')';
210249
}
211250

212-
$return['numGeometries'] = $numParts;
213-
214-
return $return;
215-
}
216-
217-
private function loadPolygonRecord() {
218-
$this->loadPolyLineRecord();
251+
return array(
252+
'numParts' => $numParts,
253+
'geometries' => $geometries,
254+
);
219255
}
220256

221257
private function loadMultiPointRecord() {

0 commit comments

Comments
 (0)