import math
import time

import os

import processing
from qgis.core import *
from qgis.utils import *
from qgis.gui import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5 import QtWidgets
import sys
import pandas as pd

from .Andalusian_Population_loader import *
from .Andalusian_Population_startup1 import *

class InterfazDeCarga(QDialog):
    def __init__(self,parent,geometria):
        super().__init__()
        self.widget=QWidget()
        self.setWindowTitle('Cargando')
        self.setGeometry(geometria.x()+geometria.width()/4,geometria.y()+geometria.height()/4,300,150)
        self.setFocus();
        dlggLayout = QVBoxLayout(self)
        self.spinner=QtWaitingSpinner(self.widget)
        dlggLayout.addWidget(self.spinner)
        self.setLayout(dlggLayout)
        self.spinner.start()
        #ventana_error(titulo="Importación finalizada",mensaje="Proceso de importación finalizado correctamente")

    def cerrar(self):
        self.spinner.stop()
        self.close()

class VentanaError(QDialog):
    def __init__(self,parent=None,titulo="ERROR",mensaje="Se ha producido un error"):
        super().__init__()
        self.ventana_errores = QWidget(self)
        self.setWindowTitle(str(titulo))
        self.setGeometry(500, 400, 300, 150)
        vLayout = QVBoxLayout(self)
        self.ventana_errores.setLayout(vLayout)

        texto=QPlainTextEdit (str(mensaje))
        texto.setReadOnly(True)
        texto.setStyleSheet("background-color: transparent;")
        texto.setMaximumHeight(100)
        #texto.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.MinimumExpanding)
        vLayout.addWidget(texto)
        vLayout.addStretch(1)

        self.buttonBox = QDialogButtonBox(self)
        self.buttonBox.setOrientation(Qt.Horizontal)
        self.buttonBox.setStandardButtons(QDialogButtonBox.Close|QDialogButtonBox.Ok)
        self.buttonBox.setCenterButtons(False)
        self.buttonBox.accepted.connect(self.aceptar)
        self.buttonBox.rejected.connect(self.cerrar)
        vLayout.addWidget(self.buttonBox, alignment=Qt.AlignRight | Qt.AlignBottom)
        #vLayoutAux.addWidget(self.buttonBox)

    def cerrar(self):
        self.close()
    def aceptar(self):
        self.accept()

