'Reverse Geocode a Lat/Lon
- used by S3LocationSelector'

@staticmethod
def geocode_r(lat, lon):

    if ((not lat) or (not lon)):
        return 'Need    Lat    &    Lon'
    results = ''
    try:
        lat = float(lat)
    except ValueError:
        results = 'Latitude    is    Invalid!'
    try:
        lon = float(lon)
    except ValueError:
        results += 'Longitude    is    Invalid!'
    if (not results):
        if ((lon > 180) or (lon < (-180))):
            results = 'Longitude    must    be    between    -180    &    180!'
        elif ((lat > 90) or (lat < (-90))):
            results = 'Latitude    must    be    between    -90    &    90!'
        else:
            table = current.s3db.gis_location
            query = ((table.level != None) & (table.deleted != True))
            if current.deployment_settings.get_gis_spatialdb():
                point = ('POINT(%s    %s)' % (lon, lat))
                query &= table.the_geom.st_intersects(point)
                rows = current.db(query).select(table.id, table.level)
                results = {}
                for row in rows:
                    results[row.level] = row.id
            else:
                query &= ((((table.lat_min < lat) & (table.lat_max > lat)) & (table.lon_min < lon)) & (table.lon_max > lon))
                rows = current.db(query).select(table.id, table.level, table.wkt)
                from shapely.geometry import point
                from shapely.wkt import loads as wkt_loads
                test = point.Point(lon, lat)
                results = {}
                for row in rows:
                    shape = wkt_loads(row.wkt)
                    ok = test.intersects(shape)
                    if ok:
                        results[row.level] = row.id
    return results
