add cordinatesquerries
This commit is contained in:
@@ -24,9 +24,6 @@ abstract class Api extends \Slim\Slim {
|
|||||||
* Default-Constructor
|
* Default-Constructor
|
||||||
*/
|
*/
|
||||||
public function __construct() {
|
public function __construct() {
|
||||||
if (!$this->sqlManager) {
|
|
||||||
$this->sqlManager = new \database\SQLManager();
|
|
||||||
}
|
|
||||||
$this->connect();
|
$this->connect();
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,12 +29,47 @@ class SpsApi extends Api {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method stert a sps-query
|
* Method start a sps-query(alias)
|
||||||
* @param array $queryArgs
|
* @param array $queryArgs
|
||||||
* @return array[num] [assoc]
|
* @return array[num] [assoc]
|
||||||
*/
|
*/
|
||||||
public function sendSpsQuery($queryArgs = array()) {
|
public function sendSpsAliasQuery($queryArgs = array()) {
|
||||||
return $this->sqlManager->sendSpsQuery($queryArgs);
|
|
||||||
|
return $this->sqlManager->sendSpsAliasQuery($queryArgs);
|
||||||
|
}
|
||||||
|
|
||||||
|
private $range = 1;
|
||||||
|
|
||||||
|
public function sendSpsCoordinateQuery($queryArgs = array()) {
|
||||||
|
if (!array_key_exists("longitude", $queryArgs) || !array_key_exists("latitude", $queryArgs)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
$latitude = $queryArgs["latitude"];
|
||||||
|
$longitude = $queryArgs["longitude"];
|
||||||
|
|
||||||
|
if (!$this->validLatitude($latitude) || !$this->validLongitude($longitude)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$queryArgs["poly"] = $this->createPolygon($latitude, $longitude, $this->range);
|
||||||
|
return $this->sqlManager->sendSpsCoordinateQuery($queryArgs);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function validLongitude($string) {
|
||||||
|
|
||||||
|
return ctype_digit(str_replace(".", "", $string));
|
||||||
|
}
|
||||||
|
|
||||||
|
private function validLatitude($string) {
|
||||||
|
return ctype_digit(str_replace(".", "", $string));
|
||||||
|
}
|
||||||
|
|
||||||
|
private function createPolygon($latitude, $longitude, $range) {
|
||||||
|
$minLat = $latitude - $range;
|
||||||
|
$minLong = $longitude - $range;
|
||||||
|
$maxLat = $latitude + $range;
|
||||||
|
$maxLong = $longitude + $range;
|
||||||
|
return "GeomFromText('Polygon(($minLat $minLong,$minLat $maxLong,$maxLat $maxLong,$maxLat $minLong,$minLat $minLong))'";
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,6 @@
|
|||||||
namespace database;
|
namespace database;
|
||||||
|
|
||||||
include_once "../../global.inc.php";
|
include_once "../../global.inc.php";
|
||||||
include_once PATH_DATABASE . "/SpsSqlManager.php";
|
|
||||||
include_once PATH_DATABASE . "/SqlManager.php";
|
include_once PATH_DATABASE . "/SqlManager.php";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -28,17 +27,101 @@ class SpsSqlManager extends SQLManager {
|
|||||||
parent::__destruct();
|
parent::__destruct();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private $selectTerm = "SELECT DISTINCT id, parent FROM sps WHERE ";
|
||||||
|
private $andTerm = " and ";
|
||||||
|
private $aliasTerm = "alias = ";
|
||||||
|
private $domainTerm = "did = ";
|
||||||
|
private $domainNameTerm = "name = ";
|
||||||
|
private $quoteTerm = "\"";
|
||||||
|
private $interSectTermStart = "Intersects(";
|
||||||
|
private $interSectTermEnd = "),plan)";
|
||||||
|
private $polyStartStr = "GeomFromText('Polygon((";
|
||||||
|
private $polyEndStr = "))'";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Methods send an query for the sps-service
|
* Methods send an query for the sps-service
|
||||||
* @param array $queryArgs
|
* @param array $queryArgs
|
||||||
* @return array [num][assoc]
|
* @return array [num][assoc]
|
||||||
*/
|
*/
|
||||||
public function sendSpsQuery($queryArgs = array()) {
|
public function sendSpsAliasQuery($queryArgs = array()) {
|
||||||
// TODO Input validitaion
|
|
||||||
$alias = $queryArgs["alias"];
|
if (array_key_exists("alias", $queryArgs)) {
|
||||||
return $this->query("SELECT * FROM sps Where alias = \"$alias\"");
|
$alias = $queryArgs["alias"];
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$domain = null;
|
||||||
|
if (array_key_exists("domain", $queryArgs)) {
|
||||||
|
$domain = $queryArgs["domain"];
|
||||||
|
}
|
||||||
|
|
||||||
|
$query = $this->selectTerm;
|
||||||
|
|
||||||
|
if ($this->validAliasString($alias)) {
|
||||||
|
$query .= $this->aliasTerm . $this->quoteTerm . $alias . $this->quoteTerm . $this->addDomainTerm($domain);
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return $this->query($query);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function sendSpsCoordinateQuery($queryArgs = array()) {
|
||||||
|
if (array_key_exists("poly", $queryArgs)) {
|
||||||
|
$poly = $queryArgs["poly"];
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$domain = null;
|
||||||
|
if (array_key_exists("domain", $queryArgs)) {
|
||||||
|
$domain = $queryArgs["domain"];
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->validPolyString($poly)) {
|
||||||
|
$query = $this->selectTerm . $this->interSectTermStart . $poly . $this->interSectTermEnd . $this->addDomainTerm($domain);
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return $this->query($query);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function addDomainTerm($domain) {
|
||||||
|
$result = null;
|
||||||
|
if ($domain != null && $this->validDomainString($domain)) {
|
||||||
|
if ($this->isDid($domain)) {
|
||||||
|
$result .= $this->andTerm . $this->domainTerm . $this->quoteTerm . $domain . $this->quoteTerm;
|
||||||
|
} else {
|
||||||
|
$result .= $this->andTerm . $this->domainNameTerm . $this->quoteTerm . $domain . $this->quoteTerm;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function validAliasString($alias) {
|
||||||
|
if ($this->validString($alias) && ctype_alnum($alias)) {
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function validDomainString($domain) {
|
||||||
|
if ($this->validString($domain) && ctype_alnum($domain)) {
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function validPolyString($poly) {
|
||||||
|
if ($this->validString($poly) && $this->startsWith($poly, $this->polyStartStr) && $this->endsWith($poly, $this->polyEndStr)) {
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function isDid($string) {
|
||||||
|
return ctype_digit($string);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ abstract class SqlManager {
|
|||||||
* Default-Constructor
|
* Default-Constructor
|
||||||
*/
|
*/
|
||||||
public function __construct() {
|
public function __construct() {
|
||||||
|
|
||||||
$this->serverAddress = \config\DBConfig::$sqlServer;
|
$this->serverAddress = \config\DBConfig::$sqlServer;
|
||||||
$this->dbName = \config\DBConfig::$sqlDBName;
|
$this->dbName = \config\DBConfig::$sqlDBName;
|
||||||
$this->userName = \config\DBConfig::$sqlDBUser;
|
$this->userName = \config\DBConfig::$sqlDBUser;
|
||||||
@@ -108,6 +108,21 @@ abstract class SqlManager {
|
|||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function validString($string) {
|
||||||
|
if (!ctype_space($string) && !ctype_cntrl($string)) {
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function startsWith($haystack, $needle) {
|
||||||
|
return !strncmp($haystack, $needle, strlen($needle));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function endsWith($haystack, $needle) {
|
||||||
|
return (substr($haystack, -strlen($needle)) === $needle);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
|||||||
@@ -1,15 +1,38 @@
|
|||||||
<?php
|
<?php
|
||||||
include_once "../../global.inc.php";
|
|
||||||
require_once PATH_API . "/SpsApi.php";
|
|
||||||
|
|
||||||
|
include_once "../../global.inc.php";
|
||||||
|
require_once PATH_API . "/SpsApi.php";
|
||||||
|
|
||||||
|
|
||||||
$app = new \api\SpsApi();
|
$app = new \api\SpsApi();
|
||||||
|
|
||||||
$app->get('/alias/:alias', function ($alias) use ($app) {
|
$app->get('/alias/:alias', function ($alias) use ($app) {
|
||||||
$args =array();
|
$args = array();
|
||||||
$args["alias"] = $alias;
|
$args["alias"] = $alias;
|
||||||
print_r($app->sendSpsQuery($args));
|
print_r($app->sendSpsAliasQuery($args));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
$app->get('/alias/:alias/domain/:domain', function ($alias, $domain) use ($app) {
|
||||||
|
$args = array();
|
||||||
|
$args["alias"] = $alias;
|
||||||
|
$args["domain"] = $domain;
|
||||||
|
print_r($app->sendSpsAliasQuery($args));
|
||||||
|
});
|
||||||
|
|
||||||
|
$app->get('/longitude/:longitude/latitude/:latitude', function ($longitude, $latitude) use ($app) {
|
||||||
|
$args = array();
|
||||||
|
$args["longitude"] = $longitude;
|
||||||
|
$args["latitude"] = $latitude;
|
||||||
|
print_r($app->sendSpsCoordinateQuery($args));
|
||||||
|
});
|
||||||
|
|
||||||
|
$app->get('/longitude/:longitude/latitude/:latitude/domain/:domain', function ($longitude, $latitude,$domain) use ($app) {
|
||||||
|
$args = array();
|
||||||
|
$args["longitude"] = $longitude;
|
||||||
|
$args["latitude"] = $latitude;
|
||||||
|
$args["domain"] = $domain;
|
||||||
|
print_r($app->sendSpsCoordinateQuery($args));
|
||||||
|
});
|
||||||
|
|
||||||
$app->run();
|
$app->run();
|
||||||
?>
|
?>
|
||||||
|
|||||||
Reference in New Issue
Block a user