class InterfazMapa(QDialog):
    def __init__(self,parent=None,lista_url_capas250=[],TipoTasa=[]):
        super().__init__()
        self.TipoTasa=TipoTasa
        self.lista_url_capas250=lista_url_capas250

        #self.colores_inv=[0 for i in range(len(TipoTasa))]
        aux=["#00ff00","#009000","#ffff00","#ff9000","#ff0000"]
        self.color_lista=[aux for i in range(len(TipoTasa))]
        aux=[0 for i in range(len(aux))]
        self.condicion_lista=[aux for i in range(len(TipoTasa))]
        self.minimo=[0 for i in range(len(TipoTasa))]
        self.maximo=[1 for i in range(len(TipoTasa))]
        self.idCurrentRow=0

        self.setWindowTitle('Caracterización de la población andaluza')
        self.setGeometry(250, 180, 900, 700)
        self.main_widget = QtWidgets.QWidget(self)
        #self.main_widget.setFocus()

        dlgLayout = QVBoxLayout()
        self.main_widget.setLayout(dlgLayout)

        #Tabs

        self.tabwidget = QTabWidget()
        #tabwidget.addTab(inequidadVLayout, "Tab 1")

        nombres_capas=[]

        self.inequidadGUI()
        self.configuracionGUI()
        dlgLayout.addWidget(self.tabwidget, 0)
        self.setLayout(dlgLayout)

    def inequidadGUI(self):
        inequidadVLayout = QVBoxLayout()
        gb_archivo = QGroupBox("Archivo")
        verticalLayoutArchivo = QVBoxLayout()
        horzLayout01 = QHBoxLayout()
        #gb_archivo.setAlignment(Qt.AlignHCenter)
        gb_archivo.setStyleSheet('QGroupBox {'
        'border: 2px outset gray;'
        'border-radius: 5px;'
        'margin-top: 0.5em;}'
        'QGroupBox::title {'
        'subcontrol-origin: margin;'
        'left: 10px;'
        'padding: 0 3px;}')
        horzLayout01.addWidget(QLabel('Nombre de la imagen:'))
        self.nombre_fichero=QLineEdit('mapa')
        horzLayout01.addWidget(self.nombre_fichero)
        verticalLayoutArchivo.addLayout(horzLayout01)

        horzLayout02 = QHBoxLayout()
        btn_directorio = QPushButton('...')
        btn_directorio.setDefault(False)
        btn_directorio.setAutoDefault(False)
        horzLayout02.addWidget(QLabel('Directorio de la imagen:'))
        self.data_path=QLineEdit(str(QgsProject.instance().readPath("./")))
        horzLayout02.addWidget(self.data_path)
        horzLayout02.addWidget(btn_directorio)
        btn_directorio.clicked.connect(self.obtener_directorio)
        verticalLayoutArchivo.addLayout(horzLayout02)
        gb_archivo.setLayout(verticalLayoutArchivo)

        inequidadVLayout.addWidget(gb_archivo)

        gb_valor_referencia = QGroupBox("Valor de Referencia")
        gb_valor_referencia.setStyleSheet('QGroupBox {'
        'border: 2px inset gray;'
        'border-radius: 5px;'
        'margin-top: 0.5em;}'
        'QGroupBox::title {'
        'subcontrol-origin: margin;'
        'left: 10px;'
        'padding: 0 3px;}')
        verticalLayoutValorReferencia = QVBoxLayout()
        self.horzLayout2 = QHBoxLayout()
        inequidadVLayout.addWidget(QLabel(""))
        self.xqlineedit=QLineEdit("294326")
        self.yqlineedit=QLineEdit("4122700")
        self.horzLayout2.addWidget(QLabel('X:'))
        self.horzLayout2.addWidget(self.xqlineedit)
        self.horzLayout2.addStretch(1)
        self.horzLayout2.addWidget(QLabel('Y:'))
        self.horzLayout2.addWidget(self.yqlineedit)
        self.horzLayout2.addStretch(1)
        self.horzLayout2.addWidget(QLabel('Distancia de visualización del mapa(m):'))
        self.bufferVisionLineedit=QLineEdit("1000")
        self.horzLayout2.addWidget(self.bufferVisionLineedit)
        self.horzLayout2.addStretch(20)
        verticalLayoutValorReferencia.addLayout(self.horzLayout2)

        horzLayout03 = QHBoxLayout()
        horzLayout03.addWidget(QLabel("Tipología:"))
        self.myCombobox_valor_referencia = QComboBox()
        self.myCombobox_valor_referencia.addItem('Andalucia')
        self.myCombobox_valor_referencia.addItem('Provincia')
        self.myCombobox_valor_referencia.addItem('Municipio')
        self.myCombobox_valor_referencia.addItem('Buffer')
        self.myCombobox_valor_referencia.setMinimumContentsLength(15)
        self.myCombobox_valor_referencia.activated.connect(self.mostrar_tipologia_valor_referencia)
        horzLayout03.addWidget(self.myCombobox_valor_referencia)
        horzLayout03.addStretch(1)
        self.valorRererenciaQlabel=QLabel('')
        horzLayout03.addWidget(self.valorRererenciaQlabel)
        self.bufferlineedit= QLineEdit('1000')
        self.bufferlineedit.setVisible(False)
        horzLayout03.addWidget(self.bufferlineedit)
        self.myCombobox_valor_referencia_provincia=QComboBox()
        self.myCombobox_valor_referencia_provincia.addItem('PuntoXY')
        self.myCombobox_valor_referencia_provincia.addItem('Almería')
        self.myCombobox_valor_referencia_provincia.addItem('Cádiz')
        self.myCombobox_valor_referencia_provincia.addItem('Córdoba')
        self.myCombobox_valor_referencia_provincia.addItem('Granada')
        self.myCombobox_valor_referencia_provincia.addItem('Huelva')
        self.myCombobox_valor_referencia_provincia.addItem('Jaén')
        self.myCombobox_valor_referencia_provincia.addItem('Málaga')
        self.myCombobox_valor_referencia_provincia.addItem('Sevilla')
        self.myCombobox_valor_referencia_provincia.setMinimumContentsLength(15)
        self.myCombobox_valor_referencia_provincia.setVisible(False)
        horzLayout03.addWidget(self.myCombobox_valor_referencia_provincia)
        self.municipiolineedit= QLineEdit('PuntoXY')

        basepath = os.path.dirname(os.path.realpath(__file__)) + "\\"
        df=pd.read_csv(basepath + "tabla.csv",sep=";",decimal=",",header=0)
        # sorting by first name
        df.sort_values("municipio", inplace = True)

        # dropping ALL duplicte values
        df.drop_duplicates(subset ="municipio",
                             keep = 'first', inplace = True)
        names=df["municipio"]
        completer = QCompleter(names)
        completer.setCaseSensitivity(QtCore.Qt.CaseInsensitive)
        self.municipiolineedit.setCompleter(completer)

        self.municipiolineedit.setVisible(False)
        horzLayout03.addWidget(self.municipiolineedit)
        horzLayout03.addStretch(20)
        verticalLayoutValorReferencia.addLayout(horzLayout03)

        gb_valor_referencia.setLayout(verticalLayoutValorReferencia)

        inequidadVLayout.addWidget(gb_valor_referencia)

        inequidadVLayout.addWidget(QLabel(""))
        titulo_listwidget=QLabel("Tasa 250x250")
        titulo_listwidget.setFont(QFont('Arial', 12))
        VertTasaLayout = QVBoxLayout()
        VertTasaLayout.addWidget(titulo_listwidget)
        self.listwidget_capas250=QListWidget()
        #self.listwidget_capas250.setSelectionMode (QAbstractItemView.ExtendedSelection) # Hold CTRL to select multiple

        incr=0
        for malla250 in self.TipoTasa:
            malla250=malla250[1]
            self.listwidget_capas250.addItem(str(incr+1) + ". "+ malla250)#myQListWidgetItem
            self.listwidget_capas250.item(incr).setFont(QFont('Arial', 14))
            incr+=1
        self.listwidget_capas250.setSpacing(5)
        self.listwidget_capas250.itemClicked.connect(self.calculaSimbologia)
        VertTasaLayout.addWidget(self.listwidget_capas250)
        HorzListLayout = QHBoxLayout()
        HorzListLayout.addLayout(VertTasaLayout)

        titulo_listwidget=QLabel("Simbologia")
        titulo_listwidget.setFont(QFont('Arial', 12))
        VertSimbologiaLayout = QVBoxLayout()
        VertSimbologiaLayout.addWidget(titulo_listwidget)
        self.listwidget_capas250_simbologia=QListWidget()

        self.listwidget_capas250_simbologia.itemDoubleClicked.connect(self.cambiar_color)
        self.listwidget_capas250_simbologia.itemPressed.connect(self.deshabilitarImportacion)
        self.listwidget_capas250_simbologia.setFixedHeight(300)
        #self.listwidget_capas250.setSelectionMode (QAbstractItemView.ExtendedSelection) # Hold CTRL to select multiple
        self.listwidget_capas250_simbologia.setDragDropMode(QAbstractItemView.InternalMove)
        self.listwidget_capas250_simbologia.setAcceptDrops(True)
        VertSimbologiaLayout.addWidget(self.listwidget_capas250_simbologia)
        btnLayout1 = QHBoxLayout()
        self.btn1_0 = QPushButton('Eliminar')
        self.btn1_0.clicked.connect(self.eliminarItemSimbologia)
        self.btn1_0.setEnabled(False)
        btnLayout1.addWidget(self.btn1_0)
        btnLayout1.addStretch(1)
        self.btn1_1 = QPushButton('Añadir')
        self.btn1_1.clicked.connect(self.anadirSimbologia)
        self.btn1_1.setEnabled(False)
        btnLayout1.addWidget(self.btn1_1)
        btnLayout1.addStretch(1)
        self.btn1_2 = QPushButton('Invertir colores')
        self.btn1_2.clicked.connect(self.invertirColor)
        self.btn1_2.setEnabled(False)
        btnLayout1.addWidget(self.btn1_2)
        VertSimbologiaLayout.addLayout(btnLayout1)

        HorzListLayout.addLayout(VertSimbologiaLayout)
        inequidadVLayout.addLayout(HorzListLayout)

        self.checkbox_capa_tasa=QtWidgets.QCheckBox("Mantener la capa de la Tasa despues de la importación")
        inequidadVLayout.addWidget(self.checkbox_capa_tasa)
        self.checkbox_centro=QtWidgets.QCheckBox("Mantener la capa del punto central despues de la importación")
        inequidadVLayout.addWidget(self.checkbox_centro)
        self.checkbox_buffer=QtWidgets.QCheckBox("Mantener la capa buffer despues de la importación")
        inequidadVLayout.addWidget(self.checkbox_buffer)
        inequidadVLayout.addStretch(1)
        btnLayout = QHBoxLayout()
        btn1 = QPushButton('Cerrar')
        btn1.clicked.connect(self.cerrar)
        btnLayout.addWidget(btn1)
        btnLayout.addStretch(1)
        self.btn1_3 = QPushButton('Aplicar')
        self.btn1_3.clicked.connect(self.aplicarCambios)
        self.btn1_3.setEnabled(True)
        self.btn1_3.setDefault(True)
        self.btn1_3.setAutoDefault(True)
        btnLayout.addWidget(self.btn1_3)
        self.btn1_4 = QPushButton('Importar')
        self.btn1_4.clicked.connect(self.ejecutar_inequidad)
        self.btn1_4.setEnabled(False)
        btnLayout.addWidget(self.btn1_4)
        inequidadVLayout.addLayout(btnLayout)

        tab1=QWidget()
        tab1.setLayout(inequidadVLayout)
        self.tabwidget.addTab(tab1, "Mapa")

    def configuracionGUI(self):
        configuracionVLayout = QVBoxLayout()

        formLayout = QFormLayout()
        btnLayout = QHBoxLayout()

        self.url_ortofoto=QLineEdit(self.lista_url_capas250[0])
        self.url_municipios=QLineEdit(self.lista_url_capas250[1])
        self.url_gridpob=QLineEdit(self.lista_url_capas250[2])
        self.url_gridcatt=QLineEdit(self.lista_url_capas250[3])
        self.url_gridmort=QLineEdit(self.lista_url_capas250[4])
        self.texto_buffer=QLineEdit(self.lista_url_capas250[5])

        formLayout.addRow('Fuente capa ortofoto(WMS):', self.url_ortofoto)
        formLayout.addRow('Fuente capa municipios(WFS):', self.url_municipios)
        formLayout.addRow('Fuente capa grid de población(WFS):', self.url_gridpob)
        formLayout.addRow('Fuente capa gridcattp de uso del suelo(WFS):', self.url_gridcatt)
        formLayout.addRow('Fuente capa grid de mortalidad(WFS):', self.url_gridmort)
        formLayout.addRow('Nombre capa buffer:', self.texto_buffer)
        configuracionVLayout.addLayout(formLayout)

        btn1 = QPushButton('Cerrar')
        btn1.clicked.connect(self.cerrar)
        btnLayout.addWidget(btn1)
        btnLayout.addStretch(1)
        btn2 = QPushButton('Aplicar')
        btn2.clicked.connect(self.AplicarUrl)
        configuracionVLayout.addStretch(1)
        btnLayout.addWidget(btn2)
        configuracionVLayout.addLayout(btnLayout)

        tab3=QWidget()
        tab3.setLayout(configuracionVLayout)
        self.tabwidget.addTab(tab3, "Configuracion")
    def AplicarUrl(self):
        layer_ortofoto = QgsRasterLayer(self.url_ortofoto.text(), "Capa ortofoto", "WMS")
        layer_municipios = QgsVectorLayer(self.url_municipios.text(), "Capa municipios", "WFS")
        layer_pob = QgsVectorLayer(self.url_gridpob.text(), "Capa pob", "WFS")
        layer_catt = QgsVectorLayer(self.url_gridcatt.text(), "Capa catt", "WFS")
        layer_mort = QgsVectorLayer(self.url_gridmort.text(), "Capa mort", "WFS")
        if not layer_ortofoto.isValid():
            VentanaError(mensaje="La fuente de la capa ortofoto no es válida").exec()
            return
        elif not layer_municipios.isValid():
            VentanaError(mensaje="La fuente de la capa municipios no es válida").exec()
            return
        elif not layer_pob.isValid():
            VentanaError(mensaje="La fuente de la capa grid de población no es válida").exec()
            return
        elif not layer_catt.isValid():
            VentanaError(mensaje="La fuente de la capa gridcattp de uso del suelo no es válida").exec()
            return
        elif not layer_mort.isValid():
            VentanaError(mensaje="La fuente de la capa grid de mortalidad no es válida").exec()
            return
        self.lista_url_capas250[0]=self.url_ortofoto.text()
        self.lista_url_capas250[1]=self.url_municipios.text()
        self.lista_url_capas250[2]=self.url_gridpob.text()
        self.lista_url_capas250[3]=self.url_gridcatt.text()
        self.lista_url_capas250[4]=self.url_gridmort.text()
        self.lista_url_capas250[5]=self.texto_buffer.text()

        if (ComprobarCambiosGridTXT(self.lista_url_capas250)):
            if(VentanaError(titulo="Aplicar cambios",mensaje="Para aplicar los cambios, QGIS se cerrará y cuando se ejecute de nuevo el Plugin, "
            "se llevará a cabo la instalación de la/s nueva/s capa/s.\n\n¿Desea continuar?").exec()):
                ActualizarUrl(self.lista_url_capas250)
                EliminarTablaCSV()
                sys.exit()
            else:#Si el usuario no acepta, se sale sin actualizar la Url en el TXT
                return
        ActualizarUrl(self.lista_url_capas250)
    def aplicarCambios(self):
        self.getSimbologia()
        self.muestraSimbologia()
        self.btn1_4.setEnabled(True)
        self.btn1_4.setDefault(True)
        self.btn1_4.setAutoDefault(True)
    def deshabilitarImportacion(self):
        self.btn1_4.setEnabled(False)
        self.btn1_4.setDefault(False)
        self.btn1_4.setAutoDefault(False)
        self.btn1_3.setDefault(True)
        self.btn1_3.setAutoDefault(True)
    def cerrar(self):
        self.close()
    def ejecutar_inequidad(self): #HACER QUE SE VEA POR DELANTE DEL QDIALOG
        #interfazCarga=InterfazDeCarga(None,self.geometry())
        #interfazCarga.exec()
        self.seleccionar_capas_criterio(importar=1)
        #ventana_error(titulo="Importación finalizada",mensaje="Proceso de importación finalizado correctamente")
        #interfazCarga.cerrar()
    def invertirColor(self):
        self.color_lista[self.idCurrentRow].reverse()
        self.muestraSimbologia()
        self.deshabilitarImportacion()
    def obtener_directorio(self):
        dialog = QFileDialog()
        self.data_path.setText(dialog.getExistingDirectory(None, 'Directorio de la imagen',self.data_path.text()))
    def calculaSimbologia(self):
        #Se inicializan las condiciones
        self.idCurrentRow=self.listwidget_capas250.currentRow()
        self.seleccionar_capas_criterio(importar=0)
        if self.listwidget_capas250_simbologia.count()==0:
            for id in range(len(self.color_lista[self.idCurrentRow])):
                self.CreaItemSimbologia(id)
        self.muestraSimbologia()
        self.btn1_0.setEnabled(True)
        self.btn1_1.setEnabled(True)
        self.btn1_2.setEnabled(True)
        self.deshabilitarImportacion()
    def muestraSimbologia(self):
        for incr in range(len(self.color_lista[self.idCurrentRow])):
            self.listwidget_capas250_simbologia.item(incr).widgetText.setText(str(self.condicion_lista[self.idCurrentRow][incr]))
            self.listwidget_capas250_simbologia.item(incr).widgetLabel.setStyleSheet(
                "border: 1px solid black;min-width: 20px;min-height: 20px;background-color: " + str(self.color_lista[self.idCurrentRow][incr]))
        self.actualiza_leyenda()
    def CreaItemSimbologia(self,id):
        myQListWidgetItem=clase_colores(id=id,color=self.color_lista[self.idCurrentRow][id],condicion=self.condicion_lista[self.idCurrentRow][id])
        self.listwidget_capas250_simbologia.addItem(myQListWidgetItem)#myQListWidgetItem
        self.listwidget_capas250_simbologia.setItemWidget(myQListWidgetItem, myQListWidgetItem.widget)
        self.listwidget_capas250_simbologia.item(id).widgetText.editingFinished.connect(self.actualiza_leyenda)
    def eliminarItemSimbologia(self):
        id=self.listwidget_capas250_simbologia.currentRow()
        if id>=0:
            self.listwidget_capas250_simbologia.takeItem(id)
            self.color_lista[self.idCurrentRow].pop(id)
            self.condicion_lista[self.idCurrentRow].pop(id)
        if self.listwidget_capas250_simbologia.count():
            self.actualiza_leyenda()
        self.deshabilitarImportacion()
    def actualiza_leyenda(self):
        for id in range(self.listwidget_capas250_simbologia.count()-1):
            legend1=self.listwidget_capas250_simbologia.item(id).widgetText.text()
            legend2=self.listwidget_capas250_simbologia.item(id+1).widgetText.text()
            texto=str(legend1) + " - " + str(legend2)
            self.listwidget_capas250_simbologia.item(id).widgetLabelLeyenda.setText(texto)
        #Ultimo valor
        self.listwidget_capas250_simbologia.item(id+1).widgetLabelLeyenda.setText(str(legend2)+" - "+str(self.maximo[self.idCurrentRow]))
    def mostrar_tipologia_valor_referencia(self):
        id=self.myCombobox_valor_referencia.currentIndex()
        if (id==0):#Andalucia
            self.valorRererenciaQlabel.setText('')
            self.bufferlineedit.setVisible(False)
            self.myCombobox_valor_referencia_provincia.setVisible(False)
            self.municipiolineedit.setVisible(False)
        elif(id==1):#Provincia
            self.valorRererenciaQlabel.setText('Provincia:')
            self.bufferlineedit.setVisible(False)
            self.myCombobox_valor_referencia_provincia.setVisible(True)
            self.municipiolineedit.setVisible(False)
        elif(id==2):#Municipio
            self.valorRererenciaQlabel.setText('Municipio:')
            self.bufferlineedit.setVisible(False)
            self.myCombobox_valor_referencia_provincia.setVisible(False)
            self.municipiolineedit.setVisible(True)
        elif (id==3):#Buffer
            self.valorRererenciaQlabel.setText('Dist. buffer(m):')
            self.bufferlineedit.setVisible(True)
            self.myCombobox_valor_referencia_provincia.setVisible(False)
            self.municipiolineedit.setVisible(False)
    def obtener_minmax(self,tipo=0,lista=[0,100]):
        CifRed=1 #numero de cifras de redondeo
        if(tipo==0):
            self.minimo[self.idCurrentRow]=lista[0]
            self.maximo[self.idCurrentRow]=lista[1]
            minimo=self.minimo[self.idCurrentRow]
            maximo=self.maximo[self.idCurrentRow]
            incr=(maximo-minimo)/5
            if not(len(self.condicion_lista[self.idCurrentRow])):
                self.condicion_lista[self.idCurrentRow]=[round(float(minimo),CifRed),round(float(minimo+incr),CifRed),round(float(minimo+2*incr),CifRed),
                round(float(minimo+3*incr),CifRed),round(float(minimo+4*incr),CifRed)]
        elif(tipo==1):
            self.minimo[self.idCurrentRow]=round(float(lista[0]),CifRed)
            self.maximo[self.idCurrentRow]=round(float(lista[5]),CifRed)
            self.condicion_lista[self.idCurrentRow]=[round(float(lista[0]),CifRed),round(float(lista[1]),CifRed),round(float(lista[2]),CifRed),
            round(float(lista[3]),CifRed),round(float(lista[4]),CifRed)]
    def getSimbologia(self):
        for id in range(self.listwidget_capas250_simbologia.count()):
            self.color_lista[self.idCurrentRow][id]=str(self.listwidget_capas250_simbologia.item(id).widgetLabel.palette().button().color().name())
            self.condicion_lista[self.idCurrentRow][id]=str(self.listwidget_capas250_simbologia.item(id).widgetText.text())
    def anadirSimbologia(self):
        color="#000000"
        condicion=0
        id=self.listwidget_capas250_simbologia.count()
        self.color_lista[self.idCurrentRow].append(str(color))
        self.condicion_lista[self.idCurrentRow].append(str(condicion))
        self.CreaItemSimbologia(id)
        self.deshabilitarImportacion()
    def cambiar_color(self):
        color_nuevo_inequidad_1=QColorDialog.getColor().name()
        id=self.listwidget_capas250_simbologia.currentRow()
        self.listwidget_capas250_simbologia.item(id).widgetLabel.setStyleSheet("border: 1px solid black;min-width: 20px;min-height: 20px;background-color: " + color_nuevo_inequidad_1)
        self.deshabilitarImportacion()
    def getcondTasa(self,zLayer):
        condTasa=0
        id=self.myCombobox_valor_referencia.currentIndex()
        if id==0:#Andalucia
            zLayer.setName("Quintiles respecto a Andalucia")
            pass
        elif id==1:#Provincia
            condTasa=self.myCombobox_valor_referencia_provincia.currentText()
            zLayer.setName("Quintiles respecto a la provincia: " + condTasa)
        elif id==2:#Municipio
            condTasa=self.municipiolineedit.text()
            zLayer.setName("Quintiles respecto al municipio: " + condTasa)
            """
            df=df[df['municipio']==condTasa]
            print(df.size)
            if (df.size>0):
                condTasa=float(df.iloc[0]['cmun'])
            """
        elif id==3:#Buffer
            condTasa=[]
            for f in zLayer.getFeatures():
                    condTasa.append(float(f['grd_floaid'].replace('.','')))
            zLayer.setName("Quintiles  respecto a buffer de " + str(self.bufferVisionLineedit.text()) + "m ")

        return condTasa
    def seleccionar_capas_criterio(self,importar=0):
        tipoVersion=getVersion()
        numDecimales=1
        IdTasa=self.listwidget_capas250.currentRow()
        myTargetField=self.TipoTasa[IdTasa][0]
        NombreLeyenda=self.TipoTasa[IdTasa][1]
        tamano_buffer=float(self.bufferVisionLineedit.text())
        x=float(self.xqlineedit.text())
        y=float(self.yqlineedit.text())
        basepath = os.path.dirname(os.path.realpath(__file__)) + "\\"
        df=pd.read_csv(basepath + "tabla.csv",sep=";",decimal=",",header=0)
        if tipoVersion==1:
            tLayer=QgsVectorLayer("Point", "punto", "memory")
            vLayer=QgsVectorLayer("Polygon", "buffer", "memory")
            tLayer.setCrs(QgsCoordinateReferenceSystem(25830))
            vLayer.setCrs(QgsCoordinateReferenceSystem(25830))
        else:
            tLayer=QgsVectorLayer("Point?crs=EPSG:25830", "punto", "memory")
            vLayer=QgsVectorLayer("Polygon?crs=EPSG:25830", "buffer", "memory")
        myField = QgsField( 'id', QVariant.Int )
        vLayer.dataProvider().addAttributes([myField])
        vLayer.updateFields()
        point_centro = QgsGeometry.fromPointXY(QgsPointXY(float(x), float(y)))#yqlineedit.text()
        vpr = tLayer.dataProvider()
        f = QgsFeature()
        f.setGeometry(point_centro)
        vpr.addFeatures([f])
        tLayer.updateExtents()
        tFeats = tLayer.getFeatures()

        for tfeat in tLayer.getFeatures():
            geom = tfeat.geometry()
            buffer = geom.buffer(tamano_buffer, 5)
            feat_vista=tfeat
            tfeat.setGeometry(buffer)
            vLayer.dataProvider().addFeatures([tfeat])

        myTargetField=str(myTargetField)
        NombreLeyenda=str(NombreLeyenda)
        IdRereferencia=self.myCombobox_valor_referencia.currentIndex()
        Layer=AnadirLayerQGIS(url=self.lista_url_capas250[2],nombre="Capa grid de poblacion", tipoCapa="WFS")

        if (IdTasa==0):#Poblacion Total
            if tipoVersion==1:
                interseccion=processing.run("qgis:extractbylocation",
                {'INPUT':Layer,
                'PREDICATE':0,
                'INTERSECT':vLayer,
                'OUTPUT':'TEMPORARY_OUTPUT'})

                zLayer=interseccion['OUTPUT']
                zLayer.setName(str(NombreLeyenda))
            else:
                interseccion=processing.run("qgis:selectbylocation",
                {'INPUT':Layer,
                'PREDICATE':0,
                'INTERSECT':vLayer,
                'METHOD':0})
                zLayer = interseccion.materialize(QgsFeatureRequest().setFilterFids(interseccion.selectedFeatureIds()))
                zLayer.setName(str(NombreLeyenda))

            myField = QgsField( myTargetField, QVariant.Double )
            zLayer.dataProvider().addAttributes([myField])
            zLayer.updateFields()
            prov = zLayer.dataProvider()
            numero_tasa=zLayer.fields().indexOf(myTargetField)
            zLayer.startEditing()

            poblacion=[]
            Tasa=[]
            for f in zLayer.getFeatures():
                    poblacion.append(float(f["pob_tot"]))
                    if all(flag > 0 for flag in [poblacion[-1]]):
                        Tasa.append(float(poblacion[-1]))
                    else:
                        Tasa.append(float(-1))

                    zLayer.changeAttributeValue(f.id(), numero_tasa, Tasa[-1])

            zLayer.commitChanges()
            zLayer.updateFields()

            #A partir de aqui es donde se modifica para cada Tasa
            condTasa=self.getcondTasa(zLayer)
            if condTasa=="PuntoXY":
                condTasa=calcularPuntoXY(Tipo=IdRereferencia,tLayer=tLayer,lista_url_capas250=self.lista_url_capas250)
                if IdRereferencia==1:
                    zLayer.setName("Quintiles respecto a la provincia: " + condTasa)
                elif IdRereferencia==2:
                    zLayer.setName("Quintiles respecto al municipio: " + condTasa)
            Tasa = df[['grd_floaid','municipio','cmun']]
            Tasa[myTargetField]=df['pob_tot']
            Tasa=Tasa[(Tasa[myTargetField] >= 0)]
            Tasa=Tasa.round({myTargetField:numDecimales})

            (minimo,percentil_20,percentil_40,percentil_60,percentil_80,maximo)=calcularValorReferencia(Tipo=IdRereferencia, Tasa=Tasa, condTasa=condTasa)

        elif (IdTasa==1):#Tasa menores 15 años
            if tipoVersion==1:
                interseccion=processing.run("qgis:extractbylocation",
                {'INPUT':Layer,
                'PREDICATE':0,
                'INTERSECT':vLayer,
                'OUTPUT':'TEMPORARY_OUTPUT'})

                zLayer=interseccion['OUTPUT']
                zLayer.setName(str(NombreLeyenda))
            else:
                interseccion=processing.run("qgis:selectbylocation",
                {'INPUT':Layer,
                'PREDICATE':0,
                'INTERSECT':vLayer,
                'METHOD':0})
                zLayer = interseccion.materialize(QgsFeatureRequest().setFilterFids(interseccion.selectedFeatureIds()))
                zLayer.setName(str(NombreLeyenda))

            myField = QgsField( myTargetField, QVariant.Double )
            zLayer.dataProvider().addAttributes([myField])
            zLayer.updateFields()
            prov = zLayer.dataProvider()
            numero_tasa=zLayer.fields().indexOf(myTargetField)
            zLayer.startEditing()

            poblacion=[]
            poblacion15=[]
            Tasa=[]
            for f in zLayer.getFeatures():
                    poblacion.append(float(f["pob_tot"]))
                    poblacion15.append(float(f["edad0015"]))
                    if all(flag > 0 for flag in [poblacion[-1],poblacion15[-1]+0.1]):
                        Tasa.append(round(float(100*poblacion15[-1]/poblacion[-1]),numDecimales))
                    else:
                        Tasa.append(float(-1))

                    zLayer.changeAttributeValue(f.id(), numero_tasa, Tasa[-1])

            zLayer.commitChanges()
            zLayer.updateFields()

            #A partir de aqui es donde se modifica para cada Tasa
            condTasa=self.getcondTasa(zLayer)
            if condTasa=="PuntoXY":
                condTasa=calcularPuntoXY(Tipo=IdRereferencia,tLayer=tLayer,lista_url_capas250=self.lista_url_capas250)
                if IdRereferencia==1:
                    zLayer.setName("Quintiles respecto a la provincia: " + condTasa)
                elif IdRereferencia==2:
                    zLayer.setName("Quintiles respecto al municipio: " + condTasa)
            dfReducido=df[(df['edad0015'] >= 0) & (df['pob_tot'] > 0)]
            Tasa = dfReducido[['grd_floaid','municipio','cmun']]
            Tasa[myTargetField]=100*(dfReducido['edad0015']/dfReducido['pob_tot'])
            Tasa=Tasa[(Tasa[myTargetField] >= 0)]
            Tasa=Tasa.round({myTargetField:numDecimales})
            (minimo,percentil_20,percentil_40,percentil_60,percentil_80,maximo)=calcularValorReferencia(Tipo=IdRereferencia, Tasa=Tasa, condTasa=condTasa)

        elif (IdTasa==2):#Tasa mayores 65 años
            if tipoVersion==1:
                interseccion=processing.run("qgis:extractbylocation",
                {'INPUT':Layer,
                'PREDICATE':0,
                'INTERSECT':vLayer,
                'OUTPUT':'TEMPORARY_OUTPUT'})

                zLayer=interseccion['OUTPUT']
                zLayer.setName(str(NombreLeyenda))
            else:
                interseccion=processing.run("qgis:selectbylocation",
                {'INPUT':Layer,
                'PREDICATE':0,
                'INTERSECT':vLayer,
                'METHOD':0})
                zLayer = interseccion.materialize(QgsFeatureRequest().setFilterFids(interseccion.selectedFeatureIds()))
                zLayer.setName(str(NombreLeyenda))

            myField = QgsField( myTargetField, QVariant.Double )
            zLayer.dataProvider().addAttributes([myField])
            zLayer.updateFields()
            prov = zLayer.dataProvider()
            numero_tasa=zLayer.fields().indexOf(myTargetField)
            zLayer.startEditing()

            poblacion=[]
            poblacion65=[]
            Tasa=[]
            for f in zLayer.getFeatures():
                    poblacion.append(float(f["pob_tot"]))
                    poblacion65.append(float(f["edad65_"]))
                    if all(flag > 0 for flag in [poblacion[-1],poblacion65[-1]+0.1]):
                        Tasa.append(round(float(100*poblacion65[-1]/poblacion[-1]),numDecimales))
                    else:
                        Tasa.append(float(-1))

                    zLayer.changeAttributeValue(f.id(), numero_tasa, Tasa[-1])

            zLayer.commitChanges()
            zLayer.updateFields()

            #A partir de aqui es donde se modifica para cada Tasa
            condTasa=self.getcondTasa(zLayer)
            if condTasa=="PuntoXY":
                condTasa=calcularPuntoXY(Tipo=IdRereferencia,tLayer=tLayer,lista_url_capas250=self.lista_url_capas250)
                if IdRereferencia==1:
                    zLayer.setName("Quintiles respecto a la provincia: " + condTasa)
                elif IdRereferencia==2:
                    zLayer.setName("Quintiles respecto al municipio: " + condTasa)
            dfReducido=df[(df['edad65_'] >= 0) & (df['pob_tot'] > 0)]
            Tasa = dfReducido[['grd_floaid','municipio','cmun']]
            Tasa[myTargetField]=100*(dfReducido['edad65_']/dfReducido['pob_tot'])
            Tasa=Tasa[(Tasa[myTargetField] >= 0)]
            Tasa=Tasa.round({myTargetField:numDecimales})

            (minimo,percentil_20,percentil_40,percentil_60,percentil_80,maximo)=calcularValorReferencia(Tipo=IdRereferencia, Tasa=Tasa, condTasa=condTasa)

        elif (IdTasa==3):#Poblacion extranejra
            if tipoVersion==1:
                interseccion=processing.run("qgis:extractbylocation",
                {'INPUT':Layer,
                'PREDICATE':0,
                'INTERSECT':vLayer,
                'OUTPUT':'TEMPORARY_OUTPUT'})

                zLayer=interseccion['OUTPUT']
                zLayer.setName(str(NombreLeyenda))
            else:
                interseccion=processing.run("qgis:selectbylocation",
                {'INPUT':Layer,
                'PREDICATE':0,
                'INTERSECT':vLayer,
                'METHOD':0})
                zLayer = interseccion.materialize(QgsFeatureRequest().setFilterFids(interseccion.selectedFeatureIds()))
                zLayer.setName(str(NombreLeyenda))

            myField = QgsField( myTargetField, QVariant.Double )
            zLayer.dataProvider().addAttributes([myField])
            zLayer.updateFields()
            prov = zLayer.dataProvider()
            numero_tasa=zLayer.fields().indexOf(myTargetField)
            zLayer.startEditing()

            den=[]
            num=[]
            aux1=[]
            aux2=[]
            Tasa=[]
            for f in zLayer.getFeatures():
                    den.append(float(f['pob_tot']))
                    aux1.append(float(f['esp']))
                    aux2.append(float(f['ue15']))
                    if aux2[-1]>0:
                        num.append(float(den[-1]-aux1[-1]-aux2[-1]))
                    else:
                        num.append(float(den[-1]-aux1[-1]))
                    num.append(float(den[-1]-aux1[-1]-aux2[-1]))
                    if all(flag > 0 for flag in [aux1[-1]+0.1,aux2[-1]+0.1,den[-1],num[-1]+0.1]):
                        Tasa.append(round(float(100*num[-1]/den[-1]),numDecimales))
                    else:
                        Tasa.append(float(-1))

                    zLayer.changeAttributeValue(f.id(), numero_tasa, Tasa[-1])

            zLayer.commitChanges()
            zLayer.updateFields()

            #A partir de aqui es donde se modifica para cada Tasa
            condTasa=self.getcondTasa(zLayer)
            if condTasa=="PuntoXY":
                condTasa=calcularPuntoXY(Tipo=IdRereferencia,tLayer=tLayer,lista_url_capas250=self.lista_url_capas250)
                if IdRereferencia==1:
                    zLayer.setName("Quintiles respecto a la provincia: " + condTasa)
                elif IdRereferencia==2:
                    zLayer.setName("Quintiles respecto al municipio: " + condTasa)
            dfReducido=df[(df['pob_tot']-df['esp']-df['ue15'] >= 0) & (df['esp'] >= 0) & (df['ue15'] >= 0) & (df['pob_tot'] > 0)]
            Tasa = dfReducido[['grd_floaid','municipio','cmun']]
            Tasa[myTargetField]=100*((dfReducido['pob_tot']-dfReducido['esp']-dfReducido['ue15'])/dfReducido['pob_tot'])
            Tasa=Tasa[(Tasa[myTargetField] >= 0)]
            Tasa=Tasa.round({myTargetField:numDecimales})

            (minimo,percentil_20,percentil_40,percentil_60,percentil_80,maximo)=calcularValorReferencia(Tipo=IdRereferencia, Tasa=Tasa, condTasa=condTasa)

        elif (IdTasa==4):#Poblacion activa
            if tipoVersion==1:
                interseccion=processing.run("qgis:extractbylocation",
                {'INPUT':Layer,
                'PREDICATE':0,
                'INTERSECT':vLayer,
                'OUTPUT':'TEMPORARY_OUTPUT'})

                zLayer=interseccion['OUTPUT']
                zLayer.setName(str(NombreLeyenda))
            else:
                interseccion=processing.run("qgis:selectbylocation",
                {'INPUT':Layer,
                'PREDICATE':0,
                'INTERSECT':vLayer,
                'METHOD':0})
                zLayer = interseccion.materialize(QgsFeatureRequest().setFilterFids(interseccion.selectedFeatureIds()))
                zLayer.setName(str(NombreLeyenda))

            myField = QgsField( myTargetField, QVariant.Double )
            zLayer.dataProvider().addAttributes([myField])
            zLayer.updateFields()
            prov = zLayer.dataProvider()
            numero_tasa=zLayer.fields().indexOf(myTargetField)
            zLayer.startEditing()

            den=[]
            num=[]
            aux1=[]
            aux2=[]
            Tasa=[]
            for f in zLayer.getFeatures():
                aux1.append(float(f['edad0015']))
                aux2.append(float(f['edad65_']))
                den.append(float(f['pob_tot']-(aux1[-1]+aux2[-1])))
                num.append(float(f['afil_ss']))
                if all(flag > 0 for flag in [aux1[-1]+0.1,aux2[-1]+0.1,den[-1],num[-1]+0.1]):
                    Tasa.append(round(float(100*num[-1]/den[-1]),numDecimales))
                else:
                    Tasa.append(float(-1))

                zLayer.changeAttributeValue(f.id(), numero_tasa, Tasa[-1])

            zLayer.commitChanges()
            zLayer.updateFields()

            #A partir de aqui es donde se modifica para cada Tasa
            condTasa=self.getcondTasa(zLayer)
            if condTasa=="PuntoXY":
                condTasa=calcularPuntoXY(Tipo=IdRereferencia,tLayer=tLayer,lista_url_capas250=self.lista_url_capas250)
                if IdRereferencia==1:
                    zLayer.setName("Quintiles respecto a la provincia: " + condTasa)
                elif IdRereferencia==2:
                    zLayer.setName("Quintiles respecto al municipio: " + condTasa)
            condTasa=self.getcondTasa(zLayer)
            dfReducido=df[(df['afil_ss'] >= 0) & (df['pob_tot'] > 0) & (f['pob_tot']-(f['edad0015']+f['edad65_'])>0)]
            Tasa = dfReducido[['grd_floaid','municipio','cmun']]
            Tasa[myTargetField]=100*((dfReducido['afil_ss'])/(dfReducido['pob_tot']-(dfReducido['edad0015']+dfReducido['edad65_'])))
            Tasa=Tasa[(Tasa[myTargetField] >= 0)]
            Tasa=Tasa.round({myTargetField:numDecimales})
            Tasa.loc[Tasa[myTargetField] > 100, myTargetField] = 100 #Se satura a 100%

            (minimo,percentil_20,percentil_40,percentil_60,percentil_80,maximo)=calcularValorReferencia(Tipo=IdRereferencia, Tasa=Tasa, condTasa=condTasa)

        elif (IdTasa==5):#Paro registrado
            if tipoVersion==1:
                interseccion=processing.run("qgis:extractbylocation",
                {'INPUT':Layer,
                'PREDICATE':0,
                'INTERSECT':vLayer,
                'OUTPUT':'TEMPORARY_OUTPUT'})

                zLayer=interseccion['OUTPUT']
                zLayer.setName(str(NombreLeyenda))
            else:
                interseccion=processing.run("qgis:selectbylocation",
                {'INPUT':Layer,
                'PREDICATE':0,
                'INTERSECT':vLayer,
                'METHOD':0})
                zLayer = interseccion.materialize(QgsFeatureRequest().setFilterFids(interseccion.selectedFeatureIds()))
                zLayer.setName(str(NombreLeyenda))

            myField = QgsField( myTargetField, QVariant.Double )
            zLayer.dataProvider().addAttributes([myField])
            zLayer.updateFields()
            prov = zLayer.dataProvider()
            numero_tasa=zLayer.fields().indexOf(myTargetField)
            zLayer.startEditing()

            den=[]
            num=[]
            aux1=[]
            aux2=[]
            Tasa=[]
            for f in zLayer.getFeatures():
                aux1.append(float(f['edad0015']))
                aux2.append(float(f['edad65_']))
                den.append(float(f['pob_tot']-(aux1[-1]+aux2[-1])))
                num.append(float(f['demp_pr']))
                if all(flag > 0 for flag in [aux1[-1]+0.1,aux2[-1]+0.1,den[-1],num[-1]+0.1]):
                    Tasa.append(round(float(100*num[-1]/den[-1]),numDecimales))
                else:
                    Tasa.append(float(-1))

                zLayer.changeAttributeValue(f.id(), numero_tasa, Tasa[-1])

            zLayer.commitChanges()
            zLayer.updateFields()

            #A partir de aqui es donde se modifica para cada Tasa
            condTasa=self.getcondTasa(zLayer)
            if condTasa=="PuntoXY":
                condTasa=calcularPuntoXY(Tipo=IdRereferencia,tLayer=tLayer,lista_url_capas250=self.lista_url_capas250)
                if IdRereferencia==1:
                    zLayer.setName("Quintiles respecto a la provincia: " + condTasa)
                elif IdRereferencia==2:
                    zLayer.setName("Quintiles respecto al municipio: " + condTasa)
            dfReducido=df[(df['demp_pr'] >= 0) & (df['pob_tot'] > 0) & (f['pob_tot']-(f['edad0015']+f['edad65_'])>0)]
            Tasa = dfReducido[['grd_floaid','municipio','cmun']]
            Tasa[myTargetField]=100*((dfReducido['demp_pr'])/(dfReducido['pob_tot']-(dfReducido['edad0015']+dfReducido['edad65_'])))
            Tasa=Tasa[(Tasa[myTargetField] >= 0)]
            Tasa=Tasa.round({myTargetField:numDecimales})

            (minimo,percentil_20,percentil_40,percentil_60,percentil_80,maximo)=calcularValorReferencia(Tipo=IdRereferencia, Tasa=Tasa, condTasa=condTasa)

        elif (IdTasa==6):#RMEs Total
            Layer2=AnadirLayerQGIS(url=self.lista_url_capas250[4],nombre="Capa grid RMEs", tipoCapa="WFS")
            if tipoVersion==1:
                interseccion=processing.run("qgis:extractbylocation",
                {'INPUT':Layer,
                'PREDICATE':0,
                'INTERSECT':vLayer,
                'OUTPUT':'TEMPORARY_OUTPUT'})
                zLayer=interseccion['OUTPUT']
                zLayer.setName(str(NombreLeyenda))

                interseccion=processing.run("qgis:extractbylocation",
                {'INPUT':Layer2,
                'PREDICATE':0,
                'INTERSECT':vLayer,
                'OUTPUT':'TEMPORARY_OUTPUT'})
                zLayer2=interseccion['OUTPUT']
            else:
                interseccion=processing.run("qgis:selectbylocation",
                {'INPUT':Layer,
                'PREDICATE':0,
                'INTERSECT':vLayer,
                'METHOD':0})
                zLayer = interseccion.materialize(QgsFeatureRequest().setFilterFids(interseccion.selectedFeatureIds()))
                zLayer.setName(str(NombreLeyenda))

            myField = QgsField( myTargetField, QVariant.Double )
            zLayer.dataProvider().addAttributes([myField])
            zLayer.updateFields()
            prov = zLayer.dataProvider()
            numero_tasa=zLayer.fields().indexOf(myTargetField)
            zLayer.startEditing()

            rme=[]
            Tasa=[]
            i=0
            for f in zLayer.getFeatures():
                for f2 in zLayer2.getFeatures():
                    if f2.geometry().equals(f.geometry()):
                        rme.append(float(f2["RMES_A00"]))
                        if all(flag > 0 for flag in [rme[-1]]):
                            Tasa.append(float(rme[-1]))
                        else:
                            Tasa.append(float(-1))

                        zLayer.changeAttributeValue(f.id(), numero_tasa, Tasa[-1])

            zLayer.commitChanges()
            zLayer.updateFields()

            #A partir de aqui es donde se modifica para cada Tasa
            condTasa=self.getcondTasa(zLayer)
            if condTasa=="PuntoXY":
                condTasa=calcularPuntoXY(Tipo=IdRereferencia,tLayer=tLayer,lista_url_capas250=self.lista_url_capas250)
                if IdRereferencia==1:
                    zLayer.setName("Quintiles respecto a la provincia: " + condTasa)
                elif IdRereferencia==2:
                    zLayer.setName("Quintiles respecto al municipio: " + condTasa)
            Tasa = df[['grd_floaid','municipio','cmun']]
            Tasa[myTargetField]=df['RMES_A00']
            Tasa=Tasa[(Tasa[myTargetField] >= 0)]
            Tasa=Tasa.round({myTargetField:numDecimales})

            (minimo,percentil_20,percentil_40,percentil_60,percentil_80,maximo)=calcularValorReferencia(Tipo=IdRereferencia, Tasa=Tasa, condTasa=condTasa)
        elif (IdTasa==7):#RMEs 45-65
            Layer2=AnadirLayerQGIS(url=self.lista_url_capas250[4],nombre="Capa grid RMEs", tipoCapa="WFS")
            if tipoVersion==1:
                interseccion=processing.run("qgis:extractbylocation",
                {'INPUT':Layer,
                'PREDICATE':0,
                'INTERSECT':vLayer,
                'OUTPUT':'TEMPORARY_OUTPUT'})
                zLayer=interseccion['OUTPUT']
                zLayer.setName(str(NombreLeyenda))

                interseccion=processing.run("qgis:extractbylocation",
                {'INPUT':Layer2,
                'PREDICATE':0,
                'INTERSECT':vLayer,
                'OUTPUT':'TEMPORARY_OUTPUT'})
                zLayer2=interseccion['OUTPUT']
            else:
                interseccion=processing.run("qgis:selectbylocation",
                {'INPUT':Layer,
                'PREDICATE':0,
                'INTERSECT':vLayer,
                'METHOD':0})
                zLayer = interseccion.materialize(QgsFeatureRequest().setFilterFids(interseccion.selectedFeatureIds()))
                zLayer.setName(str(NombreLeyenda))

            myField = QgsField( myTargetField, QVariant.Double )
            zLayer.dataProvider().addAttributes([myField])
            zLayer.updateFields()
            prov = zLayer.dataProvider()
            numero_tasa=zLayer.fields().indexOf(myTargetField)
            zLayer.startEditing()

            rme=[]
            Tasa=[]
            i=0
            for f in zLayer.getFeatures():
                for f2 in zLayer2.getFeatures():
                    if f2.geometry().equals(f.geometry()):
                        rme.append(float(f2["RMES_A45"]))
                        if all(flag > 0 for flag in [rme[-1]]):
                            Tasa.append(float(rme[-1]))
                        else:
                            Tasa.append(float(-1))

                        zLayer.changeAttributeValue(f.id(), numero_tasa, Tasa[-1])

            zLayer.commitChanges()
            zLayer.updateFields()

            #A partir de aqui es donde se modifica para cada Tasa
            condTasa=self.getcondTasa(zLayer)
            if condTasa=="PuntoXY":
                condTasa=calcularPuntoXY(Tipo=IdRereferencia,tLayer=tLayer,lista_url_capas250=self.lista_url_capas250)
                if IdRereferencia==1:
                    zLayer.setName("Quintiles respecto a la provincia: " + condTasa)
                elif IdRereferencia==2:
                    zLayer.setName("Quintiles respecto al municipio: " + condTasa)
            Tasa = df[['grd_floaid','municipio','cmun']]
            Tasa[myTargetField]=df['RMES_A45']
            Tasa=Tasa[(Tasa[myTargetField] >= 0)]
            Tasa=Tasa.round({myTargetField:numDecimales})

            (minimo,percentil_20,percentil_40,percentil_60,percentil_80,maximo)=calcularValorReferencia(Tipo=IdRereferencia, Tasa=Tasa, condTasa=condTasa)

        elif (IdTasa==8):#RMEs >65
            Layer2=AnadirLayerQGIS(url=self.lista_url_capas250[4],nombre="Capa grid RMEs", tipoCapa="WFS")
            if tipoVersion==1:
                interseccion=processing.run("qgis:extractbylocation",
                {'INPUT':Layer,
                'PREDICATE':0,
                'INTERSECT':vLayer,
                'OUTPUT':'TEMPORARY_OUTPUT'})
                zLayer=interseccion['OUTPUT']
                zLayer.setName(str(NombreLeyenda))

                interseccion=processing.run("qgis:extractbylocation",
                {'INPUT':Layer2,
                'PREDICATE':0,
                'INTERSECT':vLayer,
                'OUTPUT':'TEMPORARY_OUTPUT'})
                zLayer2=interseccion['OUTPUT']
            else:
                interseccion=processing.run("qgis:selectbylocation",
                {'INPUT':Layer,
                'PREDICATE':0,
                'INTERSECT':vLayer,
                'METHOD':0})
                zLayer = interseccion.materialize(QgsFeatureRequest().setFilterFids(interseccion.selectedFeatureIds()))
                zLayer.setName(str(NombreLeyenda))

            myField = QgsField( myTargetField, QVariant.Double )
            zLayer.dataProvider().addAttributes([myField])
            zLayer.updateFields()
            prov = zLayer.dataProvider()
            numero_tasa=zLayer.fields().indexOf(myTargetField)
            zLayer.startEditing()

            rme=[]
            Tasa=[]
            i=0
            for f in zLayer.getFeatures():
                for f2 in zLayer2.getFeatures():
                    if f2.geometry().equals(f.geometry()):
                        rme.append(float(f2["RMES_A65"]))
                        if all(flag > 0 for flag in [rme[-1]]):
                            Tasa.append(float(rme[-1]))
                        else:
                            Tasa.append(float(-1))

                        zLayer.changeAttributeValue(f.id(), numero_tasa, Tasa[-1])

            zLayer.commitChanges()
            zLayer.updateFields()

            #A partir de aqui es donde se modifica para cada Tasa
            condTasa=self.getcondTasa(zLayer)
            if condTasa=="PuntoXY":
                condTasa=calcularPuntoXY(Tipo=IdRereferencia,tLayer=tLayer,lista_url_capas250=self.lista_url_capas250)
                if IdRereferencia==1:
                    zLayer.setName("Quintiles respecto a la provincia: " + condTasa)
                elif IdRereferencia==2:
                    zLayer.setName("Quintiles respecto al municipio: " + condTasa)
            Tasa = df[['grd_floaid','municipio','cmun']]
            Tasa[myTargetField]=df['RMES_A65']
            Tasa=Tasa[(Tasa[myTargetField] >= 0)]
            Tasa=Tasa.round({myTargetField:numDecimales})

            (minimo,percentil_20,percentil_40,percentil_60,percentil_80,maximo)=calcularValorReferencia(Tipo=IdRereferencia, Tasa=Tasa, condTasa=condTasa)

        elif (IdTasa==9):#Tasa mujeres
            if tipoVersion==1:
                interseccion=processing.run("qgis:extractbylocation",
                {'INPUT':Layer,
                'PREDICATE':0,
                'INTERSECT':vLayer,
                'OUTPUT':'TEMPORARY_OUTPUT'})

                zLayer=interseccion['OUTPUT']
                zLayer.setName(str(NombreLeyenda))
            else:
                interseccion=processing.run("qgis:selectbylocation",
                {'INPUT':Layer,
                'PREDICATE':0,
                'INTERSECT':vLayer,
                'METHOD':0})
                zLayer = interseccion.materialize(QgsFeatureRequest().setFilterFids(interseccion.selectedFeatureIds()))
                zLayer.setName(str(NombreLeyenda))

            myField = QgsField( myTargetField, QVariant.Double )
            zLayer.dataProvider().addAttributes([myField])
            zLayer.updateFields()
            prov = zLayer.dataProvider()
            numero_tasa=zLayer.fields().indexOf(myTargetField)
            zLayer.startEditing()

            poblacion=[]
            poblacionm=[]
            Tasa=[]
            for f in zLayer.getFeatures():
                    poblacion.append(float(f["pob_tot"]))
                    poblacionm.append(float(f["pob_m"]))
                    if all(flag > 0 for flag in [poblacion[-1],poblacionm[-1]+0.1]):
                        Tasa.append(round(float(100*poblacionm[-1]/poblacion[-1]),numDecimales))
                    else:
                        Tasa.append(float(-1))

                    zLayer.changeAttributeValue(f.id(), numero_tasa, Tasa[-1])

            zLayer.commitChanges()
            zLayer.updateFields()

            #A partir de aqui es donde se modifica para cada Tasa
            condTasa=self.getcondTasa(zLayer)
            if condTasa=="PuntoXY":
                condTasa=calcularPuntoXY(Tipo=IdRereferencia,tLayer=tLayer,lista_url_capas250=self.lista_url_capas250)
                if IdRereferencia==1:
                    zLayer.setName("Quintiles respecto a la provincia: " + condTasa)
                elif IdRereferencia==2:
                    zLayer.setName("Quintiles respecto al municipio: " + condTasa)
            dfReducido=df[(df['pob_m'] >= 0) & (df['pob_tot'] > 0)]
            Tasa = dfReducido[['grd_floaid','municipio','cmun']]
            Tasa[myTargetField]=100*(dfReducido['pob_m']/dfReducido['pob_tot'])
            Tasa=Tasa[(Tasa[myTargetField] >= 0)]
            Tasa=Tasa.round({myTargetField:numDecimales})

            (minimo,percentil_20,percentil_40,percentil_60,percentil_80,maximo)=calcularValorReferencia(Tipo=IdRereferencia, Tasa=Tasa, condTasa=condTasa)

        """
        elif (IdTasa==3):#muj/hom
            Layer=AnadirLayerQGIS(url=self.lista_url_capas250[2],nombre="Capa grid de poblacion", tipoCapa="WFS")
            #Layer = QgsProject.instance().mapLayersByName("gridpob:gridpob_250")[0]  #gridpob:gridpob_250
            interseccion=processing.run("qgis:extractbylocation",
            {'INPUT':Layer,
            'PREDICATE':0,
            'INTERSECT':vLayer,
            'OUTPUT':'TEMPORARY_OUTPUT'})

            zLayer=interseccion['OUTPUT']
            zLayer.setName(NombreLeyenda)
            myField = QgsField( myTargetField, QVariant.Double )
            zLayer.dataProvider().addAttributes([myField])
            zLayer.updateFields()

            #QgsProject.instance().addMapLayer(zLayer)

            prov = zLayer.dataProvider()
            numero_tasa=zLayer.fields().indexOf(myTargetField)
            zLayer.startEditing()

            gidmp=[]
            hombre=[]
            mujer=[]
            Tasa=[]
            for f in zLayer.getFeatures():
                    gidmp.append(int(f["gidmp"]))
                    hombre.append(float(f["pob_h"]))
                    mujer.append(float(f["pob_m"]))
                    if all(flag > 0 for flag in [mujer[-1],hombre[-1]+0.1]):
                        Tasa.append(round(float(mujer[-1]/hombre[-1]),numDecimales))
                    else:
                        Tasa.append(float(-1))

                    zLayer.changeAttributeValue(f.id(), numero_tasa, Tasa[-1])
            zLayer.commitChanges()
            zLayer.updateFields()
            zLayer.updateExtents()
            zFeats = zLayer.getFeatures()

            dict={'gidmp':gidmp, myTargetField:Tasa}
            df=pd.DataFrame(dict)
            df2=df[df[myTargetField] >= 0]
            df2.sort_values("gidmp", inplace = True)
            df2.drop_duplicates(subset ="gidmp",keep = 'first', inplace = True)
            minimo=df2[myTargetField].min()
            percentil_20=df2[myTargetField].quantile(0.2)
            percentil_40=df2[myTargetField].quantile(0.4)
            percentil_60=df2[myTargetField].quantile(0.6)
            percentil_80=df2[myTargetField].quantile(0.8)
            maximo=df2[myTargetField].max()
            """

        #id=listwidget_capas250.currentRow()
        if importar==0:
            self.obtener_minmax(tipo=1,lista=[minimo,percentil_20,percentil_40,percentil_60,percentil_80,maximo])
        #listwidget_capas250_simbologia.item(IdTasa).actualiza_condicion()

        if (importar==1):
            #print(df2.describe(include='all') )

            myRangeList = []

            #QgsProject.instance().addMapLayer(zLayer)
            graduated_renderer = QgsGraduatedSymbolRenderer()
            lista_cond=self.condicion_lista[self.idCurrentRow]
            max_cond=self.maximo[self.idCurrentRow]
            lista_col=self.color_lista[self.idCurrentRow]
            for id in range(len(self.color_lista[self.idCurrentRow])-1):
                myColour = QColor(str(lista_col[id]))
                mySymbol2 = QgsSymbol.defaultSymbol(zLayer.geometryType())
                mySymbol2.setColor(myColour)
                mySymbol2.setOpacity(0.5)
                if tipoVersion==1:
                    graduated_renderer.addClassRange(QgsRendererRange(QgsClassificationRange(str(lista_cond[id])+' - '+str(lista_cond[id+1]), float(lista_cond[id]), float(lista_cond[id+1])), mySymbol2))
                else:
                    myRange2 = QgsRendererRange(float(lista_cond[id]), float(lista_cond[id+1]), mySymbol2,'Group 1')
                    myRangeList.append(myRange2)
                    #graduated_renderer.addClassRange(QgsRendererRange(float(lista_cond[id]), float(lista_cond[id+1])), mySymbol2,'Group 1')

            myColour = QColor(str(lista_col[id+1]))
            mySymbol2 = QgsSymbol.defaultSymbol(zLayer.geometryType())
            mySymbol2.setColor(myColour)
            mySymbol2.setOpacity(0.5)
            if tipoVersion==1:
                graduated_renderer.addClassRange(QgsRendererRange(QgsClassificationRange(str(lista_cond[id+1])+' - '+str(max_cond), float(lista_cond[id+1]), float(max_cond)), mySymbol2))
            else:
                myRange2=QgsRendererRange(float(lista_cond[id+1]), float(max_cond), mySymbol2,'Group 1')
                myRangeList.append(myRange2)
                graduated_renderer = QgsGraduatedSymbolRenderer('', myRangeList)

            # create renderer object
            graduated_renderer.setClassAttribute(myTargetField)
            zLayer.setRenderer(graduated_renderer)
            zLayer.triggerRepaint()

            #Se añade el label
            if (tamano_buffer<=2000):#Solo si está a menos de 2Km
                layer_settings  = QgsPalLayerSettings()
                text_format = QgsTextFormat()
                text_format.setFont(QFont("Arial", 6))
                buffer_settings = QgsTextBufferSettings()
                buffer_settings.setEnabled(True)
                buffer_settings.setSize(1)
                buffer_settings.setColor(QColor("white"))
                text_format.setBuffer(buffer_settings)
                layer_settings.setFormat(text_format)
                layer_settings.fieldName = myTargetField
                layer_settings.placement = 1
                layer_settings.enabled = True
                layer_settings = QgsVectorLayerSimpleLabeling(layer_settings)
                zLayer.setLabelsEnabled(True)
                zLayer.setLabeling(layer_settings)
                zLayer.triggerRepaint()

            self.importar_inequidad(zLayer,vLayer,tLayer,myTargetField,NombreLeyenda)
    def importar_inequidad(self,zLayer,vLayer,tLayer,TipoAbreviado,TituloLeyenda):#Capa de Tasa, Capa buffer, Capa punto, nombre archivo, titulo leyenda
        tamano_buffer=float(self.bufferVisionLineedit.text())
        borrar_buffer=not(self.checkbox_buffer.isChecked())
        borrar_centro=not(self.checkbox_centro.isChecked())
        borrar_capa_tasa=not(self.checkbox_capa_tasa.isChecked())
        vLayer.setName(self.texto_buffer.text()+ " " +str(tamano_buffer) + "m")
        uLayer=AnadirLayerQGIS(url=self.lista_url_capas250[0],nombre="Ortofoto", tipoCapa="WMS")
        QgsProject.instance().addMapLayer(uLayer)
        lblLayer=AnadirLayerQGIS(url=self.lista_url_capas250[1],nombre="Capa municipios", tipoCapa="WFS")
        QgsProject.instance().addMapLayer(lblLayer)
        AnadirLayerQGIS(url=vLayer,nombre="aux", tipoCapa="WFS",layer=1)
        QgsProject.instance().addMapLayer(vLayer)
        AnadirLayerQGIS(url=zLayer,nombre="aux", tipoCapa="WFS",layer=1)
        QgsProject.instance().addMapLayer(zLayer)
        AnadirLayerQGIS(url=tLayer,nombre="aux", tipoCapa="WFS",layer=1)
        QgsProject.instance().addMapLayer(tLayer)

        symbol = QgsMarkerSymbol.createSimple({'name': 'circle', 'color': 'red'})
        tLayer.renderer().setSymbol(symbol)
        tLayer.triggerRepaint()

        symbol = QgsFillSymbol.createSimple({'name': 'circle', 'width_border' : '0.66', 'style' : 'no'})
        vLayer.renderer().setSymbol(symbol)
        vLayer.triggerRepaint()

        wLayername = "buffer_vista"
        wLayer=QgsVectorLayer("Polygon", wLayername, "memory")
        QgsProject.instance().addMapLayer(wLayer)
        myField = QgsField( 'id', QVariant.Double )
        wLayer.dataProvider().addAttributes([myField])
        wLayer.updateFields()

        uLayer = QgsProject.instance().mapLayersByName(uLayer.name())[0]
        lblLayer = QgsProject.instance().mapLayersByName(lblLayer.name())[0]
        wLayer.setCrs(uLayer.crs())
        wFeats = wLayer.getFeatures()

        symbol = QgsFillSymbol.createSimple({'name': 'square', 'width_border' : '0.66', 'style' : 'no'})
        lblLayer.renderer().setSymbol(symbol)
        layer_settings  = QgsPalLayerSettings()
        text_format = QgsTextFormat()
        text_format.setFont(QFont("Arial", 8))
        buffer_settings = QgsTextBufferSettings()
        buffer_settings.setEnabled(True)
        buffer_settings.setSize(1)
        buffer_settings.setColor(QColor("white"))
        text_format.setBuffer(buffer_settings)
        layer_settings.setFormat(text_format)
        layer_settings.fieldName = "nombre"
        layer_settings.placement = 1
        layer_settings.enabled = True
        layer_settings = QgsVectorLayerSimpleLabeling(layer_settings)
        lblLayer.setLabelsEnabled(True)
        lblLayer.setLabeling(layer_settings)
        lblLayer.triggerRepaint()

        for feat_vista in tLayer.getFeatures():
            geom = feat_vista.geometry()
            dist=float(self.bufferVisionLineedit.text())
            buffer_vista = geom.buffer(dist+0.5*dist, 5)
            feat_vista.setGeometry(buffer_vista)
            wLayer.dataProvider().addFeatures([feat_vista])

        project = QgsProject.instance()
        layout = QgsPrintLayout(project)
        layout.initializeDefaults()
        layout.renderContext().setDpi(150)
        map = QgsLayoutItemMap(layout)
        map.setRect(QRectF(0, 0, 100, 100))
        map.setExtent(wLayer.extent())
        map.setLayers([tLayer,vLayer,lblLayer,zLayer,uLayer])
        map.setBackgroundColor(QColor(255, 255, 255))
        map.attemptResize(QgsLayoutSize(1753,1240, QgsUnitTypes.LayoutPixels))
        layout.addLayoutItem(map)

        legend = QgsLayoutItemLegend(layout)
        legend.setLinkedMap(map)
        legend.setLegendFilterByMapEnabled(True)
        layerTree = QgsLayerTree()
        layers_name = [tLayer.name(),vLayer.name(),zLayer.name()]
        #layers = [i for i in QgsProject().instance().mapLayers().values() if i.name() in layer_names]
        #for l in layers:
        #    layerTree.addLayer(l)
        for l_name in layers_name:
            layerTree.addLayer(QgsProject.instance().mapLayersByName(l_name)[0])

        legend.model().setRootGroup(layerTree)

        legend.setTitle(str(TituloLeyenda))
        legend.setStyleFont(QgsLegendStyle.Title, QFont("Comic Sans MS", 18))
        legend.setStyleFont(QgsLegendStyle.SymbolLabel, QFont("Comic Sans MS", 14))
        layout.addLayoutItem(legend)
        legend.attemptMove(QgsLayoutPoint(15, 10, QgsUnitTypes.LayoutPixels))
        legend.setFrameEnabled(True)
        legend.refresh()

        pdf_path = os.path.join(self.data_path.text(), self.nombre_fichero.text() + "_" + str(TipoAbreviado) + '.png')
        exporter = QgsLayoutExporter(layout)
        exporter.exportToImage(pdf_path, QgsLayoutExporter.ImageExportSettings())
        #Borrar documentos
        QgsProject.instance().removeMapLayer(wLayer)
        del wLayer
        if borrar_centro:
            QgsProject.instance().removeMapLayer(tLayer)
            del tLayer
        if borrar_buffer:
            QgsProject.instance().removeMapLayer(vLayer)
            del vLayer
        if borrar_capa_tasa:
            QgsProject.instance().removeMapLayer(zLayer)
            del zLayer

        #Se refresca el mapa
        iface.mapCanvas().refresh()

