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
|
|
|
* http://www.gnu.org/copyleft/gpl.html. |
21
|
|
|
*/ |
22
|
|
|
use ShapeFile\ShapeFile; |
23
|
|
|
use ShapeFile\ShapeRecord; |
24
|
|
|
|
25
|
|
|
class ShapeFileTest extends PHPUnit_Framework_TestCase |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Tests loading of a file |
29
|
|
|
* |
30
|
|
|
* @param string $filename Name of file |
31
|
|
|
* @param integer $records Expected number of records |
32
|
|
|
* @param integer $parts Expected number of parts in first record |
33
|
|
|
* |
34
|
|
|
* @return void |
35
|
|
|
* |
36
|
|
|
* @dataProvider provideFiles |
37
|
|
|
*/ |
38
|
|
|
public function testLoad($filename, $records, $parts) |
39
|
|
|
{ |
40
|
|
|
$shp = new ShapeFile(1); |
41
|
|
|
$shp->loadFromFile($filename); |
42
|
|
|
$this->assertEquals($records, count($shp->records)); |
43
|
|
|
if (!is_null($parts)) { |
44
|
|
|
$this->assertEquals($parts, count($shp->records[0]->SHPData["parts"])); |
45
|
|
|
} |
46
|
|
|
$this->assertEquals('', $shp->lastError); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Data provider for file loading tests. |
51
|
|
|
* |
52
|
|
|
* @return array |
53
|
|
|
*/ |
54
|
|
|
public function provideFiles() |
55
|
|
|
{ |
56
|
|
|
return array( |
57
|
|
|
array('data/capitals.*', 652, null), |
58
|
|
|
array('data/mexico.*', 32, 3), |
59
|
|
|
array('data/Czech_Republic_AL2.*', 1, 1), |
60
|
|
|
array('data/w001n05f.*', 1, 1), |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Test error handling in loader |
66
|
|
|
* |
67
|
|
|
* @param string $filename name to load |
68
|
|
|
* |
69
|
|
|
* @return void |
70
|
|
|
* |
71
|
|
|
* @dataProvider provideErrorFiles |
72
|
|
|
*/ |
73
|
|
|
public function testLoadError($filename) |
74
|
|
|
{ |
75
|
|
|
$shp = new ShapeFile(1); |
76
|
|
|
$shp->loadFromFile($filename); |
77
|
|
|
$this->assertNotEquals('', $shp->lastError); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Data provider for file loading error tests. |
82
|
|
|
* |
83
|
|
|
* @return array |
84
|
|
|
*/ |
85
|
|
|
public function provideErrorFiles() |
86
|
|
|
{ |
87
|
|
|
return array( |
88
|
|
|
array('data/no-dbf.*'), |
89
|
|
|
array('data/no-shp.*'), |
90
|
|
|
array('data/invalid-dbf.*'), |
91
|
|
|
array('data/missing.*'), |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Creates test data |
97
|
|
|
* |
98
|
|
|
* @return void |
99
|
|
|
*/ |
100
|
|
|
private function createTestData() |
101
|
|
|
{ |
102
|
|
|
$shp = new ShapeFile(1); |
103
|
|
|
|
104
|
|
|
$record0 = new ShapeRecord(1); |
105
|
|
|
$record0->addPoint(array("x" => 482131.764567, "y" => 2143634.39608)); |
106
|
|
|
|
107
|
|
|
$record1 = new ShapeRecord(11); |
108
|
|
|
$record1->addPoint(array("x" => 472131.764567, "y" => 2143634.39608, 'z' => 220, 'm' => 120)); |
109
|
|
|
|
110
|
|
|
$record2 = new ShapeRecord(21); |
111
|
|
|
$record2->addPoint(array("x" => 492131.764567, "y" => 2143634.39608, 'z' => 150, 'm' => 80)); |
112
|
|
|
|
113
|
|
|
$shp->addRecord($record0); |
114
|
|
|
$shp->addRecord($record1); |
115
|
|
|
$shp->addRecord($record2); |
116
|
|
|
|
117
|
|
|
$shp->setDBFHeader( |
118
|
|
|
array( |
119
|
|
|
array('ID', 'N', 8, 0), |
120
|
|
|
array('DESC', 'C', 50, 0) |
121
|
|
|
) |
122
|
|
|
); |
123
|
|
|
|
124
|
|
|
$shp->records[0]->DBFData['ID'] = '1'; |
125
|
|
|
$shp->records[0]->DBFData['DESC'] = 'AAAAAAAAA'; |
126
|
|
|
|
127
|
|
|
$shp->records[1]->DBFData['ID'] = '2'; |
128
|
|
|
$shp->records[1]->DBFData['DESC'] = 'BBBBBBBBBB'; |
129
|
|
|
|
130
|
|
|
$shp->records[2]->DBFData['ID'] = '3'; |
131
|
|
|
$shp->records[2]->DBFData['DESC'] = 'CCCCCCCCCCC'; |
132
|
|
|
|
133
|
|
|
$shp->saveToFile('./data/test_shape.*'); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Tests creating file |
138
|
|
|
* |
139
|
|
|
* @return void |
140
|
|
|
*/ |
141
|
|
|
public function testCreate() |
142
|
|
|
{ |
143
|
|
|
$this->createTestData(); |
144
|
|
|
|
145
|
|
|
$shp = new ShapeFile(1); |
146
|
|
|
$shp->loadFromFile('./data/test_shape.*'); |
147
|
|
|
$this->assertEquals(3, count($shp->records)); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Tests removing record from a file |
152
|
|
|
* |
153
|
|
|
* @return void |
154
|
|
|
*/ |
155
|
|
|
public function testDelete() |
156
|
|
|
{ |
157
|
|
|
$this->createTestData(); |
158
|
|
|
|
159
|
|
|
$shp = new ShapeFile(1); |
160
|
|
|
$shp->loadFromFile('./data/test_shape.*'); |
161
|
|
|
$shp->deleteRecord(1); |
162
|
|
|
$shp->saveToFile(); |
163
|
|
|
$this->assertEquals(2, count($shp->records)); |
164
|
|
|
|
165
|
|
|
$shp = new ShapeFile(1); |
166
|
|
|
$shp->loadFromFile('./data/test_shape.*'); |
167
|
|
|
$this->assertEquals(2, count($shp->records)); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Test adding record to a file |
172
|
|
|
* |
173
|
|
|
* @return void |
174
|
|
|
*/ |
175
|
|
|
public function testAdd() |
176
|
|
|
{ |
177
|
|
|
$this->createTestData(); |
178
|
|
|
|
179
|
|
|
$shp = new ShapeFile(1); |
180
|
|
|
$shp->loadFromFile('./data/test_shape.*'); |
181
|
|
|
|
182
|
|
|
$record0 = new ShapeRecord(1); |
183
|
|
|
$record0->addPoint(array("x" => 482131.764567, "y" => 2143634.39608)); |
184
|
|
|
|
185
|
|
|
$shp->addRecord($record0); |
186
|
|
|
$shp->records[3]->DBFData['ID'] = '4'; |
187
|
|
|
$shp->records[3]->DBFData['DESC'] = 'CCCCCCCCCCC'; |
188
|
|
|
|
189
|
|
|
$shp->saveToFile(); |
190
|
|
|
$this->assertEquals(4, count($shp->records)); |
191
|
|
|
|
192
|
|
|
$shp = new ShapeFile(1); |
193
|
|
|
$shp->loadFromFile('./data/test_shape.*'); |
194
|
|
|
$this->assertEquals(4, count($shp->records)); |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.