'Returns the locations for an XML export
- used by GIS.get_location_data() and S3PivotTable.geojson()
@ToDo: Support multiple locations for a single resource
(e.g. a Project working in multiple Communities)'

@staticmethod
def get_locations(table, query, join=True, geojson=True):

    db = current.db
    tablename = table._tablename
    gtable = current.s3db.gis_location
    settings = current.deployment_settings
    tolerance = settings.get_gis_simplify_tolerance()
    output = {}
    if settings.get_gis_spatialdb():
        if geojson:
            precision = settings.get_gis_precision()
            if tolerance:
                web2py_installed_version = parse_version(global_settings.web2py_version)
                web2py_installed_datetime = web2py_installed_version[4]
                if (web2py_installed_datetime >= datetime.datetime(2015, 1, 17, 0, 7, 4)):
                    rows = db(query).select(table.id, gtable.the_geom.st_simplifypreservetopology(tolerance).st_asgeojson(precision=precision).with_alias('geojson'))
                else:
                    rows = db(query).select(table.id, gtable.the_geom.st_simplify(tolerance).st_asgeojson(precision=precision).with_alias('geojson'))
            else:
                rows = db(query).select(table.id, gtable.the_geom.st_asgeojson(precision=precision).with_alias('geojson'))
            for row in rows:
                key = row[tablename].id
                if (key in output):
                    output[key].append(row.geojson)
                else:
                    output[key] = [row.geojson]
        else:
            if tolerance:
                rows = db(query).select(table.id, gtable.the_geom.st_simplify(tolerance).st_astext().with_alias('wkt'))
            else:
                rows = db(query).select(table.id, gtable.the_geom.st_astext().with_alias('wkt'))
            for row in rows:
                key = row[tablename].id
                if (key in output):
                    output[key].append(row.wkt)
                else:
                    output[key] = [row.wkt]
    else:
        rows = db(query).select(table.id, gtable.wkt)
        simplify = GIS.simplify
        if geojson:
            if join:
                for row in rows:
                    g = simplify(row['gis_location'].wkt, tolerance=tolerance, output='geojson')
                    if g:
                        key = row[tablename].id
                        if (key in output):
                            output[key].append(g)
                        else:
                            output[key] = [g]
            else:
                for row in rows:
                    g = simplify(row.wkt, tolerance=tolerance, output='geojson')
                    if g:
                        output[row.id] = g
        elif join:
            if tolerance:
                for row in rows:
                    wkt = simplify(row['gis_location'].wkt)
                    if wkt:
                        key = row[tablename].id
                        if (key in output):
                            output[key].append(wkt)
                        else:
                            output[key] = [wkt]
            else:
                for row in rows:
                    wkt = row['gis_location'].wkt
                    if wkt:
                        key = row[tablename].id
                        if (key in output):
                            output[key].append(wkt)
                        else:
                            output[key] = [wkt]
        elif tolerance:
            for row in rows:
                wkt = simplify(row.wkt)
                if wkt:
                    output[row.id] = wkt
        else:
            for row in rows:
                wkt = row.wkt
                if wkt:
                    output[row.id] = wkt
    return output