class clase_lista_malla250(QWidget):
    def __init__(self,parent=None,nombre="default_name",colores_invertidos=0):
        super().__init__()
        del(parent)
        #self.id=listwidget_capas250.currentRow()
        #self.nombre=listwidget_capas250.item(1).text()
        self.nombre=nombre

        self.widgetLabel = QLabel(self.nombre)
        self.widgetLabel.setFont(QFont('Arial', 10))
        widgetLayout = QHBoxLayout()
        widgetLayout.addWidget(self.widgetLabel)
        widgetLayout.addStretch(1)

        widgetLayout.setSizeConstraint(QLayout.SetFixedSize)
        self.setLayout(widgetLayout)
        #self.setSizeHint(self.widget_prueba.sizeHint())
        #self.mostrar_item_capa250()

class clase_colores(QListWidgetItem):
    def __init__(self, parent=None, id = 0, color = "#000000", condicion = 0):
        super().__init__()
        del(parent)

        self.id = id
        self.widget=QWidget()
        self.widgetLabel = QLabel("")
        self.widgetLabel.setFont(QFont('Arial', 10))
        self.widgetLabel.setStyleSheet("border: 1px solid black;min-width: 20px;min-height: 20px;background-color: " + str(color))
        self.widgetText = QLineEdit(str(condicion))
        self.widgetText.setFont(QFont('Arial', 10))

        self.widgetLabelLeyenda = QLabel("0-0")
        self.widgetLabelLeyenda.setFont(QFont('Arial', 10))
        widgetLayout = QHBoxLayout()
        widgetLayout.addWidget(self.widgetLabel)
        widgetLayout.addWidget(self.widgetText)
        widgetLayout.addWidget(self.widgetLabelLeyenda)
        widgetLayout.addStretch(1)

        widgetLayout.setSizeConstraint(QLayout.SetFixedSize)
        self.widget.setLayout(widgetLayout)
        self.setSizeHint(self.widget.sizeHint())


