ews_custom-python-expression.py

Custom python function of the affected layer - Andreas Neumann, 2018-10-10 11:51 AM

Download (1.13 KB)

 
1
"""
2
Define new functions using @qgsfunction. feature and parent must always be the
3
last args. Use args=-1 to pass a list of values as arguments
4
"""
5

    
6
from qgis.core import *
7
from qgis.gui import *
8

    
9
import psycopg2
10

    
11
@qgsfunction(args='auto', group='Custom', usesgeometry=False)
12
def get_ews_anlage_nr(gemeinde, feature, parent):
13
    AnlageLayer = QgsProject.instance().layerStore().mapLayersByName('EWS_Anlage')[0]
14
    aggParams = QgsAggregateCalculator.AggregateParameters()
15
    aggParams.filter = 'substr("anlage_nr",1,2) = \''+gemeinde+'\''
16
    maxVal = int(AnlageLayer.aggregate(QgsAggregateCalculator.Max,'to_int(substr("anlage_nr",4,4))',aggParams)[0] + 1)
17
    return  '{0:04d}'.format(maxVal)
18
        
19
@qgsfunction(args='auto', group='Custom', usesgeometry=True)
20
def get_ews_anlage_gemeinde_formatted(feature, parent):
21
    conn = psycopg2.connect("service='zg_produktion' user='ews' password='ews'")
22
    cur = conn.cursor()
23
    SQL = "SELECT ews.get_ews_anlage_gemeinde_formatted('{geometry_wkt}')".format(geometry_wkt=feature.geometry().asWkt(3))
24
    cur.execute(SQL)
25
    gemeinde_formatted = cur.fetchone()[0]
26
    conn.close()
27
    return gemeinde_formatted