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          try {
00052             self::parseDCMIPointBounds($f, $lat, $long);
00053             self::parseDCMIBoxBounds($f, $lat_n, $lat_s, $long_w, $long_e);
00054             if (!is_null($lat) || !is_null($long)) { // it's a point
00055                $sp_obj = new UNLVSpatialPointObject();
00056                $sp_obj->setSpatialDataSource("metadata");
00057                $sp_obj->setSpatialLat(!is_null($lat) ? $lat : -90);
00058                $sp_obj->setSpatialLong(!is_null($long) ? $long : 180);
00059                $obj->addAssociatedModel($sp_obj);
00060                return;
00061             } else if (!is_null($lat_n) || !is_null($lat_s)
00062                   || !is_null($long_w) || !is_null($long_e)) {
00063                $sp_obj = new UNLVSpatialRectObject();
00064                $sp_obj->setSpatialDataSource("metadata");
00065                $sp_obj->setSpatialNorthLat(!is_null($lat_n) ? $lat_n : 90);
00066                $sp_obj->setSpatialSouthLat(!is_null($lat_s) ? $lat_s : -90);
00067                $sp_obj->setSpatialWestLong(!is_null($long_w) ? $long_w : -180);
00068                $sp_obj->setSpatialEastLong(!is_null($long_e) ? $long_e : 180);
00069                $obj->addAssociatedModel($sp_obj);
00070                return;
00071             }
00072          } catch (DMException $e) {
00073             // boundary units in this field are unsupported; continue
00074          }
00075       }
00076 
00077       // The above failed, so try to fall back on UNLVSpatial data
00078       $ds = UNLVSpatial::getDataStore();
00079       $sp_obj = $ds->getObject($obj->getCollection(), $obj->getPtr());
00080       if ($sp_obj) {
00081          $obj->addAssociatedModel($sp_obj);
00082          return;
00083       }
00084    }
00085 
00099    private static function parseDCMIBoxBounds(DMDCElement $field,
00100          &$lat_n, &$lat_s, &$long_w, &$long_e) {
00101       $encoded_bounds = $field->getValue();
00102 
00103       // separate by semicolons
00104       $parts = explode(";", $encoded_bounds);
00105       $bounds = array();
00106       foreach ($parts as $part) {
00107          // split into key-value pairs
00108          $kv = explode("=", $part);
00109          if (count($kv) == 2) {
00110             $bounds[trim($kv[0])] = trim($kv[1]);
00111          }
00112       }
00113 
00114       if (array_key_exists("units", $bounds)) {
00115          if ($bounds['units'] && $bounds['units'] != "signed decimal degrees") {
00116             throw new DMException("Unsupported boundary units");
00117          }
00118       }
00119 
00120       $n = $s = $e = $w = null;
00121       if (array_key_exists("northlimit", $bounds)) {
00122          $n = (float) $bounds['northlimit'];
00123       }
00124       if (array_key_exists("southlimit", $bounds)) {
00125          $s = (float) $bounds['southlimit'];
00126       }
00127       if (array_key_exists("westlimit", $bounds)) {
00128          $w = (float) $bounds['westlimit'];
00129       }
00130       if (array_key_exists("eastlimit", $bounds)) {
00131          $e = (float) $bounds['eastlimit'];
00132       }
00133       
00134       if (!is_null($n) && !is_null($s) && !is_null($w) && !is_null($e)) {
00135          $lat_n = $n;
00136          $lat_s = $s;
00137          $long_w = $w;
00138          $long_e = $e;
00139       }
00140    }
00141 
00153    private static function parseDCMIPointBounds(DMDCElement $field,
00154          &$lat, &$long) {
00155       $encoded_bounds = $field->getValue();
00156 
00157       // separate by semicolons
00158       $parts = explode(";", $encoded_bounds);
00159       $bounds = array();
00160       foreach ($parts as $part) {
00161          // split into key-value pairs
00162          $kv = explode("=", $part);
00163          if (count($kv) == 2) {
00164             $bounds[trim($kv[0])] = trim($kv[1]);
00165          }
00166       }
00167 
00168       if (array_key_exists("units", $bounds)) {
00169          if ($bounds['units'] && $bounds['units'] != "signed decimal degrees") {
00170             throw new DMException("Unsupported boundary units");
00171          }
00172       }
00173       
00174       $n = $e = null;
00175       if (array_key_exists("north", $bounds)) {
00176          $n = (float) $bounds['north'];
00177       }
00178       if (array_key_exists("east", $bounds)) {
00179          $e = (float) $bounds['east'];
00180       }
00181       
00182       if (!is_null($n) && !is_null($e)) {
00183          $lat = $n;
00184          $long = $e;
00185       }
00186    }
00187 
00188 }
00189 
 All Data Structures Functions Variables