from time import time, sleep
from threading import Thread, Lock

class Player:
    """
    Temporal line, to be always at the same time
    than the currnt music
    """

    def __init__(self, sp):
        self.tps = 0
        self.sp = sp
        self.mutex = Lock()
        self.started = False

    def start(self):
        if not(self.started):
            self.started = True
            proc = Thread(target=self.run)
            proc.start()

    def run(self):
        while self.started:
            # add 1ms
            tps = time()
            while time() - tps < 0.001:
                pass
            self.mutex.acquire()
            self.tps += 1
            self.mutex.release()
        self.continu = True

    def set(self, tps):
        self.mutex.acquire()
        self.tps = tps
        self.mutex.release()

    def stop(self):
        self.started = False


from delaunay import computeVolume
from random import randint
from transform import *
import symbols as sym
import config
import time

class Point3D():
	"""generic point"""
	def __init__(self, x, y, z):
		self.x = x
		self.y = y
		self.z = z
		self.L = (self.x, self.y, self.z)

	def __str__(self):
		return str(self.L)

class PointCloud():
	"""n points in space"""
	def __init__(self, n1, n2=None):
		if n2 is None:
			self.point_array = [Point3D(randint(0, 9), randint(0, 9), randint(0, 9)) for i in range(int(n1))]
		else :
			pts = [ Point3D(t[0], t[1], t[2]) for t in n1]
			self.point_array = pts

		self.orig_array = (self.point_array).copy()

	def print_details(self):
		print("Points in Cloud : ")
		for pt in self.point_array:
			print(pt)

	def get_volume(self):
		v = computeVolume(self.point_array)
		return v

	def restore(self):
		self.point_array = (self.orig_array).copy()

	def recompute(self, transformations, idx):
		self.point_array = (self.orig_array).copy()
		total_angle = 0
		for i in range(idx+1):
			total_angle += transformations[i][1][1]
			self.transform((sym.ROTATE_EULER_MODE, transformations[i][1]))
		# self.transform((sym.ROTATE_EULER_MODE, (transformations[0][1][0], total_angle)))

	def transform(self,transform_op, normalize=False):
		pts = self.point_array
		self.point_array = [Point3D(t[0], t[1], t[2]) for t in transformAllPoints(pts, transform_op, normalize)]


