dmBridge PHP API
UNLVSpatialGenericDataStore.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 
00016 abstract class UNLVSpatialGenericDataStore extends DMMySQLDataStore {
00017 
00021    public static function getInsertObjectSQL() {
00022       return "INSERT INTO spatial_object(alias, ptr, lat, `long`, lat_n, lat_s, long_w, long_e)
00023          VALUES(:alias, :ptr, :lat, :long, :lat_n, :lat_s, :long_w, :long_e);";
00024    }
00025 
00029    public static function getNumObjectsInCollectionSQL() {
00030       return "SELECT COUNT(id) AS count
00031          FROM spatial_object
00032          WHERE alias = :alias";
00033    }
00034 
00038    public static function getObjectSQL() {
00039       $select = "SELECT lat, `long`, lat_n, lat_s, long_w, long_e ";
00040       $from = "FROM spatial_object ";
00041       $where = "WHERE alias = :alias AND ptr = :ptr ";
00042       $limit = "LIMIT 1";
00043 
00044       return $select . $from . $where . $limit . ";";
00045    }
00046 
00051    public static function getCountSQL(UNLVSpatialQuery $query) {
00052       $select = "SELECT COUNT(id) AS count ";
00053       $from = "FROM spatial_object ";
00054       $where = self::getWhere($query);    
00055       return $select . $from . $where . ";";
00056    }
00057 
00062    public static function getSQL(UNLVSpatialQuery $query) {
00063       $select = "SELECT *,
00064             (long_e - long_w) * (lat_n - lat_s) AS area,
00065             lat_s + ((lat_n - lat_s) * 0.5) AS clat,
00066             long_w + ((long_e - long_w) * 0.5) AS clong ";
00067       $from = "FROM spatial_object ";
00068       $where = self::getWhere($query);
00069 
00070       $limit = sprintf(" LIMIT %d ", $query->getNumResultsPerPage());
00071       $offset = sprintf("OFFSET %d ",
00072             ($query->getPage() - 1) * $query->getNumResultsPerPage());
00073       
00074       return $select . $from . $where . $limit . $offset . ";";
00075    }
00076 
00077    private static function getWhere(UNLVSpatialQuery $query) {
00078       $where = "WHERE 1=1 ";
00079       foreach ($query->getQueryTerms() as $term) {
00080          switch ($term->getMatchingMode()) {
00081          case "outside":
00082             switch ($term->getExampleObject()->getSpatialModelClassName()) {
00083             case "UNLVSpatialRectObject":
00084                $where .= sprintf("AND ((lat_s > %f OR lat_n < %f)
00085                      OR (long_w > %f OR long_e < %f))",
00086                      $term->getExampleObject()->getSpatialNorthLat(),
00087                      $term->getExampleObject()->getSpatialSouthLat(),
00088                      $term->getExampleObject()->getSpatialEastLong(),
00089                      $term->getExampleObject()->getSpatialWestLong());
00090                break;
00091             case "UNLVSpatialPointObject":
00092                $where .= sprintf("AND (lat > %f OR `long` < %f)",
00093                      $term->getExampleObject()->getSpatialLat(),
00094                      $term->getExampleObject()->getSpatialLong());
00095                break;
00096             }
00097             break;
00098          case "intersect":
00099             switch ($term->getExampleObject()->getSpatialModelClassName()) {
00100             case "UNLVSpatialRectObject":
00101                $where .= sprintf("AND (((long_e >= %f AND long_e <= %f)
00102                         OR (long_w <= %f AND long_w >= %f))
00103                      AND ((lat_n >= %f AND lat_n <= %f)
00104                         OR (lat_s <= %f AND lat_s >= %f)))",
00105                      $term->getExampleObject()->getSpatialWestLong(),
00106                      $term->getExampleObject()->getSpatialEastLong(),
00107                      $term->getExampleObject()->getSpatialEastLong(),
00108                      $term->getExampleObject()->getSpatialWestLong(),
00109                      $term->getExampleObject()->getSpatialSouthLat(),
00110                      $term->getExampleObject()->getSpatialNorthLat(),
00111                      $term->getExampleObject()->getSpatialNorthLat(),
00112                      $term->getExampleObject()->getSpatialSouthLat());
00113                break;
00114             case "UNLVSpatialPointObject":
00115                $where .= sprintf("AND (lat >= %f AND lat <= %f
00116                         AND `long` >= %f AND `long` <= %f)",
00117                      $term->getExampleObject()->getSpatialLat(),
00118                      $term->getExampleObject()->getSpatialLat(),
00119                      $term->getExampleObject()->getSpatialLong(),
00120                      $term->getExampleObject()->getSpatialLong());
00121                break;
00122             }
00123             break;
00124          case "within":
00125             switch ($term->getExampleObject()->getSpatialModelClassName()) {
00126             case "UNLVSpatialRectObject":
00127                $where .= sprintf("AND (lat_s >= %f AND long_w >= %f
00128                         AND lat_n <= %f AND long_e <= %f)",
00129                      $term->getExampleObject()->getSpatialSouthLat(),
00130                      $term->getExampleObject()->getSpatialWestLong(),
00131                      $term->getExampleObject()->getSpatialNorthLat(),
00132                      $term->getExampleObject()->getSpatialEastLong());
00133                break;
00134             case "UNLVSpatialPointObject":
00135                $where .= sprintf("AND (lat >= %f AND lat <= %f
00136                         AND `long` >= %f AND `long` <= %f)",
00137                      $term->getExampleObject()->getSpatialLat(),
00138                      $term->getExampleObject()->getSpatialLat(),
00139                      $term->getExampleObject()->getSpatialLong(),
00140                      $term->getExampleObject()->getSpatialLong());
00141                break;
00142             }
00143             break;
00144          }
00145       }
00146       return $where;
00147    }
00148 
00152    public static function getUpdateObjectSQL() {
00153       return "UPDATE spatial_object
00154          SET lat = :lat,
00155             `long` = :long,
00156             lat_n = :lat_n,
00157             lat_s = :lat_s,
00158             long_w = :long_w,
00159             long_e = :long_e
00160          WHERE alias = :alias AND ptr = :ptr";
00161    }
00162 
00163 }
 All Data Structures Functions Variables