PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/bin/phpsqlgeocode_zipcodes.php

https://bitbucket.org/southsidehealth/southsidehealth
PHP | 93 lines | 71 code | 9 blank | 13 comment | 18 complexity | 4668ee89465fee8059de73e624d1c821 MD5 | raw file
Possible License(s): Apache-2.0, GPL-3.0, LGPL-2.1
  1. #!/usr/local/bin/php
  2. <?php
  3. $username='phchicag_southsi';
  4. $password='UCHmed123$';
  5. $database='phchicag_southsidehealth';
  6. define("MAPS_HOST", "maps.google.com");
  7. define("KEY", "ABQIAAAAZEQNWC_mkqR1FG3LZ6IO_BQ3rXMm_haGisAq50SJwel3-DW5fBRYwsvipL7X5BjjPveR-vQi79OoGA");
  8. // Opens a connection to a MySQL server
  9. $connection = mysql_connect("localhost", $username, $password);
  10. if (!$connection) {
  11. die("Not connected : " . mysql_error());
  12. }
  13. // Set the active MySQL database
  14. $db_selected = mysql_select_db($database, $connection);
  15. if (!$db_selected) {
  16. die("Can\'t use db : " . mysql_error());
  17. }
  18. // Select all the rows in the markers table
  19. $query = "SELECT * FROM zipcodes where lat = 0";
  20. echo $query . "\n";
  21. $result = mysql_query($query);
  22. if (!$result) {
  23. die("Invalid query: " . mysql_error());
  24. }
  25. // Initialize delay in geocode speed
  26. $delay = 0;
  27. $base_url = "http://" . MAPS_HOST . "/maps/geo?output=xml" . "&key=" . KEY;
  28. // Iterate through the rows, geocoding each address
  29. while ($row = @mysql_fetch_assoc($result)) {
  30. $geocode_pending = true;
  31. while ($geocode_pending) {
  32. $address = '';
  33. /*if(!empty($row["state"])){
  34. $address = $row["state"] . "," ;
  35. }*/
  36. if(!empty($row["zipcode"])){
  37. if($row["zipcode"] < 10000){
  38. $address = $row["state"] . ", USA 0" . $row["zipcode"];
  39. }
  40. else{
  41. $address = $row["state"] . ", USA " . $row["zipcode"];
  42. }
  43. }
  44. $id = $row["id"];
  45. echo $address . "\n";
  46. $request_url = $base_url . "&q=" . urlencode($address);
  47. $xml = simplexml_load_file($request_url, NULL, LIBXML_NOWARNING) or die("url not loading");
  48. $status = $xml->Response->Status->code;
  49. if (strcmp($status, "200") == 0) {
  50. // Successful geocode
  51. print ("Successfully geocoded " . $address . "\n");
  52. $geocode_pending = false;
  53. $coordinates = $xml->Response->Placemark->Point->coordinates;
  54. $coordinatesSplit = split(",", $coordinates);
  55. // Format: Longitude, Latitude, Altitude
  56. $lat = $coordinatesSplit[1];
  57. $lng = $coordinatesSplit[0];
  58. $query = sprintf("UPDATE zipcodes" .
  59. " SET lat = '%s', lng = '%s' " .
  60. " WHERE id = '%s' LIMIT 1;",
  61. mysql_real_escape_string($lat),
  62. mysql_real_escape_string($lng),
  63. mysql_real_escape_string($id));
  64. $update_result = mysql_query($query);
  65. if (!$update_result) {
  66. die("Invalid query: " . mysql_error());
  67. }
  68. } else if (strcmp($status, "620") == 0) {
  69. // sent geocodes too fast
  70. $delay += 100000;
  71. }else if ((strcmp($status, "602") == 0) && $include_address2 == false) {
  72. $include_address2 = true;
  73. }
  74. else {
  75. // failure to geocode
  76. $geocode_pending = false;
  77. print "Address " . $address . " failed to geocoded. ";
  78. print "Received status " . $status . "
  79. \n";
  80. }
  81. usleep($delay);
  82. }
  83. }
  84. ?>