Created
September 10, 2012 22:23
-
-
Save ChrisRisner/3694420 to your computer and use it in GitHub Desktop.
GeoDemo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** Adds a new SLUG to the DB (and file) */ | |
public function addPOI($description, $type, $url, $latitude, $longitude, $id) { | |
//Add to DB | |
$con = mysql_connect($this->db_server,$this->db_user,$this->db_password); | |
if (!$con) | |
{ | |
die('Could not connect: ' . mysql_error()); | |
} | |
mysql_select_db($this->db_name, $con); | |
$sqlInsert = "INSERT INTO geodata (id, type, description, url, location) values ('$id', $type, '$description', '$url', GeomFromText( ' POINT($latitude $longitude) '))"; | |
if (!mysql_query($sqlInsert,$con)) | |
{ | |
die('Error: ' . mysql_error()); | |
} | |
mysql_close($con); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** API new POI */ | |
$app->match('/api/location/postpointofinterest/', function (Request $request) use ($app){ | |
$description = $request->get('Description'); | |
$type = $request->get('Type'); | |
$url = $request->get('Url'); | |
$latitude = $request->get('Latitude'); | |
$longitude = $request->get('Longitude'); | |
$id = $request->get('Id'); | |
//We should do validation on passed in data here | |
//Try adding the POI and either return a successful 201 or a horrible 500 | |
try { | |
$app['geo']->addPOI($description, $type, $url, $latitude, $longitude, $id); | |
} catch (Exception $e) { | |
return new Response('', 500); | |
} | |
return new Response('', 201); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** API Method to fetch all URLs */ | |
$app->match('/api/Location/FindPointsOfInterestWithinRadius', function () use ($app){ | |
$latitude = $app['request']->get('latitude'); | |
$longitude = $app['request']->get('longitude'); | |
$radiusInMeters = $app['request']->get('radiusInMeters'); | |
$resultArray = $app['geo']->getPointsOfInterestInArea($latitude, $longitude, $radiusInMeters); | |
return $app->json($resultArray, 200); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** Shows the home page */ | |
$app->get('/', function() use ($app){ | |
echo 'Hello geo home'; | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** Gets a Shared Access Signature. This can be used by our clients to upload a blob **/ | |
$app->match('/api/blobsas/get', function () use ($app){ | |
$container = $app['request']->get('container'); | |
$blobname = $app['request']->get('blobname'); | |
$storageClient = new Microsoft_WindowsAzure_Storage_Blob('blob.core.windows.net', STORAGE_ACCOUNT_NAME, STORAGE_ACCOUNT_KEY); | |
$sharedAccessUrl = $storageClient->generateSharedAccessUrl( | |
$container, | |
$blobname, | |
'b', | |
'w', | |
$storageClient ->isoDate(time()), | |
$storageClient ->isoDate(time() + 3000) | |
); | |
return new Response('"'.$sharedAccessUrl.'"', 201); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Symfony\Component\HttpFoundation\Request; | |
use Symfony\Component\HttpFoundation\ParameterBag; | |
use Symfony\Component\HttpFoundation\Response; | |
/** Bootstraping */ | |
require_once __DIR__.'/../vendor/Silex/silex.phar'; | |
require_once '/../OldSdk/Microsoft/WindowsAzure/Storage/Blob.php'; | |
$app = new Silex\Application(); | |
$app['autoloader']->registerNamespaces(array('Geo' => __DIR__,)); | |
$app->register(new Geo\GeoCodeExtension()); | |
/** Decodes JSON Requests */ | |
$app->before(function (Request $request) { | |
if (0 === strpos($request->headers->get('Content-Type'), 'application/json')) { | |
$data = json_decode($request->getContent(), true); | |
$request->request = new ParameterBag(is_array($data) ? $data : array()); | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Geo; | |
use Silex\ServiceProviderInterface; | |
use Silex\Application; | |
class GeoCodeExtension implements ServiceProviderInterface { | |
public function register(Application $app){ | |
$app['geo'] = $app->share(function() use($app){ | |
return new GeoCode(); | |
}); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** Gets all points of interest. This wouldn't be a good method to use if you had tons of POIs **/ | |
public function getAllPointsOfInterest($latitude, $longitude, $radiusInMeters) { | |
$db_url_list = array(); | |
$con = mysql_connect($this->db_server,$this->db_user,$this->db_password); | |
if (!$con) | |
{ | |
die('Could not connect: ' . mysql_error()); | |
} | |
mysql_select_db($this->db_name, $con); | |
$result = mysql_query("SELECT Id, Type, Description, Url, AsText(Location) FROM geodata"); | |
echo $result; | |
$poi_list = array(); | |
while($row = mysql_fetch_array($result)){ | |
$poi_list[$row['Id']] = $row; | |
} | |
mysql_close($con); | |
return $poi_list; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** Gets the points of interest for a specific area **/ | |
public function getPointsOfInterestInArea($latitude, $longitude, $radiusInMeters) { | |
$db_url_list = array(); | |
$con = mysql_connect($this->db_server,$this->db_user,$this->db_password); | |
if (!$con) | |
{ | |
die('Could not connect: ' . mysql_error()); | |
} | |
mysql_select_db($this->db_name, $con); | |
//code from http://www.movable-type.co.uk/scripts/latlong-db.html | |
$lat = $latitude; | |
$lon = $longitude; | |
$rad = $radiusInMeters; | |
$R = 6371; | |
// first-cut bounding box (in degrees) | |
$maxLat = $lat + rad2deg($rad/$R); | |
$minLat = $lat - rad2deg($rad/$R); | |
// compensate for degrees longitude getting smaller with increasing latitude | |
$maxLon = $lon + rad2deg($rad/$R/cos(deg2rad($lat))); | |
$minLon = $lon - rad2deg($rad/$R/cos(deg2rad($lat))); | |
// convert origin of filter circle to radians | |
$lat = deg2rad($lat); | |
$lon = deg2rad($lon); | |
$result = mysql_query(" | |
Select Id, Type, Description,Url, X(Location) as Lat, Y(Location) as Lon, | |
acos(sin($lat)*sin(radians(x(location))) + cos($lat)*cos(radians(x(location)))*cos(radians(y(location))-$lon))*$R As D | |
From ( | |
Select Id, Type, Description, Url, x(location), y(location), location | |
From geodata | |
Where x(location)>$minLat And x(location)<$maxLat | |
And y(location)>$minLon And y(location)<$maxLon | |
) As FirstCut | |
Where acos(sin($lat)*sin(radians(x(location))) + cos($lat)*cos(radians(x(location)))*cos(radians(y(location))-$lon))*$R < $rad | |
Order by D"); | |
$poi_list = array(); | |
$count = 0; | |
//Read all of our POIs into an array | |
while($row = mysql_fetch_array($result)) | |
{ | |
$item_array = array(); | |
$item_array['Id'] = $row['Id']; | |
$item_array['Type'] = intval($row['Type']); | |
$item_array['Description'] = $row['Description']; | |
$item_array['Url'] = $row['Url']; | |
$item_array['Latitude'] = floatval($row['Lat']); | |
$item_array['Longitude'] = floatval($row['Lon']); | |
//$poi_list[$row['Id']] = $row;//$row['Url']; | |
$poi_list[$count] = $item_array; | |
$count++; | |
} | |
mysql_close($con); | |
return $poi_list; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$app = require __DIR__.'/src/app.php'; | |
$app['debug'] = true; | |
$app->run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment