def view(): main = Main(db) pref = main.get_user_pref() new_resource = flask.request.args.get("new_resource", False) if new_resource == "true": new_resource = True else: new_resource = False db_name = flask.request.args.get("database_type", pref[0]) typ = flask.request.args.get("type", pref[1]) country = flask.request.args.get("country", pref[2]) state = flask.request.args.get("state", pref[3]) return_type = flask.request.args.get("return_type", "") if not return_type: flask.abort(404) return return_type = return_type.lower() keys = [] values = [] if return_type == "database_type": keys, values = main.get_databases() elif return_type == "type": if not db_name: db_name = "PowerPlants" keys, values = main.get_types(db_name) elif return_type == "country": if not typ: flask.abort(404) return keys, values = main.get_countries(typ, new_resource=new_resource) elif return_type == "state": if not country: flask.abort(404) return keys, values = main.get_states(country, new_resource=new_resource) values.insert(0, [0, "All"]) return flask.jsonify(keys=keys, values=values)
def view(): main = Main(db) pref = main.get_user_pref() modules = [] select = Select(db) met = select.read("metadata") metadata = met.fetchall() module = {} module['heading'] = "Cumulative Capacity by Category" module['content'] = [] module['content'].append("<p style='text-align: center'><b>Total Cumulative Capacity: %s MWe</b></p>" % sum([t['Cumulative_Capacity'] for t in types])) module['content'].append("<div id='pie_chart' style='width: 900px; height: 500px;'></div>") module['content'] = "".join(module['content']) modules.append(module) #keys, types = main.get_types_for_country(country_id) for t in types: type_name = main.get_type_name(t['Type_ID']).replace("_", " ") db_name = main.get_databases(type_id=t['Type_ID'])[1][0][0] dbn = "" if db_name == "PowerPlants": dbn = "power plants" else: dbn = "" s_link = [] s_link.append("<a href='/summary/type/") s_link.append(str(t['Type_ID'])) s_link.append("/" + str(country_id)) s_link.append("'>Click Here</a>") map_link = [] map_link.append("<a href='/map/") map_link.append(db_name.lower() + "/") map_link.append(str(t['Type_ID']) + "/") map_link.append(str(country_id) + "/0/") map_link.append("'>Map</a>") module = {} module['heading'] = type_name module['content'] = module_content.format(type_name=type_name, db=dbn, total=t['Number_of_Plants'], cumulative_capacity=t['Cumulative_Capacity'], summary_link="".join(s_link), map_link="".join(map_link), pie_id=type_name, pie_value=t['Cumulative_Capacity']) modules.append(module) main.store_user_pref(pref[0], country_id, pref[1], pref[3]) user_pref = main.make_html_user_pref() title = "Summary for " + country_name return flask.render_template("country_summary.html", modules=modules, title=title, country=country_name, user_pref=user_pref, body_onload="Chart.plotPieChart('', 'pie_chart')")
def view(country_id=None, country_abbr=None): main = Main(db) pref = main.get_user_pref() if not country_id and not country_abbr: url = "/".join(["/summary/country", pref[2]]) #url = main.get_search_redirect_url("summary/country", # return_type='country_summary') return flask.redirect(url) elif country_abbr: country_id = main.get_country_id_from_abbr(country_abbr) return flask.redirect("/summary/country/" + str(country_id)) country_name = main.get_country_name(country_id) modules = [] select = Select(db) res = select.read("metadata", columns=["Type_ID", "Number_of_Plants", "Cumulative_Capacity_Total"], where=[["Country_ID", "=", country_id]], order_by=["Type_ID", "asc"]) types = res.fetchall() module_content = """ <table style="width: 90%"> <tr> <td style="width: 70%">Number of {type_name} {db}:</td> <td>{total}</td> </tr> <tr> <td style="width: 70%">Total Cumulative Capacity (MWe):</td> <td>{cumulative_capacity}</td> </tr> <tr> <td style="width: 70%">Map All {type_name} {db}:</td> <td>{map_link}</td> </tr> <tr> <td style="width: 70%">List All Units in Individual Plants</td> <td>{all_units_link}</td> </tr> <tr> <td style="width: 70%">Complete summary:</td> <td>{summary_link}</td> </tr> </table> <input type='hidden' id='{pie_id}' class='pie_chart_values' value='{pie_value}' /> """ module = {} module['heading'] = "Installed Capacity by Category" module['content'] = [] module['content'].append("<p style='text-align: center'><b>Total Cumulative Capacity: %s MWe</b></p>" % sum([t['Cumulative_Capacity_Total'] for t in types])) module['content'].extend(["<div id='pie_chart'>", "<div id='pie_chart_1'>", "<div id='pie_chart_1_title' style='text-align: center'>", "<b>Traditional Energy Sources</b></div>", "</div>", "<div id='pie_chart_2'>", "<div id='pie_chart_2_title' style='text-align: center'>", "<b>Renewable Energy Sources</b></div>", "</div>", "</div>"]) module['content'] = "".join(module['content']) modules.append(module) #keys, types = main.get_types_for_country(country_id) for t in types: type_name = main.get_type_name(t['Type_ID']).replace("_", " ") db_name = main.get_databases(type_id=t['Type_ID'])[1][0][0] dbn = "" if db_name == "PowerPlants": dbn = "power plants" else: dbn = "" s_link = [] s_link.append("<a href='/summary/type/") s_link.append(str(t['Type_ID'])) s_link.append("/" + str(country_id)) s_link.append("'>Click Here</a>") map_link = [] map_link.append("<a href='/map/") map_link.append(db_name.lower() + "/") map_link.append(str(t['Type_ID']) + "/") map_link.append(str(country_id) + "/0/") map_link.append("'>Map</a>") all_units = [] all_units.append("<a href='/allunits/") all_units.append(pref[0] + "/") all_units.append(str(t['Type_ID']) + "/") all_units.append(str(country_id)) all_units.append("'>All Units</a>") module = {} module['heading'] = type_name module['content'] = module_content.format(type_name=type_name, db=dbn, total=t['Number_of_Plants'], cumulative_capacity=t['Cumulative_Capacity_Total'], summary_link="".join(s_link), map_link="".join(map_link), all_units_link="".join(all_units), pie_id=type_name, pie_value=t['Cumulative_Capacity_Total']) modules.append(module) main.store_user_pref(pref[0], country_id, pref[1], pref[3]) user_pref = main.make_html_user_pref() title = "Summary for " + country_name return flask.render_template("country_summary.html", modules=modules, title=title, country=country_name, user_pref=user_pref, body_onload="CountrySummary.init()")
class DecodePolygon(object): """ Decodes the Google encoded polygon/polyline string into a list of lat lng tuples. """ def __init__(self): self.conn = connection.Db().session self.main = Main(connection.Db()) self.moderation = Moderation(connection.Db()) self.select = Select(connection.Db()) @staticmethod def decode_GMap(encoded_string): index = 0 lat = 0 lng = 0 coords = [] str_len = len(encoded_string) #print(encoded_string) #print() while index < str_len: shift = 0 result = 0 stay_in_loop = True while stay_in_loop and index < str_len: # GET THE LATITUDE b = ord(encoded_string[index]) - 63 result |= (b & 0x1f) << shift shift += 5 index += 1 if not b >= 0x20: stay_in_loop = False # Python ternary instruction.. d_lat = ~(result >> 1) if (result & 1) else (result >> 1) lat += d_lat shift = 0 result = 0 stay_in_loop = True while stay_in_loop and index < str_len: # GET THE LONGITUDE b = ord(encoded_string[index]) - 63 result |= (b & 0x1f) << shift shift += 5 index += 1 if not b >= 0x20: stay_in_loop = False # Python ternary instruction.. d_lng = ~(result >> 1) if (result & 1) else (result >> 1) lng += d_lng lng_num = lng * 1e-5 lat_num = lat * 1e-5 coords.append([str(lat_num), str(lng_num)]) return coords def decode_all(self): databases = self.main.get_databases() session = self.conn.cursor() for db in databases[1]: print(db[0]) t_keys, types = self.main.get_types(db[0]) overlays_table = "_Overlays" for typ in types: type_overlay = typ[1] + overlays_table result = self.select.read(type_overlay, columns=['Overlay_ID', 'Points']) points = result.fetchall() for point in points: if not point["Points"]: continue decoded_point = self.decode_GMap(point["Points"]) #print(point, decoded_point) sql = "UPDATE " + type_overlay + " SET Points=%(decoded_point)s WHERE Overlay_ID=%(overlay_id)s" print(sql, point["Overlay_ID"]) params = { "overlay_id": point["Overlay_ID"], "decoded_point": json.dumps(decoded_point) } session.execute(sql, params) self.conn.commit() session.close()