-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathhtmlExpansionAlgorithm.py
140 lines (120 loc) · 5.12 KB
/
htmlExpansionAlgorithm.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# -*- coding: utf-8 -*-
"""
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""
import os
from qgis.PyQt.QtCore import QVariant, QCoreApplication
from qgis.PyQt.QtGui import QIcon
from qgis.core import QgsFields, QgsField
from qgis.core import (
QgsProcessing,
QgsProcessingAlgorithm,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterField,
QgsProcessingParameterString,
QgsProcessingException,
QgsProcessingParameterEnum,
QgsProcessingParameterFeatureSink)
def tr(string):
return QCoreApplication.translate('Processing', string)
class HTMLExpansionAlgorithm(QgsProcessingAlgorithm):
"""
Algorithm to expand HTML tables located in a description field into additional
attributes.
"""
PrmInputLayer = 'InputLayer'
PrmDescriptionField = 'DescriptionField'
PrmExpansionTags = 'ExpansionTags'
PrmOutputLayer = 'OutputLayer'
PrmExpansionType = 'ExpansionType'
def initAlgorithm(self, config):
self.addParameter(
QgsProcessingParameterFeatureSource(
self.PrmInputLayer,
tr('Input layer'),
[QgsProcessing.TypeVector])
)
self.addParameter(
QgsProcessingParameterField(
self.PrmDescriptionField,
tr('Description field'),
defaultValue='description',
parentLayerParameterName=self.PrmInputLayer,
type=QgsProcessingParameterField.String
)
)
self.addParameter(
QgsProcessingParameterString(
self.PrmExpansionTags,
tr('Comma separated list of expansion tags - Left blank autogenerates all tags'),
optional=True)
)
self.addParameter(
QgsProcessingParameterEnum(
self.PrmExpansionType,
tr('How to expand the description field'),
options=[
tr('Expand from a 2 column HTML table'),
tr('Expand from "tag = value" pairs'),
tr('Expand from "tag: value" pairs')],
defaultValue=0,
optional=False)
)
self.addParameter(
QgsProcessingParameterFeatureSink(
self.PrmOutputLayer,
tr('Output layer'))
)
def processAlgorithm(self, parameters, context, feedback):
from .htmlParser import HTMLExpansionProcess
source = self.parameterAsSource(parameters, self.PrmInputLayer, context)
field = self.parameterAsString(parameters, self.PrmDescriptionField, context)
tags = self.parameterAsString(parameters, self.PrmExpansionTags, context).strip()
type = self.parameterAsInt(parameters, self.PrmExpansionType, context)
feedback.pushInfo(tags)
if not field:
msg = tr('Must have a valid description field')
feedback.reportError(msg)
raise QgsProcessingException(msg)
# Set up the HTML expansion processor
self.htmlProcessor = HTMLExpansionProcess(source, field, type)
self.htmlProcessor.addFeature.connect(self.addFeature)
# Have it generate a list of all possible expansion field names
if self.PrmExpansionTags in parameters and tags != '':
expansionNames = [x.strip() for x in tags.split(',')]
feedback.pushInfo('{}'.format(expansionNames))
self.htmlProcessor.setDesiredFields(expansionNames)
else:
self.htmlProcessor.autoGenerateFileds()
srcCRS = source.sourceCrs()
wkbtype = source.wkbType()
# Create a copy of the fields for the output
fieldsout = QgsFields(source.fields())
for item in self.htmlProcessor.uniqueDesiredNames(source.fields().names()):
fieldsout.append(QgsField(item, QVariant.String))
(self.sink, dest_id) = self.parameterAsSink(parameters,
self.PrmOutputLayer, context, fieldsout, wkbtype, srcCRS)
self.htmlProcessor.processSource()
self.htmlProcessor.addFeature.disconnect(self.addFeature)
return {self.PrmOutputLayer: dest_id}
def addFeature(self, f):
self.sink.addFeature(f)
def name(self):
return 'htmlexpansion'
def icon(self):
return QIcon(os.path.dirname(__file__) + '/icons/html.svg')
def displayName(self):
return tr('Expand HTML description field')
def group(self):
return tr('Vector conversion')
def groupId(self):
return 'vectorconversion'
def createInstance(self):
return HTMLExpansionAlgorithm()