Seattle has a pretty awesome approach to data availability and transparency through data.Seattle.gov. The city has thousands of data sets available (from in-car police video records to land zoning to real-time emergency feeds) and Socrata, a Seattle-based company, has worked with the city (and many other cities) to allow developers to engage this data however they like. I spent some time playing around with some of the data sets and decided it’d be nice to know when police and fire events occurred near my apartment.
I setup a script to pull the fire and police calls for events occurring within 500 meters of my apartment and started storing them into a local database (Socrata makes it so simple – amazing work by that team). While reading it from the API, I check the proximity of the event to my address and also the type of event (burglary, suspicious person, traffic stop, etc) and trigger emails for the ones I really want to know about (such as a near by rape, burglary, shooting, vehicle theft, etc). I decided to store all events, even traffic stops, just because. I may find a use for it later – who knows…
After I’ve scrubbed through and sent any notifications for events I care about, I display the data in a simple table in my existing home dashboard and highlight red any rows for events which are within certain square area of my apartment.
To add a nice visual, I also plot the most recent events on a map using the Google Maps API. Police events are noted with blue pins, fire events are noted with red pins:
Clicking the pins will give us some details about the event:
All told, it was a pretty simple project which helped me gain some experience with the Google Maps API and also poke around with some of the data the city provides. I’m sure I’ll be doing a bit more of that in the future. These two projects have been integrated back into my home automation dashboard so I can continue to build on them in the future.
Month: December 2016
Location to HTTP – Send GPS data to remote script
I wanted to capture my phone’s location information and store it remotely (on my server) so I could do with it as I please. Most of the apps I found were severely limited in that they only mapped within the app, were riddled with ads, crashed frequently, or were untrustworthy (they’re storing my location, after all). So, I decided to build my own app that did what I wanted.
Location to HTTP
Location to HTTP is an app I’m working on which allows users to input a remote script URL to capture a device’s location information via POST.
How it works
- Create a remote script to capture the variables $lat and $lon via POST (example below).
- Download the app and enter the URL of that script.
- The app will automatically send the location information every 5-10 minutes to the script.
Possible Uses
- Track your phone’s location so you can retrace your steps if you lose it.
- Store your location history for displaying in custom interfaces.
- Share your location with friends/family when travelling or hiking.
- Trigger home automation events based on your location (turn on the heat, turn on the lights, unlock the doors, feed the pets, water the plants, etc.).
- Measure the amount of time you spend at specific locations.
Sample Script
The beauty (and utility) of this app is that you can do whatever you like with the output. In my use case, I want to store my location history to track my travel history. I do this using a simply PHP script and MySQL database.
Database Structure
CREATE TABLE IF NOT EXISTS `phone_location` ( `id` int(11) NOT NULL AUTO_INCREMENT, `lat` varchar(200) NOT NULL, `lon` varchar(200) NOT NULL, `time` varchar(200) NOT NULL, `address` varchar(200) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
PHP Script
<? //Prevent caching and set content-type to json so app can display it correctly. header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); header("Cache-Control: post-check=0, pre-check=0", false); header("Pragma: no-cache"); header('Content-Type: application/json'); //Pull address for latitude and longitude from Google API (max 25,000 calls per day for free) function getaddress($lat,$lon) { $url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng='.trim($lat).','.trim($lon); $json = @file_get_contents($url); $data=json_decode($json); $status = $data->status; if($status=="OK"){ return $data->results[0]->formatted_address; } else { return false; } } //Set the response we want to display in the app date_default_timezone_set('America/Los_Angeles'); $timestamp = date('Y-m-d h:i:s'); $success_response = "Successfully captured location @ $timestamp PST!"; //Connect to MySQL database $conn = mysqli_connect("", "", "", ""); if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } //Capture the latitude ($lat) and longitude ($lon) passed from the app $lat=$_REQUEST['lat']; $lon=$_REQUEST['lon']; //Get the address from Google API $address= getaddress($lat,$lon); if(!$address){ $address= "Not found"; } //Insert record into database $sql = "INSERT INTO phone_location (lat, lon, time, address) VALUES ('$lat', '$lon', '$timestamp', '$address')"; if (mysqli_query($conn, $sql)) { echo json_encode($success_response); } else { echo json_encode("Error: Unable to create posting."); } mysqli_close($conn); ?>
Google API Integration for Address Data
As shown in the example above, you can easily get address information using the Google API and reverse geocoding.