Skip to content

Commit

Permalink
cleaned up the code a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
Dave Thau committed Jan 23, 2012
1 parent e3df4d3 commit 6e58742
Showing 1 changed file with 32 additions and 15 deletions.
47 changes: 32 additions & 15 deletions src/application/report_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from ft import FT
from flask import Response, abort, request
from StringIO import StringIO
from models import FustionTablesNames, StatsStore, FusionTablesPolygons
from models import FustionTablesNames, StatsStore


class ReportType(object):
Expand Down Expand Up @@ -61,6 +61,12 @@ def get_stats(self, report, table):
logging.error("no stats for %s" % report_id)
abort(404)
return stats

def get_polygon_name(table, id):
all_table_name = FustionTablesNames.all()
filtered_table_names = all_table_names.filter('table_id =', table)
table_names=filtered_table_names.fetch(1)[0].as_dict()
name = table_names.get(id, id)

@staticmethod
def factory(format):
Expand All @@ -78,9 +84,11 @@ class CSVReportType(ReportType):

def write_header(self):
if self.zone:
self.csv_file.writerow(('report_id', 'start_date', 'end_date', 'deforested', 'degraded'))
self.csv_file.writerow(('report_id', 'start_date', 'end_date',
'deforested', 'degraded'))
else:
self.csv_file.writerow(('report_id', 'start_date', 'end_date', 'zone_id', 'deforested', 'degraded'))
self.csv_file.writerow(('report_id', 'start_date', 'end_date',
'zone_id', 'deforested', 'degraded'))

def write_footer(self):
pass
Expand All @@ -89,8 +97,7 @@ def write_row(self, report, stats, table=None, kml=None):
name = None

if table and not self.zone:
table_names = FustionTablesNames.all().filter('table_id =', table).fetch(1)[0].as_dict()
name = table_names.get(stats['id'], stats['id'])
name = get_polygon_name(table, stats['id'])

if name:
self.csv_file.writerow((str(report.key().id()),
Expand All @@ -115,7 +122,8 @@ def response(self, file_name):
self.f.truncate(0)
return Response(result,
headers={
"Content-Disposition": "attachment; filename=\"" + file_name + ".csv\""
"Content-Disposition": "attachment; filename=\"" + file_name +
".csv\""
},
mimetype='text/csv')

Expand All @@ -127,8 +135,11 @@ def write_header(self):
self.f.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
self.f.write("<kml xmlns=\"http://www.opengis.net/kml/2.2\">")
self.f.write("<Document>")
self.f.write("<Style id=\"transGreenPoly\"><LineStyle><width>2.5</width></LineStyle><PolyStyle><color>3d00ff00</color></PolyStyle>")
self.f.write("<BalloonStyle><text>$[description]</text></BalloonStyle></Style>")
self.f.write("<Style id=\"transGreenPoly\"><LineStyle>" +
"<width>2.5</width></LineStyle><PolyStyle>" +
"<color>3d00ff00</color></PolyStyle>")
self.f.write("<BalloonStyle><text>$[description]</text>" +
"</BalloonStyle></Style>")

def write_footer(self):
self.f.write("</Document>")
Expand All @@ -138,11 +149,10 @@ def write_row(self, report, stats, table=None, kml=None):
name = None

if table:
table_names = FustionTablesNames.all().filter('table_id =', table).fetch(1)[0].as_dict()
name = table_names.get(stats['id'], stats['id'])
kml = self.kml(table, stats['id'])
name = get_polygon_name(table, stats['id'])
kml = self.kml(table, stats['id'])
else:
name = "Custom Polygon"
name = "Custom Polygon"

description = self.description(name, stats)

Expand All @@ -161,7 +171,8 @@ def response(self, file_name):
self.f.truncate(0)
return Response(result,
headers={
"Content-Disposition": "attachment; filename=\"" + file_name + ".kml\""
"Content-Disposition": "attachment; filename=\"" + file_name +
".kml\""
},
mimetype='text/kml')

Expand All @@ -177,13 +188,19 @@ def kml(self, table, row_id):
else:
id = 'name'

info = cl.sql("select geometry from %s where %s = %s" % (table, id, row_id))
info = cl.sql("select geometry from %s where %s = %s" %
(table, id, row_id))
polygon = info.split('\n')[1]
polygon = polygon.replace("\"", "")
return polygon

def description(self, name, stats):
desc = "<![CDATA[<table><tr><td><h2>" + name + "</h2></td><td></td></tr><tr><td><b>Deforestation: </b></td><td>" + str(stats['def']) + "km<sup>2</sup></td></tr><tr><td><b>Degradation: </b></td><td>" + str(stats['deg']) + "km<sup>2</sup></td></tr></table>]]>"
desc = "<![CDATA[<table><tr><td><h2>" + name
desc += "</h2></td><td></td></tr>"
desc += "<tr><td><b>Deforestation: </b></td><td>"
desc += str(stats['def']) + "km<sup>2</sup></td></tr>"
desc += "<tr><td><b>Degradation: </b></td><td>" + str(stats['deg'])
desc += "km<sup>2</sup></td></tr></table>]]>"
return desc


Expand Down

0 comments on commit 6e58742

Please # to comment.