You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

294 lines
10 KiB
Python

# -*- coding: utf-8 -*-
"""
/***************************************************************************
GemeindeMessdatenImport
A QGIS plugin
Importiert Messdaten im TXT-Format in die Gemeindeeigene Messdaten-Datenbank.
-------------------
begin : 2024-02-01
git sha : $Format:%H$
copyright : (C) 2024 by Sebastian Pertl
email : sebastian.pertl@fridolfing.bayern.de
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
"""
from qgis.PyQt.QtCore import QSettings, QTranslator, QCoreApplication
from qgis.PyQt.QtGui import QIcon
from qgis.PyQt.QtWidgets import QAction, QComboBox
from qgis.PyQt import uic
from qgis.PyQt import QtWidgets
from qgis.PyQt.QtWidgets import QFileDialog
from qgis.core import QgsVectorLayer, QgsVectorFileWriter, QgsProviderRegistry
from qgis.utils import iface
# Initialize Qt resources from file resources.py
from .resources import *
# Import the code for the dialog
#from .gdeMessdatenImport_dialog import GemeindeMessdatenImportDialog
import os
import os.path
class GemeindeMessdatenImport:
"""QGIS Plugin Implementation."""
def __init__(self, iface):
"""Constructor.
:param iface: An interface instance that will be passed to this class
which provides the hook by which you can manipulate the QGIS
application at run time.
:type iface: QgsInterface
"""
# Save reference to the QGIS interface
self.iface = iface
# initialize plugin directory
self.plugin_dir = os.path.dirname(__file__)
# Declare instance attributes
# Eintrag Menüband Oben
self.actions = []
self.menu = u'&Gemeinde Messdaten Import'
# Check if plugin was started the first time in current QGIS session
# Must be set in initGui() to survive plugin reloads
self.first_start = None
def initGui(self):
"""Create the menu entries and toolbar icons inside the QGIS GUI."""
#
self.toolBar = self.iface.addToolBar("Gemeinde Pertl tools")
icon_path = os.path.join(self.plugin_dir, 'icon_txt.png')
self.act1 = QAction(QIcon(icon_path), u'Import Messdaten', self.iface.mainWindow())
self.act1.triggered.connect(self.run1)
self.toolBar.addAction(self.act1)
self.iface.addPluginToMenu(self.menu, self.act1)
icon_path = os.path.join(self.plugin_dir, 'icon_dupl.png')
self.act2 = QAction(QIcon(icon_path), u'Lösche Vorhandene', self.iface.mainWindow())
self.act2.triggered.connect(self.run2)
self.toolBar.addAction(self.act2)
self.iface.addPluginToMenu(self.menu, self.act2)
icon_path = os.path.join(self.plugin_dir, 'icon_db.png')
self.act3 = QAction(QIcon(icon_path), u'Gemeinde Übertrag', self.iface.mainWindow())
self.act3.triggered.connect(self.run3)
self.toolBar.addAction(self.act3)
self.iface.addPluginToMenu(self.menu, self.act3)
icon_path = os.path.join(self.plugin_dir, 'icon_db.png')
self.act4 = QComboBox(self.iface.mainWindow())
self.act4.addItem('KG')
self.act4.addItem('EG')
self.act4.addItem('1.OG')
self.act4.addItem('2.OG')
self.act4.addItem('3.OG')
self.act4.currentTextChanged.connect(self.run4)
self.toolBar.addWidget(self.act4)
#self.toolBar.addAction(self.act4)
#self.iface.addPluginToMenu(self.menu, self.act4)
self.first_start = True
def unload(self):
"""Removes the plugin menu item and icon from QGIS GUI."""
for action in self.actions:
self.iface.removePluginMenu(u'&Gemeinde Messdaten Import', action)
self.iface.removeToolBarIcon(action)
def run_init(self):
# Create the dialog with elements (after translation) and keep reference
# Only create GUI ONCE in callback, so that it will only load when the plugin is started
if self.first_start == True:
self.first_start = False
self.dlg = GemeindeMessdatenImportDialog()
def run1(self):
"""Run method that performs all the real work"""
self.run_init()
self.dlg.run()
result = True
if result:
# Do something useful here - delete the line containing pass and
# substitute with your code.
pass
def run2(self):
"""Run method that performs all the real work"""
self.run_init()
self.dlg.run_del_dup()
result = True
if result:
# Do something useful here - delete the line containing pass and
# substitute with your code.
pass
def run3(self):
"""Run method that performs all the real work"""
self.run_init()
self.dlg.run_save()
result = True
if result:
# Do something useful here - delete the line containing pass and
# substitute with your code.
pass
def run4(self):
print(self.act4.currentIndex())
print(self.act4.currentText())
class GemeindeMessdatenImportDialog():
def __init__(self, parent=None):
"""Constructor."""
self.inp_file = 'T:/RIWA-GIS/Layer/fridolfing.gpkg'
self.md = QgsProviderRegistry.instance().providerMetadata("ogr")
self.con = self.md.createConnection( self.inp_file, {})
def run(self):
qfd = QFileDialog()
filename = QFileDialog.getOpenFileName(qfd, "Wähle Vermessungsdaten","::{645ff040-5081-101b-9f08-00aa002f954e}", '*.txt')
csv = filename[0]
rutacsv = 'file:///' + csv + '?delimiter=,&xField=field3&yField=field2'
tabla = QgsVectorLayer(rutacsv, os.path.basename(csv), 'delimitedtext')
opt = QgsVectorFileWriter.SaveVectorOptions()
opt.EditionCapability = QgsVectorFileWriter.CanAddNewLayer
opt.layerName = "messdaten_imp"
opt.actionOnExistingFile = QgsVectorFileWriter.CreateOrOverwriteLayer
QgsVectorFileWriter.writeAsVectorFormat(tabla, self.inp_file, opt)
# Datensätze bereinigen
query = """UPDATE messdaten_imp SET
field6 = replace(field6 ,"HSDV:",""),
field7 = replace(field7 ,"VSDV:",""),
field8 = replace(field8 ,"STATUS:",""),
field9 = replace(field9 ,"SATS:",""),
field10 = replace(field10,"AGE:",""),
field11 = replace(field11,"PDOP:",""),
field12 = replace(field12,"HDOP:",""),
field13 = replace(field13,"VDOP:",""),
field14 = replace(field14,"TDOP:",""),
field15 = replace(field15,"GDOP:",""),
field16 = replace(field16,"NSDV:",""),
field17 = replace(field17,"ESDV:",""),
field18 = replace(field18,"DATE:",""),
field19 = replace(field19,"TIME:","");"""
self.con.executeSql(query)
## Erzeuge Geometrie für Vorschau
#query = """UPDATE messdaten_imp SET geom = AsGPB(setsrid(makepoint(field3, field2), 25832));"""
#con.executeSql(query)
# Datum formatieren
query = """UPDATE messdaten_imp SET field18 = substr(field18,7,4)||'-'||substr(field18,1,2)||'-'||substr(field18,4,2);"""
self.con.executeSql(query)
iface.mapCanvas().refreshAllLayers()
def run_del_dup(self):
# Duplikate löschen
query = """DELETE FROM messdaten_imp WHERE field3 || field2 || field4 in (SELECT rw || hw || höhe FROM messdaten);"""
self.con.executeSql(query)
query = """DELETE FROM messdaten_imp WHERE field1 in (SELECT Nummer FROM messdaten);"""
self.con.executeSql(query)
iface.mapCanvas().refreshAllLayers()
def run_save(self):
# Daten übertragen
query = """INSERT INTO
messdaten
(
"fid",
"geom",
"Nummer",
"RW",
"HW",
"Höhe",
"Beschreibung",
"HSDV",
"VSDV",
"STATUS",
"SATS",
"AGE",
"PDOP",
"HDOP",
"VDOP",
"TDOP",
"GDOP",
"NSDV",
"ESDV",
"aufnahme_dat",
"aufnahme_uhr",
"import_dat",
"Anmerkung",
"Quelle"
)
SELECT
NULL,
AsGPB(setsrid(makepoint(field3, field2), 25832)),
field1,
field3,
field2,
field4,
field5,
field6 ,
field7 ,
field8 ,
field9 ,
field10,
field11,
field12,
field13,
field14,
field15,
field16,
field17, --ESDV
field18,
field19,
DATE('now'),
'',
'Vermessung Bauhof'
FROM messdaten_imp;"""
self.con.executeSql(query)
# Anzahl der Objekte aktuallisieren
query = """DELETE FROM messdaten_imp WHERE field1 in (SELECT Nummer FROM messdaten);"""
self.con.executeSql(query)
# Import-Tabelle leeren
query = """DELETE FROM messdaten_imp;"""
self.con.executeSql(query)
iface.mapCanvas().refreshAllLayers()