Facebook
From Ja, 3 Years ago, written in Python.
Embed
Download Paste or View Raw
Hits: 144
  1. from qgis import *
  2. def check_file(file):
  3.     """
  4.    Funka przyjmuje plik, w zależności od rozszerzenia zapisuje jako warste Vectorową lub Rastrtową
  5.    """
  6.     projekt = QgsProject.instance()
  7.     projekt.fileName()
  8.     (filepath, filename) = os.path.split(file)
  9.     (name,extension) = os.path.splitext(filename)
  10.     if extension == ".shp" :
  11.         try:
  12.             vector =iface.addVectorLayer(os.path.join(filepath,filename),"new","ogr")
  13.             return vector
  14.         except:
  15.             print("Sth is wrong with value")
  16.     elif extension == ".tif":
  17.         try:
  18.             raster = iface.addRasterLayer(file)
  19.             return raster
  20.         except:
  21.             print("Sth is wrong with value")
  22.     else:
  23.         print("Sth is wrong with value")
  24.        
  25.        
  26.  
  27. raster = check_file('C:/Users/misie/Downloads/bogota.tif')
  28.  
  29. def safe(raster,path):
  30.     """
  31.    Po dodaniu przez poprzednia funkcje warstwy rastrowej,
  32.    funkcja safe zapisze wysokość,szerokość, zakres, typ rastra, liczbe kanałów oraz układ odniesienie
  33.    :param raster: object = check_file(path in string)
  34.    :param path: string
  35.    :return: file with dict
  36.    """
  37.     import json
  38.     dane = {'width':raster.width(),
  39.             'height':raster.height(),
  40.             'extent':raster.extent().toString(),
  41.             'type':raster.rasterType(),
  42.             'band_number':raster.bandCount(),
  43.             'crs': raster.crs().authid()}
  44.     with open(path,'w') as file:
  45.              file.write(json.dumps(dane))
  46.  
  47.    
  48. safe(raster,'C:/Users/misie/Desktop/file1.txt')
  49.    
  50. class Builings:
  51.  
  52.     def __init__(self,name,spot,filepath,value):
  53.         """
  54.        
  55.        :param name: string
  56.        :param spot: string
  57.        :param filepath: path/string
  58.        :param value: string
  59.        """
  60.         self.name = name
  61.         self.spot = spot
  62.         self.filepath = filepath
  63.         self.value = value
  64.    
  65.     def __str__(self):
  66.         return self.name
  67.        
  68.     def project(self):
  69.         projekt = QgsProject.instance()
  70.         projekt.setFileName(self.name)
  71.         projekt.write(self.spot)
  72.         projekt.read(self.spot)
  73.         return projekt.fileName()
  74.    
  75.     def load(self):
  76.         """
  77.        Funkcja ładuje nową warstwe z ścieżki podaje i szuka pliku 'Budynki_JG.shp'
  78.        :return: zwraca vector
  79.        """
  80.         vector =iface.addVectorLayer(os.path.join(self.filepath,'Budynki_JG.shp'),"budynki","ogr")
  81.         return vector
  82.        
  83.     def count(self):
  84.         """
  85.        Funkcja ma na celu obliczenie powierzchni dla kolumny 'building'  dla szukanego obiektu "value"
  86.        iteracja po wszystkich obiektach w celu spełnienia warunku
  87.        """
  88.         warstwa = iface.activeLayer()
  89.         pr = warstwa.dataProvider()
  90.         pr.addAttributes([QgsField('Nowa',QVariant.Double)])
  91.         warstwa.updateFields()
  92.         with edit(warstwa):
  93.             for obiekt in warstwa.getFeatures():
  94.                 if obiekt['building'] == '{self.value}':
  95.                     obiekt.setAttribute(obiekt.fieldNameIndex('Nowa'),obiekt.geometry().area())
  96.                 warstwa.updateFeature(obiekt)
  97.        
  98.  
  99.  
  100. new = Builings(name='Projekt',spot ='C:/Users/misie/Desktop/',filepath='C:/Users/misie/Downloads/',value='residential')
  101. new.project()
  102. new.load()
  103. new.count()
  104.