PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/include/AMP/Geo/Geo.php

https://github.com/radicaldesigns/amp
PHP | 325 lines | 238 code | 55 blank | 32 comment | 42 complexity | e473f8fe10f77d61b61ac533f492a66b MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, BSD-3-Clause, LGPL-2.0, CC-BY-SA-3.0, AGPL-1.0
  1. <?php
  2. require_once "HTTP/Request.php";
  3. require_once "XML/Unserializer.php";
  4. class rdfDocument
  5. {
  6. var $channel;
  7. var $geo;
  8. function getItems($amount)
  9. {
  10. return array_splice($this->geo,0,$amount);
  11. }
  12. }
  13. Class Geo {
  14. var $dbcon;
  15. var $Street;
  16. var $City;
  17. var $State;
  18. var $Zip;
  19. var $lat;
  20. var $long;
  21. //options
  22. var $city_fulltext = false;
  23. var $city_soundex = false;
  24. function Geo(&$dbcon,$Street=NULL,$City=NULL,$State=NULL,$Zip=NULL, $options=null) {
  25. $this->dbcon = $dbcon;
  26. $this->Street =$Street;
  27. $this->City =$City;
  28. $this->State =$State;
  29. $this->Zip =$Zip;
  30. if(isset($options)) {
  31. $this->setOptions($options);
  32. }
  33. if ( isset($this->Street) ) {
  34. if ( (isset($this->City) && $this->City && isset($this->State) && $this->State )
  35. or (isset($this->Zip) && $this->Zip)) {
  36. $this->geocoder_getdata();
  37. }
  38. }
  39. if ( (!isset($this->lat)) && isset($this->City) && $this->City
  40. && isset($this->State) && $this->State) {
  41. $this->city_lookup();
  42. }
  43. if ((!isset($this->lat)) && isset($this->Zip) && $this->Zip) {
  44. $this->zip_lookup();
  45. }
  46. }
  47. function setOptions($options) {
  48. if(!is_array($options)) {
  49. $options = array($options);
  50. }
  51. foreach($options as $option => $value) {
  52. $settings = get_class_vars(get_class($this));
  53. if(array_key_exists($value, $settings)) {
  54. $this->$value = true;
  55. } else {
  56. $this->$option = $value;
  57. }
  58. }
  59. }
  60. function google_getdata(){
  61. $req = new HTTP_Request( "http://google.com" );
  62. $req->setMethod( HTTP_REQUEST_METHOD_GET );
  63. // assumes $address is *not* urlencoded
  64. $geoaddress = $this->Street.", ".$this->City.", ".$this->State.", ".$this->Zip;
  65. $req->addQueryString( 'address', $geoaddress );
  66. $req->addHeader( "User-Agent", "RadicalDesigns/AMP" );
  67. if ( !PEAR::isError( $req->sendRequest() ) ) {
  68. $result = $req->getResponseBody();
  69. } else {
  70. // failed
  71. $result = $req->getResponseHeader();
  72. //print "there was an error...";
  73. }
  74. // echo '<pre><br>geocode result<br>';var_dump($result);echo '</pre><br>';
  75. $result = '<?xml version="1.0" encoding="iso-8859-1"?>' . "\n" . $result . "\n";
  76. $xmlparser = new XML_Unserializer();
  77. $parse_result = $xmlparser->unserialize( $result, false );
  78. /*
  79. if ( PEAR::isError( $parse_result )) {
  80. print 'yah<BR>';
  81. } else {
  82. print 'nah<BR>';
  83. }
  84. */
  85. $data = $xmlparser->getUnserializedData();
  86. if (array_key_exists('geo:lat', $data['geo:Point'])) {
  87. //return array( $data['geo:Point']['geo:lat'], $data['geo:Point']['geo:long'],$result );
  88. $this->lat = $data['geo:Point']['geo:lat'];
  89. $this->long = $data['geo:Point']['geo:long'];
  90. } else {
  91. #print_r (($data));
  92. //return array( $data['geo:Point'][0]['geo:lat'], $data['geo:Point'][0]['geo:long'],$result );
  93. }
  94. }
  95. function geocoder_getdata() {
  96. $req = new HTTP_Request( "http://rpc.geocoder.us/service/rest" );
  97. $req->setMethod( HTTP_REQUEST_METHOD_GET );
  98. // assumes $address is *not* urlencoded
  99. $geoaddress = $this->Street.", ".$this->City.", ".$this->State.", ".$this->Zip;
  100. $req->addQueryString( 'address', $geoaddress );
  101. $req->addHeader( "User-Agent", "RadicalDesigns/AMP" );
  102. if ( !PEAR::isError( $req->sendRequest() ) ) {
  103. $result = $req->getResponseBody();
  104. } else {
  105. // failed
  106. $result = $req->getResponseHeader();
  107. //print "there was an error...";
  108. }
  109. // echo '<pre><br>geocode result<br>';var_dump($result);echo '</pre><br>';
  110. $xmlparser = new XML_Unserializer();
  111. $parse_result = $xmlparser->unserialize( $result, false );
  112. /*
  113. if ( PEAR::isError( $parse_result )) {
  114. print 'yah<BR>';
  115. } else {
  116. print 'nah<BR>';
  117. }
  118. */
  119. $data = $xmlparser->getUnserializedData();
  120. if (array_key_exists('geo:lat', $data['geo:Point'])) {
  121. //return array( $data['geo:Point']['geo:lat'], $data['geo:Point']['geo:long'],$result );
  122. $this->lat = $data['geo:Point']['geo:lat'];
  123. $this->long = $data['geo:Point']['geo:long'];
  124. } else {
  125. #print_r (($data));
  126. //return array( $data['geo:Point'][0]['geo:lat'], $data['geo:Point'][0]['geo:long'],$result );
  127. }
  128. }
  129. function city_lookup() {
  130. $sql = "select latitude,longitude from zipcodes where city = ".$this->dbcon->qstr($this->City)." and state = ".$this->dbcon->qstr($this->State);
  131. $R= $this->dbcon->CacheExecute($sql)or DIE("Error getting location list in functon get_latlong ".$sql.$this->dbcon->ErrorMsg());
  132. if ( $this->city_fulltext && !(($R->Fields("latitude")) && ($R->Fields("longitude"))) ){
  133. $sql = "SELECT latitude, longitude from zipcodes WHERE MATCH (city) AGAINST (".$this->dbcon->qstr($this->City).") AND state = ".$this->dbcon->qstr($this->State);
  134. $R= $this->dbcon->CacheExecute($sql)or DIE("Error getting location list in functon get_latlong ".$sql.$this->dbcon->ErrorMsg());
  135. }
  136. if ( $this->city_soundex && !(($R->Fields("latitude")) && ($R->Fields("longitude"))) ){
  137. $sql = "SELECT latitude, longitude from zipcodes WHERE city SOUNDS LIKE ".$this->dbcon->qstr($this->City)." AND state = ".$this->dbcon->qstr($this->State);
  138. $R= $this->dbcon->CacheExecute($sql)or DIE("Error getting location list in functon get_latlong ".$sql.$this->dbcon->ErrorMsg());
  139. }
  140. if ( ($R->Fields("latitude")) && ($R->Fields("longitude")) ){
  141. $this->lat = $R->Fields("latitude") ;
  142. $this->long = $R->Fields("longitude");
  143. }
  144. }
  145. function zip_lookup() {
  146. $sql = "select latitude,longitude from zipcodes where zip = ".$this->dbcon->qstr($this->Zip);
  147. if ($R=$this->dbcon->CacheGetRow($sql)) {
  148. $this->lat = $R["latitude"] ;
  149. $this->long = $R["longitude"];
  150. }
  151. }
  152. //a function that retruns an array of zip codes with a radius of mise from a set zip code
  153. function zip_radius($radius) {
  154. $zip_query='SELECT zip, zip,latitude,longitude, (ACOS((SIN(' . $this->lat . '/57.2958) * SIN(latitude/57.2958)) + (COS(' . $this->lat . '/57.2958) * COS(latitude/57.2958) * COS(longitude/57.2958 - ' . $this->long. '/57.2958)))) * 3963 AS distance FROM zipcodes WHERE (latitude >= ' . $this->lat . ' - (' . $radius . '/111)) AND (latitude <= ' . $this->lat . ' + (' . $radius . '/111)) AND (longitude >= ' . $this->long . '- (' . $radius . '/111)) AND (longitude <= ' . $this->long. '+ (' . $radius . '/111)) ORDER BY distance ASC;';
  155. if ( $zipset=$this->dbcon->CacheGetAssoc($zip_query) ) {
  156. return $zipset;
  157. } else {
  158. return false;
  159. }
  160. }
  161. //a function the returns links to othe maping programs
  162. function map_links() {
  163. $link['MapQuest'] = "http://www.mapquest.com/maps/map.adp?address=".$this->Street."&city=".$this->City."&state=".$this->State."&zipcode=".$this->Zip."&cid=lfmaplink";
  164. $link['Google Maps'] = "http://maps.google.com/maps?q=".$this->Street." ".$this->City." ".$this->State." ".$this->Zip;
  165. // the url is encoded so this only retuns the city, I guess we could query it get the url (or have a db field)
  166. $link['Yahoo Maps'] = "maps.yahoo.com/maps_result?csz=".$this->City."%2C+".$this->State."+".$this->Zip."&country=us&cat=&trf=0";
  167. return $link;
  168. }
  169. function geo_showmap() {
  170. global $Web_url;
  171. $mapcode = "mapgen?lon=" . $this->long . "&lat=" . $this->lat . "&wid=0.035&ht=0.035&iht=320&iwd=320&mark=" . $this->long . "," . $this->lat . ",redpin";
  172. ?>
  173. <script language="JavaScript">
  174. function getOffsets (evt) {
  175. var target = evt.target;
  176. if (typeof target.offsetLeft == 'undefined') {
  177. target = target.parentNode;
  178. }
  179. var pageCoords = getPageCoords(target);
  180. var eventCoords = {
  181. x: window.pageXOffset + evt.clientX,
  182. y: window.pageYOffset + evt.clientY
  183. };
  184. var offsets = {
  185. offsetX: eventCoords.x - pageCoords.x,
  186. offsetY: eventCoords.y - pageCoords.y
  187. }
  188. return offsets;
  189. }
  190. function getPageCoords (element) {
  191. var coords = {x : 0, y : 0};
  192. while (element) {
  193. coords.x += element.offsetLeft;
  194. coords.y += element.offsetTop;
  195. element = element.offsetParent;
  196. }
  197. return coords;
  198. }
  199. var mapCenterLat = <?php echo $this->lat ; ?>, mapCenterLon = <?php echo $this->long ;?>,
  200. mapPixels = 320, mapWid = 0.035;
  201. var mapLat = mapCenterLat, mapLon = mapCenterLon,
  202. mapZoom = 3, mapMaxWid = 50, mapMinWid = .001;
  203. function mapZoomOut (event) {
  204. if (mapWid * mapZoom < mapMaxWid)
  205. mapWid *= mapZoom;
  206. else
  207. mapWid = mapMaxWid;
  208. mapRedraw();
  209. return false;
  210. }
  211. function mapZoomIn (event) {
  212. if (mapWid / mapZoom > mapMinWid)
  213. mapWid /= mapZoom;
  214. else
  215. mapWid = mapMinWid;
  216. mapRedraw();
  217. return false;
  218. }
  219. function mapCenter (event) {
  220. var perPixel = mapWid / mapPixels,
  221. mapCenter = mapPixels / 2;
  222. var off = getOffsets(event);
  223. var x = off.offsetX,
  224. y = off.offsetY;
  225. mapLat += (mapCenter - y) * perPixel;
  226. mapLon += (x - mapCenter) * perPixel /
  227. Math.cos(mapLat * Math.PI / 180);
  228. mapRedraw();
  229. return false;
  230. }
  231. function mapRecenter (event) {
  232. mapLat = mapCenterLat;
  233. mapLon = mapCenterLon;
  234. mapRedraw();
  235. return false;
  236. }
  237. function mapRedraw () {
  238. document.mapLoading.src = "<?php echo $Web_url ;?>system/images/red-dot.png";
  239. document.mapImage.src =
  240. "http://tiger.census.gov/cgi-bin/mapgen?" +
  241. "lon=" + mapLon + "&lat=" + mapLat +
  242. "&wid=" + mapWid + "&ht=" + mapWid +
  243. "&iht=" + mapPixels + "&iwd=" + mapPixels +
  244. "&mark=" + mapCenterLon + "," + mapCenterLat + ",redpin";
  245. }
  246. </script>
  247. <table border="0" cellspacing="0" cellpadding="20" align="center">
  248. <tr>
  249. <td width =320 ><div onClick="return mapCenter(event)"><img
  250. name="mapImage" src="http://tiger.census.gov/cgi-bin/<?php echo $mapcode ; ?>"
  251. width="320" height="320" border="2"
  252. style="cursor: crosshair"
  253. onLoad="document.mapLoading.src = "<?php echo $Web_url ;?>system/images/green-dot.png";" /></div></td>
  254. <td valign="top"><a href="" onClick="return mapZoomIn()"><img
  255. src="<?php echo $Web_url ;?>system/images/zoom-in.png" width="32" height="32" border="0" /></a>
  256. <br>
  257. <br >
  258. <a href="" onClick="return mapZoomOut()"><img
  259. src="<?php echo $Web_url ;?>system/images/zoom-out.png" width="32" height="32" border="0" /></a>
  260. <br><br>
  261. <a href="" onClick="return mapRecenter()"><img
  262. src="<?php echo $Web_url ;?>system/images/recenter.png" width="32" height="32" border="0" /></a>
  263. <br><br>
  264. <img name="mapLoading"
  265. src="<?php echo $Web_url ;?>system/images/red-dot.png" width="32" height="32" border="0" /></td>
  266. </tr>
  267. </table> <?php
  268. }
  269. }
  270. ?>