Advertisement
Guest User

Untitled

a guest
May 24th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.sql.*;
  2. import java.util.Scanner;
  3.  
  4.  
  5. public class CollisionAvoider {
  6.  
  7. static final String JDBC_DRIVER = "org.mariadb.jdbc.Driver";
  8. static final String DB_URL = "jdbc:mariadb://localhost/mining";
  9.  
  10. // Database credentials
  11. static final String USER = "root";
  12. static final String PASS = "123456";
  13. private static Scanner reader;
  14. public static Connection conn;
  15.  
  16. private double distance(double lat1, double lon1, double lat2, double lon2, String unit) {
  17. double theta = lon1 - lon2;
  18. double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));
  19. dist = Math.acos(dist);
  20. dist = rad2deg(dist);
  21. dist = dist * 60 * 1.1515;
  22. if (unit == "K") {
  23. dist = dist * 1.609344;
  24. } else if (unit == "N") {
  25. dist = dist * 0.8684;
  26. }
  27.  
  28. return (dist);
  29. }
  30.  
  31. /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
  32. /*:: This function converts decimal degrees to radians :*/
  33. /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
  34. private double deg2rad(double deg) {
  35. return (deg * Math.PI / 180.0);
  36. }
  37.  
  38. /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
  39. /*:: This function converts radians to decimal degrees :*/
  40. /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
  41. private double rad2deg(double rad) {
  42. return (rad * 180 / Math.PI);
  43. }
  44.  
  45. public void makeConnection() throws ClassNotFoundException, SQLException
  46. {
  47. Class.forName(JDBC_DRIVER);
  48. System.out.println("Connecting to a selected database...");
  49. conn = DriverManager.getConnection(
  50. DB_URL, USER, PASS);
  51. System.out.println("Connected database successfully...");
  52. }
  53.  
  54. public double fahrenheitToCelcius(double fahrenheit)
  55. {
  56. //(°F - 32) x 5/9 = °C
  57. return (fahrenheit - 32) * 5.0/9.0;
  58. }
  59.  
  60. public boolean checkBadWeather(double latitude, double longitude, String day, String month, String year) throws SQLException
  61. {
  62. String query = "SELECT * FROM gsod g, stations s WHERE (g.temp < 32 OR g.fog = 1 OR g.rain_drizzle = 1 OR g.snow_ice_pellets = 1 OR g.hail =1 OR g.thunder = 1 OR g.tornado_funnel_cloud = 1) AND g.year = '" + year + "' AND g.mo = '" + month + "' AND g.da = '" +
  63. day + "' AND s.usaf = g.stn AND s.wban = g.wban";
  64.  
  65. // create the java statement
  66. Statement st = conn.createStatement();
  67.  
  68. // execute the query, and get a java resultset
  69. ResultSet rs = st.executeQuery(query);
  70. // iterate through the java resultset
  71. while (rs.next())
  72. {
  73. double distanceBetween = distance(rs.getFloat("s.lat"), rs.getFloat("s.lon"), latitude, longitude, "K");
  74. if (distanceBetween < 10)
  75. {
  76. return true;
  77. }
  78. }
  79. return false;
  80. }
  81.  
  82. public boolean checkBadWeather(String street, String day, String month, String year) throws SQLException
  83. {
  84. String query = "SELECT * FROM gsod g, stations s, nypd_mv_collisions m WHERE (g.temp < 32 OR g.fog = 1 OR g.rain_drizzle = 1 OR g.snow_ice_pellets = 1 OR g.hail =1 OR g.thunder = 1 OR g.tornado_funnel_cloud = 1) AND g.year = '" + year + "' AND g.mo = '" + month + "' AND g.da = '" +
  85. day + "' AND s.usaf = g.stn AND s.wban = g.wban AND (m.on_street_name LIKE '" + street + "' OR m.off_street_name LIKE '" + street + "' OR m.cross_street_name LIKE '" + street + "')";
  86.  
  87. // create the java statement
  88. Statement st = conn.createStatement();
  89.  
  90. // execute the query, and get a java resultset
  91. ResultSet rs = st.executeQuery(query);
  92. // iterate through the java resultset
  93. while (rs.next())
  94. {
  95. double distanceBetween = distance(rs.getFloat("s.lat"), rs.getFloat("s.lon"), rs.getFloat("m.latitude"), rs.getFloat("m.longitude"), "K");
  96. if (distanceBetween < 10)
  97. {
  98. return true;
  99. }
  100. }
  101. return false;
  102. }
  103.  
  104.  
  105.  
  106. public int numberOfCrashes(String street, String date) throws SQLException
  107. {
  108. String query = "SELECT count(*) as crash_count FROM nypd_mv_collisions m WHERE (m.on_street_name LIKE '" + street + "' OR m.off_street_name LIKE '" + street + "' OR m.cross_street_name LIKE '" + street + "') AND DATE(m.timestamp) = STR_TO_DATE('" + date + "', '%Y-%m-%d')";
  109.  
  110. // create the java statement
  111. Statement st = conn.createStatement();
  112.  
  113. // execute the query, and get a java resultset
  114. ResultSet rs = st.executeQuery(query);
  115. rs.first();
  116. return rs.getInt("crash_count");
  117. }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement