close Warning: AdminModule failed with TracError: Unable to instantiate component <class 'trac.admin.web_ui.BasicsAdminPanel'> (super(type, obj): obj (type BasicsAdminPanel) is not an instance or subtype of type (Markup).)

source: src/webservice/tests/TestSpatiallite.php@ 8f38d1e

Last change on this file since 8f38d1e was 8f38d1e, checked in by Joachim Langenbach <joachim.langenbach@…>, 11 years ago

PHP test for sqlite spatalite functionalities

  • Property mode set to 100644
File size: 6.9 KB
Line 
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
21class TestSpatialiteExtension extends PHPUnit_Framework_TestCase
22{
23 public static function setUpBeforeClass()
24 {
25 self::$_databaseFile = tempnam(sys_get_temp_dir(), "siw");
26 # PDO SQlite is not able to load extensions (caused by the sqlite pdo driver)
27 # therefore we use PHP SQlite module
28 self::$_database = new SQLite3(self::$_databaseFile);
29 }
30
31 public static function tearDownAfterClass()
32 {
33 self::$_database->close();
34 unlink(self::$_databaseFile);
35 self::$_database = null;
36 self::$_databaseFile = null;
37 }
38
39 public function testLoadSpatialite()
40 {
41 $this->assertInstanceOf("SQLite3", self::$_database);
42
43 # loading SpatiaLite as an extension
44 if(!self::$_database->loadExtension("mod_spatialite.so")){
45 echo "Could not load SpatialLite extension: ". self::$_database->lastErrorMsg();
46 return;
47 }
48
49 $this->assertTrue(self::$_database->exec("BEGIN"));
50 $this->assertTrue(self::$_database->exec("SELECT InitSpatialMetadata()"));
51 $this->assertTrue(self::$_database->exec("COMMIT"));
52
53 # reporting some version info
54 $rs = self::$_database->query("SELECT spatialite_version()");
55 $this->assertInstanceOf('SQLite3Result', $rs);
56 $row = $rs->fetchArray();
57 $this->assertInternalType("array", $row);
58 $this->assertGreaterThan(1, count($row));
59 $this->assertTrue(!empty($row[0]));
60 $row = $rs->fetchArray();
61 $this->assertTrue(!$row);
62 }
63
64 public function testDistance()
65 {
66 $this->assertInstanceOf("SQLite3", self::$_database);
67
68 $table = "cities";
69
70 # creating a POINT table
71 $sql = "CREATE TABLE ". $table ." (";
72 $sql .= "id INTEGER NOT NULL PRIMARY KEY,";
73 $sql .= "name TEXT NOT NULL)";
74 $this->assertTrue(self::$_database->exec($sql));
75
76 # creating a POINT Geometry column named center,
77 # taking two coordinates in SRID 4326 (WGS84) format
78 $sql = "SELECT AddGeometryColumn(\"". $table ."\", ";
79 $sql .= "\"center\", 32632, \"POINT\", \"XY\")";
80 $this->assertTrue(self::$_database->exec($sql));
81
82 # insert some cities
83 # 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,
85 # CITY, LATITUDE, LONGITUDE
86 $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);
94
95 # distances in meter
96 $distances = array();
97 $distances["Berlin"] = array();
98 $distances["Berlin"]["Potsdam"] = 27185;
99 $distances["Berlin"]["Hamburg"] = 255518;
100 $distances["Berlin"]["Dresden"] = 164954;
101 $distances["Berlin"]["Cape Town"] = 9624137;
102 $distances["Berlin"]["New York City"] = 6385266;
103 $distances["Berlin"]["Sydney"] = 16092238;
104
105 $distances["Potsdam"] = array();
106 $distances["Potsdam"]["Hamburg"] = 242231;
107 $distances["Potsdam"]["Dresden"] = 156823;
108 $distances["Potsdam"]["Cape Town"] =9612288;
109 $distances["Potsdam"]["New York City"] = 6370104;
110 $distances["Potsdam"]["Sydney"] = 16118486;
111
112 $distances["Hamburg"] = array();
113 $distances["Hamburg"]["Dresden"] = 377003;
114 $distances["Hamburg"]["Cape Town"] = 9760489;
115 $distances["Hamburg"]["New York City"] = 6129909;
116 $distances["Hamburg"]["Sydney"] = 16275543;
117
118 $distances["Dresden"] = array();
119 $distances["Dresden"]["Cape Town"] = 9459562;
120 $distances["Dresden"]["New York City"] = 6478589;
121 $distances["Dresden"]["Sydney"] = 16109156;
122
123 $distances["Cape Town"] = array();
124 $distances["Cape Town"]["New York City"] = 12564111;
125 $distances["Cape Town"]["Sydney"] = 11013398;
126
127 $distances["New York City"] = array();
128 $distances["New York City"]["Sydney"] = 15988687;
129
130 $this->assertTrue(self::$_database->exec("BEGIN"));
131 $sql = "INSERT INTO ". $table ." (name, center) VALUES(:name, GeomFromText(:center, 32632))";
132 $sqlStatement = self::$_database->prepare($sql);
133 foreach ($values as $value){
134 $sqlStatement->clear();
135 $this->assertEquals(3, count($value));
136 $this->assertTrue($sqlStatement->bindValue(":name", $value[0]));
137 $this->assertTrue($sqlStatement->bindValue(":center", "POINT(". $value[1] ." ". $value[2] .")"));
138 $this->assertInstanceOf("SQLite3Result", $sqlStatement->execute());
139 }
140 $this->assertTrue(self::$_database->exec("COMMIT"));
141
142 # check the inserts
143 $sql = "SELECT id, name, X(center), Y(center) FROM ". $table ." WHERE name=:name";
144 $sqlStatement = self::$_database->prepare($sql);
145 foreach ($values as $value){
146 $sqlStatement->clear();
147 $this->assertEquals(3, count($value));
148 $this->assertTrue($sqlStatement->bindValue(":name", $value[0]));
149 $result = $sqlStatement->execute();
150 $this->assertInstanceOf("SQLite3Result", $result);
151 while($row = $result->fetchArray()){
152 $this->assertGreaterThan(4, count($row));
153 $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]);
157 }
158 }
159
160 # calculate the distances
161 # Calling Distance() with three parameters, which returns distance in Meter
162 $sql = "SELECT t1.name, t2.name, Distance(t1.center, t2.center) As distance";
163 $sql .= " FROM ". $table ." AS t1, ". $table ." AS t2";
164 $sql .= " WHERE t1.name=:from AND t2.name=:to";
165 $sqlStatement = self::$_database->prepare($sql);
166 foreach(array_keys($distances) as $start){
167 foreach(array_keys($distances[$start]) as $end){
168 $sqlStatement->clear();
169 # Calculate distance from start --> end
170 $this->assertTrue($sqlStatement->bindValue(":from", $start));
171 $this->assertTrue($sqlStatement->bindValue(":to", $end));
172 $result = $sqlStatement->execute();
173 $this->assertInstanceOf("SQLite3Result", $result);
174 while($row = $result->fetchArray()){
175 $this->assertGreaterThan(3, count($row));
176 $this->assertEquals($start, $row[0]);
177 $this->assertEquals($end, $row[1]);
178 $this->assertEquals($distances[$start][$end], $row[2], $start ." --> ". $end);
179 }
180 }
181 }
182 }
183
184 protected static $_databaseFile;
185 protected static $_database;
186}
187?>
Note: See TracBrowser for help on using the repository browser.