Example #1
0
def get_estimates_data_service(input_datetime=None, input_date=None, lat=None, lon=None):
    db = MySQLdb.connect("localhost","pollution","pollution","pollution_monitoring" )

    # prepare a cursor object using cursor() method
    cursor = db.cursor()
    # return a grid back
    if input_datetime and not input_date:
        #http://162.222.176.235/modeling/get_estimates_data?input_datetime=2016-09-10%2010:00:00
        #get the coords of sydney, correct and offset lat/lon to centre of each square in grid, for google maps
        coords = np.array(get_coords_sydney(centre_offset=False))
        coords = coords.reshape(10000,2)
        coords = coords.tolist()
        try:
            sql_str = """select grid_location_row, grid_location_col, co_original from {0} where datetime="{1}" order by grid_location_row, grid_location_col;""".format(estimates_table, input_datetime)
            cursor.execute(sql_str)
        except:
            raise Exception("Error in : ", sql_str)

        results = cursor.fetchall()

        if len(results) == 0:
            results = generate_data_from_model(input_datetime, input_date, lat, lon)
            #results = {"error":"no results found for datetime input. Please check your input again"}

        results = [(row[0], row[1], float(row[2])) for row in results]
        results = zip(coords, results)
        #print results
        results = {input_datetime : results}

    elif input_date and lat and lon:
        #http://162.222.176.235/modeling/get_estimates_data?input_date=2015-09-11&lat=-33.92313&lon=150.98812
        #if this is slow, it's an index problem
        grid_location_row, grid_location_col = get_index(lat, lon)

        try:
            sql_str = """select time, co_original from {} where date="{}" and grid_location_row={} and grid_location_col={} order by datetime;""".format(estimates_table, input_date, grid_location_row, grid_location_col)
            cursor.execute(sql_str)
        except:
            raise Exception("Error in : ", sql_str)

        results = cursor.fetchall()

        if len(results) == 0:
            results = generate_data_from_model(input_datetime, input_date, lat, lon)

        results = {input_date : [(row[0], float(row[1])) for row in results], "grid_location_row": grid_location_row, "grid_location_col": grid_location_col}
    else:
        results = {"error":"no output given the provided input parameters. Please check your input again"}

    return results
timeseries = False

if form.getvalue('lat') is not None or form.getvalue('lat') is not None:
    lat = float(form.getvalue('lat'))
    lon = float(form.getvalue('lon'))
    timeseries = True
db = MySQLdb.connect("localhost","pollution","pollution","pollution_monitoring" )

# prepare a cursor object using cursor() method
cursor = db.cursor()
# return a grid back
if not timeseries:
    #http://162.222.176.235/cgi-bin/get_estimates_data.py?input_datetime=2015-09-10%2010:00:00
    #get the coords of sydney, correct and offset lat/lon to centre of each square in grid, for google maps
    coords = np.array(get_coords_sydney(centre_offset=False))
    coords = coords.reshape(10000,2)
    coords = coords.tolist()
    sql_str = """select grid_location, co from {0} where datetime="{1}" order by grid_location;""".format(estimates_table, input_datetime)
    cursor.execute(sql_str)
    results = cursor.fetchall()
    results = [(row[0], float(row[1])) for row in results]
    results = zip(coords, results)
    #print results
    results = {input_datetime : results}
elif input_datetime is None :
    # e.g. http://162.222.176.23/cgi-bin/get_estimates_data.py?input_date=2015-09-10&lat=-33.92313&lon=150.98812
    #TODO - if this is slow, it's an index problem
    location = get_flattened_index(lat, lon)

    sql_str = """select time, co from {0} where date="{1}" and grid_location={2} order by datetime;""".format(estimates_table, input_date, location)
#print get_coords_sydney()


#define grid square size
GRID_RES = 100

# create instance of FieldStorage
form			= cgi.FieldStorage()

# Get data from fields
fname			= form.getvalue('first_name')
lname			= form.getvalue('last_name')

map_coord_array = [[0 for i in xrange(GRID_RES)] for x in xrange(GRID_RES)]
map_coord_array = get_coords_sydney();

#the line below gives error page but can check the coords in array immediately
#print {0}.format(map_coord_array[1][1])
#for i in xrange(GRID_RES):
#  for j in xrange(GRID_RES):
#    print map_coord_array[i][j]


data_points = [ [-33.91793,151.22693],
                [-33.91993, 151.22703],
                [-33.91500, 151.22803]]
weights = [3,1,10]
#TODO - add the scale in some corner of the gmaps window

#TODO