# this lecture is mainly about List and Random Variable
# A list act similar to an array in python
# I have some questions either can we add some elements in a list or not, is at static or Dynamic in nature

states_of_america = ["Delaware", "Pennsylvania", "New Jersey", "Georgia", "Connecticut",
                     "Massachusetts", "Maryland", "South Carolina", "New Hampshire", "Virginia",
                     "New York", "North Carolina", "Rhode Island", "Vermont", "Kentucky", "Tennessee",
                     "Ohio", "Louisiana", "Indiana", "Mississippi", "Illinois", "Alabama", "Maine",
                     "Missouri", "Arkansas", "Michigan", "Florida", "Texas", "Iowa", "Wisconsin", "California",
                     "Minnesota", "Oregon", "Kansas", "West Virginia", "Nevada", "Nebraska", "Colorado",
                     "North Dakota", "South Dakota", "Montana", "Washington", "Idaho", "Wyoming", "Utah",
                     "Oklahoma", "New Mexico", "Arizona", "Alaska", "Hawaii"]

print(states_of_america)
print(len(states_of_america))
print(states_of_america[3])

fruits = ["Strawberries", "Kale", "Nectarines", "Apples", "Grapes", "Peaches",
          "Cherries", ]

Vegies = ["Pears", "Tomatoes", "Celery", "Potatoes", "Spinach"]

