Skip to content

Commit

Permalink
Merge pull request #18 from nijel/test-dbf-lookup
Browse files Browse the repository at this point in the history
Test dbf lookup
  • Loading branch information
nijel authored Nov 17, 2016
2 parents 3cdf3fd + a5e9c45 commit 17652f7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/ShapeFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,15 +226,15 @@ public function setDBFHeader($header) {
* @return integer
*/
public function getIndexFromDBFData($field, $value) {
$result = -1;
$count = count($this->records) - 1;
for ($i = 0; $i < $count; $i++) {
if (isset($this->records[$i]->DBFData[$field]) && (strtoupper($this->records[$i]->DBFData[$field]) == strtoupper($value))) {
$result = $i;
foreach ($this->records as $index => $record) {
if (isset($record->DBFData[$field]) &&
(trim(strtoupper($record->DBFData[$field])) == strtoupper($value))
) {
return $index;
}
}

return $result;
return -1;
}

private function _loadDBFHeader() {
Expand Down
17 changes: 17 additions & 0 deletions tests/ShapeFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -378,4 +378,21 @@ public function shapes()
),
);
}

public function testSearch()
{
$shp = new ShapeFile(0);
$shp->loadFromFile('data/capitals.*');
/* Nonexisting entry or no dbase support */
$this->assertEquals(
-1,
$shp->getIndexFromDBFData('CNTRY_NAME', 'nonexisting')
);
if (ShapeFile::supports_dbase()) {
$this->assertEquals(
218,
$shp->getIndexFromDBFData('CNTRY_NAME', 'Czech Republic')
);
}
}
}

0 comments on commit 17652f7

Please sign in to comment.