Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Geographic Computation in Perl
Presented by Ian Kluft
Silicon Valley Perl
March 26, 2015
Santa Clara, California
Safety warningSafety warning
If you think the world is flat,
this presentation will make your head explode.
We use geographic info all the time
● Maps on desktop/mobile
● Car navigation
● Traffic maps
● Find local services
– Resta...
Maps go way back
● Paper maps go back to ancient times
● Electronic age: GPS makes mapping more accessible
● Now every sma...
Coordinate system
Latitude and longitude
● Any position on Earth has a coordinate
● 3-dimensional positioning uses 3 numbe...
Latitude and longitude
What kind of degrees?
● Not from a university
● Not related to temperature
● They are angles!
● Mor...
Earth is not a perfect sphere
● Earth is an ellipsoid: bulges out at equator
– Centrifugal force from rotation causes this...
Lots of angles in Geospatial Data
● Many computations involve angles
– Latitude and longitude are angles
● Manipulations u...
Great Circle Routes
● Great Circle: direct route
over Earth's surface
– Along a line that would go
around the sphere
– i.e...
Great Circle Formulas
● See “Aviation Formulary” site
http://williams.best.vwh.net/avform.htm
● Distance between points
● ...
Great Circle Formula Example
Distance between points
● From scratch:
d=acos(sin(lat1)*sin(lat2)+cos(lat1)*cos(lat2)*cos(lo...
Perl Modules
● Math::Trig – trigonometry and great circle functions
● GPS::Babel – convert between GPS file formats
● Geo:...
Perl Modules (continued)
● Geo::Mercator - convert coordinates to flat-projection map
● Geo::Weather - retrieve weather fr...
Example 1
CanSat Search at Black Rock Desert
Location: Black Rock Desert, Nevada
Problem: Rocket launched, payload missing...
Perl Script to Plot Search Grid
● Search area was 3x3 mile parallelogram
● Each side divided into 10 sections, 1584' long
...
Projecting Search Grid Waypoints
● Nested loop: i = 0-10 over, j = 0-10 up
● Over = 270° heading, up = 330° heading
● Conv...
Sample code
convert feet to radians
● Convert distance in feet to radians over Earth's surface
# conversion: distance in f...
Sample code
compute waypoint from course & distance
sub gc_waypoint
{
my $lat1 = shift; # latitude (radians)
my $lon1 = sh...
Example code
compute coordinates of search grid point
# project a waypoint in the search area
# shape of a parallelogram w...
Example code (continued)
compute coordinates of search grid point
# compute final projected waypoint in search area
my ( $...
Search Area Grid Map
Result of the script shown on Google Earth
Result: Success!!!
Expected to be worse than needle in a haystack
Payload found 2500' west of rocket landing site
Example 2
Live APRS Balloon Tracking Map
Problem: Display APRS tracking data for a balloon and our team
who are tracking i...
APRS.FI posted their Perl code
● Popular APRS mapping site APRS.FI uses Perl
● They posted Ham::APRS::FAP (“fabulous APRS ...
Custom APRS Mapping Script
● Command-line parameter sets callsigns to log
● Subscribe to APRS-IS via Ham::APRS::FAP
● For ...
Balloon Chase on Google Maps
● A balloon on Feb 5, 2011 was lost in the ocean
● Teams chased it until Carmel – others watc...
More Geographic Computation ideas
● Can I see the aurora on my next airline flight?
– Compute maximum latitude of its grea...
Upcoming SlideShare
Loading in …5
×

Geographic Computation in Perl

1,109 views

Published on

"Geographic Computation in Perl" presented by Ian Kluft at Silicon Valley Perl in Santa Clara, California on March 26, 2015.

Published in: Software
  • Be the first to comment

Geographic Computation in Perl

  1. 1. Geographic Computation in Perl Presented by Ian Kluft Silicon Valley Perl March 26, 2015 Santa Clara, California
  2. 2. Safety warningSafety warning If you think the world is flat, this presentation will make your head explode.
  3. 3. We use geographic info all the time ● Maps on desktop/mobile ● Car navigation ● Traffic maps ● Find local services – Restaurants – Shopping – Auto shop ● Nearby social peeps ● Find my car ● Speedometer ● Run/bike/walk map ● Astronomy ● Other web/mobile apps
  4. 4. Maps go way back ● Paper maps go back to ancient times ● Electronic age: GPS makes mapping more accessible ● Now every smartphone has a GPS receiver ● There's more to GPS & maps than most people realize ● Simplified public views try to hide the technical stuff ● We're going to look at some of those details...
  5. 5. Coordinate system Latitude and longitude ● Any position on Earth has a coordinate ● 3-dimensional positioning uses 3 numbers – Latitude (north/south) – Longitude (east/west) – Altitude (relative to mean sea level/MSL) ● Latitude & longitude are specified in degrees ● Altitude is specified in distance
  6. 6. Latitude and longitude What kind of degrees? ● Not from a university ● Not related to temperature ● They are angles! ● More precisely, angles from the center of the Earth ● Latitude = 0-90° up or down ● Longitude = 0-180° either way around
  7. 7. Earth is not a perfect sphere ● Earth is an ellipsoid: bulges out at equator – Centrifugal force from rotation causes this ● Geoid: mathematical models for Earth ellipsoid – Good models come from satellite measurement ● Coordinates must use the same geographic reference system – Otherwise comparing apples and oranges – WGS84 most widely used coordinate system today ● Sea level and altitude are relative to this model
  8. 8. Lots of angles in Geospatial Data ● Many computations involve angles – Latitude and longitude are angles ● Manipulations use trigonometric functions ● Trig functions use radians – So numbers in degrees must be converted to and from radians – One circle = 360 degrees = 2 * pi radians – Radians derive 2 * pi from distance around circle relative to radius
  9. 9. Great Circle Routes ● Great Circle: direct route over Earth's surface – Along a line that would go around the sphere – i.e. from San Jose to London crosses Greenland – Flat-projection maps distort great circle routes to look like curves Map generated by the Great Circle Mapper copyright © Karl L. Swartz. http://www.gcmap.com/
  10. 10. Great Circle Formulas ● See “Aviation Formulary” site http://williams.best.vwh.net/avform.htm ● Distance between points ● Course between points ● Latitude of point on GC ● Lat/lon given radial & dist ● Intersection of 2 radials ● Max latitude of a GC ● GC crossing a parallel ● Intermediate points on a GC ● Cross track error ● Along track distance ● Point known offset from GC
  11. 11. Great Circle Formula Example Distance between points ● From scratch: d=acos(sin(lat1)*sin(lat2)+cos(lat1)*cos(lat2)*cos(lon1-lon2)) ● In Perl: use Math::Trig 'great_circle_distance'; $distance = great_circle_distance($theta0, $phi0, $theta1, $phi1, [, $rho]); ● Convert angles to radians, theta = longitude, phi = latitude ● Rho is the radius of the Earth – use 6366.71 km – Don't forget to convert result from km to miles if needed
  12. 12. Perl Modules ● Math::Trig – trigonometry and great circle functions ● GPS::Babel – convert between GPS file formats ● Geo::Formatter - encode & decode lat/lon strings ● Geo::GNIS - parse USGS Geographic Names data ● Geo::GPX - read & write GPX open GPS data files ● Geo::Horizon - distance to visual horizon ● Geo::IP - estimate lat/lon coordinates for IP address
  13. 13. Perl Modules (continued) ● Geo::Mercator - convert coordinates to flat-projection map ● Geo::Weather - retrieve weather from Weather Channel ● Ham::Locator – convert between lat/lon & maidenhead coords ● Net::GPSD3 - interface to GPSD server on your laptop ● Geo::Google::StaticMaps – Google Maps API wrapper ● Geo::OSM::Tiles - OpenStreetMap tiles ● And many, many more – go search CPAN!
  14. 14. Example 1 CanSat Search at Black Rock Desert Location: Black Rock Desert, Nevada Problem: Rocket launched, payload missing ● Soka University (Japan) students needed data ● AeroPac club knew rocket landing coordinates ● Turned to Stratofox Aerospace Tracking Team ● Transmitter batteries died before their contact ● I wrote a Perl script to generate a grid search
  15. 15. Perl Script to Plot Search Grid ● Search area was 3x3 mile parallelogram ● Each side divided into 10 sections, 1584' long ● 10x10 loop projects each computed waypoint ● Command-line controls output to text or GPX ● “gpsbabel” turns GPX into many formats – Raw Garmin waypoint data for upload to GPS – KML for display on Google Earth
  16. 16. Projecting Search Grid Waypoints ● Nested loop: i = 0-10 over, j = 0-10 up ● Over = 270° heading, up = 330° heading ● Convert lat/lon from degrees to radians ● Use Great Circle projection formula – Compute intermediate point from i “over” – Compute final point from j “up” ● Convert new lat/lon from radians to degrees ● Code available at svperl.org with slides
  17. 17. Sample code convert feet to radians ● Convert distance in feet to radians over Earth's surface # conversion: distance in feet to radians sub dist_ft2rad { my $ft = shift; my $km = $ft / 3280.8399; # ft/km return $km/6371.0; # divide by FAI standard Earth radius in km }
  18. 18. Sample code compute waypoint from course & distance sub gc_waypoint { my $lat1 = shift; # latitude (radians) my $lon1 = shift; # longitude (radians) my $tc = shift; # true course (radians) my $d = shift; # distance (radians) my $lat = asin(sin($lat1)*cos($d)+cos($lat1)*sin($d)*cos($tc)); my $dlon = atan2(sin($tc)*sin($d)*cos($lat1),cos($d)-sin($lat1)*sin($lat)); my $lon=fmod($lon1-$dlon+pi,2*pi) - pi; return ( $lat, $lon ); # lat/lon in radians }
  19. 19. Example code compute coordinates of search grid point # project a waypoint in the search area # shape of a parallelogram with sides at headings 030 (NNE) and 090 (east) # sides are on heading 1 (030 degrees) and heading 2 (090 degrees) # increments are 0-10 # each increment is 1584 ft so that 10 of them is 3 miles sub project_waypoint { my $h1_inc = shift; my $h2_inc = shift; # compute intermediate point on the first side of parallelogram my ( $lat_r1, $lon_r1 ) = gc_waypoint ( deg2rad( $point_start[0]), deg2rad( $point_start[1]), $h1_heading, $rad_per_increment * $h1_inc );
  20. 20. Example code (continued) compute coordinates of search grid point # compute final projected waypoint in search area my ( $lat_r2, $lon_r2 ) = gc_waypoint ( $lat_r1, $lon_r1, $h2_heading, $rad_per_increment * $h2_inc ); # convert radians to degrees my $lat = rad2deg( $lat_r2 ); my $lon = rad2deg( $lon_r2 ); return ( $lat, $lon ); }
  21. 21. Search Area Grid Map Result of the script shown on Google Earth
  22. 22. Result: Success!!! Expected to be worse than needle in a haystack Payload found 2500' west of rocket landing site
  23. 23. Example 2 Live APRS Balloon Tracking Map Problem: Display APRS tracking data for a balloon and our team who are tracking it ● APRS = Automatic Packet Reporting System, Ham Radio data protocol for transmitting your position ● The balloon and the tracking team all transmit their positions via APRS every minute or so ● Sites to display APRS data on a map don't do what we want
  24. 24. APRS.FI posted their Perl code ● Popular APRS mapping site APRS.FI uses Perl ● They posted Ham::APRS::FAP (“fabulous APRS parser”) on CPAN for all Perl developers ● You need Ham Radio license to transmit APRS ● You do not need Ham Radio license to receive APRS data ● Many volunteers forward APRS packets to APRS-IS servers on the Internet ● Everyone can subscribe to data streams there
  25. 25. Custom APRS Mapping Script ● Command-line parameter sets callsigns to log ● Subscribe to APRS-IS via Ham::APRS::FAP ● For each incoming line: – Parse line w/ Ham::APRS::FAP – Save data in a per-callsign list – Update output file in GeoRSS format ● Output file URL can be used in near real-time as an input to Google Maps ● Unfortunately, Google Maps dropped GeoRSS in 2014 update
  26. 26. Balloon Chase on Google Maps ● A balloon on Feb 5, 2011 was lost in the ocean ● Teams chased it until Carmel – others watched
  27. 27. More Geographic Computation ideas ● Can I see the aurora on my next airline flight? – Compute maximum latitude of its great circle ● Web map API “mashups” ● Social networking proximity ● Nearby USGS GNIS landmarks ● Local searches are “Location based services” – The sky is the limit

×