Dirty_Dozen = [fruits, Vegies]

print(Dirty_Dozen[1][1])

#######

fruits = ["Strawberries", "Nectarines", "Apples", "Grapes", "Peaches", "Cherries", "Pears"]
fruits[-1] = "Melons"
fruits.append("Lemons")
print(fruits)


def back(i, j, num):
    global min_num
    num += num_map[i][j]
    if i == n - 1 and j == n - 1:
        if min_num > num:
            min_num = num
    else:
        if i + 1 < n:
            back(i + 1, j, num)
        if j + 1 < n:
            back(i, j + 1, num)

t = int(input())
for tc in range(1, t + 1):
    min_num = 0xffffff
    n = int(input())
    num_map = [list(map(int, input().split())) for _ in range(n)]
    back(0,0,0)
    print('#{} {}'.format(tc, min_num))


# codeup - 스택 - 수식계산 (2020-09-22)
# https://codeup.kr/problem.php?id=3127

from collections import deque
import sys
sys.stdin = open("codeup/[스택]3127_수식계산.txt",'r')

T = int(input())
for _ in range(T):
    s = deque(input().split())
    method = ['*','+','-']
    first = True
    val = 0
    st = []
    while(s) :
        tmp = s.popleft()
        if tmp in method :
            if tmp == '*' :
                val = st.pop() * st.pop()
            elif tmp == '+' :
                val = st.pop() + st.pop()
            else :
                val = -st.pop() + st.pop()

            st.append(val)
        else :
            st.append(int(tmp))
    print(st[0])


