Skip to content

Commit

Permalink
Everything now passes tests
Browse files Browse the repository at this point in the history
  • Loading branch information
phayes committed Dec 2, 2014
1 parent 79f16f0 commit b91e8ad
Show file tree
Hide file tree
Showing 17 changed files with 150 additions and 186 deletions.
3 changes: 2 additions & 1 deletion geoPHP.inc
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,8 @@ class geoPHP

// First char is a tab, space or carriage-return. trim it and try again
if ($bytes[1] == 9 || $bytes[1] == 10 || $bytes[1] == 32) {
return geoPHP::detectFormat(ltrim($input));
$ltinput = ltrim($input);
return geoPHP::detectFormat($ltinput);
}

// Detect WKB or EWKB -- first byte is 1 (little endian indicator)
Expand Down
7 changes: 6 additions & 1 deletion lib/adapters/GeoJSON.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ private function objToGeom($obj) {
}

private function arrayToPoint($array) {
return new Point($array[0], $array[1]);
if (!empty($array)) {
return new Point($array[0], $array[1]);
}
else {
return new Point();
}
}

private function arrayToLineString($array) {
Expand Down
24 changes: 20 additions & 4 deletions lib/adapters/GeoRSS.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ protected function geomFromXML() {

protected function getPointsFromCoords($string) {
$coords = array();

if (empty($string)) {
return $coords;
}

$latlon = explode(' ',$string);
foreach ($latlon as $key => $item) {
if (!($key % 2)) {
Expand All @@ -101,8 +106,15 @@ protected function parsePoints() {
$points = array();
$pt_elements = $this->xmlobj->getElementsByTagName('point');
foreach ($pt_elements as $pt) {
$point_array = $this->getPointsFromCoords(trim($pt->firstChild->nodeValue));
$points[] = $point_array[0];
if ($pt->hasChildNodes()) {
$point_array = $this->getPointsFromCoords(trim($pt->firstChild->nodeValue));
}
if (!empty($point_array)) {
$points[] = $point_array[0];
}
else {
$points[] = new Point();
}
}
return $points;
}
Expand Down Expand Up @@ -188,9 +200,13 @@ protected function geometryToGeoRSS($geom) {
}

private function pointToGeoRSS($geom) {
return '<'.$this->nss.'point>'.$geom->getY().' '.$geom->getX().'</'.$this->nss.'point>';
$out = '<'.$this->nss.'point>';
if (!$geom->isEmpty()) {
$out .= $geom->getY().' '.$geom->getX();
}
$out .= '</'.$this->nss.'point>';
return $out;
}


private function linestringToGeoRSS($geom) {
$output = '<'.$this->nss.'line>';
Expand Down
17 changes: 15 additions & 2 deletions lib/adapters/KML.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,12 @@ protected function childElements($xml, $nodename = '') {

protected function parsePoint($xml) {
$coordinates = $this->_extractCoordinates($xml);
return new Point($coordinates[0][0],$coordinates[0][1]);
if (!empty($coordinates)) {
return new Point($coordinates[0][0],$coordinates[0][1]);
}
else {
return new Point();
}
}

protected function parseLineString($xml) {
Expand All @@ -130,6 +135,9 @@ protected function parsePolygon($xml) {
$components = array();

$outer_boundary_element_a = $this->childElements($xml, 'outerboundaryis');
if (empty($outer_boundary_element_a)) {
return new Polygon(); // It's an empty polygon
}
$outer_boundary_element = $outer_boundary_element_a[0];
$outer_ring_element_a = $this->childElements($outer_boundary_element, 'linearring');
$outer_ring_element = $outer_ring_element_a[0];
Expand Down Expand Up @@ -205,7 +213,12 @@ private function geometryToKML($geom) {
}

private function pointToKML($geom) {
return '<'.$this->nss.'Point><'.$this->nss.'coordinates>'.$geom->getX().",".$geom->getY().'</'.$this->nss.'coordinates></'.$this->nss.'Point>';
$out = '<'.$this->nss.'Point>';
if (!$geom->isEmpty()) {
$out .= '<'.$this->nss.'coordinates>'.$geom->getX().",".$geom->getY().'</'.$this->nss.'coordinates>';
}
$out .= '</'.$this->nss.'Point>';
return $out;
}

private function linestringToKML($geom, $type = FALSE) {
Expand Down
16 changes: 12 additions & 4 deletions lib/adapters/WKB.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,12 @@ function getGeometry(&$mem) {

function getPoint(&$mem) {
$point_coords = unpack("d*", fread($mem,$this->dimension*8));
return new Point($point_coords[1],$point_coords[2]);
if (!empty($point_coords)) {
return new Point($point_coords[1],$point_coords[2]);
}
else {
return new Point(); // EMPTY point
}
}

function getLinstring(&$mem) {
Expand Down Expand Up @@ -198,9 +203,12 @@ public function write(Geometry $geometry, $write_as_hex = FALSE) {

function writePoint($point) {
// Set the coords
$wkb = pack('dd',$point->x(), $point->y());

return $wkb;
if (!$point->isEmpty()) {
$wkb = pack('dd',$point->x(), $point->y());
return $wkb;
} else {
return '';
}
}

function writeLineString($line) {
Expand Down
30 changes: 27 additions & 3 deletions lib/geometry/Point.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,15 @@ class Point extends Geometry
* @param numeric $y The y coordinate (or latitude)
* @param numeric $z The z coordinate (or altitude) - optional
*/
public function __construct($x, $y, $z = NULL) {
public function __construct($x = NULL, $y = NULL, $z = NULL) {

// Check if it's an empty point
if ($x === NULL && $y === NULL) {
$this->coords = array(NULL, NULL);
$this->dimension = 0;
return;
}

// Basic validation on x and y
if (!is_numeric($x) || !is_numeric($y)) {
throw new Exception("Cannot construct Point. x and y should be numeric");
Expand Down Expand Up @@ -119,7 +127,12 @@ public function dimension() {
}

public function isEmpty() {
return FALSE;
if ($this->dimension == 0) {
return TRUE;
}
else {
return FALSE;
}
}

public function numPoints() {
Expand All @@ -131,7 +144,18 @@ public function getPoints() {
}

public function equals($geometry) {
return ($this->x() == $geometry->x() && $this->y() == $geometry->y());
if (get_class($geometry) != 'Point') {
return FALSE;
}
if (!$this->isEmpty() && !$geometry->isEmpty()) {
return ($this->x() == $geometry->x() && $this->y() == $geometry->y());
}
else if ($this->isEmpty() && $geometry->isEmpty()) {
return TRUE;
}
else {
return FALSE;
}
}

public function isSimple() {
Expand Down
5 changes: 5 additions & 0 deletions lib/geometry/Polygon.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ class Polygon extends Collection
{
protected $geom_type = 'Polygon';

// The boundary of a polygin is it's outer ring
public function boundary() {
return $this->exteriorRing();
}

public function area($exterior_only = FALSE, $signed = FALSE) {
if ($this->isEmpty()) return 0;

Expand Down
2 changes: 1 addition & 1 deletion tests/input/box.georss
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<georss:box>42.943 -71.032 43.039 -69.856</georss:box>
<box>42.943 -71.032 43.039 -69.856</box>
2 changes: 1 addition & 1 deletion tests/input/circle.georss
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<georss:circle>42.943 -71.032 500</georss:circle>
<circle>42.943 -71.032 500</circle>
2 changes: 1 addition & 1 deletion tests/input/line.georss
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<georss:line>45.256 -110.45 46.46 -109.48 43.84 -109.86</georss:line>
<line>45.256 -110.45 46.46 -109.48 43.84 -109.86</line>
4 changes: 2 additions & 2 deletions tests/input/polygon.georss
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<georss:polygon>
<polygon>
45.256 -110.45 46.46 -109.48 43.84 -109.86 45.256 -110.45
</georss:polygon>
</polygon>
4 changes: 2 additions & 2 deletions tests/input/track.gpx
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
<ele>4.46</ele>
<time>2009-10-17T18:37:26Z</time>
</trkpt>
<trkpt lat="47.644548" lon="-122.326897">
<trkpt lat="47.644550" lon="-122.326897">
<ele>4.94</ele>
<time>2009-10-17T18:37:31Z</time>
</trkpt>
<trkpt lat="47.644548" lon="-122.326897">
<trkpt lat="47.644552" lon="-122.326899">
<ele>6.87</ele>
<time>2009-10-17T18:37:34Z</time>
</trkpt>
Expand Down
19 changes: 16 additions & 3 deletions tests/test.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
<?php

// Uncomment to test
# run_test();
if (getenv("GEOPHP_RUN_TESTS") == 1) {
run_test();
}
else {
print "Skipping tests. Please set GEOPHP_RUN_TESTS=1 environment variable if you wish to run tests\n";
}

function run_test() {
set_time_limit(0);

set_error_handler("FailOnError");

header("Content-type: text");

include_once('../geoPHP.inc');
Expand All @@ -30,7 +37,7 @@ function run_test() {
test_detection($value, $format, $file);
}
}
print "Testing Done!";
print "\e[32m" . "PASS". "\e[39m\n";
}

function test_geometry($geometry) {
Expand Down Expand Up @@ -238,8 +245,14 @@ function test_detection($value, $format, $file) {
$detected = geoPHP::detectFormat($value);
if ($detected != $format) {
if ($detected) print 'detected as ' . $detected . "\n";
else print "not detected\n";
else print "format not detected\n";
}
// Make sure it loads using auto-detect
geoPHP::load($value);
}

function FailOnError($error_level, $error_message, $error_file, $error_line, $error_context) {
echo "$error_level: $error_message in $error_file on line $error_line\n";
echo "\e[31m" . "FAIL" . "\e[39m\n";
exit(1);
}
108 changes: 0 additions & 108 deletions tests/tests/aliasesTest.php

This file was deleted.

4 changes: 4 additions & 0 deletions tests/tests/geosTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ function setUp() {
}

function testGeos() {
if (!geoPHP::geosInstalled()) {
echo "Skipping GEOS -- not installed";
return;
}
foreach (scandir('./input', SCANDIR_SORT_NONE) as $file) {
$parts = explode('.',$file);
if ($parts[0]) {
Expand Down
Loading

0 comments on commit b91e8ad

Please sign in to comment.