Changeset 8c356c1 for src/webservice/tests/SpatialiteTest.php
- Timestamp:
- Aug 26, 2015, 8:14:40 PM (11 years ago)
- Branches:
- master
- Children:
- b38cc03
- Parents:
- 8f38d1e
- File:
-
- 1 moved
-
src/webservice/tests/SpatialiteTest.php (moved) (moved from src/webservice/tests/TestSpatiallite.php ) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/webservice/tests/SpatialiteTest.php
r8f38d1e r8c356c1 19 19 */ 20 20 21 class TestSpatialiteExtensionextends PHPUnit_Framework_TestCase21 class SpatialiteTest extends PHPUnit_Framework_TestCase 22 22 { 23 23 public static function setUpBeforeClass() 24 24 { 25 global $testconfig; 26 25 27 self::$_databaseFile = tempnam(sys_get_temp_dir(), "siw"); 26 28 # PDO SQlite is not able to load extensions (caused by the sqlite pdo driver) 27 29 # therefore we use PHP SQlite module 28 30 self::$_database = new SQLite3(self::$_databaseFile); 31 32 $file = __DIR__ ."/testconfig/testconfig.php"; 33 self::assertFileExists($file); 34 require($file); 35 require_once($testconfig["webserviceDirectory"] ."/SiWeather/Webservice/WebserviceConfig.php"); 36 37 self::$_webserviceConfig = new SiWeather\Webservice\WebserviceConfig(); 38 self::assertNotNull(self::$_webserviceConfig); 29 39 } 30 40 31 41 public static function tearDownAfterClass() 32 42 { 33 self:: $_database->close();34 unlink(self::$_databaseFile);43 self::assertTrue(self::$_database->close()); 44 self::assertTrue(unlink(self::$_databaseFile)); 35 45 self::$_database = null; 36 46 self::$_databaseFile = null; … … 42 52 43 53 # loading SpatiaLite as an extension 44 if(!self::$_database->loadExtension( "mod_spatialite.so")){54 if(!self::$_database->loadExtension(self::$_webserviceConfig->getConfig("spatialiteExtension"))){ 45 55 echo "Could not load SpatialLite extension: ". self::$_database->lastErrorMsg(); 46 56 return; … … 77 87 # taking two coordinates in SRID 4326 (WGS84) format 78 88 $sql = "SELECT AddGeometryColumn(\"". $table ."\", "; 79 $sql .= "\"center\", 32632, \"POINT\", \"XY\")";89 $sql .= "\"center\", 4326, \"POINT\", \"XY\")"; 80 90 $this->assertTrue(self::$_database->exec($sql)); 81 91 82 92 # insert some cities 83 93 # the center must be described in WKT (s. http://giswiki.org/wiki/WKT) format for a point. 84 # since we use WGS84 as a coordinate system, 94 # Since we use WGS84 with SRID 4326 as a coordinate system, it expects POINT(LONGITUDE LATITUDE) 95 # as values. 85 96 # CITY, LATITUDE, LONGITUDE 97 # example Berlin: 98 # 52°31′7″N, 13°24′30″E 99 # --> 52.518611° Latitude and 13.408333° Longitude 100 # --> $values[] = array("name" => "Berlin", "latitude" => 52.518611, "longitude" => 13.408333); 101 # --> POINT("13.408333 52.518611") 86 102 $values = array(); 87 $values[] = array(" Berlin", 52.518611,13.408333);88 $values[] = array(" Potsdam", 52.395833,13.061389);89 $values[] = array(" Hamburg", 53.550556,9.993333);90 $values[] = array(" Dresden", 51.049259,13.73836);91 $values[] = array(" Cape Town", -33.922667,18.416689);92 $values[] = array(" New York City", 40.712778,-74.005833);93 $values[] = array(" Sydney", -33.85,151.2);103 $values[] = array("name" => "Berlin", "latitude" => 52.518611, "longitude" => 13.408333); 104 $values[] = array("name" => "Potsdam", "latitude" => 52.395833, "longitude" => 13.061389); 105 $values[] = array("name" => "Hamburg", "latitude" => 53.550556, "longitude" => 9.993333); 106 $values[] = array("name" => "Dresden", "latitude" => 51.049259, "longitude" => 13.73836); 107 $values[] = array("name" => "Cape Town", "latitude" => -33.922667, "longitude" => 18.416689); 108 $values[] = array("name" => "New York City", "latitude" => 40.712778, "longitude" => -74.005833); 109 $values[] = array("name" => "Sydney", "latitude" => -33.85, "longitude" => 151.2); 94 110 95 111 # distances in meter … … 129 145 130 146 $this->assertTrue(self::$_database->exec("BEGIN")); 131 $sql = "INSERT INTO ". $table ." (name, center) VALUES(:name, GeomFromText(:center, 32632))";147 $sql = "INSERT INTO ". $table ." (name, center) VALUES(:name, GeomFromText(:center, 4326))"; 132 148 $sqlStatement = self::$_database->prepare($sql); 133 149 foreach ($values as $value){ 134 150 $sqlStatement->clear(); 135 151 $this->assertEquals(3, count($value)); 136 $this->assertTrue($sqlStatement->bindValue(":name", $value[ 0]));137 $this->assertTrue($sqlStatement->bindValue(":center", "POINT(". $value[ 1] ." ". $value[2] .")"));152 $this->assertTrue($sqlStatement->bindValue(":name", $value["name"])); 153 $this->assertTrue($sqlStatement->bindValue(":center", "POINT(". $value["longitude"] ." ". $value["latitude"] .")")); 138 154 $this->assertInstanceOf("SQLite3Result", $sqlStatement->execute()); 155 $sqlStatement->reset(); 139 156 } 140 157 $this->assertTrue(self::$_database->exec("COMMIT")); 141 158 142 159 # check the inserts 143 $sql = "SELECT id, name, X(center) , Y(center)FROM ". $table ." WHERE name=:name";160 $sql = "SELECT id, name, X(center) as longitude, Y(center) as latitude FROM ". $table ." WHERE name=:name"; 144 161 $sqlStatement = self::$_database->prepare($sql); 145 162 foreach ($values as $value){ 146 163 $sqlStatement->clear(); 147 164 $this->assertEquals(3, count($value)); 148 $this->assertTrue($sqlStatement->bindValue(":name", $value[ 0]));165 $this->assertTrue($sqlStatement->bindValue(":name", $value["name"])); 149 166 $result = $sqlStatement->execute(); 150 167 $this->assertInstanceOf("SQLite3Result", $result); … … 152 169 $this->assertGreaterThan(4, count($row)); 153 170 $this->assertGreaterThan(-1, $row[0]); 154 $this->assertEquals($value[ 0], $row[1]);155 $this->assertEquals($value[ 1], $row[2]);156 $this->assertEquals($value[ 2], $row[3]);171 $this->assertEquals($value["name"], $row[1]); 172 $this->assertEquals($value["longitude"], $row[2]); 173 $this->assertEquals($value["latitude"], $row[3]); 157 174 } 175 $sqlStatement->reset(); 158 176 } 159 177 160 178 # calculate the distances 161 179 # Calling Distance() with three parameters, which returns distance in Meter 162 $sql = "SELECT t1.name, t2.name, Distance(t1.center, t2.center) As distance";180 $sql = "SELECT t1.name, t2.name, Round(Distance(t1.center, t2.center, 1), 0) As distance"; 163 181 $sql .= " FROM ". $table ." AS t1, ". $table ." AS t2"; 164 182 $sql .= " WHERE t1.name=:from AND t2.name=:to"; … … 176 194 $this->assertEquals($start, $row[0]); 177 195 $this->assertEquals($end, $row[1]); 178 $this->assertEquals($distances[$start][$end], $row[2], $start ." --> ". $end); 196 # 0.5% difference of the distance is accepted 197 $delta = abs($distances[$start][$end] - $row[2])/$distances[$start][$end]; 198 $this->assertLessThan(0.005, $delta, $start ." --> ". $end .":\n Expected: ". $distances[$start][$end] ."\n Is: ". $row[2]); 179 199 } 200 $sqlStatement->reset(); 180 201 } 181 202 } … … 184 205 protected static $_databaseFile; 185 206 protected static $_database; 207 protected static $_webserviceConfig; 186 208 } 187 209 ?>
Note:
See TracChangeset
for help on using the changeset viewer.
![(please configure the [header_logo] section in trac.ini)](/siweather/chrome/site/your_project_logo.png)