'Export Projects as GeoJSON Polygons to view on the map
- currently assumes that theme_percentages=True
@ToDo: complete'

@staticmethod
def project_polygons(r, **attr):

    db = current.db
    s3db = current.s3db
    ptable = s3db.project_project
    ttable = s3db.project_theme
    tptable = s3db.project_theme_project
    pltable = s3db.project_location
    ltable = s3db.gis_location
    themes = db((ttable.deleted == False)).select(ttable.id, ttable.name, orderby=ttable.name)
    countries = {}
    query = ((((ptable.deleted == False) & (tptable.project_id == ptable.id)) & (ptable.id == pltable.project_id)) & (ltable.id == pltable.location_id))
    projects = db(query).select()
    for project in projects:
        _countries = project.location_id
        if (len(_countries) == 1):
            country = _countries[0]
            if (country in countries):
                budget = project.project_project.total_annual_budget()
                theme = project.project_theme_project.theme_id
                percentage = project.project_theme_project.percentage
                countries[country][theme] += (budget * percentage)
            else:
                name = db((ltable.id == country)).select(ltable.name).first().name
                countries[country] = dict(name=name)
                for theme in themes:
                    countries[country][theme.id] = 0
                budget = project.project_project.total_annual_budget()
                theme = project.project_theme_project.theme_id
                percentage = project.project_theme_project.percentage
                countries[country][theme] += (budget * percentage)
    query = ltable.id.belongs(countries)
    locations = db(query).select(ltable.id, ltable.wkt)
    for location in locations:
        pass
    output = json.dumps({})
    current.response.headers['Content-Type'] = 'application/json'
    return output
