Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refactored datasources.

  • Loading branch information...
commit 40fbee7e0937ff88b683842b6a3901498bc82d4c 1 parent 5f89807
@varunpant authored
View
8 JavaHeatMaps/gheat/src/main/java/gheat/Tile.java
@@ -10,8 +10,7 @@
public class Tile {
private static int[] _zoomOpacity = new Opacity().BuildZoomMapping();
- private Tile()
- {
+ private Tile() {
}
@@ -174,8 +173,8 @@ private static BufferedImage ApplyWeightToImage(BufferedImage image, double weig
//I want to make the color more intense (White/bright)
tempWeight = weight;
- //AlphaComposite composite = AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.0f);
- // graphic.setComposite(composite);
+ AlphaComposite composite = AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.0f);
+ graphic.setComposite(composite);
graphic.drawImage(
image, // Source Image
@@ -236,6 +235,7 @@ public static BufferedImage GetEmptyTile(BufferedImage colorScheme, int zoomOpac
) * 255.0f);
graphic.setColor(new Color(colorSchemePixelColor.getRed(), colorSchemePixelColor.getGreen(), colorSchemePixelColor.getBlue(), zoomOpacity));
+
graphic.fillRect(0, 0, HeatMap.SIZE, HeatMap.SIZE);
graphic.dispose();
View
41 JavaHeatMaps/gheat/src/main/java/gheat/datasources/DBPool.java
@@ -15,6 +15,26 @@
private static DataSource ds;
private static SharedPoolDataSource tds;
+ static {
+ Properties dataSourceProperties = getProperties();
+ DriverAdapterCPDS cpds = new DriverAdapterCPDS();
+ try {
+ cpds.setDriver(dataSourceProperties.getProperty("driverClass"));
+ } catch (ClassNotFoundException e) {
+ e.printStackTrace();
+ }
+ cpds.setUrl(dataSourceProperties.getProperty("url"));
+ cpds.setUser(dataSourceProperties.getProperty("username"));
+ cpds.setPassword(dataSourceProperties.getProperty("password"));
+
+ tds = new SharedPoolDataSource();
+ tds.setConnectionPoolDataSource(cpds);
+ tds.setMaxActive(Integer.valueOf(dataSourceProperties.getProperty("maxActive")));
+ tds.setMaxWait(Integer.valueOf(dataSourceProperties.getProperty("maxWait")));
+
+ ds = tds;
+ }
+
private static Properties getProperties() {
Properties dataSourceProperties = new Properties();
try {
@@ -26,27 +46,6 @@ private static Properties getProperties() {
}
public static Connection getConnection() throws SQLException {
- if (ds == null) {
- Properties dataSourceProperties = getProperties();
- DriverAdapterCPDS cpds = new DriverAdapterCPDS();
- try {
- cpds.setDriver(dataSourceProperties.getProperty("driverClass"));
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- cpds.setUrl(dataSourceProperties.getProperty("url"));
- cpds.setUser(dataSourceProperties.getProperty("username"));
- cpds.setPassword(dataSourceProperties.getProperty("password"));
-
- tds = new SharedPoolDataSource();
- tds.setConnectionPoolDataSource(cpds);
- tds.setMaxActive(Integer.valueOf(dataSourceProperties.getProperty("maxActive")));
- tds.setMaxWait(Integer.valueOf(dataSourceProperties.getProperty("maxWait")));
-
- ds = tds;
-
- }
-
return ds.getConnection();
}
View
5 JavaHeatMaps/gheat/src/main/java/gheat/datasources/DataManager.java
@@ -56,8 +56,5 @@ private static DataPoint AdjustMapPixelsToTilePixels(DataPoint tileXYPoint, Data
return new DataPoint(mapPixelPoint.getX() - (tileXYPoint.getX() * HeatMap.SIZE), mapPixelPoint.getY() - (tileXYPoint.getY() * HeatMap.SIZE));
}
- public void close()
- {
- this.dataSource.close();
- }
+
}
View
4 JavaHeatMaps/gheat/src/main/java/gheat/datasources/FileDataSource.java
@@ -53,6 +53,7 @@ private void LoadPointsFromFile(String source) {
@Override
public PointLatLng[] GetList(DataPoint tlb, DataPoint lrb, int zoom, Projections _projection) {
+
List<PointLatLng> llList = null;
PointLatLng ptlb;
@@ -78,8 +79,5 @@ private void LoadPointsFromFile(String source) {
return llList.toArray(new PointLatLng[llList.size()]);
}
- @Override
- public void close() {
- }
}
View
2  JavaHeatMaps/gheat/src/main/java/gheat/datasources/HeatMapDataSource.java
@@ -8,6 +8,4 @@
public interface HeatMapDataSource {
PointLatLng[] GetList(DataPoint tlb, DataPoint lrb, int zoom, Projections _projection);
-
- void close();
}
View
45 JavaHeatMaps/gheat/src/main/java/gheat/datasources/PostGisDataSource.java
@@ -7,6 +7,7 @@
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
+import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
@@ -14,16 +15,9 @@
public class PostGisDataSource implements HeatMapDataSource {
-
- PreparedStatement pst = null;
- ResultSet rs = null;
- List<PointLatLng> llList;
- Connection con = null;
-
static String query = null;
public PostGisDataSource(String query) {
- llList = new ArrayList<PointLatLng>();
this.query = query;
}
@@ -41,21 +35,21 @@ public PostGisDataSource(String query) {
llList = getData(plrb.getLongitude(), plrb.getLatitude(), ptlb.getLongitude(), ptlb.getLatitude());
+ PointLatLng[] result =new PointLatLng[llList.size()];
+ for (int i = 0; i < llList.size(); i++) {
+ result[i] = llList.get(i);
+ }
-
- return llList.toArray(new PointLatLng[llList.size()]);
+ return result;
}
- @Override
- public void close() {
- try {
- DBPool.CloseConnections();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
private List<PointLatLng> getData(double llx, double lly, double ulx, double uly) {
+ List<PointLatLng> llList = new ArrayList<PointLatLng>();
+ Connection con = null;
+
+ PreparedStatement pst = null;
+ ResultSet rs = null;
try {
con = DBPool.getConnection();
@@ -73,12 +67,11 @@ public void close() {
while (rs.next()) {
String wkt = rs.getString("geom");
String[] points = wkt.replace("POINT(", "").replace(")", "").split(" "); //:!
- double offenses = rs.getDouble("weight");
+ double weight = rs.getDouble("weight");
+ double longitude = Double.parseDouble(points[1]);//x
+ double latitude = Double.parseDouble(points[0]); //y
- double longitude = Double.parseDouble(points[0]);//x
- double latitude = Double.parseDouble(points[1]); //y
-
- PointLatLng pt = new PointLatLng(latitude, longitude, offenses);
+ PointLatLng pt = new PointLatLng(latitude, longitude, weight);
llList.add(pt);
}
} catch (Exception ex) {
@@ -86,7 +79,13 @@ public void close() {
lgr.log(Level.SEVERE, ex.getMessage(), ex);
} finally {
-
+ try {
+ rs.close();
+ pst.close();
+ con.close();
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
return llList;
}
}
View
14 JavaHeatMaps/gheat/src/main/java/gheat/datasources/QuadTreeDataSource.java
@@ -10,7 +10,7 @@
public class QuadTreeDataSource implements HeatMapDataSource {
- QuadTree qt = new QuadTree(-180.000000, -90.000000, 180.000000, 90.000000);
+ static QuadTree qt = new QuadTree(-180.000000, -90.000000, 180.000000, 90.000000);
public QuadTreeDataSource(String filePath) {
LoadPointsFromFile(filePath);
@@ -21,7 +21,6 @@ public QuadTreeDataSource(String filePath) {
@Override
public PointLatLng[] GetList(DataPoint tlb, DataPoint lrb, int zoom, Projections _projection) {
-
PointLatLng ptlb;
PointLatLng plrb;
@@ -37,21 +36,12 @@ public QuadTreeDataSource(String filePath) {
}
-
-
-
-
- @Override
- public void close() {
- qt.clear();
- }
-
private void LoadPointsFromFile(String source) {
String[] item;
String[] lines = readAllTextFileLines(source);
for (String line : lines) {
item = line.split(",");
- qt.set(Double.parseDouble(item[2]), Double.parseDouble(item[1]), Double.parseDouble(item[0]));
+ qt.set(Double.parseDouble(item[0]), Double.parseDouble(item[1]), 0);
}
}
View
4 JavaHeatMaps/gheat/src/test/java/HeatMapTest.java
@@ -4,10 +4,6 @@
public class HeatMapTest extends TestCase {
-
-
-
-
public void testOverloadedMethod() throws Exception {
// ThemeManager.init("..\\res\\etc\\");
View
38 JavaHeatMaps/heatmaps/src/main/java/JavaHeatMaps/App.java
@@ -1,8 +1,6 @@
package JavaHeatMaps;
-import gheat.datasources.DataManager;
-import gheat.datasources.HeatMapDataSource;
-import gheat.datasources.QuadTreeDataSource;
+import gheat.datasources.*;
import gheat.graphics.ThemeManager;
import org.eclipse.jetty.server.Server;
@@ -18,23 +16,18 @@
*/
public class App {
public static DataManager dataManager = null;
- final static String query = "SELECT ST_AsText(\"wkb_geometry\") as geom ,\"offences\" as weight FROM crimedata WHERE \"wkb_geometry\" @ ST_MakeEnvelope(?,?,?,?,4326)";
-
+ static URL classpathResource = Thread.currentThread().getContextClassLoader().getResource("");
public static void main(String[] args) throws Exception {
if (dataManager == null) {
- URL classpathResource = Thread.currentThread().getContextClassLoader().getResource("");
ThemeManager.init(classpathResource.getPath() + "res/etc/");
- // HeatMapDataSource dataSource = new FileDataSource(classpathResource.getPath() + "points.txt");
- // HeatMapDataSource dataSource = new QuadTreeDataSource(classpathResource.getPath() + "points.txt");
- //HeatMapDataSource dataSource = new PostGisDataSource(query);
-
- HeatMapDataSource dataSource = new QuadTreeDataSource(classpathResource.getPath() + "points.txt");
+ //HeatMapDataSource dataSource = getFileDataSource();
+ //HeatMapDataSource dataSource = getQuadTreeDataSource();
+ HeatMapDataSource dataSource = getPostGisDataSource();
dataManager = new DataManager(dataSource);
-
System.out.println("======================================= Initialised =======================================");
}
@@ -44,7 +37,24 @@ public static void main(String[] args) throws Exception {
server.start();
server.join();
- // dataManager.close();
-
+ }
+ /*
+ Gets PostGIS data source.
+ * */
+ private static PostGisDataSource getPostGisDataSource() {
+ String query = "SELECT ST_AsText(\"wkb_geometry\") as geom ,\"offences\" as weight FROM crimedata WHERE \"wkb_geometry\" @ ST_MakeEnvelope(?,?,?,?,4326)";
+ return new PostGisDataSource(query);
+ }
+ /*
+ Gets File tree data source.
+ * */
+ private static FileDataSource getFileDataSource() {
+ return new FileDataSource(classpathResource.getPath() + "points.txt");
+ }
+ /*
+ Gets quad tree data source.
+ * */
+ private static QuadTreeDataSource getQuadTreeDataSource() {
+ return new QuadTreeDataSource(classpathResource.getPath() + "points.txt");
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.