source: src/webservice/tests/SpatialiteTest.php@ 8c356c1

Last change on this file since 8c356c1 was 8c356c1, checked in by Joachim Langenbach <joachim.langenbach@…>, 11 years ago
  • Finished setup
    • Added tests for setup
    • Added translation to setup
  • 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
[8f38d1e]27 self::$_databaseFile = tempnam(sys_get_temp_dir(), "siw");
28 # PDO SQlite is not able to load extensions (caused by the sqlite pdo driver)
29 # therefore we use PHP SQlite module
30 self::$_database = new SQLite3(self::$_databaseFile);
[8c356c1]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);
[8f38d1e]39 }
40
41 public static function tearDownAfterClass()
42 {
[8c356c1]43 self::assertTrue(self::$_database->close());
44 self::assertTrue(unlink(self::$_databaseFile));
[8f38d1e]45 self::$_database = null;
46 self::$_databaseFile = null;
47 }
48
49 public function testLoadSpatialite()
50 {
51 $this->assertInstanceOf("SQLite3", self::$_database);
52
53 # loading SpatiaLite as an extension
[8c356c1]54 if(!self::$_database->loadExtension(self::$_webserviceConfig->getConfig("spatialiteExtension"))){
[8f38d1e]55 echo "Could not load SpatialLite extension: ". self::$_database->lastErrorMsg();
56 return;
57 }
58
59 $this->assertTrue(self::$_database->exec("BEGIN"));
60 $this->assertTrue(self::$_database->exec("SELECT InitSpatialMetadata()"));
61 $this->assertTrue(self::$_database->exec("COMMIT"));
62
63 # reporting some version info
64 $rs = self::$_database->query("SELECT spatialite_version()");
65 $this->assertInstanceOf('SQLite3Result', $rs);
66 $row = $rs->fetchArray();
67 $this->assertInternalType("array", $row);
68 $this->assertGreaterThan(1, count($row));
69 $this->assertTrue(!empty($row[0]));
70 $row = $rs->fetchArray();
71 $this->assertTrue(!$row);
72 }
73
74 public function testDistance()
75 {
76 $this->assertInstanceOf("SQLite3", self::$_database);
77
78 $table = "cities";
79
80 # creating a POINT table
81 $sql = "CREATE TABLE ". $table ." (";
82 $sql .= "id INTEGER NOT NULL PRIMARY KEY,";
83 $sql .= "name TEXT NOT NULL)";
84 $this->assertTrue(self::$_database->exec($sql));
85
86 # creating a POINT Geometry column named center,
87 # taking two coordinates in SRID 4326 (WGS84) format
88 $sql = "SELECT AddGeometryColumn(\"". $table ."\", ";
[8c356c1]89 $sql .= "\"center\", 4326, \"POINT\", \"XY\")";
[8f38d1e]90 $this->assertTrue(self::$_database->exec($sql));
91
92 # insert some cities
93 # the center must be described in WKT (s. http://giswiki.org/wiki/WKT) format for a point.
[8c356c1]94 # Since we use WGS84 with SRID 4326 as a coordinate system, it expects POINT(LONGITUDE LATITUDE)
95 # as values.
[8f38d1e]96 # CITY, LATITUDE, LONGITUDE
[8c356c1]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")
[8f38d1e]102 $values = array();
[8c356c1]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);
[8f38d1e]110
111 # distances in meter
112 $distances = array();
113 $distances["Berlin"] = array();
114 $distances["Berlin"]["Potsdam"] = 27185;
115 $distances["Berlin"]["Hamburg"] = 255518;
116 $distances["Berlin"]["Dresden"] = 164954;
117 $distances["Berlin"]["Cape Town"] = 9624137;
118 $distances["Berlin"]["New York City"] = 6385266;
119 $distances["Berlin"]["Sydney"] = 16092238;
120
121 $distances["Potsdam"] = array();
122 $distances["Potsdam"]["Hamburg"] = 242231;
123 $distances["Potsdam"]["Dresden"] = 156823;
124 $distances["Potsdam"]["Cape Town"] =9612288;
125 $distances["Potsdam"]["New York City"] = 6370104;
126 $distances["Potsdam"]["Sydney"] = 16118486;
127
128 $distances["Hamburg"] = array();
129 $distances["Hamburg"]["Dresden"] = 377003;
130 $distances["Hamburg"]["Cape Town"] = 9760489;
131 $distances["Hamburg"]["New York City"] = 6129909;
132 $distances["Hamburg"]["Sydney"] = 16275543;
133
134 $distances["Dresden"] = array();
135 $distances["Dresden"]["Cape Town"] = 9459562;
136 $distances["Dresden"]["New York City"] = 6478589;
137 $distances["Dresden"]["Sydney"] = 16109156;
138
139 $distances["Cape Town"] = array();
140 $distances["Cape Town"]["New York City"] = 12564111;
141 $distances["Cape Town"]["Sydney"] = 11013398;
142
143 $distances["New York City"] = array();
144 $distances["New York City"]["Sydney"] = 15988687;
145
146 $this->assertTrue(self::$_database->exec("BEGIN"));
[8c356c1]147 $sql = "INSERT INTO ". $table ." (name, center) VALUES(:name, GeomFromText(:center, 4326))";
[8f38d1e]148 $sqlStatement = self::$_database->prepare($sql);
149 foreach ($values as $value){
150 $sqlStatement->clear();
151 $this->assertEquals(3, count($value));
[8c356c1]152 $this->assertTrue($sqlStatement->bindValue(":name", $value["name"]));
153 $this->assertTrue($sqlStatement->bindValue(":center", "POINT(". $value["longitude"] ." ". $value["latitude"] .")"));
[8f38d1e]154 $this->assertInstanceOf("SQLite3Result", $sqlStatement->execute());
[8c356c1]155 $sqlStatement->reset();
[8f38d1e]156 }
157 $this->assertTrue(self::$_database->exec("COMMIT"));
158
159 # check the inserts
[8c356c1]160 $sql = "SELECT id, name, X(center) as longitude, Y(center) as latitude FROM ". $table ." WHERE name=:name";
[8f38d1e]161 $sqlStatement = self::$_database->prepare($sql);
162 foreach ($values as $value){
163 $sqlStatement->clear();
164 $this->assertEquals(3, count($value));
[8c356c1]165 $this->assertTrue($sqlStatement->bindValue(":name", $value["name"]));
[8f38d1e]166 $result = $sqlStatement->execute();
167 $this->assertInstanceOf("SQLite3Result", $result);
168 while($row = $result->fetchArray()){
169 $this->assertGreaterThan(4, count($row));
170 $this->assertGreaterThan(-1, $row[0]);
[8c356c1]171 $this->assertEquals($value["name"], $row[1]);
172 $this->assertEquals($value["longitude"], $row[2]);
173 $this->assertEquals($value["latitude"], $row[3]);
[8f38d1e]174 }
[8c356c1]175 $sqlStatement->reset();
[8f38d1e]176 }
177
178 # calculate the distances
179 # Calling Distance() with three parameters, which returns distance in Meter
[8c356c1]180 $sql = "SELECT t1.name, t2.name, Round(Distance(t1.center, t2.center, 1), 0) As distance";
[8f38d1e]181 $sql .= " FROM ". $table ." AS t1, ". $table ." AS t2";
182 $sql .= " WHERE t1.name=:from AND t2.name=:to";
183 $sqlStatement = self::$_database->prepare($sql);
184 foreach(array_keys($distances) as $start){
185 foreach(array_keys($distances[$start]) as $end){
186 $sqlStatement->clear();
187 # Calculate distance from start --> end
188 $this->assertTrue($sqlStatement->bindValue(":from", $start));
189 $this->assertTrue($sqlStatement->bindValue(":to", $end));
190 $result = $sqlStatement->execute();
191 $this->assertInstanceOf("SQLite3Result", $result);
192 while($row = $result->fetchArray()){
193 $this->assertGreaterThan(3, count($row));
194 $this->assertEquals($start, $row[0]);
195 $this->assertEquals($end, $row[1]);
[8c356c1]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]);
[8f38d1e]199 }
[8c356c1]200 $sqlStatement->reset();
[8f38d1e]201 }
202 }
203 }
204
205 protected static $_databaseFile;
206 protected static $_database;
[8c356c1]207 protected static $_webserviceConfig;
[8f38d1e]208}
209?>
Note: See TracBrowser for help on using the repository browser.