from axipy import view_manager, Map, IndividualThematicLayer, Style, provider_manager, Layer


def openTables(path_tabs):
    list_tabs=[]
    for path in path_tabs:
        list_tabs.append(provider_manager.openfile(path))
    return list_tabs
def createMap(tabs):
    layers=[]
    for tab in tabs:
        layers.append(Layer.create(tab))
    '''
    for layer in layers:
        layer.visible=False
    '''
    map=Map(layers)
    return map
def find_style(value,list_style):
    for item in list_style:
        if item['value']==value:
            return item
    return None
def createIndividual(map:Map,name_layer,theme_expres,list_value_theme):
    layers=map.layers
    layer_for_theme=None
    for ly in layers:
        if ly.title==name_layer:
            layer_for_theme=ly
            break
    if layer_for_theme is not None:
        individual = IndividualThematicLayer(theme_expres)
        layer_for_theme.thematic.add(individual)
        for i in range(individual.count):
            value = individual.get_value(i)
            val_style=find_style(value,list_value_theme)
            if val_style is not None:
                individual.set_style(i, Style.from_mapinfo(val_style['style']))

list_tab_path=[r"E:\Support\ImportAxiomaWor\proekt_1690358665\Adm\AdmBorder.TAB",r"E:\Support\ImportAxiomaWor\proekt_1690358665\Land\LandUse.TAB"]
tabs=openTables(list_tab_path)
map=createMap(tabs)
list_theme_indi=[]
list_theme_indi.append({'value':'601010305','style':'Pen (50,2,16752895)'})
list_theme_indi.append({'value':'601010305','style':'Pen (50,2,14474460)'})
list_theme_indi.append({'value':'601020400','style':'Pen (50,2,16777215)'})
createIndividual(map,"AdmBorder","CLASSID",list_theme_indi)
map_view=view_manager.create_mapview(map)