Files
geodb/geoapi/api/SpsApi.php
2013-06-26 12:12:10 +02:00

210 lines
5.1 KiB
PHP

<?php
namespace api;
include_once "../../global.inc.php";
include_once PATH_DATABASE . "/SpsSqlManager.php";
include_once PATH_UTILITTY . "/XmlManager.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 alias paramter
* @var string
*/
public static $routeParameterDomain = "/domain/:domain";
/**
* 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";
/*
* Varible for the range of the searchpolygon
* @var float
*/
private $range = 1;
/*
* Varible for the fist chars of the string for a Polygon
* @var string
*/
private $polyStartStr = "GeomFromText('Polygon((";
/*
* Varible for the last chars of the string for a Polygon
* @var string
*/
private $polyEndStr = "))'";
/**
* maximum value of latitude
* @var float
*/
private $maxLat = 180;
/**
* minimum value of latitude
* @var float
*/
private $minLat = -180;
/**
* maximum value of longitude
* @var float
*/
private $maxLong = 180;
/**
* minimum value of longitude
* @var float
*/
private $minLong = -180;
/**
* Default-Constructor
*/
public function __construct() {
$this->sqlManager = new \database\SpsSqlManager();
parent::__construct();
$this->contentType("Content-type: application/xml;charset=utf-8");
}
/**
* 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 \utiliy\XmlManager::arrayToSpsXml($result);
}
/**
* Method start a sps-query(Coordinates)
* @param array $queryArgs
* @return querry result as xml
*/
public function sendSpsCoordinateQuery($queryArgs) {
if (!array_key_exists(SpsApi::$keyLong, $queryArgs) || !array_key_exists(SpsApi::$keyLat, $queryArgs)) {
return null;
}
$latitude = $queryArgs[SpsApi::$keyLat];
$longitude = $queryArgs[SpsApi::$keyLong];
if (!$this->validLatitude($latitude) || !$this->validLongitude($longitude)) {
return null;
}
$queryArgs[SpsApi::$keyPoly] = $this->createPolygon($latitude, $longitude, $this->range);
$result = $this->sqlManager->sendSpsCoordinateQuery($queryArgs);
return \utiliy\XmlManager::arrayToSpsXml($result);
}
/**
* 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 <= $this->maxLong && $string >= $this->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 <= $this->maxLat && $string >= $this->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 $this->polyStartStr . "$minLat $minLong,$minLat $maxLong,$maxLat $maxLong,$maxLat $minLong,$minLat $minLong" . $this->polyEndStr;
}
}
?>