Where in the World?<br />Geo-data in PHP<br />
80% of data has a geo-component<br />
Some  background<br />
Mercator - 1569<br />
51 28’ 38”N0 0’ 0” E<br />Munich, 11E<br />Paris, 2E<br />Madrid, 3W<br />Prime meridian<br />Know your datum!<br />
Points<br />Paris<br />52°20'28N, 04°53'25E<br />12 Grimmauld Place, London<br />What makes up geo-data?<br />
Lines<br />M4<br />Sunset Boulevard<br />Dover TSS<br />What makes up geo-data?<br />
Polygons<br />London<br />Somerset<br />Contour<br />What makes up geo-data?<br />
Is point x within y?<br />Questions, questions…<br />
Does line x pass through y?<br />Questions, questions…<br />
How far is point x from point y?<br />Questions, questions…<br />
Which region is point x within?<br />Regions can overlap:<br />District:     ClaphamBorough:  WandswothCity:         	 Lon...
80% of data has a geo-component<br />So how do I find it?<br />
Geo-data from web visitors<br />
Every web visitor offers 2 sources of geo-data:<br /><ul><li> IP address
 Ask the browser!</li></ul>Where is my visitor?<br />
home<br />51 26’ 55.2” N<br />0 0’ 0.1” W<br />exchange<br />Accurate?<br />IP gives you the location of the Serving are...
include("geoip.inc"); <br />$gi = geoip_open("/usr/local/share/GeoIP/GeoIP.dat",GEOIP_STANDARD);<br />$country_code = geoi...
Pear extension
Apache module</li></ul>Use the code…<br />
function myCallback(position) {<br />  // called if the browser reports its location<br />}<br />if (navigator.geolocation...
Geo-data from content<br />
“Journey’s End”by nathanhayag<br />“MacRitchie Reservoir, Singapore”<br />Geo-data encoded in the image file:<br />1°22'18...
$exif = exif_read_data('file.jpg');<br />$long[‘deg’] = $exif['GPSLongitude'][0];<br />$long[‘min’] = $exif['GPSLongitude'...
“Journey’s End”by nathanhayag<br />“MacRitchie Reservoir, Singapore”<br />Parse text for location data….<br />Usual PHP in...
“Journey’s End”by nathanhayag<br />“MacRitchie Reservoir, Singapore”<br />Feeling brave?<br />Image recognition and associ...
External sources<br />
Don’t forget…<br />
Primitive data-types<br />$long = 51.5723<br />$lat = -0.0179<br />Class structures<br />$point = new GISPoint;<br />$poin...
MySQL storage<br />CREATE TABLE tweet(<br />  id BIGINT,<br />  user VARCHAR(15),<br />  message VARCHAR(140),<br />  loca...
MySQL Spatial extensions<br />CREATE TABLE tweet(<br />  id BIGINT,<br />  user VARCHAR(15),<br />  message VARCHAR(140),<...
MySQL<br />MS SQL<br />Oracle<br />Postgres – PostGIS<br />Geo-data in the DB<br />
WKT: Well Known Text<br />POINT(6 10) LINESTRING(3 4,10 50,20 25)<br />WKB: Well Known Binary<br />1010010001001010<br />G...
Binary Terrain<br />Coverage (ArcGIS)<br />DEM <br />Spatial Data File (Autodesk)<br />…<br />More (obscure) formats<br />
Discovery<br />Storage<br />Analysis<br />
Geo-centric:Cartesian<br />http://trac.osgeo.org/proj/wiki/man_cs2cs<br />Coordinate conversion<br />
Cartesian geometry<br />a<br />(x1, y1)<br />√<br />(y1 – y2)2 + (x2 – x1)2<br />y1 – y2<br />b<br />x2 – x1<br />(x2, y2)...
Geo-centric geometry<br />Greater circle route<br />a<br />(λs, Φs)<br />Haversine formula<br />b<br />(λf, Φf)<br />Dista...
function haversine($a) {<br />  return (sin($a/2) * sin($a/2));<br />}<br />function ahaversine($a) {<br />  return 2 * as...
SELECT DISTANCE(a, b);<br />SELECT INTERSECTS (a, b);<br />SELECT DISJOINT (a, b);<br />SELECT OVERLAPS (a, b);<br />SELEC...
Discovery<br />Storage<br />Analysis<br />Visualisation<br />
Map APIs<br />
Simple overlays<br />
Heatmaps<br />
Custom visualisation<br />
Image generation<br />
Upcoming conferences<br /><ul><li>Bristol Indiemedia  Bristol – June
Upcoming SlideShare
Loading in …5
×

Where in the world

1,552 views

Published on

Slides from my php geodata talk at DPC10.

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,552
On SlideShare
0
From Embeds
0
Number of Embeds
17
Actions
Shares
0
Downloads
0
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide
  • - Different datums: WGS84, NAD27, NAD83…
  • Simple as yes or no…
  • Distance between 2 points…(don&apos;t mention solutions)
  • Regions can overlap…
  • Remember this?So, if 80%...DISCOVERY
  • Accuracy: to nearest exchangeReliability: proxies, VPNs, Networks structure
  • Other IP geo-databases availableAPI options
  • Single positionContinously updating positionPermit stale data
  • EXIF – used by some camerasGives latitude, longitude, and lat-reg (N/S), long-ref (E/W)
  • Twitter4 squareGowallaSocial graphSearch by hash-tags; user-configured integration…endless choiceCustom file input
  • Having gone to all this effort to find this data, you’re going to throw it away.
  • Also get analysis tools
  • OGC - Open Geospatial Consortium - standards
  • Please document the format…for your colleague&apos;s sanity!
  • Co-ordinate conversion is complicatedUse a library
  • Cartesian geometry for a grid based system
  • Haversine corrects for projection distortion over long distancesSpheroid geometrySuffers from antipodal errors – use Vincenty insteadNote the Greek
  • JS
  • Most overlays require cartesian geometry – need to convert.
  • Go see Mike&apos;s uncon talk at 2pm
  • Where in the world

    1. 1. Where in the World?<br />Geo-data in PHP<br />
    2. 2. 80% of data has a geo-component<br />
    3. 3. Some background<br />
    4. 4. Mercator - 1569<br />
    5. 5. 51 28’ 38”N0 0’ 0” E<br />Munich, 11E<br />Paris, 2E<br />Madrid, 3W<br />Prime meridian<br />Know your datum!<br />
    6. 6. Points<br />Paris<br />52°20'28N, 04°53'25E<br />12 Grimmauld Place, London<br />What makes up geo-data?<br />
    7. 7. Lines<br />M4<br />Sunset Boulevard<br />Dover TSS<br />What makes up geo-data?<br />
    8. 8. Polygons<br />London<br />Somerset<br />Contour<br />What makes up geo-data?<br />
    9. 9. Is point x within y?<br />Questions, questions…<br />
    10. 10. Does line x pass through y?<br />Questions, questions…<br />
    11. 11. How far is point x from point y?<br />Questions, questions…<br />
    12. 12. Which region is point x within?<br />Regions can overlap:<br />District: ClaphamBorough: WandswothCity: LondonCountry: UK<br />Questions, questions…<br />
    13. 13. 80% of data has a geo-component<br />So how do I find it?<br />
    14. 14. Geo-data from web visitors<br />
    15. 15. Every web visitor offers 2 sources of geo-data:<br /><ul><li> IP address
    16. 16. Ask the browser!</li></ul>Where is my visitor?<br />
    17. 17. home<br />51 26’ 55.2” N<br />0 0’ 0.1” W<br />exchange<br />Accurate?<br />IP gives you the location of the Serving area interface (telco exchange).<br />Some exchanges can be 5 miles away!<br />home<br />office<br />Reliable?<br />Proxies<br />VPNs<br />Network distributions (ever tried geo-locating an AOL IP?)<br />exchange<br />The problem with IP<br />
    18. 18. include("geoip.inc"); <br />$gi = geoip_open("/usr/local/share/GeoIP/GeoIP.dat",GEOIP_STANDARD);<br />$country_code = geoip_country_code_by_addr($gi, "24.24.24.24");<br />$country_name = geoip_country_name_by_addr($gi, "24.24.24.24”); <br />Free data library from MaxMind….<br /><ul><li>Pure PHP
    19. 19. Pear extension
    20. 20. Apache module</li></ul>Use the code…<br />
    21. 21. function myCallback(position) {<br /> // called if the browser reports its location<br />}<br />if (navigator.geolocation) {navigator.geolocation.getCurrentPosition(myCallback);<br />}<br />W3C spec:http://dev.w3.org/geo/api/spec-source.html<br />Mozilla, where art thou?<br />
    22. 22. Geo-data from content<br />
    23. 23. “Journey’s End”by nathanhayag<br />“MacRitchie Reservoir, Singapore”<br />Geo-data encoded in the image file:<br />1°22'18”N103°49'27"E<br />A picture says a thousand words<br />
    24. 24. $exif = exif_read_data('file.jpg');<br />$long[‘deg’] = $exif['GPSLongitude'][0];<br />$long[‘min’] = $exif['GPSLongitude'][1] / 60;<br />$long[‘sec’] = $exif['GPSLongitude'][2] / 3600;<br />$lat[‘deg’] = $exif['GPSLatitude'][0];<br />$lat[‘min’] = $exif['GPSLatitude'][1] / 60;<br />$lat[‘sec’] = $exif['GPSLatitude'][2] / 3600;<br />Reading EXIF<br />
    25. 25. “Journey’s End”by nathanhayag<br />“MacRitchie Reservoir, Singapore”<br />Parse text for location data….<br />Usual PHP information retrieval techniques apply<br />http://phpir.com/ to learn more<br />A picture says a thousand words<br />
    26. 26. “Journey’s End”by nathanhayag<br />“MacRitchie Reservoir, Singapore”<br />Feeling brave?<br />Image recognition and association<br />A picture says a thousand words<br />
    27. 27. External sources<br />
    28. 28. Don’t forget…<br />
    29. 29. Primitive data-types<br />$long = 51.5723<br />$lat = -0.0179<br />Class structures<br />$point = new GISPoint;<br />$point->setLatitude(51,21,35,'N');<br />$point->setLongitude(0,1,21, 'W');<br />Geo-data in PHP variables<br />
    30. 30. MySQL storage<br />CREATE TABLE tweet(<br /> id BIGINT,<br /> user VARCHAR(15),<br /> message VARCHAR(140),<br /> location VARCHAR(16)<br /> );<br />INSERT INTO tweet VALUES (<br /> 15924144610,<br /> 'manarth',<br /> 'hello!',<br /> '0512732N0000319W'<br /> ); <br />Persistent storage<br />
    31. 31. MySQL Spatial extensions<br />CREATE TABLE tweet(<br /> id BIGINT,<br /> user VARCHAR(15),<br /> message VARCHAR(140),<br />location POINT<br /> );<br />INSERT INTO tweet VALUES (<br /> 15924144610,<br /> 'manarth',<br /> 'hello!',<br />PointFromWKT(Point(51.1,-0.2))<br /> ); <br />Spatial extensions<br />
    32. 32. MySQL<br />MS SQL<br />Oracle<br />Postgres – PostGIS<br />Geo-data in the DB<br />
    33. 33. WKT: Well Known Text<br />POINT(6 10) LINESTRING(3 4,10 50,20 25)<br />WKB: Well Known Binary<br />1010010001001010<br />GeoJSON<br />{<br /> "geometry": {<br /> "type" : "Point",<br /> "coordinates" : [97.03125, 39.7265625] }<br />}<br />Write to disk?<br />
    34. 34. Binary Terrain<br />Coverage (ArcGIS)<br />DEM <br />Spatial Data File (Autodesk)<br />…<br />More (obscure) formats<br />
    35. 35. Discovery<br />Storage<br />Analysis<br />
    36. 36. Geo-centric:Cartesian<br />http://trac.osgeo.org/proj/wiki/man_cs2cs<br />Coordinate conversion<br />
    37. 37. Cartesian geometry<br />a<br />(x1, y1)<br />√<br />(y1 – y2)2 + (x2 – x1)2<br />y1 – y2<br />b<br />x2 – x1<br />(x2, y2)<br />Distance from a to b<br />
    38. 38. Geo-centric geometry<br />Greater circle route<br />a<br />(λs, Φs)<br />Haversine formula<br />b<br />(λf, Φf)<br />Distance from a to b<br />
    39. 39. function haversine($a) {<br /> return (sin($a/2) * sin($a/2));<br />}<br />function ahaversine($a) {<br /> return 2 * asin(sqrt($a));<br />}<br />function haversine_separation($a, $b) {<br /> // assume a radius of 3956 miles.<br /> define ('RADIUS', 3956);<br /> // convert degree inputs to radians<br /> $a = array(deg2rad($a['long']), deg2rad($a['lat']));<br /> $b = array(deg2rad($b['long']), deg2rad($b['lat']));<br /> $x = haversine($a['long'] - $b['long']) + (cos($a['long']) * cos($b['long']) * haversine(abs($a['lat'] - $b['lat'])));<br /> // $d (distance) is therefore R * inverse haversine($x)<br /> $d = RADIUS * ihaversine($x);<br /> return $d;<br />}<br />Haversine calculations<br />
    40. 40. SELECT DISTANCE(a, b);<br />SELECT INTERSECTS (a, b);<br />SELECT DISJOINT (a, b);<br />SELECT OVERLAPS (a, b);<br />SELECT CONTAINS (a, b);<br />SELECT WITHIN (a, b);<br />Some limitations – currently measures against MBR – Minimum Bounding Rectangle<br />Spatial analysis in MySQL<br />
    41. 41. Discovery<br />Storage<br />Analysis<br />Visualisation<br />
    42. 42. Map APIs<br />
    43. 43. Simple overlays<br />
    44. 44. Heatmaps<br />
    45. 45. Custom visualisation<br />
    46. 46. Image generation<br />
    47. 47. Upcoming conferences<br /><ul><li>Bristol Indiemedia Bristol – June
    48. 48. Ignite BristolBristol - July
    49. 49. DrupalconCopenhagen - August
    50. 50. PHP NWManchester - October</li></ul>Upcoming conferences<br /><ul><li>DrupalconCopenhagen - August
    51. 51. PHP NWManchester - October
    52. 52. PHP BarcelonaBarcelona - October
    53. 53. ZendConSanta Clara - October</li></ul>Loremipsum<br />Loremipsum dolor sit amet, consecteturadipisicingelit, sed do eiusmodtemporincididuntutlabore et dolore magna aliqua. Utenim ad minim veniam,<br />quisnostrud exercitation ullamcolaboris nisi utaliquip ex ea commodoconsequat. Duisauteirure dolor in reprehenderit in voluptatevelitessecillumdoloreeu<br />fugiatnullapariatur. Excepteursintoccaecatcupidatat non proident, sunt in culpa qui officiadeseruntmollitanim id estlaborum<br />Invisible feedback<br />
    54. 54. Discovery<br />Storage<br />Analysis<br />Visualisation<br />
    55. 55. 80% of data has a geo-component<br />
    56. 56. Marcus Deglos<br />marcus@deglos.com<br />http://deglos.com/<br />@manarth<br />Thanks for coming!<br />http://joind.in/1555<br />

    ×