Accessing spatial data programmatically

Via the HTTP API

Spatial data access via the HTTP API is read-only. When the UNLVSpatial module is enabled, its HTTP API documentation will appear in the dmBridge HTTP API documentation and will not be duplicated here.

Via the PHP API

Loading spatial data

The following example will retrieve an object's spatial data.

$col = DMCollectionFactory::getCollection("/sample");
$obj = DMObjectFactory::getObject($col, 5);
UNLVSpatialObjectFactory::spatializeObject($obj);

try {
    if ($obj->getSpatialModelClassName() == "UNLVSpatialRectObject") {
        echo $obj->getSpatialNorthLat();
        echo $obj->getSpatialSouthLat();
        echo $obj->getSpatialWestLong();
        echo $obj->getSpatialEastLong();
    } else { // it must be a point
        echo $obj->getSpatialLat();
        echo $obj->getSpatialLong();
    }
} catch (DMNoSuchMethodException $e) {
    echo "This object has no spatial metadata.";
}

Querying spatial data

Spatial data can be easily retrieved using a UNLVSpatialQuery. An example:

$example_obj = new UNLVSpatialRectObject();
$example_obj->setSpatialNorthLat(45.5);
$example_obj->setSpatialSouthLat(24.6);
$example_obj->setSpatialWestLong(-125);
$example_obj->setSpatialEastLong(-115);

$term = new UNLVSpatialQueryTerm();
$term->setExampleObject($example_obj);
$term->setMatchingMode("within");

$query = new UNLVSpatialQuery();
$query->addQueryTerm($term);

$results = $query->getSearchResults();
print_r($results);

UNLVSpatialQuery has additional methods for pagination, specifying search collections, and more. See the PHP API documentation for more information.

Adding and updating spatial data

The recommended way of adding spatial data is to instantiate a new UNLVSpatialObject; associate it with an existing DMObject; and persist it, as in the following example:

// Update a rectangle object
$collection = DMCollectionFactory::getCollection("/whatever");
$obj = DMObjectFactory::getObject($collection, 123);

$sp_obj = new UNLVSpatialRectObject();
$sp_obj->setSpatialNorthLat(45);
$sp_obj->setSpatialSouthLat(40);
$sp_obj->setSpatialWestlong(-115);
$sp_obj->setSpatialEastLong(-110);
$obj->addAssociatedModel($sp_obj);

UNLVSpatial::getDataStore()->updateObject($obj);


// Update a point object
$collection = DMCollectionFactory::getCollection("/whatever");
$obj = DMObjectFactory::getObject($collection, 456);

$sp_obj = new UNLVSpatialPointObject();
$sp_obj->setSpatialLat(45);
$sp_obj->setSpatialLong(-110);
$obj->addAssociatedModel($sp_obj);

UNLVSpatial::getDataStore()->updateObject($obj);

The call to updateObject() will insert the data if it does not already exist, and update it if it does.

Deleting spatial data

To delete spatial data, first we retrieve the DMObject whose spatial data we want to delete, then we "delete" it:

$collection = DMCollectionFactory::getCollection("/whatever");
$obj = DMObjectFactory::getObject($collection, 123);

UNLVSpatial::getDataStore()->deleteObject($obj);

The call to deleteObject() does not actually delete the given object -- only its spatial metadata.