1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* phpMyAdmin ShapeFile library |
4
|
|
|
* <https://github.com/phpmyadmin/shapefile/> |
5
|
|
|
* |
6
|
|
|
* Copyright 2006-2007 Ovidio <ovidio AT users.sourceforge.net> |
7
|
|
|
* Copyright 2016 Michal Čihař <[email protected]> |
8
|
|
|
* |
9
|
|
|
* This program is free software; you can redistribute it and/or |
10
|
|
|
* modify it under the terms of the GNU General Public License |
11
|
|
|
* as published by the Free Software Foundation. |
12
|
|
|
* |
13
|
|
|
* This program is distributed in the hope that it will be useful, |
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16
|
|
|
* GNU General Public License for more details. |
17
|
|
|
* |
18
|
|
|
* You should have received a copy of the GNU General Public License |
19
|
|
|
* along with this program; if not, you can download one from |
20
|
|
|
* https://www.gnu.org/copyleft/gpl.html. |
21
|
|
|
*/ |
22
|
|
|
namespace ShapeFileTest; |
23
|
|
|
|
24
|
|
|
use ShapeFile\ShapeFile; |
25
|
|
|
use ShapeFile\ShapeRecord; |
26
|
|
|
|
27
|
|
|
class ShapeFileTest extends \PHPUnit_Framework_TestCase |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* Tests loading of a file |
31
|
|
|
* |
32
|
|
|
* @param string $filename Name of file |
33
|
|
|
* @param integer $records Expected number of records |
34
|
|
|
* @param integer $parts Expected number of parts in first record |
35
|
|
|
* |
36
|
|
|
* @return void |
37
|
|
|
* |
38
|
|
|
* @dataProvider provideFiles |
39
|
|
|
*/ |
40
|
|
|
public function testLoad($filename, $records, $parts) |
41
|
|
|
{ |
42
|
|
|
$shp = new ShapeFile(1); |
43
|
|
|
$shp->loadFromFile($filename); |
44
|
|
|
$this->assertEquals('', $shp->lastError); |
45
|
|
|
$this->assertEquals($records, count($shp->records)); |
46
|
|
|
if (!is_null($parts)) { |
47
|
|
|
$this->assertEquals($parts, count($shp->records[0]->SHPData["parts"])); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Data provider for file loading tests. |
53
|
|
|
* |
54
|
|
|
* @return array |
55
|
|
|
*/ |
56
|
|
|
public function provideFiles() |
57
|
|
|
{ |
58
|
|
|
return array( |
59
|
|
|
array('data/capitals.*', 652, null), |
60
|
|
|
array('data/mexico.*', 32, 3), |
61
|
|
|
array('data/Czech_Republic_AL2.*', 1, 1), |
62
|
|
|
array('data/w001n05f.*', 16, 1), |
63
|
|
|
array('data/bc_hospitals.*', 44, null), |
64
|
|
|
array('data/multipoint.*', 312, null), |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Test error handling in loader |
70
|
|
|
* |
71
|
|
|
* @param string $filename name to load |
72
|
|
|
* |
73
|
|
|
* @return void |
74
|
|
|
* |
75
|
|
|
* @dataProvider provideErrorFiles |
76
|
|
|
*/ |
77
|
|
|
public function testLoadError($filename) |
78
|
|
|
{ |
79
|
|
|
$shp = new ShapeFile(1); |
80
|
|
|
$shp->loadFromFile($filename); |
81
|
|
|
$this->assertNotEquals('', $shp->lastError); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Data provider for file loading error tests. |
86
|
|
|
* |
87
|
|
|
* @return array |
88
|
|
|
*/ |
89
|
|
|
public function provideErrorFiles() |
90
|
|
|
{ |
91
|
|
|
$result = array( |
92
|
|
|
array('data/no-shp.*'), |
93
|
|
|
array('data/missing.*'), |
94
|
|
|
); |
95
|
|
|
|
96
|
|
|
if (ShapeFile::supports_dbase()) { |
97
|
|
|
$result[] = array('data/no-dbf.*'); |
98
|
|
|
$result[] = array('data/invalid-dbf.*'); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $result; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Creates test data |
106
|
|
|
* |
107
|
|
|
* @return void |
108
|
|
|
*/ |
109
|
|
|
private function createTestData() |
110
|
|
|
{ |
111
|
|
|
$shp = new ShapeFile(1); |
112
|
|
|
|
113
|
|
|
$record0 = new ShapeRecord(1); |
114
|
|
|
$record0->addPoint(array("x" => 482131.764567, "y" => 2143634.39608)); |
115
|
|
|
|
116
|
|
|
$record1 = new ShapeRecord(11); |
117
|
|
|
$record1->addPoint(array("x" => 472131.764567, "y" => 2143634.39608, 'z' => 220, 'm' => 120)); |
118
|
|
|
|
119
|
|
|
$record2 = new ShapeRecord(21); |
120
|
|
|
$record2->addPoint(array("x" => 492131.764567, "y" => 2143634.39608, 'z' => 150, 'm' => 80)); |
121
|
|
|
|
122
|
|
|
$record3 = new ShapeRecord(3); |
123
|
|
|
$record3->addPoint(array("x" => 482131.764567, "y" => 2143634.39608), 0); |
124
|
|
|
$record3->addPoint(array("x" => 482132.764567, "y" => 2143635.39608), 0); |
125
|
|
|
$record3->addPoint(array("x" => 482131.764567, "y" => 2143635.39608), 1); |
126
|
|
|
$record3->addPoint(array("x" => 482132.764567, "y" => 2143636.39608), 1); |
127
|
|
|
|
128
|
|
|
$shp->addRecord($record0); |
129
|
|
|
$shp->addRecord($record1); |
130
|
|
|
$shp->addRecord($record2); |
131
|
|
|
$shp->addRecord($record3); |
132
|
|
|
|
133
|
|
|
$shp->setDBFHeader( |
134
|
|
|
array( |
135
|
|
|
array('ID', 'N', 8, 0), |
136
|
|
|
array('DESC', 'C', 50, 0) |
137
|
|
|
) |
138
|
|
|
); |
139
|
|
|
|
140
|
|
|
$shp->records[0]->DBFData['ID'] = '1'; |
141
|
|
|
$shp->records[0]->DBFData['DESC'] = 'AAAAAAAAA'; |
142
|
|
|
|
143
|
|
|
$shp->records[1]->DBFData['ID'] = '2'; |
144
|
|
|
$shp->records[1]->DBFData['DESC'] = 'BBBBBBBBBB'; |
145
|
|
|
|
146
|
|
|
$shp->records[2]->DBFData['ID'] = '3'; |
147
|
|
|
$shp->records[2]->DBFData['DESC'] = 'CCCCCCCCCCC'; |
148
|
|
|
|
149
|
|
|
$shp->records[3]->DBFData['ID'] = '4'; |
150
|
|
|
$shp->records[3]->DBFData['DESC'] = 'CCCCCCCCCCC'; |
151
|
|
|
|
152
|
|
|
$shp->saveToFile('./data/test_shape.*'); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Tests creating file |
157
|
|
|
* |
158
|
|
|
* @return void |
159
|
|
|
*/ |
160
|
|
|
public function testCreate() |
161
|
|
|
{ |
162
|
|
|
if (!ShapeFile::supports_dbase()) { |
163
|
|
|
$this->markTestSkipped('dbase extension missing'); |
164
|
|
|
} |
165
|
|
|
$this->createTestData(); |
166
|
|
|
|
167
|
|
|
$shp = new ShapeFile(1); |
168
|
|
|
$shp->loadFromFile('./data/test_shape.*'); |
169
|
|
|
$this->assertEquals(4, count($shp->records)); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Tests removing record from a file |
174
|
|
|
* |
175
|
|
|
* @return void |
176
|
|
|
*/ |
177
|
|
|
public function testDelete() |
178
|
|
|
{ |
179
|
|
|
if (!ShapeFile::supports_dbase()) { |
180
|
|
|
$this->markTestSkipped('dbase extension missing'); |
181
|
|
|
} |
182
|
|
|
$this->createTestData(); |
183
|
|
|
|
184
|
|
|
$shp = new ShapeFile(1); |
185
|
|
|
$shp->loadFromFile('./data/test_shape.*'); |
186
|
|
|
$shp->deleteRecord(1); |
187
|
|
|
$shp->saveToFile(); |
188
|
|
|
$this->assertEquals(3, count($shp->records)); |
189
|
|
|
|
190
|
|
|
$shp = new ShapeFile(1); |
191
|
|
|
$shp->loadFromFile('./data/test_shape.*'); |
192
|
|
|
$this->assertEquals(3, count($shp->records)); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Test adding record to a file |
197
|
|
|
* |
198
|
|
|
* @return void |
199
|
|
|
*/ |
200
|
|
|
public function testAdd() |
201
|
|
|
{ |
202
|
|
|
if (!ShapeFile::supports_dbase()) { |
203
|
|
|
$this->markTestSkipped('dbase extension missing'); |
204
|
|
|
} |
205
|
|
|
$this->createTestData(); |
206
|
|
|
|
207
|
|
|
$shp = new ShapeFile(1); |
208
|
|
|
$shp->loadFromFile('./data/test_shape.*'); |
209
|
|
|
|
210
|
|
|
$record0 = new ShapeRecord(1); |
211
|
|
|
$record0->addPoint(array("x" => 482131.764567, "y" => 2143634.39608)); |
212
|
|
|
|
213
|
|
|
$shp->addRecord($record0); |
214
|
|
|
$shp->records[4]->DBFData['ID'] = '4'; |
215
|
|
|
$shp->records[4]->DBFData['DESC'] = 'CCCCCCCCCCC'; |
216
|
|
|
|
217
|
|
|
$shp->saveToFile(); |
218
|
|
|
$this->assertEquals(5, count($shp->records)); |
219
|
|
|
|
220
|
|
|
$shp = new ShapeFile(1); |
221
|
|
|
$shp->loadFromFile('./data/test_shape.*'); |
222
|
|
|
$this->assertEquals(5, count($shp->records)); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Test shape naming. |
227
|
|
|
* |
228
|
|
|
* @return void |
229
|
|
|
*/ |
230
|
|
|
public function testShapeName() |
231
|
|
|
{ |
232
|
|
|
$obj = new ShapeRecord(1); |
233
|
|
|
$this->assertEquals('Point', $obj->getShapeName()); |
234
|
|
|
$obj = new Shapefile(1); |
235
|
|
|
$this->assertEquals('Point', $obj->getShapeName()); |
236
|
|
|
$obj = new ShapeRecord(-1); |
237
|
|
|
$this->assertEquals('Shape -1', $obj->getShapeName()); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Test shapes save/load round robin |
242
|
|
|
* |
243
|
|
|
* @param int $type Shape type |
244
|
|
|
* @param array $points Points |
245
|
|
|
* |
246
|
|
|
* @return void |
247
|
|
|
* |
248
|
|
|
* @dataProvider shapes |
249
|
|
|
*/ |
250
|
|
|
public function testShapeSaveLoad($type, $points) |
251
|
|
|
{ |
252
|
|
|
$filename = "./data/test_shape-$type.*"; |
253
|
|
|
$shp = new ShapeFile($type); |
254
|
|
|
|
255
|
|
|
$record0 = new ShapeRecord($type); |
256
|
|
|
|
257
|
|
|
foreach ($points as $point) { |
258
|
|
|
$record0->addPoint($point[0], $point[1]); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
$shp->addRecord($record0); |
262
|
|
|
|
263
|
|
|
$shp->saveToFile($filename); |
264
|
|
|
|
265
|
|
|
$shp2 = new ShapeFile($type); |
266
|
|
|
$shp2->loadFromFile($filename); |
267
|
|
|
|
268
|
|
|
$this->assertEquals( |
269
|
|
|
count($shp->records), |
270
|
|
|
count($shp2->records) |
271
|
|
|
); |
272
|
|
|
|
273
|
|
|
$record = $shp->records[0]; |
274
|
|
|
$record2 = $shp2->records[0]; |
275
|
|
|
|
276
|
|
|
$items = array('numparts', 'numpoints'); |
277
|
|
|
foreach ($items as $item) { |
278
|
|
|
if (isset($record->SHPData[$item])) { |
279
|
|
|
$this->assertEquals( |
280
|
|
|
$record->SHPData[$item], |
281
|
|
|
$record2->SHPData[$item] |
282
|
|
|
); |
283
|
|
|
} |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/* Test deletion works */ |
287
|
|
|
$record->deletePoint(); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Test shapes save/load round robin with z coordinate |
292
|
|
|
* |
293
|
|
|
* @param int $type Shape type |
294
|
|
|
* @param array $points Points |
295
|
|
|
* |
296
|
|
|
* @return void |
297
|
|
|
* |
298
|
|
|
* @dataProvider shapes |
299
|
|
|
*/ |
300
|
|
|
public function testZetShapeSaveLoad($type, $points) |
301
|
|
|
{ |
302
|
|
|
$this->testShapeSaveLoad($type + 10, $points); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* Test shapes save/load round robin with measure |
307
|
|
|
* |
308
|
|
|
* @param int $type Shape type |
309
|
|
|
* @param array $points Points |
310
|
|
|
* |
311
|
|
|
* @return void |
312
|
|
|
* |
313
|
|
|
* @dataProvider shapes |
314
|
|
|
*/ |
315
|
|
|
public function testMeasureShapeSaveLoad($type, $points) |
316
|
|
|
{ |
317
|
|
|
$this->testShapeSaveLoad($type + 20, $points); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* Data provider for save/load testing |
322
|
|
|
* |
323
|
|
|
* @return array |
324
|
|
|
*/ |
325
|
|
|
public function shapes() |
326
|
|
|
{ |
327
|
|
|
return array( |
328
|
|
|
array( |
329
|
|
|
1, |
330
|
|
|
array( |
331
|
|
|
array(array('x' => 10, 'y' => 20), 0), |
332
|
|
|
) |
333
|
|
|
), |
334
|
|
|
array( |
335
|
|
|
3, |
336
|
|
|
array( |
337
|
|
|
array(array('x' => 10, 'y' => 20), 0), |
338
|
|
|
array(array('x' => 20, 'y' => 20), 0), |
339
|
|
|
array(array('x' => 20, 'y' => 20), 1), |
340
|
|
|
array(array('x' => 20, 'y' => 10), 1), |
341
|
|
|
) |
342
|
|
|
), |
343
|
|
|
array( |
344
|
|
|
5, |
345
|
|
|
array( |
346
|
|
|
array(array('x' => 10, 'y' => 20), 0), |
347
|
|
|
array(array('x' => 20, 'y' => 20), 0), |
348
|
|
|
array(array('x' => 20, 'y' => 20), 1), |
349
|
|
|
array(array('x' => 20, 'y' => 10), 1), |
350
|
|
|
array(array('x' => 20, 'y' => 10), 2), |
351
|
|
|
array(array('x' => 10, 'y' => 20), 2), |
352
|
|
|
) |
353
|
|
|
), |
354
|
|
|
array( |
355
|
|
|
8, |
356
|
|
|
array( |
357
|
|
|
array(array('x' => 10, 'y' => 20), 0), |
358
|
|
|
array(array('x' => 20, 'y' => 20), 0), |
359
|
|
|
array(array('x' => 20, 'y' => 10), 0), |
360
|
|
|
) |
361
|
|
|
), |
362
|
|
|
); |
363
|
|
|
} |
364
|
|
|
} |
365
|
|
|
|