'Update GIS Locations\' Materialized path, Lx locations, Lat/Lon & the_geom
@param feature: a feature dict to update the tree for
- if not provided then update the whole tree
@param all_locations: passed to recursive calls to indicate that this
is an update of the whole tree. Used to avoid repeated attempts to
update hierarchy locations with missing data (e.g. lacking some
ancestor level).
@param propagating: passed to recursive calls to indicate that this
is a propagation update. Used to avoid repeated attempts to
update hierarchy locations with missing data (e.g. lacking some
ancestor level).
returns the path of the feature
Called onaccept for locations (async, where-possible)'

@staticmethod
def update_location_tree(feature=None, all_locations=False, propagating=False):

    if GIS.disable_update_location_tree:
        return None
    db = current.db
    try:
        table = db.gis_location
    except:
        table = current.s3db.gis_location
    spatial = current.deployment_settings.get_gis_spatialdb()
    update_location_tree = GIS.update_location_tree
    wkt_centroid = GIS.wkt_centroid
    fields = (table.id, table.name, table.level, table.path, table.parent, table.L0, table.L1, table.L2, table.L3, table.L4, table.L5, table.lat, table.lon, table.wkt, table.inherited)
    def fixup(feature):
        '\n                                                                Fix    all    the    issues    with    a    Feature,    assuming    that\n                                                                -    the    corrections    are    in    the    feature\n                                                                -    or    they    are    Bounds    /    Centroid    /    WKT    /    the_geom    issues\n                                                '
        form = Storage()
        form.vars = form_vars = feature
        form.errors = Storage()
        if (not form_vars.get('wkt')):
            form_vars.update(gis_feature_type='1')
        wkt_centroid(form)
        if form.errors:
            current.log.error(('S3GIS:    %s' % form.errors))
        else:
            wkt = form_vars.wkt
            if (wkt and (not wkt.startswith('POI'))):
                form_vars.update(inherited=False)
            if ('update_record' in form_vars):
                new_vars = {}
                table_fields = table.fields
                for v in form_vars:
                    if (v in table_fields):
                        new_vars[v] = form_vars[v]
                form_vars = new_vars
            try:
                db((table.id == feature.id)).update(**form_vars)
            except MemoryError:
                current.log.error(('S3GIS:    Unable    to    set    bounds    &    centroid    for    feature    %s:    MemoryError' % feature.id))
    def propagate(parent):
        '\n                                                                Propagate    Lat/Lon    down    to    any    Features    which    inherit    from    this    one\n\n                                                                @param    parent:    gis_location    id    of    parent\n                                                '
        query = ((table.parent == parent) & (table.inherited == True))
        rows = db(query).select(*fields)
        for row in rows:
            try:
                update_location_tree(row, propagating=True)
            except RuntimeError:
                current.log.error(('Cannot    propagate    inherited    latlon    to    child    %s    of    location    ID    %s:    too    much    recursion' % (row.id, parent)))
    if (not feature):
        all_locations = True
        all_fields = (table.id, table.name, table.gis_feature_type, table.L0, table.L1, table.L2, table.L3, table.L4, table.lat, table.lon, table.wkt, table.inherited, table.lat_min, table.lon_min, table.lat_max, table.lon_max, table.path, table.parent)
        for level in ('L0', 'L1', 'L2', 'L3', 'L4', 'L5', None):
            query = ((table.level == level) & (table.deleted == False))
            try:
                features = db(query).select(*all_fields)
            except MemoryError:
                current.log.error(('S3GIS:    Unable    to    update    Location    Tree    for    level    %s:    MemoryError' % level))
            else:
                for feature in features:
                    feature['level'] = level
                    wkt = feature['wkt']
                    if (wkt and (not wkt.startswith('POI'))):
                        feature['inherited'] = False
                    update_location_tree(feature)
        return
    id = (str(feature['id']) if ('id' in feature) else None)
    if (not id):
        raise ValueError
    level = feature.get('level', False)
    name = feature.get('name', False)
    path = feature.get('path', False)
    if (all_locations and path and level):
        return path
    lat = feature.get('lat', False)
    lon = feature.get('lon', False)
    wkt = feature.get('wkt', False)
    L0 = feature.get('L0', False)
    if (level == 'L0'):
        if ((name is False) or (path is False) or (lat is False) or (lon is False) or (wkt is False) or (L0 is False)):
            feature = db((table.id == id)).select(table.id, table.name, table.path, table.lat, table.lon, table.wkt, table.L0, limitby=(0, 1)).first()
            name = feature.name
            path = feature.path
            lat = feature.lat
            lon = feature.lon
            wkt = feature.wkt
            L0 = feature.L0
        if ((path != id) or (L0 != name) or (not wkt) or (lat is None)):
            path = id
            if (lat is False):
                lat = None
            if (lon is False):
                lon = None
            fix_vars = dict(inherited=False, path=path, lat=lat, lon=lon, wkt=(wkt or None), L0=name, L1=None, L2=None, L3=None, L4=None, L5=None)
            feature.update(**fix_vars)
            fixup(feature)
        if (not all_locations):
            propagate(id)
        return path
    fixup_required = False
    inherited = feature.get('inherited', None)
    parent = feature.get('parent', False)
    L1 = feature.get('L1', False)
    if (level == 'L1'):
        if ((inherited is None) or (name is False) or (parent is False) or (path is False) or (lat is False) or (lon is False) or (wkt is False) or (L0 is False) or (L1 is False)):
            feature = db((table.id == id)).select(table.id, table.inherited, table.name, table.parent, table.path, table.lat, table.lon, table.wkt, table.L0, table.L1, limitby=(0, 1)).first()
            inherited = feature.inherited
            name = feature.name
            parent = feature.parent
            path = feature.path
            lat = feature.lat
            lon = feature.lon
            wkt = feature.wkt
            L0 = feature.L0
            L1 = feature.L1
        if parent:
            _path = ('%s/%s' % (parent, id))
            _L0 = db((table.id == parent)).select(table.name, table.lat, table.lon, limitby=(0, 1)).first()
            L0_name = _L0.name
            L0_lat = _L0.lat
            L0_lon = _L0.lon
        else:
            _path = id
            L0_name = None
            L0_lat = None
            L0_lon = None
        if (inherited or (lat is None) or (lon is None)):
            fixup_required = True
            inherited = True
            lat = L0_lat
            lon = L0_lon
        elif ((path != _path) or (L0 != L0_name) or (L1 != name) or (not wkt)):
            fixup_required = True
        if fixup_required:
            if (lat is False):
                lat = None
            if (lon is False):
                lon = None
            fix_vars = dict(inherited=inherited, path=_path, lat=lat, lon=lon, wkt=(wkt or None), L0=L0_name, L1=name, L2=None, L3=None, L4=None, L5=None)
            feature.update(**fix_vars)
            fixup(feature)
        if (not all_locations):
            propagate(id)
        return _path
    L2 = feature.get('L2', False)
    if (level == 'L2'):
        if ((inherited is None) or (name is False) or (parent is False) or (path is False) or (lat is False) or (lon is False) or (wkt is False) or (L0 is False) or (L1 is False) or (L2 is False)):
            feature = db((table.id == id)).select(table.id, table.inherited, table.name, table.parent, table.path, table.lat, table.lon, table.wkt, table.L0, table.L1, table.L2, limitby=(0, 1)).first()
            inherited = feature.inherited
            name = feature.name
            parent = feature.parent
            path = feature.path
            lat = feature.lat
            lon = feature.lon
            wkt = feature.wkt
            L0 = feature.L0
            L1 = feature.L1
            L2 = feature.L2
        if parent:
            Lx = db((table.id == parent)).select(table.name, table.level, table.parent, table.lat, table.lon, limitby=(0, 1)).first()
            if (Lx.level == 'L1'):
                L1_name = Lx.name
                _parent = Lx.parent
                if _parent:
                    _path = ('%s/%s/%s' % (_parent, parent, id))
                    L0_name = db((table.id == _parent)).select(table.name, limitby=(0, 1), cache=current.s3db.cache).first().name
                else:
                    _path = ('%s/%s' % (parent, id))
                    L0_name = None
            elif (Lx.level == 'L0'):
                _path = ('%s/%s' % (parent, id))
                L0_name = Lx.name
                L1_name = None
            else:
                current.log.error(('Parent    of    L2    Location    ID    %s    has    invalid    level:    %s    is    %s' % (id, parent, Lx.level)))
                return ('%s/%s' % (parent, id))
            Lx_lat = Lx.lat
            Lx_lon = Lx.lon
        else:
            _path = id
            L0_name = None
            L1_name = None
            Lx_lat = None
            Lx_lon = None
        if (inherited or (lat is None) or (lon is None)):
            fixup_required = True
            inherited = True
            lat = Lx_lat
            lon = Lx_lon
            wkt = None
        elif ((path != _path) or (L0 != L0_name) or (L1 != L1_name) or (L2 != name) or (not wkt)):
            fixup_required = True
        if fixup_required:
            if (lat is False):
                lat = None
            if (lon is False):
                lon = None
            fix_vars = dict(inherited=inherited, path=_path, lat=lat, lon=lon, wkt=(wkt or None), L0=L0_name, L1=L1_name, L2=name, L3=None, L4=None, L5=None)
            feature.update(**fix_vars)
            fixup(feature)
        if (not all_locations):
            propagate(id)
        return _path
    L3 = feature.get('L3', False)
    if (level == 'L3'):
        if ((inherited is None) or (name is False) or (parent is False) or (path is False) or (lat is False) or (lon is False) or (wkt is False) or (L0 is False) or (L1 is False) or (L2 is False) or (L3 is False)):
            feature = db((table.id == id)).select(table.id, table.inherited, table.name, table.parent, table.path, table.lat, table.lon, table.wkt, table.L0, table.L1, table.L2, table.L3, limitby=(0, 1)).first()
            inherited = feature.inherited
            name = feature.name
            parent = feature.parent
            path = feature.path
            lat = feature.lat
            lon = feature.lon
            wkt = feature.wkt
            L0 = feature.L0
            L1 = feature.L1
            L2 = feature.L2
            L3 = feature.L3
        if parent:
            Lx = db((table.id == parent)).select(table.id, table.name, table.level, table.parent, table.path, table.lat, table.lon, table.L0, table.L1, limitby=(0, 1)).first()
            if (Lx.level == 'L2'):
                L0_name = Lx.L0
                L1_name = Lx.L1
                L2_name = Lx.name
                _path = Lx.path
                if (propagating or (_path and L0_name and L1_name)):
                    _path = ('%s/%s' % (_path, id))
                else:
                    _path = update_location_tree(Lx, all_locations)
                    _path = ('%s/%s' % (_path, id))
                    Lx = db((table.id == parent)).select(table.L0, table.L1, table.lat, table.lon, limitby=(0, 1)).first()
                    L0_name = Lx.L0
                    L1_name = Lx.L1
            elif (Lx.level == 'L1'):
                L0_name = Lx.L0
                L1_name = Lx.name
                L2_name = None
                _path = Lx.path
                if (propagating or (_path and L0_name)):
                    _path = ('%s/%s' % (_path, id))
                else:
                    _path = update_location_tree(Lx, all_locations)
                    _path = ('%s/%s' % (_path, id))
                    Lx = db((table.id == parent)).select(table.L0, table.lat, table.lon, limitby=(0, 1)).first()
                    L0_name = Lx.L0
            elif (Lx.level == 'L0'):
                _path = ('%s/%s' % (parent, id))
                L0_name = Lx.name
                L1_name = None
                L2_name = None
            else:
                current.log.error(('Parent    of    L3    Location    ID    %s    has    invalid    level:    %s    is    %s' % (id, parent, Lx.level)))
                return ('%s/%s' % (parent, id))
            Lx_lat = Lx.lat
            Lx_lon = Lx.lon
        else:
            _path = id
            L0_name = None
            L1_name = None
            L2_name = None
            Lx_lat = None
            Lx_lon = None
        if (inherited or (lat is None) or (lon is None)):
            fixup_required = True
            inherited = True
            lat = Lx_lat
            lon = Lx_lon
            wkt = None
        elif ((path != _path) or (L0 != L0_name) or (L1 != L1_name) or (L2 != L2_name) or (L3 != name) or (not wkt)):
            fixup_required = True
        if fixup_required:
            if (lat is False):
                lat = None
            if (lon is False):
                lon = None
            fix_vars = dict(inherited=inherited, path=_path, lat=lat, lon=lon, wkt=(wkt or None), L0=L0_name, L1=L1_name, L2=L2_name, L3=name, L4=None, L5=None)
            feature.update(**fix_vars)
            fixup(feature)
        if (not all_locations):
            propagate(id)
        return _path
    L4 = feature.get('L4', False)
    if (level == 'L4'):
        if ((inherited is None) or (name is False) or (parent is False) or (path is False) or (lat is False) or (lon is False) or (wkt is False) or (L0 is False) or (L1 is False) or (L2 is False) or (L3 is False) or (L4 is False)):
            feature = db((table.id == id)).select(table.id, table.inherited, table.name, table.parent, table.path, table.lat, table.lon, table.wkt, table.L0, table.L1, table.L2, table.L3, table.L4, limitby=(0, 1)).first()
            inherited = feature.inherited
            name = feature.name
            parent = feature.parent
            path = feature.path
            lat = feature.lat
            lon = feature.lon
            wkt = feature.wkt
            L0 = feature.L0
            L1 = feature.L1
            L2 = feature.L2
            L3 = feature.L3
            L4 = feature.L4
        if parent:
            Lx = db((table.id == parent)).select(table.id, table.name, table.level, table.parent, table.path, table.lat, table.lon, table.L0, table.L1, table.L2, limitby=(0, 1)).first()
            if (Lx.level == 'L3'):
                L0_name = Lx.L0
                L1_name = Lx.L1
                L2_name = Lx.L2
                L3_name = Lx.name
                _path = Lx.path
                if (propagating or (_path and L0_name and L1_name and L2_name)):
                    _path = ('%s/%s' % (_path, id))
                else:
                    _path = update_location_tree(Lx, all_locations)
                    _path = ('%s/%s' % (_path, id))
                    Lx = db((table.id == parent)).select(table.L0, table.L1, table.L2, table.lat, table.lon, limitby=(0, 1)).first()
                    L0_name = Lx.L0
                    L1_name = Lx.L1
                    L2_name = Lx.L2
            elif (Lx.level == 'L2'):
                L0_name = Lx.L0
                L1_name = Lx.L1
                L2_name = Lx.name
                L3_name = None
                _path = Lx.path
                if (propagating or (_path and L0_name and L1_name)):
                    _path = ('%s/%s' % (_path, id))
                else:
                    _path = update_location_tree(Lx, all_locations)
                    _path = ('%s/%s' % (_path, id))
                    Lx = db((table.id == parent)).select(table.L0, table.L1, table.lat, table.lon, limitby=(0, 1)).first()
                    L0_name = Lx.L0
                    L1_name = Lx.L1
            elif (Lx.level == 'L1'):
                L0_name = Lx.L0
                L1_name = Lx.name
                L2_name = None
                L3_name = None
                _path = Lx.path
                if (propagating or (_path and L0_name)):
                    _path = ('%s/%s' % (_path, id))
                else:
                    _path = update_location_tree(Lx, all_locations)
                    _path = ('%s/%s' % (_path, id))
                    Lx = db((table.id == parent)).select(table.L0, table.lat, table.lon, limitby=(0, 1)).first()
                    L0_name = Lx.L0
            elif (Lx.level == 'L0'):
                _path = ('%s/%s' % (parent, id))
                L0_name = Lx.name
                L1_name = None
                L2_name = None
                L3_name = None
            else:
                current.log.error(('Parent    of    L3    Location    ID    %s    has    invalid    level:    %s    is    %s' % (id, parent, Lx.level)))
                return ('%s/%s' % (parent, id))
            Lx_lat = Lx.lat
            Lx_lon = Lx.lon
        else:
            _path = id
            L0_name = None
            L1_name = None
            L2_name = None
            L3_name = None
            Lx_lat = None
            Lx_lon = None
        if (inherited or (lat is None) or (lon is None)):
            fixup_required = True
            inherited = True
            lat = Lx_lat
            lon = Lx_lon
            wkt = None
        elif ((path != _path) or (L0 != L0_name) or (L1 != L1_name) or (L2 != L2_name) or (L3 != L3_name) or (L4 != name) or (not wkt)):
            fixup_required = True
        if fixup_required:
            if (lat is False):
                lat = None
            if (lon is False):
                lon = None
            fix_vars = dict(inherited=inherited, path=_path, lat=lat, lon=lon, wkt=(wkt or None), L0=L0_name, L1=L1_name, L2=L2_name, L3=L3_name, L4=name, L5=None)
            feature.update(**fix_vars)
            fixup(feature)
        if (not all_locations):
            propagate(id)
        return _path
    L5 = feature.get('L5', False)
    if (level == 'L5'):
        if ((inherited is None) or (name is False) or (parent is False) or (path is False) or (lat is False) or (lon is False) or (wkt is False) or (L0 is False) or (L1 is False) or (L2 is False) or (L3 is False) or (L4 is False) or (L5 is False)):
            feature = db((table.id == id)).select(table.id, table.inherited, table.name, table.parent, table.path, table.lat, table.lon, table.wkt, table.L0, table.L1, table.L2, table.L3, table.L4, table.L5, limitby=(0, 1)).first()
            inherited = feature.inherited
            name = feature.name
            parent = feature.parent
            path = feature.path
            lat = feature.lat
            lon = feature.lon
            wkt = feature.wkt
            L0 = feature.L0
            L1 = feature.L1
            L2 = feature.L2
            L3 = feature.L3
            L4 = feature.L4
            L5 = feature.L5
        if parent:
            Lx = db((table.id == parent)).select(table.id, table.name, table.level, table.parent, table.path, table.lat, table.lon, table.L0, table.L1, table.L2, table.L3, limitby=(0, 1)).first()
            if (Lx.level == 'L4'):
                L0_name = Lx.L0
                L1_name = Lx.L1
                L2_name = Lx.L2
                L3_name = Lx.L3
                L4_name = Lx.name
                _path = Lx.path
                if (propagating or (_path and L0_name and L1_name and L2_name and L3_name)):
                    _path = ('%s/%s' % (_path, id))
                else:
                    _path = update_location_tree(Lx, all_locations)
                    _path = ('%s/%s' % (_path, id))
                    Lx = db((table.id == parent)).select(table.L0, table.L1, table.L2, table.L3, table.lat, table.lon, limitby=(0, 1)).first()
                    L0_name = Lx.L0
                    L1_name = Lx.L1
                    L2_name = Lx.L2
                    L3_name = Lx.L3
            elif (Lx.level == 'L3'):
                L0_name = Lx.L0
                L1_name = Lx.L1
                L2_name = Lx.L2
                L3_name = Lx.name
                L4_name = None
                _path = Lx.path
                if (propagating or (_path and L0_name and L1_name and L2_name)):
                    _path = ('%s/%s' % (_path, id))
                else:
                    _path = update_location_tree(Lx, all_locations)
                    _path = ('%s/%s' % (_path, id))
                    Lx = db((table.id == parent)).select(table.L0, table.L1, table.L2, table.lat, table.lon, limitby=(0, 1)).first()
                    L0_name = Lx.L0
                    L1_name = Lx.L1
                    L2_name = Lx.L2
            elif (Lx.level == 'L2'):
                L0_name = Lx.L0
                L1_name = Lx.L1
                L2_name = Lx.name
                L3_name = None
                L4_name = None
                _path = Lx.path
                if (propagating or (_path and L0_name and L1_name)):
                    _path = ('%s/%s' % (_path, id))
                else:
                    _path = update_location_tree(Lx, all_locations)
                    _path = ('%s/%s' % (_path, id))
                    Lx = db((table.id == parent)).select(table.L0, table.L1, table.lat, table.lon, limitby=(0, 1)).first()
                    L0_name = Lx.L0
                    L1_name = Lx.L1
            elif (Lx.level == 'L1'):
                L0_name = Lx.L0
                L1_name = Lx.name
                L2_name = None
                L3_name = None
                L4_name = None
                _path = Lx.path
                if (propagating or (_path and L0_name)):
                    _path = ('%s/%s' % (_path, id))
                else:
                    _path = update_location_tree(Lx, all_locations)
                    _path = ('%s/%s' % (_path, id))
                    Lx = db((table.id == parent)).select(table.L0, table.lat, table.lon, limitby=(0, 1)).first()
                    L0_name = Lx.L0
            elif (Lx.level == 'L0'):
                _path = ('%s/%s' % (parent, id))
                L0_name = Lx.name
                L1_name = None
                L2_name = None
                L3_name = None
                L4_name = None
            else:
                current.log.error(('Parent    of    L3    Location    ID    %s    has    invalid    level:    %s    is    %s' % (id, parent, Lx.level)))
                return ('%s/%s' % (parent, id))
            Lx_lat = Lx.lat
            Lx_lon = Lx.lon
        else:
            _path = id
            L0_name = None
            L1_name = None
            L2_name = None
            L3_name = None
            L4_name = None
            Lx_lat = None
            Lx_lon = None
        if (inherited or (lat is None) or (lon is None)):
            fixup_required = True
            inherited = True
            lat = Lx_lat
            lon = Lx_lon
            wkt = None
        elif ((path != _path) or (L0 != L0_name) or (L1 != L1_name) or (L2 != L2_name) or (L3 != L3_name) or (L4 != L4_name) or (L5 != name) or (not wkt)):
            fixup_required = True
        if fixup_required:
            if (lat is False):
                lat = None
            if (lon is False):
                lon = None
            fix_vars = dict(inherited=inherited, path=_path, lat=lat, lon=lon, wkt=(wkt or None), L0=L0_name, L1=L1_name, L2=L2_name, L3=L3_name, L4=L4_name, L5=name)
            feature.update(**fix_vars)
            fixup(feature)
        if (not all_locations):
            propagate(id)
        return _path
    if ((inherited is None) or (level is False) or (name is False) or (parent is False) or (path is False) or (lat is False) or (lon is False) or (wkt is False) or (L0 is False) or (L1 is False) or (L2 is False) or (L3 is False) or (L4 is False) or (L5 is False)):
        feature = db((table.id == id)).select(table.id, table.inherited, table.level, table.name, table.parent, table.path, table.lat, table.lon, table.wkt, table.L0, table.L1, table.L2, table.L3, table.L4, table.L5, limitby=(0, 1)).first()
        inherited = feature.inherited
        level = feature.level
        name = feature.name
        parent = feature.parent
        path = feature.path
        lat = feature.lat
        lon = feature.lon
        wkt = feature.wkt
        L0 = feature.L0
        L1 = feature.L1
        L2 = feature.L2
        L3 = feature.L3
        L4 = feature.L4
        L5 = feature.L5
    L0_name = (name if (level == 'L0') else None)
    L1_name = (name if (level == 'L1') else None)
    L2_name = (name if (level == 'L2') else None)
    L3_name = (name if (level == 'L3') else None)
    L4_name = (name if (level == 'L4') else None)
    L5_name = (name if (level == 'L5') else None)
    if parent:
        Lx = db((table.id == parent)).select(table.id, table.name, table.level, table.parent, table.path, table.lat, table.lon, table.L0, table.L1, table.L2, table.L3, table.L4, limitby=(0, 1)).first()
        if (Lx.level == 'L5'):
            L0_name = Lx.L0
            L1_name = Lx.L1
            L2_name = Lx.L2
            L3_name = Lx.L3
            L4_name = Lx.L4
            L5_name = Lx.name
            _path = Lx.path
            if (propagating or (_path and L0_name and L1_name and L2_name and L3_name and L4_name)):
                _path = ('%s/%s' % (_path, id))
            else:
                _path = update_location_tree(Lx, all_locations)
                _path = ('%s/%s' % (_path, id))
                Lx = db((table.id == parent)).select(table.L0, table.L1, table.L2, table.L3, table.L4, table.lat, table.lon, limitby=(0, 1)).first()
                L0_name = Lx.L0
                L1_name = Lx.L1
                L2_name = Lx.L2
                L3_name = Lx.L3
                L4_name = Lx.L4
        elif (Lx.level == 'L4'):
            L0_name = Lx.L0
            L1_name = Lx.L1
            L2_name = Lx.L2
            L3_name = Lx.L3
            L4_name = Lx.name
            _path = Lx.path
            if (propagating or (_path and L0_name and L1_name and L2_name and L3_name)):
                _path = ('%s/%s' % (_path, id))
            else:
                _path = update_location_tree(Lx, all_locations)
                _path = ('%s/%s' % (_path, id))
                Lx = db((table.id == parent)).select(table.L0, table.L1, table.L2, table.L3, table.lat, table.lon, limitby=(0, 1)).first()
                L0_name = Lx.L0
                L1_name = Lx.L1
                L2_name = Lx.L2
                L3_name = Lx.L3
        elif (Lx.level == 'L3'):
            L0_name = Lx.L0
            L1_name = Lx.L1
            L2_name = Lx.L2
            L3_name = Lx.name
            _path = Lx.path
            if (propagating or (_path and L0_name and L1_name and L2_name)):
                _path = ('%s/%s' % (_path, id))
            else:
                _path = update_location_tree(Lx, all_locations)
                _path = ('%s/%s' % (_path, id))
                Lx = db((table.id == parent)).select(table.L0, table.L1, table.L2, table.lat, table.lon, limitby=(0, 1)).first()
                L0_name = Lx.L0
                L1_name = Lx.L1
                L2_name = Lx.L2
        elif (Lx.level == 'L2'):
            L0_name = Lx.L0
            L1_name = Lx.L1
            L2_name = Lx.name
            _path = Lx.path
            if (propagating or (_path and L0_name and L1_name)):
                _path = ('%s/%s' % (_path, id))
            else:
                _path = update_location_tree(Lx, all_locations)
                _path = ('%s/%s' % (_path, id))
                Lx = db((table.id == parent)).select(table.L0, table.L1, table.lat, table.lon, limitby=(0, 1)).first()
                L0_name = Lx.L0
                L1_name = Lx.L1
        elif (Lx.level == 'L1'):
            L0_name = Lx.L0
            L1_name = Lx.name
            _path = Lx.path
            if (propagating or (_path and L0_name)):
                _path = ('%s/%s' % (_path, id))
            else:
                _path = update_location_tree(Lx, all_locations)
                _path = ('%s/%s' % (_path, id))
                Lx = db((table.id == parent)).select(table.L0, table.lat, table.lon, limitby=(0, 1)).first()
                L0_name = Lx.L0
        elif (Lx.level == 'L0'):
            _path = ('%s/%s' % (parent, id))
            L0_name = Lx.name
        else:
            current.log.error(('Parent    of    L3    Location    ID    %s    has    invalid    level:    %s    is    %s' % (id, parent, Lx.level)))
            return ('%s/%s' % (parent, id))
        Lx_lat = Lx.lat
        Lx_lon = Lx.lon
    else:
        _path = id
        Lx_lat = None
        Lx_lon = None
    if (inherited or (lat is None) or (lon is None)):
        fixup_required = True
        inherited = True
        lat = Lx_lat
        lon = Lx_lon
        wkt = None
    elif ((path != _path) or (L0 != L0_name) or (L1 != L1_name) or (L2 != L2_name) or (L3 != L3_name) or (L4 != L4_name) or (L5 != L5_name) or (not wkt)):
        fixup_required = True
    if fixup_required:
        if (lat is False):
            lat = None
        if (lon is False):
            lon = None
        fix_vars = dict(inherited=inherited, path=_path, lat=lat, lon=lon, wkt=(wkt or None), L0=L0_name, L1=L1_name, L2=L2_name, L3=L3_name, L4=L4_name, L5=L5_name)
        feature.update(**fix_vars)
        fixup(feature)
    if (not all_locations):
        propagate(id)
    return _path
