'Get the base location of the instance(s)
@param _fields: fields to retrieve from the location records (None for ALL)
@param _filter: filter for the locations
@param as_rows: return the result as Rows object
@param empty: return None if no locations (set to False by gis.get_location_data())
@return: the base location(s) of the current instance'

def get_base_location(self, _fields=None, _filter=None, as_rows=False, empty=True):

    db = current.db
    s3db = current.s3db
    ltable = s3db[LOCATION]
    rtable = self.rtable
    locations = []
    for r in self.records:
        location = None
        query = None
        if (LOCATION_ID in r):
            query = (ltable.id == r[LOCATION_ID])
            if rtable:
                query = (query & (rtable[LOCATION_ID] == ltable.id))
                if (TRACK_ID in r):
                    query = (query & (rtable[TRACK_ID] == r[TRACK_ID]))
        elif (TRACK_ID in r):
            q = (self.table[TRACK_ID] == r[TRACK_ID])
            trackable = db(q).select(limitby=(0, 1)).first()
            table = s3db[trackable.instance_type]
            if (LOCATION_ID in table.fields):
                query = ((table[TRACK_ID] == r[TRACK_ID]) & (table[LOCATION_ID] == ltable.id))
        if query:
            if (_filter is not None):
                query = (query & _filter)
            if (not _fields):
                location = db(query).select(ltable.ALL, limitby=(0, 1)).first()
            else:
                location = db(query).select(limitby=(0, 1), *_fields).first()
        if location:
            locations.append(location)
        elif (not empty):
            locations.append(Row({'lat': None, 'lon': None}))
    if as_rows:
        return Rows(records=locations, compact=False)
    if (not locations):
        return None
    elif (len(locations) == 1):
        return locations[0]
    else:
        return locations
