dmBridge PHP API
UNLVSpatialSQLiteDataStore.php
00001 <?php
00002 #
00003 # UNLVSpatial: a dmBridge module for spatial content
00004 #
00005 # Copyright © 2011 Board of Regents of the Nevada System of Higher
00006 # Education, on behalf of the University of Nevada, Las Vegas
00007 #
00008 
00013 class UNLVSpatialSQLiteDataStore extends DMSQLiteDataStore {
00014 
00015    private static $instance;
00016 
00022    public static function getSetupSQL() {
00023       return array(
00024          'CREATE TABLE "spatial_object" (
00025             "id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL ,
00026             "alias" VARCHAR NOT NULL,
00027             "ptr" INTEGER NOT NULL,
00028             "lat" FLOAT,
00029             "long" FLOAT,
00030             "lat_s" FLOAT,
00031             "lat_n" FLOAT,
00032             "long_w" FLOAT,
00033             "long_e" FLOAT,
00034             "timestamp" DATETIME DEFAULT CURRENT_TIMESTAMP
00035          );'
00036       );
00037    }
00038 
00044    public static function getUpgradeSQL() {
00045       return array(
00046          // currently empty because there is no previous version from which
00047          // to upgrade.
00048       );
00049    }
00050 
00055    public static function getInstance() {
00056       if (!self::$instance instanceof self) {
00057          self::$instance = new self;
00058       }
00059       return self::$instance;
00060    }
00061 
00066    public function getNumObjectsInCollection($col) {
00067       $sql = UNLVSpatialGenericDataStore::getNumObjectsInCollectionSQL();
00068       $params = array(':alias' => $col->getAlias());
00069       $result = $this->read($sql, $params);
00070       return (int) $result[0]['count'];
00071    }
00072 
00082    public function getObject(DMCollection $col, $ptr) {
00083       $sql = UNLVSpatialGenericDataStore::getObjectSQL();
00084       $params = array(
00085          ':alias' => $col->getAlias(),
00086          ':ptr' => $ptr
00087       );
00088 
00089       $result = $this->read($sql, $params);
00090 
00091       if (count($result)) {
00092          if ($result[0]['lat_n']) { // it's a box
00093             $sp_obj = new UNLVSpatialRectObject();
00094             $sp_obj->setSpatialDataSource("local");
00095             $sp_obj->setSpatialNorthLat($result[0]['lat_n']);
00096             $sp_obj->setSpatialSouthLat($result[0]['lat_s']);
00097             $sp_obj->setSpatialWestLong($result[0]['long_w']);
00098             $sp_obj->setSpatialEastLong($result[0]['long_e']);
00099             return $sp_obj;
00100          } else if ($result[0]['lat']) { // it's a point
00101             $sp_obj = new UNLVSpatialPointObject();
00102             $sp_obj->setSpatialDataSource("local");
00103             $sp_obj->setSpatialLat($result[0]['lat']);
00104             $sp_obj->setSpatialLong($result[0]['long']);
00105             return $sp_obj;
00106          }
00107       }
00108       return null;
00109    }
00110 
00114    public function deleteAllObjectsInCollection(DMCollection $col) {
00115       $params = array(
00116          ':alias' => $col->getAlias()
00117       );
00118       $sql = "DELETE FROM spatial_object WHERE alias = :alias";
00119       $this->write($sql, $params);
00120    }
00121 
00126    public function deleteObject(DMObject $obj) {
00127       $sql = "DELETE FROM spatial_object
00128          WHERE alias = :alias AND ptr = :ptr";
00129       $params = array(
00130          ':alias' => $obj->getCollection()->getAlias(),
00131          ':ptr' => $obj->getPtr()
00132       );
00133       $this->write($sql, $params);
00134    }
00135 
00140    public function updateObject(DMObject $obj) {
00141       // decide whether to insert or to update
00142       $sql = "SELECT COUNT(id) AS count
00143          FROM spatial_object
00144          WHERE alias = :alias AND ptr = :ptr";
00145       $params = array(
00146          ':alias' => $obj->getCollection()->getAlias(),
00147          ':ptr' => $obj->getPtr()
00148       );
00149       $result = $this->read($sql, $params);
00150 
00151       if ($result[0]['count'] > 0) { // update
00152          $sql = UNLVSpatialGenericDataStore::getUpdateObjectSQL();
00153       } else { // insert
00154          $sql = UNLVSpatialGenericDataStore::getInsertObjectSQL();
00155       }
00156 
00157       $params = array(
00158          ':alias' => $obj->getCollection()->getAlias(),
00159          ':ptr' => $obj->getPtr(),
00160          ':lat' => null,
00161          ':long' => null,
00162          ':lat_n' => null,
00163          ':lat_s' => null,
00164          ':long_w' => null,
00165          ':long_e' => null
00166       );
00167       switch ($obj->getSpatialModelClassName()) {
00168       case "UNLVSpatialPointObject":
00169          $params[':lat'] = $obj->getSpatialLat();
00170          $params[':long'] = $obj->getSpatialLong();
00171          break;
00172       case "UNLVSpatialRectObject":
00173          $params[':lat_n'] = $obj->getSpatialNorthLat();
00174          $params[':lat_s'] = $obj->getSpatialSouthLat();
00175          $params[':long_w'] = $obj->getSpatialWestLong();
00176          $params[':long_e'] = $obj->getSpatialEastLong();
00177          break;
00178       }
00179 
00180       $this->write($sql, $params);
00181    }
00182 
00187    public function getNumObjectsMatchingQuery(UNLVSpatialQuery $query) {
00188       $sql = UNLVSpatialGenericDataStore::getCountSQL($query);
00189       $result = $this->read($sql, array());
00190       return $result[0]['count'];
00191    }
00192 
00197    public function getObjectsMatchingQuery(UNLVSpatialQuery $query) {
00198       $sql = UNLVSpatialGenericDataStore::getSQL($query);
00199       $objects = array();
00200       foreach ($this->read($sql, array()) as $result) {
00201          $collection = DMCollectionFactory::getCollection($result['alias']);
00202          $obj = DMObjectFactory::getObject($collection, $result['ptr']);
00203 
00204          if ($result['lat_n']) { // it's a rect
00205             $sp_obj = new UNLVSpatialRectObject();
00206             $sp_obj->setSpatialNorthLat($result['lat_n']);
00207             $sp_obj->setSpatialSouthLat($result['lat_s']);
00208             $sp_obj->setSpatialWestLong($result['long_w']);
00209             $sp_obj->setSpatialEastLong($result['long_e']);
00210             $obj->addAssociatedModel($sp_obj);
00211          } else { // it's a point
00212             $sp_obj = new UNLVSpatialPointObject();
00213             $sp_obj->setSpatialLat($result['lat']);
00214             $sp_obj->setSpatialLong($result['long']);
00215             $obj->addAssociatedModel($sp_obj);
00216          }
00217 
00218          $objects[] = $obj;
00219       }
00220       return $objects;
00221    }
00222 
00223 }
 All Data Structures Functions Variables