import math

for t in range(1, int(input())+1):
    a, b = input().split()
    la, lb = len(a), len(b)

    v = la * lb // math.gcd(la, lb)
    a = a * (v // la)
    b = b * (v // lb)

    res = "yes" if a == b else "no"
    print("#{} {}".format(t, res))


from typing import MutableSequence
import numpy as np
import string
import random

def mutateString(DNA):
    dna_char = random.choice(string.ascii_letters + string.punctuation + string.digits)
    dna_index = random.randint(0, len(DNA))
    dna_int = random.randint(1,10)
    if dna_int == 1:
        DNA = DNA[:dna_index] + dna_char + DNA[dna_index+1:]
    return DNA
dna = mutateString(input("enter initial DNA string: "))

compete_DNA = mutateString(dna)

target_DNA = "Tian Xiaoyang_!9017"

def fitness1(x, y, n):
    fit_val = 0
    x = compete_DNA
    y = target_DNA
    n = len(target_DNA)
    for i in range (n):
       fit_val= x[i] - y[i]
    return fit_val

def fitness2(x, y, n):
    x = compete_DNA
    y = target_DNA
    n = len(target_DNA)
    for i in range (n):
        fit_val2= (x[i] - y)[i]*(x[i] - y[i])
    return fit_val2

def fitness3(x, y, n):
    x = compete_DNA
    y = target_DNA
    n = len(target_DNA)
    for i in range (n):
        if x[i] != y[i]:
            fit_val3 = 1
    return fit_val3

def fitness_():
    x = compete_DNA
    y = target_DNA
    n =len(target_DNA)
    fit_val = 0
    for i in range(n):
        if x [i] != y[i]:
            fit_val += 1
    return fit_val

def fitness_():
    x = compete_DNA
    y = target_DNA
    n =len(target_DNA)
    fit_val = 0
    for i in range(n):
        if x [i] == y[i]:
            fit_val += 1
    return fit_val

def fitness_():
    x = compete_DNA
    y = target_DNA
    n =len(target_DNA)
    fit_val = 0
    if len(y) == n:
        fit_val = 1
    return fit_val

dna = mutateString(input("enter initial DNA string: "))

print("mutated DNA: ", dna)


# -*- coding: utf-8 -*-

# 对文件内的字符串排序, python sort_in_file.py [file]
import os

import sys

def sort_sfile(file):
    with open(file, 'r') as f:
        lines = f.readlines()
        lines = [line.lower() for line in lines]
        print(f'\nOriginal lines: {[(i, lines) for i, lines in enumerate(lines)]}')

    lines.sort()
    print(f'\nSorted lines: {[(i, lines) for i, lines in enumerate(lines)]}')
    ret = input('\nWould you want to write it to file? (y/n):')
    if ret.lower() == 'y':
        with open(file, 'w') as f:
            f.writelines(lines)

if __name__ == '__main__':
    file = sys.argv[1]
    sort_sfile(file)


from collections import deque, defaultdict

N, M = map(int, input().split())
A = [deque(map(int, input().split())) for _ in range(N)]
X = [False] * (M + 1)

ans = N

while True:
  D = defaultdict(int)
  m = 0
  for a in A:
    if len(a) == 0:
      print(ans)
      exit()

    a0 = a[0]
    D[a0] += 1
    m = max(m, D[a0])

  K = [k for k, v in D.items() if v == m]
  for k in K: X[k] = True
  ans = min(ans, m)

  for a in A:
    while a and X[a[0]]: a.popleft()


from maxcutpy.solvers.cemaxcut import CrossEntropyMaxCut
from maxcutpy.solvers.bmmaxcut import BureirMonteiroMaxCut
from maxcutpy.solvers.randommaxcut import RandomMaxCut
from maxcutpy.solvers.abstractmaxcut import AbstractMaxCut

from typing import List, Optional
from sklearn.preprocessing import LabelEncoder

import math

import numpy as np
import pandas as pd
import networkx as nx

class MaxCutManager:

    def __init__(self, solver_name: str, num_batches: int, solver_args: Optional[dict] = {}, seed: Optional[int] = 12345, matrix: Optional[np.array] = None, graph: Optional[nx.Graph] = None, dataframe: Optional[pd.DataFrame] = None):

        self.seed = seed
        self.generator = np.random.default_rng(self.seed)
        self.le = None

        self.solver_args = solver_args
        self.solver_name = solver_name
        self.num_batches = num_batches

        if matrix is not None:
            self.origin_matrix = matrix
        elif graph is not None:
            UG = self._transform_D_2_U(graph)
            self.origin_matrix = nx.to_numpy_matrix(UG)
        elif dataframe is not None:
            if dataframe.shape[1] != 3:
                raise RuntimeError("Dataframe should be in long format: [NODEA, NODEB, EDGE_WEIGHT]!")

            dataframe.columns = ["NODEA", "NODEB", "EDGE_WEIGHT"]
            UG = self._transform_long_2_nx(dataframe)
            self.origin_matrix = nx.to_numpy_matrix(UG)
        else:
            raise RuntimeError("At least one of `matrix`, `graph`, or `dataframe` must not be None!")

        self.partition_vector = None

    def score_cut_vectors(self, cut_vectors: np.array) -> List[np.float64]:
        """A function to calculate the score of a set of sample cut vectors

        Args:
            cut_vectors:    A numpy matrix of the cut vectors.
                Must be of shape (N, n) where n is the number of vertices and
                N is the number of sample cut vectors

        Returns:
            A List of np.float64 scores, N in length, which is the score of each
            cut vector

        """

        ##Needs to be replaced by a better, vectorized (or parallel) method
        scores = []
        for i in range(cut_vectors.shape[0]):
            cut_0 = np.argwhere(cut_vectors[i,:] == 0).reshape((1,-1))[0]
            cut_1 = np.argwhere(cut_vectors[i,:] == 1).reshape((1,-1))[0]

            pairs = np.array(np.meshgrid(cut_0, cut_1)).T.reshape(-1, 2)

            score_i = sum([self.matrix[x, y] for x, y in pairs])
            scores.append(score_i)

        return scores

    def generate_cut_vectors(self, N: int, n: int, p: List[np.float64]) -> np.array:
        """A function to generate a sample set of cut vectors

        Args:
            N: The integer number of samples to generate
            n: The integer number of vertices in each cut
            p: A list of float probabilities used in generating the cut vectors

        Returns:
            A numpy matrix of shape (N, n) where each row is a cut vector

        """
        assert len(p) == n

        cut_vectors = self.generator.binomial(n=1, p=p, size=(N, n))

        return cut_vectors

    def _transform_long_2_nx(self, dataframe):

        nodes_left = pd.concat([dataframe["NODEA"], dataframe["NODEB"]]).unique()

        self_pointing = pd.DataFrame(columns=["NODEA", "NODEB", "EDGE_WEIGHT"])
        self_pointing["NODEA"] = nodes_left
        self_pointing["NODEB"] = nodes_left
        self_pointing["EDGE_WEIGHT"] = 0

        partial_df = pd.concat([dataframe, self_pointing])

        self.le = LabelEncoder()
        self.le.fit(list(set(partial_df["NODEA"].values).union(set(partial_df["NODEB"].values))))
        partial_df.loc[:, "NODEA"] = self.le.transform(partial_df["NODEA"])
        partial_df.loc[:, "NODEB"] = self.le.transform(partial_df["NODEB"])

        G = nx.DiGraph()
        nw_view = list(partial_df.itertuples(index=False, name=None))
        G.add_weighted_edges_from(nw_view)

        return self._transform_D_2_U(G)

    def _transform_D_2_U(self, G):
        UG = G.to_undirected()
        for node in G:
            for ngbr in nx.neighbors(G, node):
                if node in nx.neighbors(G, ngbr):
                    UG.edges[node, ngbr]['weight'] = (
                        .5*G.edges[node, ngbr]['weight'] + .5*G.edges[ngbr, node]['weight']
                    )

        return UG

    def get_best_cut_vector(self, format: Optional[str] ="Indicator"):
        if self.best_cut_vector is None:
            raise RuntimeError("Best Cut Vector has not yet been found! Run batch_split to find the best cut vector!")

        l_format = format.lower()
        if l_format == "indicator":
            return self.best_cut_vector
        elif l_format == "index":
            return self._split_vec_2_parts(self.best_cut_vector)
        elif l_format == "labels":
            if self.le is None:
                raise RuntimeError(f"`labels` mode is only usable when this object was created with a dataframe")

            p0, p1 = self._split_vec_2_parts(self.best_cut_vector)
            p0_labels = self.le.inverse_transform(p0)
            p1_labels = self.le.inverse_transform(p1)

            return p0_labels, p1_labels
        else:
            raise RuntimeError(f"Unknown Best Cut Vector format: {format}")

    def _split_vec_2_parts(self, vector):
        part_0, part_1 = [], []

        for e, v in enumerate(vector):
            if v == 0:
                part_0.append(e)
            else:
                part_1.append(e)

        return part_0, part_1

    def split_all(self):
        if self.partition_vector is None:
            binary_style = self._split_line(self.origin_matrix, 0)

            integer_style = [int(bs, 2) for bs in binary_style]

            self.partition_vector = integer_style

        return self.partition_vector

    def _split_line(self, matrix_part, batch_depth):

        if self.solver_name == "RandomMaxCut":
            solver = RandomMaxCut(seed=self.seed, matrix=matrix_part, **self.solver_args)
        elif self.solver_name == "CrossEntropyMaxCut":
            solver = CrossEntropyMaxCut(seed=self.seed, matrix=matrix_part, **self.solver_args)
        elif self.solver_name == "BureirMonteiroMaxCut":
            solver = BureirMonteiroMaxCut(seed=self.seed, matrix=matrix_part, **self.solver_args)
        else:
            raise RuntimeError(f"Invalid solver {self.solver_name}!")

        cut_vector = solver.batch_split()

        if (batch_depth + 1 >= int(math.log(self.num_batches, 2.0))):
            #Base condition
            #Return partition
            return [str(v) for v in cut_vector]
        else:
            #Split matrix_part into two submatrices based on partition
            batch_1, batch_2 = solver._split_vec_2_parts(cut_vector)

            matrix_part_1 = matrix_part[:, batch_1][batch_1, :]
            matrix_part_2 = matrix_part[:, batch_2][batch_2, :]

            part_1, part_2 = self._split_line(matrix_part_1, batch_depth+1), self._split_line(matrix_part_2, batch_depth+1)

            part_1 = ["0" + v for v in part_1]
            part_2 = ["1" + v for v in part_2]

            #Recombine part_1 and part_2 into the same state
            recombined_partition = []
            p1_c = 0
            p2_c = 0
            for i in range(matrix_part.shape[0]):
                if i in batch_1:
                    recombined_partition.append(part_1[p1_c])
                    p1_c += 1
                else:
                    recombined_partition.append(part_2[p2_c])
                    p2_c += 1

            return recombined_partition

    def label_batches(self):
        if self.le is None:
            raise RuntimeError(f"This function is only usable when this object was created with a dataframe")
        if self.partition_vector is None:
            raise RuntimeError(f"This function is only usable after calling `split_all`")

        labeled_df = pd.DataFrame()
        labeled_df["NODE"] = self.le.inverse_transform(list(range(len(self.partition_vector))))
        labeled_df["BATCH"] = self.partition_vector

        return labeled_df

    def score_batches(self):
        if self.partition_vector is None:
            raise RuntimeError(f"This function is only usable after calling `split_all`")

if __name__ == "__main__":
    dataframe = pd.DataFrame()
    dataframe["NODE1"] = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"]
    dataframe["NODE2"] = ["B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "A"]
    dataframe["Weight"] = list(range(12))

    manager = MaxCutManager("CrossEntropyMaxCut", num_batches=4, dataframe=dataframe)

    manager.split_all()

    print(manager.label_batches())


def make_list(n):
    lst=[]
    for i in range(1,n+1):
        lst.append(i)
    return lst

def apart(k,n):
    lst=[]
    lst.append(make_list(n))
    for r in range(1,k+1):
        for c in range(n):
            if c==0:
                lst.append([1])
            elif c==1:
                lst[r].append(lst[r-1][c] + lst[r-1][c-1])
            else:
                lst[r].append(lst[r-1][c] + lst[r][c-1])
    return lst[k][n-1]

for T in range(int(input())):
    k = int(input())
    n = int(input())
    print(apart(k,n))

백준/Bronze/2775. 부녀회장이 될테야/부녀회장이 될테야.py

부녀회장이 될테야.py


"""
Script for reading and writing .qb files
"""

import numpy as np
import struct

class QbMatrix(object):

    def __init__(self, name, data, pos):
        self.name = name
        self.pos = pos
        self.data = data

class Qb(object):

    def __init__(self):
        self.version = 0x0101
        self.colorFormat = 0
        self.zAxisOrientation = 0
        self.compressed = 0
        self.visibilityMaskEncoded = 0
        self.matrixList = []

    def load(self, file):
        self.version = struct.unpack("I", file.read(4))[0]
        self.colorFormat = struct.unpack("I", file.read(4))[0]
        self.zAxisOrientation = struct.unpack("I", file.read(4))[0]
        self.compressed = struct.unpack("I", file.read(4))[0]
        self.visibilityMaskEncoded = struct.unpack("I", file.read(4))[0]
        numMatrices = struct.unpack("I", file.read(4))[0]
        self.matrixList = []

        for i in range(numMatrices):
            self.matrixList.append(self.loadMatrix(file))

    def loadMatrix(self, file):
        nameLength = struct.unpack("B", file.read(1))[0]
        name = struct.unpack(str(nameLength)+"s", file.read(nameLength))[0]

        size = struct.unpack("III", file.read(12))

        pos = struct.unpack("iii", file.read(12))

        matrix = np.ndarray(shape=size)

        if self.compressed == 0:
            sizeLength = size[0]*size[1]*size[2]
            matrix = np.ndarray(shape=size, dtype=int, buffer=file.read(sizeLength * 4))
        else:
            CODEFLAG = 2
            NEXTSLICEFLAG = 6

            z = 0
            while (z < size[2]):
                z += 1
                index = 0

                while True:
                    data = struct.unpack("I", file.read(4))[0]

                    if data == NEXTSLICEFLAG:
                        break
                    elif data == CODEFLAG:
                        count = struct.unpack("I", file.read(4))[0]
                        data = struct.unpack("I", file.read(4))[0]

                        for j in range(count):
                            x = index % size[0] + 1
                            y = index // size[0] + 1
                            index += 1
                            matrix[x + y*size[0] + z*size[0]*size[1]] = data
                    else:
                        x = index % sizex + 1
                        y = index // sizex + 1
                        index += 1
                        matrix[x + y*size[0] + z*size[0]*size[1]] = data

        return QbMatrix(name, matrix, pos)

    def save(self, file):
        self.compressed = 0 #Compression saving not supported

        file.write(struct.pack("I", self.version))
        file.write(struct.pack("I", self.colorFormat))
        file.write(struct.pack("I", self.zAxisOrientation))
        file.write(struct.pack("I", self.compressed))
        file.write(struct.pack("I", self.visibilityMaskEncoded))
        file.write(struct.pack("I", len(self.matrixList)))

        for matrix in self.matrixList:
            self.saveMatrix(file, matrix)

    def saveMatrix(self, file, matrix):
        file.write(struct.pack("B", len(matrix.name)))
        file.write(struct.pack(str(len(matrix.name.encode('ascii')))+"s", matrix.name.encode('ascii')))
        file.write(struct.pack("III", matrix.data.shape[0], matrix.data.shape[1], matrix.data.shape[2]))
        file.write(struct.pack("iii", matrix.pos[0], matrix.pos[1], matrix.pos[2]))
        for z in range(matrix.data.shape[2]):
            for y in range(matrix.data.shape[1]):
                for x in range(matrix.data.shape[0]):
                    file.write(struct.pack("I", matrix.data[x, y, z]))


from django.shortcuts import render
from .models import FliaWetzel
from django.http import HttpResponse
from django.template import Template

# Create your views here.
def wetzel(request):

    familia1= FliaWetzel(nombre= "Santiago", apellido= "Wetzel", edad= 58, año_nacimiento= 1964)
    familia1.save()

    familia2= FliaWetzel(nombre= "Laura", apellido= "Lazarte", edad= 53, año_nacimiento= 1969)
    familia2.save()

    familia3= FliaWetzel(nombre= "Matias", apellido= "Wetzel", edad= 28, año_nacimiento= 1994)
    familia3.save()

    familia4= FliaWetzel(nombre= "Bruno", apellido= "Wetzel", edad= 26, año_nacimiento= 1997)
    familia4.save()

    familia5= FliaWetzel(nombre= "Naiara", apellido= "Wetzel", edad= 23, año_nacimiento= 1999)
    familia5.save()

    familia6= FliaWetzel(nombre= "Tobias", apellido= "Lazarte", edad= 17, año_nacimiento= 2005)
    familia6.save()

    familia= FliaWetzel.objects.all()

    return render(request, "template.html", {"familia" : familia})
    return HttpResponse()


# 백준 #7869 두 원
'''
    Algorithm: 기하학, 수학, 조건 분기
    Time Complexity: O(1)

    1. 두 원이 겹치지 않을 때 --> 0
    2. 한 원이 다른 원에 포함될 때 -> 작은 원의 넓이
    3. 그 외:
        각 원의 겹치는 부분 부채꼴의 넓이 - 삼각형의 넓이를 더해준다.

    소수점 3자리가 무조건이었다..
'''
import sys
import math

input = sys.stdin.readline

def solution(x1: float, y1: float, r1: float, x2: float, y2: float, r2: float) -> float:
    def get_angle(a: float, b: float, c: float) -> float:
        ''' 세 변이 a, b, c인 삼각형의 a와b의 사이각을 라디안으로 반환하는 함수 '''
        return math.acos((a**2 + b**2 - c**2)/(2*a*b))

    def get_triangle_area(theta: float, r: float) -> float:
        ''' 반지름이 r이고 그 사이각이 theta인 삼각형의 넓이를 반환 '''
        return (r**2 * math.sin(theta)) / 2

    def get_sector_area(theta: float, r: float) -> float:
        ''' 반지름이 r이고 각이 theta인 부채꼴의 넓이를 반환 '''
        return (r**2 * theta) / 2

    center_line = math.sqrt(math.pow(x2-x1, 2) + math.pow(y2-y1, 2))

    if r1 + r2 <= center_line:
        S = float(0)

    elif abs(r2-r1) >= center_line:
        S = math.pi * (min(r1, r2)**2)

    else:
        theta1 = 2 * get_angle(r1, center_line, r2)
        theta2 = 2 * get_angle(r2, center_line, r1)

        S1 = get_sector_area(theta1, r1) - get_triangle_area(theta1, r1)
        S2 = get_sector_area(theta2, r2) - get_triangle_area(theta2, r2)
        S = S1 + S2

    S = str(round(S, 3))
    l = len(S.split('.')[-1])

    while l < 3:
        S += '0'
        l += 1

    return S

if __name__ == "__main__":
    print(solution(*map(float, input().split())))