# -*- coding: utf-8 -*-
"""
Copyright (c) 2016 Biuro Urządzania Lasu i Geodezji Leśnej

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), 
to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 
OTHER DEALINGS IN THE SOFTWARE.
"""


"""Moduł z definicjami warstw"""

from qgis.core import *
from PyQt5.QtCore import *
from Mapa_PU.model.konfiguracja import *

import Mapa_PU.kontroler.komunikaty
class Warstwy:
    """Klasa z definicjami warstw"""
    #Marcin Śliwa
    def __init__(self,Konfiguracja):
        """konstruktor"""
        self.konfiguracja=Konfiguracja
    
    def generujWarstwy(self):
        """generowanie warstw"""
        try:
            self.w=[self.__o_podst(),self.__pnsw(),self.__linie(),self.__os_pkt(),self.__os_pow(),self.__dz_ewid()
                    ,self.__uzytki(),self.__op_pow(),self.__op_oddz(),self.__oddzial(),self.__pow()]
            
            for warstwa in self.w:
                self.zapiszWarstwe(warstwa)
                
            
        except Exception as e:
            komunikaty.blad_systemowy(e)
    def zapiszWarstwe(self,warstwa):
        """zapis warstwy
        :param warstwa: warstwa
        """
        try:
            plik=self.konfiguracja.warstwy+"\\"+warstwa.name()
            #self.blad=QgsVectorFileWriter.writeAsVectorFormat(warstwa,plik,None,None,"ESRI Shapefile")
            #q=QString()
            blad,blad_opis=QgsVectorFileWriter.writeAsVectorFormat(warstwa,plik,"utf-8",warstwa.crs(),"ESRI Shapefile")
            if blad!=QgsVectorFileWriter.NoError:
                 komunikaty.blad('Błąd zapiszWarstwe',blad_opis)
        except Exception as e:
            raise e
            #print e
    
    def __o_podst(self):
        """Warstwa O_PODST
        :return warstwa
        """
        try:
            w = QgsVectorLayer("Polygon?crs=epsg:2180", "O_PODST", "memory")
            pr=w.dataProvider()
            pr.addAttributes([
                        QgsField("ID", QVariant.Int,"Integer",6),
                        QgsField("ADR_BDL",  QVariant.String,"String",25),
                        QgsField("ADR_ADM", QVariant.String,"String",25),
                        QgsField("NR_EW", QVariant.String,"String",25),
                        QgsField("NR_KONT", QVariant.Int,"Integer",4)
                        ])
            w.updateFields()
            return w
        except Exception as e:
            komunikaty.blad_systemowy(e)
            
    def __pnsw(self):
        """Warstwa PNSW
        :return warstwa
        """
        try:
            w = QgsVectorLayer("Polygon?crs=epsg:2180", "PNSW", "memory")
            pr=w.dataProvider()
            pr.addAttributes([
                        QgsField("ID", QVariant.Int,"Integer",6),
                        QgsField("ADR_BDL",  QVariant.String,"String",25),
                        QgsField("KOD_PNSW", QVariant.String,"String",12),
                        QgsField("NR_PNSW", QVariant.Int,"Integer",4),
                        QgsField("NR_EW", QVariant.String,"String",25),
                        QgsField("ADR_ADM", QVariant.String,"String",25)
                        ])
            w.updateFields()
            return w
        except Exception as e:
            komunikaty.blad_systemowy(e)
            
    def __linie(self):
        """Warstwa lini
        :return warstwa
        """
        try:
            w = QgsVectorLayer("linestring?crs=epsg:2180", "LINIE", "memory")
            pr=w.dataProvider()
            pr.addAttributes([
                        QgsField("ID", QVariant.Int,"Integer",6),
                        QgsField("KOD", QVariant.String,"String",12),
                        QgsField("SZER", QVariant.Double,"double",4,1)
                       
                        ])
            w.updateFields()
            return w
        except Exception as e:
            komunikaty.blad_systemowy(e)
            
    def __os_pkt(self):
        """Warstwa osobliwosci punktowych
        :return warstwa
        """
        try:
            w = QgsVectorLayer("point?crs=epsg:2180", "OS_PKT", "memory")
            pr=w.dataProvider()
            pr.addAttributes([
                        QgsField("ID", QVariant.Int,"Integer",6),
                        QgsField("ADR_BDL",  QVariant.String,"String",25),
                        QgsField("KOD_OSOBL", QVariant.String,"String",12),
                        QgsField("NR_OSOBL", QVariant.Int,"Integer",4)
                                               
                        ])
            w.updateFields()
            return w
        except Exception as e:
            komunikaty.blad_systemowy(e)
            
    def __os_pow(self):
        """Warstwa osobliwości powierzchniowych
        :return warstwa
        """
        try:
            w = QgsVectorLayer("Polygon?crs=epsg:2180", "OS_POW", "memory")
            pr=w.dataProvider()
            pr.addAttributes([
                        QgsField("ID", QVariant.Int,"Integer",6),
                        QgsField("ADR_BDL",  QVariant.String,"String",25),
                        QgsField("KOD_OSOBL", QVariant.String,"String",12),
                        QgsField("NR_OSOBL", QVariant.Int,"Integer",4),
                        QgsField("POW_OSOBL", QVariant.Double,"double",11,4)
                        ])
            w.updateFields()
            return w
        except Exception as e:
            komunikaty.blad_systemowy(e)
            
    def __dz_ewid(self):
        """Warstwa działek ewidencyjnych
        :return warstwa
        """
        try:
            w = QgsVectorLayer("Polygon?crs=epsg:2180", "DZ_EWID", "memory")
            pr=w.dataProvider()
            pr.addAttributes([
                        QgsField("ID", QVariant.Int,"Integer",6),
                        QgsField("ADR_ADM",  QVariant.String,"String",25),
                        QgsField("NR_EW", QVariant.String,"String",25)
                        
                        ])
            w.updateFields()
            return w
        except Exception as e:
            komunikaty.blad_systemowy(e)
            
    def __uzytki(self):
        """Warstwa uzytków
        :return warstwa
        """
        try:
            w = QgsVectorLayer("Polygon?crs=epsg:2180", "UZYTKI", "memory")
            pr=w.dataProvider()
            pr.addAttributes([
                        QgsField("ID", QVariant.Int,"Integer",6),
                        QgsField("ADR_ADM",  QVariant.String,"String",25),
                        QgsField("NR_EW", QVariant.String,"String",25),
                        QgsField("NR_KONT", QVariant.Int,"Integer",4)
                        ])
            w.updateFields()
            return w
        except Exception as e:
            komunikaty.blad_systemowy(e)
            
    def __op_pow(self):
        """Warstwa lokalizacji opisów wydzieleń
        :return warstwa
        """
        try:
            w = QgsVectorLayer("point?crs=epsg:2180", "OP_POW", "memory")
            pr=w.dataProvider()
            pr.addAttributes([
                        QgsField("ID", QVariant.Int,"Integer",6),
                        QgsField("ADR_BDL",  QVariant.String,"String",25)
                        ])
            w.updateFields()
            return w
        except Exception as e:
            komunikaty.blad_systemowy(e)
            
    def __op_oddz(self):
        """Warstwa lokalizacji opisów oddziałów
        :return warstwa
        """
        try:
            w = QgsVectorLayer("point?crs=epsg:2180", "OP_ODDZ", "memory")
            pr=w.dataProvider()
            pr.addAttributes([
                        QgsField("ID", QVariant.Int,"Integer",6),
                        QgsField("ADR_BDL",  QVariant.String,"String",25)
                        ])
            w.updateFields()
            return w
        except Exception as e:
            komunikaty.blad_systemowy(e)
            
    def __oddzial(self):
        """Warstwa oddziałów leśnych
        :return warstwa
        """
        try:
            w = QgsVectorLayer("Polygon?crs=epsg:2180", "ODDZIAL", "memory")
            pr=w.dataProvider()
            pr.addAttributes([
                        QgsField("ID", QVariant.Int,"Integer",6),
                        QgsField("ADR_BDL",  QVariant.String,"String",25)
                        ])
            w.updateFields()
            return w
        except Exception as e:
            komunikaty.blad_systemowy(e)
            
    def __pow(self):
        """Warstwa wydzieleń
        :return warstwa
        """
        try:
            w = QgsVectorLayer("Polygon?crs=epsg:2180", "POW", "memory")
            pr=w.dataProvider()
            pr.addAttributes([
                        QgsField("ID", QVariant.Int,"Integer",6),
                        QgsField("ADR_BDL",  QVariant.String,"String",25),
                        QgsField("L_EWID",  QVariant.String,"String",1)
                        ])
            w.updateFields()
            return w
        except Exception as e:
            komunikaty.blad_systemowy(e)