View difference between Paste ID: VXXj9sJK and MCnDqdBV
SHOW: | | - or go back to the newest paste.
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 = '" +
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
	public double averageCrashesPerDay(String street) throws SQLException
105
	{
106
		String query = "SELECT (count(*) / 365) 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 + "')";
107
108
		// create the java statement
109
		Statement st = conn.createStatement();
110
		  
111
		// execute the query, and get a java resultset
112
		ResultSet rs = st.executeQuery(query);
113
		rs.first();
114
		return rs.getDouble("crash_count");
115
	}
116
	
117
	public int numberOfCrashes(String street, String date) throws SQLException
118
	{
119
		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')";
120
121
		// create the java statement
122
		Statement st = conn.createStatement();
123
		  
124
		// execute the query, and get a java resultset
125
		ResultSet rs = st.executeQuery(query);
126
		rs.first();
127
		return rs.getInt("crash_count");
128
	}
129
	
130
	public double increase(double first, double second)
131
	{
132
		double ratio = first / second;
133
		return ratio * 100.0;
134
	}
135
	
136
	public double increaseInRisk(double riskIncrease1, double riskIncrease2, double crashIncreaseCount1, double crashIncreaseCount2)
137
	{
138
		if (riskIncrease1 > riskIncrease2 && crashIncreaseCount1 > crashIncreaseCount2)
139
		{
140
			return increase(riskIncrease1, riskIncrease2);
141
		}
142
		else
143
		{
144
			return 0;
145
		}
146
	}
147
	
148
	public double compareTwoStreets(String street1, String street2, String date) throws SQLException
149
	{
150
		double average1 = averageCrashesPerDay(street1);
151
		double average2 = averageCrashesPerDay(street2);
152
		double number1 = numberOfCrashes(street1, date);
153
		double number2 = numberOfCrashes(street2, date);
154
		double riskInrease1 = increase(number1, average1);
155
		double riskInrease2 = increase(number2, average2);
156
		return increaseInRisk(riskInrease1, riskInrease2, number1 - average1, number2 - average2);
157
	}
158
}