source: src/webservice/tests/SpatialiteTest.php@ b38cc03

Last change on this file since b38cc03 was b38cc03, checked in by Joachim Langenbach <joachim.langenbach@…>, 11 years ago
  • Translated more messages
    • Stripped out test externals, because they are not needed
    • Improved Setup class, to create all needed database tables
    • Smaller general improvements on the test classes
    • Extended the SetupTest extent
  • Property mode set to 100644
File size: 8.3 KB
RevLine 
[8f38d1e]1<?php
2/*
3 Copyright (C) 2015 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 MA 02110-1301 USA.
19*/
20
[8c356c1]21class SpatialiteTest extends PHPUnit_Framework_TestCase
[8f38d1e]22{
23 public static function setUpBeforeClass()
24 {
[8c356c1]25 global $testconfig;
26
27 $file = __DIR__ ."/testconfig/testconfig.php";
28 self::assertFileExists($file);
29 require($file);
30 require_once($testconfig["webserviceDirectory"] ."/SiWeather/Webservice/WebserviceConfig.php");
31 self::$_webserviceConfig = new SiWeather\Webservice\WebserviceConfig();
32 self::assertNotNull(self::$_webserviceConfig);
[b38cc03]33
34 self::$_databaseFile = tempnam(sys_get_temp_dir(), "siw");
35 # PDO SQlite is not able to load extensions (caused by the sqlite pdo driver)
36 # therefore we use PHP SQlite module
37 self::$_database = new SQLite3(self::$_databaseFile);
[8f38d1e]38 }
39
40 public static function tearDownAfterClass()
41 {
[8c356c1]42 self::assertTrue(self::$_database->close());
43 self::assertTrue(unlink(self::$_databaseFile));
[8f38d1e]44 self::$_database = null;
45 self::$_databaseFile = null;
46 }
47
48 public function testLoadSpatialite()
49 {
50 $this->assertInstanceOf("SQLite3", self::$_database);
51
52 # loading SpatiaLite as an extension
[8c356c1]53 if(!self::$_database->loadExtension(self::$_webserviceConfig->getConfig("spatialiteExtension"))){
[8f38d1e]54 echo "Could not load SpatialLite extension: ". self::$_database->lastErrorMsg();
55 return;
56 }
57
58 $this->assertTrue(self::$_database->exec("BEGIN"));
59 $this->assertTrue(self::$_database->exec("SELECT InitSpatialMetadata()"));
60 $this->assertTrue(self::$_database->exec("COMMIT"));
61
62 # reporting some version info
63 $rs = self::$_database->query("SELECT spatialite_version()");
64 $this->assertInstanceOf('SQLite3Result', $rs);
65 $row = $rs->fetchArray();
66 $this->assertInternalType("array", $row);
67 $this->assertGreaterThan(1, count($row));
68 $this->assertTrue(!empty($row[0]));
69 $row = $rs->fetchArray();
70 $this->assertTrue(!$row);
71 }
72
73 public function testDistance()
74 {
75 $this->assertInstanceOf("SQLite3", self::$_database);
76
77 $table = "cities";
78
79 # creating a POINT table
80 $sql = "CREATE TABLE ". $table ." (";
81 $sql .= "id INTEGER NOT NULL PRIMARY KEY,";
82 $sql .= "name TEXT NOT NULL)";
83 $this->assertTrue(self::$_database->exec($sql));
84
85 # creating a POINT Geometry column named center,
86 # taking two coordinates in SRID 4326 (WGS84) format
87 $sql = "SELECT AddGeometryColumn(\"". $table ."\", ";
[8c356c1]88 $sql .= "\"center\", 4326, \"POINT\", \"XY\")";
[8f38d1e]89 $this->assertTrue(self::$_database->exec($sql));
90
91 # insert some cities
92 # the center must be described in WKT (s. http://giswiki.org/wiki/WKT) format for a point.
[8c356c1]93 # Since we use WGS84 with SRID 4326 as a coordinate system, it expects POINT(LONGITUDE LATITUDE)
94 # as values.
[8f38d1e]95 # CITY, LATITUDE, LONGITUDE
[8c356c1]96 # example Berlin:
97 # 52°31′7″N, 13°24′30″E
98 # --> 52.518611° Latitude and 13.408333° Longitude
99 # --> $values[] = array("name" => "Berlin", "latitude" => 52.518611, "longitude" => 13.408333);
100 # --> POINT("13.408333 52.518611")
[8f38d1e]101 $values = array();
[8c356c1]102 $values[] = array("name" => "Berlin", "latitude" => 52.518611, "longitude" => 13.408333);
103 $values[] = array("name" => "Potsdam", "latitude" => 52.395833, "longitude" => 13.061389);
104 $values[] = array("name" => "Hamburg", "latitude" => 53.550556, "longitude" => 9.993333);
105 $values[] = array("name" => "Dresden", "latitude" => 51.049259, "longitude" => 13.73836);
106 $values[] = array("name" => "Cape Town", "latitude" => -33.922667, "longitude" => 18.416689);
107 $values[] = array("name" => "New York City", "latitude" => 40.712778, "longitude" => -74.005833);
108 $values[] = array("name" => "Sydney", "latitude" => -33.85, "longitude" => 151.2);
[8f38d1e]109
110 # distances in meter
111 $distances = array();
112 $distances["Berlin"] = array();
113 $distances["Berlin"]["Potsdam"] = 27185;
114 $distances["Berlin"]["Hamburg"] = 255518;
115 $distances["Berlin"]["Dresden"] = 164954;
116 $distances["Berlin"]["Cape Town"] = 9624137;
117 $distances["Berlin"]["New York City"] = 6385266;
118 $distances["Berlin"]["Sydney"] = 16092238;
119
120 $distances["Potsdam"] = array();
121 $distances["Potsdam"]["Hamburg"] = 242231;
122 $distances["Potsdam"]["Dresden"] = 156823;
123 $distances["Potsdam"]["Cape Town"] =9612288;
124 $distances["Potsdam"]["New York City"] = 6370104;
125 $distances["Potsdam"]["Sydney"] = 16118486;
126
127 $distances["Hamburg"] = array();
128 $distances["Hamburg"]["Dresden"] = 377003;
129 $distances["Hamburg"]["Cape Town"] = 9760489;
130 $distances["Hamburg"]["New York City"] = 6129909;
131 $distances["Hamburg"]["Sydney"] = 16275543;
132
133 $distances["Dresden"] = array();
134 $distances["Dresden"]["Cape Town"] = 9459562;
135 $distances["Dresden"]["New York City"] = 6478589;
136 $distances["Dresden"]["Sydney"] = 16109156;
137
138 $distances["Cape Town"] = array();
139 $distances["Cape Town"]["New York City"] = 12564111;
140 $distances["Cape Town"]["Sydney"] = 11013398;
141
142 $distances["New York City"] = array();
143 $distances["New York City"]["Sydney"] = 15988687;
144
145 $this->assertTrue(self::$_database->exec("BEGIN"));
[8c356c1]146 $sql = "INSERT INTO ". $table ." (name, center) VALUES(:name, GeomFromText(:center, 4326))";
[8f38d1e]147 $sqlStatement = self::$_database->prepare($sql);
148 foreach ($values as $value){
149 $sqlStatement->clear();
150 $this->assertEquals(3, count($value));
[8c356c1]151 $this->assertTrue($sqlStatement->bindValue(":name", $value["name"]));
152 $this->assertTrue($sqlStatement->bindValue(":center", "POINT(". $value["longitude"] ." ". $value["latitude"] .")"));
[8f38d1e]153 $this->assertInstanceOf("SQLite3Result", $sqlStatement->execute());
[8c356c1]154 $sqlStatement->reset();
[8f38d1e]155 }
156 $this->assertTrue(self::$_database->exec("COMMIT"));
157
158 # check the inserts
[8c356c1]159 $sql = "SELECT id, name, X(center) as longitude, Y(center) as latitude FROM ". $table ." WHERE name=:name";
[8f38d1e]160 $sqlStatement = self::$_database->prepare($sql);
161 foreach ($values as $value){
162 $sqlStatement->clear();
163 $this->assertEquals(3, count($value));
[8c356c1]164 $this->assertTrue($sqlStatement->bindValue(":name", $value["name"]));
[8f38d1e]165 $result = $sqlStatement->execute();
166 $this->assertInstanceOf("SQLite3Result", $result);
167 while($row = $result->fetchArray()){
168 $this->assertGreaterThan(4, count($row));
169 $this->assertGreaterThan(-1, $row[0]);
[8c356c1]170 $this->assertEquals($value["name"], $row[1]);
171 $this->assertEquals($value["longitude"], $row[2]);
172 $this->assertEquals($value["latitude"], $row[3]);
[8f38d1e]173 }
[8c356c1]174 $sqlStatement->reset();
[8f38d1e]175 }
176
177 # calculate the distances
178 # Calling Distance() with three parameters, which returns distance in Meter
[8c356c1]179 $sql = "SELECT t1.name, t2.name, Round(Distance(t1.center, t2.center, 1), 0) As distance";
[8f38d1e]180 $sql .= " FROM ". $table ." AS t1, ". $table ." AS t2";
181 $sql .= " WHERE t1.name=:from AND t2.name=:to";
182 $sqlStatement = self::$_database->prepare($sql);
183 foreach(array_keys($distances) as $start){
184 foreach(array_keys($distances[$start]) as $end){
185 $sqlStatement->clear();
186 # Calculate distance from start --> end
187 $this->assertTrue($sqlStatement->bindValue(":from", $start));
188 $this->assertTrue($sqlStatement->bindValue(":to", $end));
189 $result = $sqlStatement->execute();
190 $this->assertInstanceOf("SQLite3Result", $result);
191 while($row = $result->fetchArray()){
192 $this->assertGreaterThan(3, count($row));
193 $this->assertEquals($start, $row[0]);
194 $this->assertEquals($end, $row[1]);
[8c356c1]195 # 0.5% difference of the distance is accepted
196 $delta = abs($distances[$start][$end] - $row[2])/$distances[$start][$end];
197 $this->assertLessThan(0.005, $delta, $start ." --> ". $end .":\n Expected: ". $distances[$start][$end] ."\n Is: ". $row[2]);
[8f38d1e]198 }
[8c356c1]199 $sqlStatement->reset();
[8f38d1e]200 }
201 }
202 }
203
204 protected static $_databaseFile;
205 protected static $_database;
[8c356c1]206 protected static $_webserviceConfig;
[8f38d1e]207}
208?>
Note: See TracBrowser for help on using the repository browser.