Files
geodb/geoapi/api/SpsApi.php
2013-07-10 13:24:07 +02:00

246 lines
6.0 KiB
PHP

<?php
namespace api;
include_once "../../global.inc.php";
include_once PATH_DATABASE . "/SpsSqlManager.php";
require_once PATH_API . "/Api.php";
/**
* This class provides some spezial SpsAPI methods
* @author stubbfel
* @since 20.06.2013
*/
class SpsApi extends Api {
/**
* Route string for the alias paramter
* @var string
*/
public static $routeParameterAlias = "/alias/:alias";
/**
* Route string for the domain paramter
* @var string
*/
public static $routeParameterDomain = "(/domain/:domain)";
/**
* Route string for the range paramter
* @var string
*/
public static $routeParameterRange = "(/range/:range)";
/**
* Route string for the Longitude paramter
* @var string
*/
public static $routeParameterLongitude = "/longitude/:longitude";
/**
* Route string for the latitude paramter
* @var string
*/
public static $routeParameterLatitude = "/latitude/:latitude";
/**
* Keyword for alias arguments
* @var string
*/
public static $keyAlias = "alias";
/**
* Keyword for domain arguments
* @var string
*/
public static $keyDomain = "domain";
/**
* Keyword for polygon arguments
* @var string
*/
public static $keyPoly = "poly";
/**
* Keyword for longitude arguments
* @var string
*/
public static $keyLong = "longitude";
/**
* Keyword for latitude arguments
* @var string
*/
public static $keyLat = "latitude";
/**
* Keyword for range arguments
* @var string
*/
public static $keyRange = "range";
/*
* Varible for the range of the searchpolygon
* @var float
*/
private static $range = 0.001;
/*
* Varible for the fist chars of the string for a Polygon
* @var string
*/
private static $polyStartStr = "GeomFromText('Polygon((";
/*
* Varible for the last chars of the string for a Polygon
* @var string
*/
private static $polyEndStr = "))'";
/**
* maximum value of latitude
* @var float
*/
private static $maxLat = 180;
/**
* minimum value of latitude
* @var float
*/
private static $minLat = -180;
/**
* maximum value of longitude
* @var float
*/
private static $maxLong = 180;
/**
* minimum value of longitude
* @var float
*/
private static $minLong = -180;
/**
* Constructor
* @param array[assoc] $headers - RequestHeader
*/
public function __construct($headers = array()) {
$this->sqlManager = new \database\SpsSqlManager();
parent::__construct($headers);
}
/**
* Default-DeConstructor
*/
public function __destruct() {
parent::__destruct();
}
/**
* Method start a sps-query(alias)
* @param array $queryArgs
* @return querry result as xml
*/
public function sendSpsAliasQuery($queryArgs) {
$result = $this->sqlManager->sendSpsAliasQuery($queryArgs);
return $this->serialManager->arrayToSps($result);
}
/**
* Method start a sps-query(Coordinates)
* @param array $queryArgs
* @return querry result as xml
*/
public function sendSpsCoordinateQuery($queryArgs) {
// check arguments of the query
if (!array_key_exists(self::$keyLong, $queryArgs) || !array_key_exists(self::$keyLat, $queryArgs)) {
return null;
}
$latitude = $queryArgs[self::$keyLat];
$longitude = $queryArgs[self::$keyLong];
if (!$this->validLatitude($latitude) || !$this->validLongitude($longitude)) {
return null;
}
// build a request polygon
if (array_key_exists(self::$keyRange, $queryArgs)) {
$newRange = $queryArgs[self::$keyRange];
if ($this->validRange($newRange)) {
self::$range = $newRange;
} else {
return;
}
}
$queryArgs[self::$keyPoly] = $this->createPolygon($latitude, $longitude, self::$range);
// send querry
$result = $this->sqlManager->sendSpsCoordinateQuery($queryArgs);
return $this->serialManager->arrayToSps($result);
}
/**
* Method check if a string is a valid Longitude
* @param string $string
* @return bool
*/
private function validRange($string) {
$digit = str_replace(".", "", $string);
if (ctype_digit($digit)) {
return TRUE;
};
return FALSE;
}
/**
* Method check if a string is a valid Longitude
* @param string $string
* @return bool
*/
private function validLongitude($string) {
$digitLessPoint = str_replace(".", "", $string);
$digit = str_replace("-", "", $digitLessPoint);
if (ctype_digit($digit)) {
if ($string <= self::$maxLong && $string >= self::$minLong) {
return TRUE;
}
};
return FALSE;
}
/**
* Method check if a string is a valid Latitude
* @param strinf $string
* @return bool
*/
private function validLatitude($string) {
$digitLessPoint = str_replace(".", "", $string);
$digit = str_replace("-", "", $digitLessPoint);
if (ctype_digit($digit)) {
if ($string <= self::$maxLat && $string >= self::$minLat) {
return TRUE;
}
};
return FALSE;
}
/**
* Method create a Polygon
* @param float $latitude
* @param float $longitude
* @param float $range
* @return string
*/
private function createPolygon($latitude, $longitude, $range) {
$minLat = $latitude - $range;
$minLong = $longitude - $range;
$maxLat = $latitude + $range;
$maxLong = $longitude + $range;
return self::$polyStartStr . "$minLat $minLong,$minLat $maxLong,$maxLat $maxLong,$maxLat $minLong,$minLat $minLong" . self::$polyEndStr;
}
}
?>