Skip to content

Commit

Permalink
much faster filtering of sightings
Browse files Browse the repository at this point in the history
  • Loading branch information
camillol committed Oct 26, 2011
1 parent c90ff16 commit dc00403
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 106 deletions.
93 changes: 31 additions & 62 deletions Sighting.pde
Expand Up @@ -102,76 +102,45 @@ void loadCities()
println(stopWatch());
}

String getStringQueryByDateTime(){
class SightingsFilter {
final static int yearFirst = 2000, yearLast = 2011;
int viewMinYear = yearFirst, viewMaxYear = yearLast;
int viewMinMonth = 1, viewMaxMonth = 12;
int viewMinHour = 0, viewMaxHour = 23;

ArrayList queryString = new ArrayList();
String finalQueryString="";
String string_;

if (!yearMin.equals(yearLabelsToPrint[0]) || !yearMax.equals(yearLabelsToPrint[yearLabelsToPrint.length-1])){
for(int i = int(yearMin); i<= int(yearMax); i++){
if (!btwMonths || (btwMonths && (monthMin.equals(monthLabels[0]) && monthMax.equals(monthLabels[monthLabels.length-1])))){
if (!btwTime || (btwTime && (timeMin.equals(timeLabels[0]) && timeMax.equals(timeLabels[timeLabels.length-1])))){
queryString.add(i+".%.%");
}
else{
for(int j = int(timeMin) ; j <= int(timeMax) ; j++){
queryString.add(i+"%"+nf(j,2)+":%:%");
}
}
}
else{
for(int j= int(monthMin); j<= int(monthMax);j++){
if (!btwTime || (btwTime && (timeMin.equals(timeLabels[0]) && timeMax.equals(timeLabels[timeLabels.length-1])))){
queryString.add(i+"."+nf(j,2)+".%");
}
else{
for(int m = int(timeMin) ; m <= int(timeMax);m++){
queryString.add(i+"."+nf(j,2)+".% "+nf(m,2)+":%:%");
}
}
}
}
}
}
else{
if (!btwMonths || (btwMonths && (monthMin.equals(monthLabels[0]) && monthMax.equals(monthLabels[monthLabels.length-1])))){
if (btwTime && (!timeMin.equals(timeLabels[0]) || !timeMax.equals(timeLabels[timeLabels.length-1]))){
for(int j = int(timeMin) ; j <= int(timeMax) ; j++){
queryString.add("%"+nf(j,2)+":%:%");
}
}
}
else{
for(int j= int(monthMin); j<= int(monthMax);j++){
if (!btwTime || (btwTime && (timeMin.equals(timeLabels[0]) && timeMax.equals(timeLabels[timeLabels.length-1])))){
queryString.add("%."+nf(j,2)+".%");
}
else{
for(int m = int(timeMin) ; m <= int(timeMax);m++){
queryString.add("%."+nf(j,2)+".% "+nf(m,2)+":%:%");
}
}
}
}
String whereClause()
{
StringBuffer where = new StringBuffer();

if (viewMinYear > yearFirst) where.append("cast(strftime('%Y',occurred_at) as integer) >= " + viewMinYear + " and ");
if (viewMaxYear < yearLast) where.append("cast(strftime('%Y',occurred_at) as integer) <= " + viewMaxYear + " and ");
if (viewMinMonth > 1) where.append("cast(strftime('%m',occurred_at) as integer) >= " + viewMinMonth + " and ");
if (viewMaxMonth < 12) where.append("cast(strftime('%m',occurred_at) as integer) <= " + viewMaxMonth + " and ");
if (viewMinHour > 0) where.append("cast(strftime('%H',occurred_at) as integer) >= " + viewMinHour + " and ");
if (viewMaxHour < 23) where.append("cast(strftime('%H',occurred_at) as integer) <= " + viewMaxHour + " and ");

where.append("1 ");
return where.toString();
}

for (int i=0; i< queryString.size();i++){
finalQueryString = finalQueryString + ((finalQueryString.length() > 0)?(" or occurred_at like '" +queryString.get(i)+"'"):(" occurred_at like '" +queryString.get(i)+"'"));

boolean equals(SightingsFilter other)
{
return viewMinYear == other.viewMinYear &&
viewMaxYear == other.viewMaxYear &&
viewMinMonth == other.viewMinMonth &&
viewMaxMonth == other.viewMaxMonth &&
viewMinHour == other.viewMinHour &&
viewMaxHour == other.viewMaxHour;
}
println("query " + finalQueryString);
return finalQueryString;
}

void reloadCitySightingCounts()
{
stopWatch();
print("query db for sighting counts...");
String stringQuery = getStringQueryByDateTime();

db.query("select cities.id, count(*) as sighting_count, count(distinct type_id) as types_count,type_id"
+ " from cities join sightings on sightings.city_id = cities.id join shapes on shape_id = shapes.id"
+ ((stringQuery.length() > 0)?(" where "+stringQuery):"")
+ " where " + activeFilter.whereClause()
+ " group by cities.id");

minCountSightings = 1000;
Expand Down Expand Up @@ -244,9 +213,9 @@ void loadSightingTypes()

List<Sighting> sightingsForCity(Place p)
{
String stringQuery = getStringQueryByDateTime();

db.query("select * from sightings join shapes on shape_id = shapes.id where city_id = "+p.id+((stringQuery.length() > 0)?(" and ("+stringQuery+")"):"")+" order by occurred_at;");
db.query("select * from sightings join shapes on shape_id = shapes.id"
+ " where city_id = " + p.id + " and " + activeFilter.whereClause()
+ " order by occurred_at;");

ArrayList<Sighting> sightings = new ArrayList<Sighting>();
while (db.next()) {
Expand Down
77 changes: 33 additions & 44 deletions cs424p3.pde
Expand Up @@ -16,7 +16,7 @@ SightingDetailsView sightingDetailsView;
DateFormat dateTimeFormat= new SimpleDateFormat("EEEE, MMMM dd, yyyy HH:mm");
DateFormat dateFormat= new SimpleDateFormat("EEEE, MMMM dd, yyyy");
DateFormat shortDateFormat= new SimpleDateFormat("MM/dd/yyyy HH:mm");
DateFormat dbDateFormat= new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
DateFormat dbDateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

color backgroundColor = 0;
color textColor = 255;
Expand All @@ -28,6 +28,7 @@ color[] UFOColors = {#000000,#ffffff,#555555,#333333,#444444,#555555,#666666};

int normalFontSize = 13;
int smallFontSize = 9 ;

String[] monthLabelsToPrint = {"January","February","March","April","May","June","July","August","September","October","November","December"};
String[] monthLabels = {"01","02","03","04","05","06","07","08","09","10","11","12"};
String[] yearLabels = {"'00","'01","'02","'03","'04","'05","'06","'07","'08","'09","'10","'11"};
Expand All @@ -45,12 +46,13 @@ Map<Integer,SightingType> sightingTypeMap;

Sighting clickedSighting;
Boolean showAirports=false;
String yearMin,yearMax,monthMin,monthMax,timeMin,timeMax;
Boolean btwMonths = false;
Boolean btwTime = false;
String byType = "";
Boolean isDragging = false;

SightingsFilter activeFilter;

import de.bezier.data.sql.*;

SQLite db;
Expand All @@ -70,14 +72,8 @@ void setup()
if (!db.connect()) println("DB connection failed!");
db.execute("PRAGMA cache_size=100000;");


yearMin = yearLabelsToPrint[0];
yearMax = yearLabelsToPrint[yearLabelsToPrint.length-1];
monthMin = monthLabels[0];
monthMax = monthLabels[monthLabels.length-1];
timeMin = timeLabels[0];
timeMax = timeLabels[timeLabels.length-1];

activeFilter = new SightingsFilter();

loadSightingTypes();
loadCities();
loadAirports();
Expand Down Expand Up @@ -146,46 +142,39 @@ void mouseClicked()
println(tmpByType);

if (btwTime != settingsView.timeCheckbox.value || btwMonths != settingsView.monthCheckbox.value || !byType.equals(tmpByType)){
btwTime = settingsView.timeCheckbox.value;
btwMonths = settingsView.monthCheckbox.value;
byType = tmpByType;
monthMin = monthLabels[settingsView.monthSlider.minIndex()];
monthMax = monthLabels[settingsView.monthSlider.maxIndex()];
timeMin = timeLabels[settingsView.timeSlider.minIndex()];
timeMax = timeLabels[settingsView.timeSlider.maxIndex()];

reloadCitySightingCounts();
mapv.rebuildOverlay();
detailsAnimator.target(height);
btwTime = settingsView.timeCheckbox.value;
btwMonths = settingsView.monthCheckbox.value;;
updateFilter();
}

rootView.mouseClicked(mouseX, mouseY);
}

void updateFilter()
{
SightingsFilter newFilter = new SightingsFilter();
newFilter.viewMinYear = 2000 + settingsView.yearSlider.minIndex();
newFilter.viewMaxYear = 2000 + settingsView.yearSlider.maxIndex();
if (btwMonths) {
newFilter.viewMinMonth = 1 + settingsView.monthSlider.minIndex();
newFilter.viewMaxMonth = 1 + settingsView.monthSlider.maxIndex();
}
if (btwTime) {
newFilter.viewMinHour = 1 + settingsView.timeSlider.minIndex();
newFilter.viewMaxHour = 1 + settingsView.timeSlider.maxIndex();
}

if (!newFilter.equals(activeFilter)) {
activeFilter = newFilter;
reloadCitySightingCounts();
mapv.rebuildOverlay();
detailsAnimator.target(height);
}
}

void mouseReleased(){
if (isDragging){
if (!yearMin.equals(yearLabelsToPrint[settingsView.yearSlider.minIndex()]) || !yearMax.equals(yearLabelsToPrint[settingsView.yearSlider.maxIndex()])){
yearMin = yearLabelsToPrint[settingsView.yearSlider.minIndex()] ;
yearMax = yearLabelsToPrint[settingsView.yearSlider.maxIndex()];
reloadCitySightingCounts();
mapv.rebuildOverlay();
detailsAnimator.target(height);
}
else if (btwMonths && (!monthMin.equals(monthLabels[settingsView.monthSlider.minIndex()]) || !monthMax.equals(monthLabels[settingsView.monthSlider.maxIndex()]))){
monthMin = monthLabels[settingsView.monthSlider.minIndex()];
monthMax = monthLabels[settingsView.monthSlider.maxIndex()];
reloadCitySightingCounts();
mapv.rebuildOverlay();
detailsAnimator.target(height);
}
else if (btwTime && (!timeMin.equals(timeLabels[settingsView.timeSlider.minIndex()]) || !timeMax.equals(timeLabels[settingsView.timeSlider.maxIndex()]))){
timeMin = timeLabels[settingsView.timeSlider.minIndex()];
timeMax = timeLabels[settingsView.timeSlider.maxIndex()];
reloadCitySightingCounts();
mapv.rebuildOverlay();
detailsAnimator.target(height);
}

if (isDragging) {
updateFilter();
}
isDragging = false;
}
Expand Down

0 comments on commit dc00403

Please sign in to comment.