This repository was archived by the owner on Jan 7, 2026. It is now read-only.
forked from Ojaybee/ags-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathags_2_db_algorithm.py
More file actions
490 lines (399 loc) · 14.8 KB
/
ags_2_db_algorithm.py
File metadata and controls
490 lines (399 loc) · 14.8 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
# -*- coding: utf-8 -*-
"""
/***************************************************************************
AGSTools
A QGIS plugin
This plugin parses an AGS file and creates a database from it
Generated by Plugin Builder: http://g-sherman.github.io/Qgis-Plugin-Builder/
-------------------
begin : 2023-04-19
copyright : (C) 2025 by Oliver Burdekin / burdGIS
email : info@burdgis.com
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
"""
__author__ = 'Oliver Burdekin / burdGIS'
__date__ = '2023-04-19'
__copyright__ = '(C) 2025 by Oliver Burdekin / burdGIS'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
from qgis.PyQt.QtCore import QCoreApplication, QSettings, QVariant
from qgis.core import (QgsApplication,
QgsSettings,
QgsProcessingAlgorithm,
QgsProcessingParameterFile,
QgsProcessingParameterFileDestination,
QgsProcessingParameterCrs,
QgsCoordinateReferenceSystem,
QgsVectorLayer,
QgsVectorLayer,
QgsField,
QgsFields,
QgsFeature,
QgsGeometry,
QgsPointXY,
QgsVectorFileWriter,
QgsProject,
QgsCoordinateTransform,
QgsProcessingException,
QgsDataSourceUri,
QgsMapLayer,
QgsProviderRegistry
)
from qgis.utils import iface
from io import StringIO
# import sqlite3
import os
class AGS2DBAlgorithm(QgsProcessingAlgorithm):
"""
This is an example algorithm that takes a vector layer and
creates a new identical one.
It is meant to be used as an example of how to create your own
algorithms and explain methods and variables used to do it. An
algorithm like this will be available in all elements, and there
is not need for additional work.
All Processing algorithms should extend the QgsProcessingAlgorithm
class.
"""
# Constants used to refer to parameters and outputs. They will be
# used when calling the algorithm from another algorithm, or when
# calling from the QGIS console.
OUTPUT = 'OUTPUT'
INPUT = 'INPUT'
CRS = 'CRS'
def initAlgorithm(self, config):
"""
Here we define the inputs and output of the algorithm, along
with some other properties.
"""
# We add the input vector features source. It can have any kind of
# geometry.
self.addParameter(
QgsProcessingParameterFile(
self.INPUT,
self.tr('Input File'),
)
)
self.addParameter(
QgsProcessingParameterCrs(
self.CRS,
'Coordinate Reference System',
defaultValue=QgsCoordinateReferenceSystem('EPSG:27700')
)
)
# We add a feature sink in which to store our processed features (this
# usually takes the form of a newly created vector layer when the
# algorithm is run in QGIS).
self.addParameter(
QgsProcessingParameterFileDestination(
self.OUTPUT,
self.tr('Output File'),
fileFilter='GeoPackage (*.gpkg);;SpatiaLite (*.sqlite)'
)
)
def parse_ags_file(self, file_contents):
def is_numeric(value):
"""Utility function to check if value can be converted to a number."""
try:
float(value)
return True
except ValueError:
return False
lines = file_contents.split('\n')
data = {}
current_group = None
headers = []
for line in lines:
if not line.strip():
continue
temp = line.strip().split('","')
temp = [item.strip('"') for item in temp]
if temp[0] == 'GROUP':
current_group = temp[1]
data[current_group] = [] # Initialize the group
elif temp[0] == 'HEADING':
headers = temp[1:]
elif temp[0] == 'UNIT':
if current_group and headers:
unit_values = temp[1:]
# Pad missing values with empty strings
if len(unit_values) < len(headers):
unit_values.extend([''] * (len(headers) - len(unit_values)))
# Validate and skip invalid unit rows
if all(not value.strip() for value in unit_values): # All empty or whitespace
continue
data[f"{current_group}_units"] = dict(zip(headers, unit_values))
elif temp[0] == 'DATA':
# Add data rows to the current group
if current_group and headers:
record = dict(zip(headers, temp[1:]))
data[current_group].append(record)
# Detect column types for each group independently
column_types = {}
for group_name, records in data.items():
if group_name.endswith("_units"):
continue
if records:
group_headers = list(records[0].keys())
column_types[group_name] = {}
for header in group_headers:
all_numeric = all(is_numeric(record.get(header)) for record in records if record.get(header))
column_types[group_name][header] = "REAL" if all_numeric else "TEXT"
return data, column_types
def createLOCAFeatures(self, records, column_types, crs, feedback):
"""
Create a memory point layer for LOCA features using available coordinate fields.
- Checks for LOCA_NATE/LOCA_NATN, LOCA_LOCX/LOCA_LOCY, LOCA_LAT/LOCA_LON in that order.
- Logs if features or all fail to have coordinates.
- Reprojects if using LAT/LON from EPSG:4326 to target CRS.
"""
# Determine which coordinate fields are present and use them in priority:
coord_priority = [
('LOCA_NATE', 'LOCA_NATN'),
('LOCA_LOCX', 'LOCA_LOCY'),
('LOCA_LAT', 'LOCA_LON')
]
chosen_coords = None
for pair in coord_priority:
# Check if both fields exist
if all(p in column_types['LOCA'] for p in pair):
x_field, y_field = pair
# Check if there's at least one record with non-empty coordinates
has_data = False
for record in records:
x_val = record.get(x_field)
y_val = record.get(y_field)
if x_val and x_val.strip() != '' and y_val and y_val.strip() != '':
has_data = True
break
if has_data:
chosen_coords = pair
break
# Create the layer
layer = QgsVectorLayer("Point?crs={}".format(crs.authid()), "LOCA", "memory")
data_provider = layer.dataProvider()
# Add fields
fields = QgsFields()
for field_name, field_type in column_types['LOCA'].items():
qgis_field_type = QVariant.Double if field_type == "REAL" else QVariant.String
fields.append(QgsField(field_name, qgis_field_type))
data_provider.addAttributes(fields)
layer.updateFields()
if chosen_coords is None:
feedback.reportError("No recognized coordinate fields found in LOCA. Creating empty point layer.")
return layer
x_field, y_field = chosen_coords
# Set up coordinate transform if needed
# If we're using LAT/LON, we assume they're in EPSG:4326 and must transform to chosen CRS
transform = None
if chosen_coords == ('LOCA_LAT', 'LOCA_LON'):
source_crs = QgsCoordinateReferenceSystem('EPSG:4326')
transform = QgsCoordinateTransform(source_crs, crs, QgsProject.instance())
features_with_coords = 0
for record in records:
# Get attributes
attrs = []
for header, ftype in column_types['LOCA'].items():
val = record.get(header)
if ftype == "REAL":
if val is None or val.strip() == '':
attrs.append(None)
else:
attrs.append(float(val))
else:
attrs.append(val if val is not None else None)
# Attempt to get coordinates
x_val = record.get(x_field)
y_val = record.get(y_field)
if x_val is None or x_val.strip() == '' or y_val is None or y_val.strip() == '':
# No coordinates for this feature
feedback.pushInfo(f"LOCA feature missing coordinates: {record}")
# We still add the feature but without geometry
f = QgsFeature()
f.setAttributes(attrs)
f.setGeometry(None)
data_provider.addFeature(f)
continue
# Convert to float
x = float(x_val)
y = float(y_val)
# Transform if LAT/LON
if transform:
pt = transform.transform(QgsPointXY(y, x)) # careful with order: LAT = Y, LON = X
else:
pt = QgsPointXY(x, y)
f = QgsFeature()
f.setAttributes(attrs)
f.setGeometry(QgsGeometry.fromPointXY(pt))
data_provider.addFeature(f)
features_with_coords += 1
layer.updateExtents()
if features_with_coords == 0:
feedback.reportError("No LOCA features had valid coordinates. The LOCA layer will have no geometries.")
return layer
def create_database_connection(self, output_path, feedback):
# Only works for gpkg
md = QgsProviderRegistry.instance().providerMetadata("ogr")
conn = md.createConnection(output_path, {})
conn_name = os.path.splitext(os.path.basename(output_path))[0]
md.saveConnection(conn, conn_name)
# Refresh database connections in the Browser Panel
iface.browserModel().refresh()
def add_svg_paths(self, feedback):
svg_path = os.path.join(os.path.dirname(__file__), 'styles', 'svg')
svg_paths = QgsApplication.svgPaths()
if svg_path not in svg_paths:
svg_paths.append(svg_path)
QgsApplication.setDefaultSvgPaths(svg_paths)
feedback.pushInfo("Added custom SVG path for symbols.")
def loadLayerAndApplyStyle(self, output_path, table_name, qml_path, feedback):
# Load the layer using OGR provider. GPK and SpatiaLite are supported.
layer_path = f"{output_path}|layername={table_name}"
layer = QgsVectorLayer(layer_path, table_name, "ogr")
if not layer.isValid():
feedback.pushInfo(f"Table '{table_name}' cannot be added.")
return None
QgsProject.instance().addMapLayer(layer)
feedback.pushInfo(f"Added '{table_name}' layer to the project.")
if layer.isValid() and os.path.exists(qml_path):
layer.loadNamedStyle(qml_path)
layer.triggerRepaint()
feedback.pushInfo("Applied QML style to the LOCA layer.")
if iface:
iface.layerTreeView().refreshLayerSymbology(layer.id())
return layer
def processAlgorithm(self, parameters, context, feedback):
# Define the output path
output_path = self.parameterAsFileOutput(parameters, self.OUTPUT, context)
ext = os.path.splitext(output_path)[1].lower()
feedback.pushInfo(f"Writing all groups GeoPackage to: {output_path}")
# Remove existing GeoPackage if it exists
if os.path.exists(output_path):
os.remove(output_path)
feedback.pushInfo(f"Removed existing GeoPackage at: {output_path}")
# Load AGS4 file contents
ags_file_path = self.parameterAsFile(parameters, self.INPUT, context)
with open(ags_file_path, 'r') as ags_file:
file_contents = ags_file.read()
# Get chosen CRS
crs = self.parameterAsCrs(parameters, self.CRS, context)
# Parse the AGS file
data, column_types = self.parse_ags_file(file_contents)
first_layer = True
transform_context = context.transformContext()
for group_name, records in data.items():
if group_name.endswith("_units"):
continue # Skip unit tables
feedback.pushInfo(f"Processing group: {group_name}")
if group_name.upper() == "LOCA":
# Create spatial LOCA layer
layer = self.createLOCAFeatures(records, column_types, crs, feedback)
else:
# Non-spatial layer
layer = QgsVectorLayer("NoGeometry", group_name, "memory")
data_provider = layer.dataProvider()
# Add fields
fields = QgsFields()
for field_name, field_type in column_types[group_name].items():
qgis_field_type = QVariant.Double if field_type == "REAL" else QVariant.String
fields.append(QgsField(field_name, qgis_field_type))
data_provider.addAttributes(fields)
layer.updateFields()
# Populate layer
for record in records:
feature = QgsFeature()
attrs = []
for header, ftype in column_types[group_name].items():
val = record.get(header)
if ftype == "REAL":
if val is None or val.strip() == '':
attrs.append(None)
else:
attrs.append(float(val))
else:
attrs.append(val if val is not None else None)
feature.setAttributes(attrs)
data_provider.addFeature(feature)
layer.updateExtents()
# Export layer
options = QgsVectorFileWriter.SaveVectorOptions()
options.driverName = "GPKG"
if ext == '.gpkg':
options.driverName = "GPKG"
elif ext == '.sqlite':
options.driverName = "SQLite"
options.driverOptions = ["SPATIALITE=YES"]
else:
raise QgsProcessingException("Unsupported file format selected.")
options.layerName = group_name
options.fileEncoding = "UTF-8"
# Geometry type will be derived automatically for V3 method
if first_layer:
options.actionOnExistingFile = QgsVectorFileWriter.CreateOrOverwriteFile
else:
options.actionOnExistingFile = QgsVectorFileWriter.CreateOrOverwriteLayer
error, newFilename, newLayer, errorMessage = QgsVectorFileWriter.writeAsVectorFormatV3(
layer,
output_path,
transform_context,
options
)
if error == QgsVectorFileWriter.NoError:
feedback.pushInfo(f"Successfully wrote group '{group_name}' to GeoPackage.")
else:
feedback.reportError(f"Error writing group '{group_name}' to GeoPackage: {errorMessage}")
first_layer = False
feedback.pushInfo("ALL GROUPS WRITTEN - DB CREATED")
# After the writing is done, call the helper functions:
self.add_svg_paths(feedback)
qml_path = os.path.join(os.path.dirname(__file__), 'styles', 'loca_spatial.qml')
# Load and style the LOCA layer
self.loadLayerAndApplyStyle(output_path, "LOCA", qml_path, feedback)
self.create_database_connection(output_path, feedback)
return {self.OUTPUT: output_path}
def processing_log(self, message):
"""
Logs a message to the Processing log.
"""
self.logMessage(message)
def name(self):
"""
Returns the algorithm name, used for identifying the algorithm. This
string should be fixed for the algorithm, and must not be localised.
The name should be unique within each provider. Names should contain
lowercase alphanumeric characters only and no spaces or other
formatting characters.
"""
return 'AGS2DB'
def displayName(self):
"""
Returns the translated algorithm name, which should be used for any
user-visible display of the algorithm name.
"""
return self.tr(self.name())
def group(self):
"""
Returns the name of the group this algorithm belongs to. This string
should be localised.
"""
return self.tr(self.groupId())
def groupId(self):
"""
Returns the unique ID of the group this algorithm belongs to. This
string should be fixed for the algorithm, and must not be localised.
The group id should be unique within each provider. Group id should
contain lowercase alphanumeric characters only and no spaces or other
formatting characters.
"""
return ''
def tr(self, string):
return QCoreApplication.translate('Processing', string)
def createInstance(self):
return AGS2DBAlgorithm()