'Sets bounds for all locations without them.
If shapely is present, and a location has wkt, bounds of the geometry
are used.  Otherwise, the (lat, lon) are used as bounds.'

@staticmethod
def set_all_bounds():

    try:
        from shapely.wkt import loads as wkt_loads
        SHAPELY = True
    except:
        SHAPELY = False
    db = current.db
    table = current.s3db.gis_location
    no_bounds = ((((((table.lon_min == None) & (table.lat_min == None)) & (table.lon_max == None)) & (table.lat_max == None)) & (table.lat != None)) & (table.lon != None))
    if SHAPELY:
        wkt_no_bounds = ((no_bounds & (table.wkt != None)) & (table.wkt != ''))
        for location in db(wkt_no_bounds).select(table.wkt):
            try:
                shape = wkt_loads(location.wkt)
            except:
                current.log.error('Error    reading    WKT', location.wkt)
                continue
            bounds = shape.bounds
            table[location.id] = dict(lon_min=bounds[0], lat_min=bounds[1], lon_max=bounds[2], lat_max=bounds[3])
    db(no_bounds).update(lon_min=table.lon, lat_min=table.lat, lon_max=table.lon, lat_max=table.lat)
