-
Notifications
You must be signed in to change notification settings - Fork 0
/
qgis_functions.py
86 lines (64 loc) · 3.31 KB
/
qgis_functions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# -*- coding: utf-8 -*-
from qgis.core import *
from PyQt4.QtCore import QVariant
from qgis.analysis import QgsZonalStatistics
class qgis_api:
polygonLayer = None
def __init__(self):
QgsApplication.setPrefixPath(r"C:\OSGeo4W64\bin", True)
qgis = QgsApplication([], False)
qgis.initQgis()
def remove_attribute_if_exists (self,vector_file,attribute_name):
polygonLayer = QgsVectorLayer(vector_file, 'zonepolygons', 'ogr')
caps = polygonLayer.dataProvider().capabilities()
ind = polygonLayer.dataProvider().fieldNameIndex(attribute_name)
if ind != -1:
if caps & QgsVectorDataProvider.DeleteAttributes:
res = polygonLayer.dataProvider().deleteAttributes([ind])
def zonal_stat(self, vector_file,raster_file_name, stat_type = "Majority"):
polygonLayer = QgsVectorLayer(vector_file, 'zonepolygons', 'ogr')
zoneStat = ''
if stat_type == "mean":
zoneStat = QgsZonalStatistics (polygonLayer, raster_file_name, '_', 1,QgsZonalStatistics.Mean)
else:
zoneStat = QgsZonalStatistics (polygonLayer, raster_file_name, '_', 1,QgsZonalStatistics.Majority)
zoneStat.calculateStatistics(None)
def feature_selection_export(self,vector_file,expression, path_expression_result):
polygonLayer = QgsVectorLayer(vector_file, 'zonepolygons', 'ogr')
exp = QgsExpression(expression)
selection = polygonLayer.getFeatures(QgsFeatureRequest(exp))
ids = [i.id() for i in selection]
polygonLayer.setSelectedFeatures(ids)
QgsVectorFileWriter.writeAsVectorFormat(polygonLayer,path_expression_result,'utf-8',None,'ESRI Shapefile',True)
def lowercase_attribute(self, vector_file):
polygonLayer = QgsVectorLayer(vector_file, 'zonepolygons', 'ogr')
k = 0
fields = polygonLayer.pendingFields()
polygonLayer.startEditing()
for (field) in fields:
polygonLayer.renameAttribute(k, field.name().lower())
k += 1
polygonLayer.commitChanges()
def set_attribute_to_value(self, vector_file, field, int_value):
polygonLayer = QgsVectorLayer(vector_file, 'zonepolygons', 'ogr')
polygonLayer.startEditing()
index = polygonLayer.fieldNameIndex(field)
for feature in polygonLayer.getFeatures():
polygonLayer.changeAttributeValue(feature.id(), index, int_value)
polygonLayer.commitChanges()
def set_shadow_to_water(self, vector_file, field):
polygonLayer = QgsVectorLayer(vector_file, 'zonepolygons', 'ogr')
polygonLayer.startEditing()
index = polygonLayer.fieldNameIndex(field)
for feature in polygonLayer.getFeatures():
if feature[field] == 6:
polygonLayer.changeAttributeValue(feature.id(), index, 1)
polygonLayer.commitChanges()
def add_training_attribute(self,vector_file,training_class_field):
polygonLayer = QgsVectorLayer(vector_file, 'zonepolygons', 'ogr')
polygonLayer.startEditing()
caps = polygonLayer.dataProvider().capabilities()
if caps & QgsVectorDataProvider.AddAttributes:
polygonLayer.dataProvider().addAttributes([QgsField(training_class_field, QVariant.Int)])
polygonLayer.updateFields()
polygonLayer.commitChanges()