dmBridge PHP API
UNLVSpatialObjectFactory.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 
00022 abstract class UNLVSpatialObjectFactory {
00023 
00037    public static function spatializeObject(DMObject $obj) {
00038       try {
00039          $obj->getSpatialModelClassName();
00040          // if that worked, the object is already spatialized
00041          return;
00042       } catch (DMNoSuchMethodException $e) {
00043          // object has no bounds currently associated with it, so continue
00044       }
00045 
00046       // First, attempt to get DCMI Box bounds data from the object's
00047       // metadata. We don't know for sure the nick of the field it's in, so
00048       // try them all.
00049       foreach ($obj->getMetadata() as $f) {
00050          $lat = $long = $lat_n = $lat_s = $long_w = $long_e = null;
00051          self::parseDCMIPointBounds($f, $lat, $long);
00052          self::parseDCMIBoxBounds($f, $lat_n, $lat_s, $long_w, $long_e);
00053          if (!is_null($lat) || !is_null($long)) { // it's a point
00054             $sp_obj = new UNLVSpatialPointObject();
00055             $sp_obj->setSpatialDataSource("metadata");
00056             $sp_obj->setSpatialLat(!is_null($lat) ? $lat : -90);
00057             $sp_obj->setSpatialLong(!is_null($long) ? $long : 180);
00058             $obj->addAssociatedModel($sp_obj);
00059             return;
00060          } else if (!is_null($lat_n) || !is_null($lat_s)
00061                || !is_null($long_w) || !is_null($long_e)) {
00062             $sp_obj = new UNLVSpatialRectObject();
00063             $sp_obj->setSpatialDataSource("metadata");
00064             $sp_obj->setSpatialNorthLat(!is_null($lat_n) ? $lat_n : 90);
00065             $sp_obj->setSpatialSouthLat(!is_null($lat_s) ? $lat_s : -90);
00066             $sp_obj->setSpatialWestLong(!is_null($long_w) ? $long_w : -180);
00067             $sp_obj->setSpatialEastLong(!is_null($long_e) ? $long_e : 180);
00068             $obj->addAssociatedModel($sp_obj);
00069             return;
00070          }
00071       }
00072 
00073       // The above failed, so try to fall back on UNLVSpatial data
00074       $ds = UNLVSpatial::getDataStore();
00075       $sp_obj = $ds->getObject($obj->getCollection(), $obj->getPtr());
00076       if ($sp_obj) {
00077          $obj->addAssociatedModel($sp_obj);
00078          return;
00079       }
00080    }
00081 
00095    private static function parseDCMIBoxBounds(DMDCElement $field,
00096          &$lat_n, &$lat_s, &$long_w, &$long_e) {
00097       $encoded_bounds = $field->getValue();
00098 
00099       // separate by semicolons
00100       $parts = explode(";", $encoded_bounds);
00101       $bounds = array();
00102       foreach ($parts as $part) {
00103          // split into key-value pairs
00104          $kv = explode("=", $part);
00105          if (count($kv) == 2) {
00106             $bounds[trim($kv[0])] = trim($kv[1]);
00107          }
00108       }
00109 
00110       if (array_key_exists("units", $bounds)) {
00111          if ($bounds['units'] && $bounds['units'] != "signed decimal degrees") {
00112             throw new DMException("Unsupported boundary units");
00113          }
00114       }
00115 
00116       $n = $s = $e = $w = null;
00117       if (array_key_exists("northlimit", $bounds)) {
00118          $n = (float) $bounds['northlimit'];
00119       }
00120       if (array_key_exists("southlimit", $bounds)) {
00121          $s = (float) $bounds['southlimit'];
00122       }
00123       if (array_key_exists("westlimit", $bounds)) {
00124          $w = (float) $bounds['westlimit'];
00125       }
00126       if (array_key_exists("eastlimit", $bounds)) {
00127          $e = (float) $bounds['eastlimit'];
00128       }
00129       
00130       if (!is_null($n) && !is_null($s) && !is_null($w) && !is_null($e)) {
00131          $lat_n = $n;
00132          $lat_s = $s;
00133          $long_w = $w;
00134          $long_e = $e;
00135       }
00136    }
00137 
00149    private static function parseDCMIPointBounds(DMDCElement $field,
00150          &$lat, &$long) {
00151       $encoded_bounds = $field->getValue();
00152 
00153       // separate by semicolons
00154       $parts = explode(";", $encoded_bounds);
00155       $bounds = array();
00156       foreach ($parts as $part) {
00157          // split into key-value pairs
00158          $kv = explode("=", $part);
00159          if (count($kv) == 2) {
00160             $bounds[trim($kv[0])] = trim($kv[1]);
00161          }
00162       }
00163 
00164       if (array_key_exists("units", $bounds)) {
00165          if ($bounds['units'] && $bounds['units'] != "signed decimal degrees") {
00166             throw new DMException("Unsupported boundary units");
00167          }
00168       }
00169       
00170       $n = $e = null;
00171       if (array_key_exists("north", $bounds)) {
00172          $n = (float) $bounds['north'];
00173       }
00174       if (array_key_exists("east", $bounds)) {
00175          $e = (float) $bounds['east'];
00176       }
00177       
00178       if (!is_null($n) && !is_null($e)) {
00179          $lat = $n;
00180          $long = $e;
00181       }
00182    }
00183 
00184 }
00185 
 All Data Structures Functions Variables