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.
269 lines
9.8 KiB
Python
269 lines
9.8 KiB
Python
7 months ago
|
# -*- 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 import uic
|
||
|
from qgis.PyQt import QtWidgets
|
||
|
from qgis.PyQt.QtWidgets import QFileDialog
|
||
|
from qgis.PyQt.QtCore import QSettings, QTranslator, QCoreApplication
|
||
|
from qgis.PyQt.QtGui import QIcon
|
||
|
from qgis.PyQt.QtWidgets import QAction, QComboBox, QLabel
|
||
|
|
||
|
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
|
||
|
import toml
|
||
|
|
||
|
|
||
|
class GemeindeTools:
|
||
|
"""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.messdatenImportActions = []
|
||
|
self.menu = u'&Gemeinde Tools-Menu'
|
||
|
|
||
|
# 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
|
||
|
|
||
|
with open(os.path.join(self.plugin_dir, 'config.toml'), 'r') as f:
|
||
|
self.config = toml.load(f)
|
||
|
|
||
|
self.geschossNavi = ''
|
||
|
|
||
|
def initGui(self):
|
||
|
"""Create the menu entries and toolbar icons inside the QGIS GUI."""
|
||
|
|
||
|
self.messdatenImport = GemeindeMessdatenImport(self.config['database']['messdaten'])
|
||
|
|
||
|
self.messdatenImportMenu = u"Gemeinde-Tools: Messdaten Import"
|
||
|
|
||
|
self.toolBar1 = self.iface.addToolBar(self.messdatenImportMenu)
|
||
|
|
||
|
icon_path = os.path.join(self.plugin_dir, 'icon/txt.png')
|
||
|
action = QAction(QIcon(icon_path), u'Import Messdaten', self.iface.mainWindow())
|
||
|
action.triggered.connect(self.messdatenImport.run_import_txt)
|
||
|
self.toolBar1.addAction(action)
|
||
|
self.iface.addPluginToMenu(self.messdatenImportMenu, action) # Menüband oben
|
||
|
self.messdatenImportActions.append(action)
|
||
|
|
||
|
icon_path = os.path.join(self.plugin_dir, 'icon/dupl.png')
|
||
|
action = QAction(QIcon(icon_path), u'Lösche Vorhandene', self.iface.mainWindow())
|
||
|
action.triggered.connect(self.messdatenImport.run_del_dup)
|
||
|
self.toolBar1.addAction(action)
|
||
|
self.iface.addPluginToMenu(self.messdatenImportMenu, action)
|
||
|
self.messdatenImportActions.append(action)
|
||
|
|
||
|
icon_path = os.path.join(self.plugin_dir, 'icon/db.png')
|
||
|
action = QAction(QIcon(icon_path), u'Gemeinde Übertrag', self.iface.mainWindow())
|
||
|
action.triggered.connect(self.messdatenImport.run_save)
|
||
|
self.toolBar1.addAction(action)
|
||
|
self.iface.addPluginToMenu(self.messdatenImportMenu, action)
|
||
|
self.messdatenImportActions.append(action)
|
||
|
|
||
|
|
||
|
self.toolBar2 = self.iface.addToolBar("Gemeinde-Tools: Geschossnavigation")
|
||
|
|
||
|
self.lab = QLabel(self.iface.mainWindow())
|
||
|
self.lab.setText("Geschossauswahl: ")
|
||
|
self.toolBar2.addWidget(self.lab)
|
||
|
|
||
|
self.act = QComboBox(self.iface.mainWindow())
|
||
|
for k in self.config['geschossnavi']['ziele']:
|
||
|
self.act.addItem(self.config['geschossnavi']['ziele'][k])
|
||
|
#self.act.currentTextChanged.connect(self.geschossNavi.run)
|
||
|
self.toolBar2.addWidget(self.act)
|
||
|
|
||
|
|
||
|
|
||
|
self.first_start = True
|
||
|
|
||
|
|
||
|
def unload(self):
|
||
|
"""Removes the plugin menu item and icon from QGIS GUI."""
|
||
|
for action in self.messdatenImportActions:
|
||
|
self.iface.removePluginMenu(self.messdatenImportMenu, action)
|
||
|
self.iface.removeToolBarIcon(action)
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
class GemeindeMessdatenImport():
|
||
|
def __init__(self, inp_file, parent=None):
|
||
|
"""Constructor."""
|
||
|
|
||
|
self.inp_file = inp_file
|
||
|
|
||
|
self.md = QgsProviderRegistry.instance().providerMetadata("ogr")
|
||
|
self.con = self.md.createConnection( self.inp_file, {})
|
||
|
|
||
|
|
||
|
def run_import_txt(self):
|
||
|
|
||
|
qfd = QFileDialog()
|
||
|
filename = QFileDialog.getOpenFileName(qfd, "Wähle Vermessungsdaten","::{645ff040-5081-101b-9f08-00aa002f954e}", '*.txt')
|
||
|
|
||
|
csv = filename[0]
|
||
|
rutacsv = 'file:///' + csv + '?delimiter=,&xField=field_3&yField=field_2&useHeader=no'
|
||
|
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
|
||
|
field_6 = replace(field_6 ,"HSDV:",""),
|
||
|
field_7 = replace(field_7 ,"VSDV:",""),
|
||
|
field_8 = replace(field_8 ,"STATUS:",""),
|
||
|
field_9 = replace(field_9 ,"SATS:",""),
|
||
|
field_10 = replace(field_10,"AGE:",""),
|
||
|
field_11 = replace(field_11,"PDOP:",""),
|
||
|
field_12 = replace(field_12,"HDOP:",""),
|
||
|
field_13 = replace(field_13,"VDOP:",""),
|
||
|
field_14 = replace(field_14,"TDOP:",""),
|
||
|
field_15 = replace(field_15,"GDOP:",""),
|
||
|
field_16 = replace(field_16,"NSDV:",""),
|
||
|
field_17 = replace(field_17,"ESDV:",""),
|
||
|
field_18 = replace(field_18,"DATE:",""),
|
||
|
field_19 = replace(field_19,"TIME:","");"""
|
||
|
self.con.executeSql(query)
|
||
|
|
||
|
## Erzeuge Geometrie für Vorschau
|
||
|
#query = """UPDATE messdaten_imp SET geom = AsGPB(setsrid(makepoint(field_3, field_2), 25832));"""
|
||
|
#con.executeSql(query)
|
||
|
|
||
|
# Datum formatieren
|
||
|
query = """UPDATE messdaten_imp SET field_18 = substr(field_18,7,4)||'-'||substr(field_18,1,2)||'-'||substr(field_18,4,2);"""
|
||
|
self.con.executeSql(query)
|
||
|
|
||
|
iface.mapCanvas().refreshAllLayers()
|
||
|
|
||
|
def run_del_dup(self):
|
||
|
# Duplikate löschen
|
||
|
query = """DELETE FROM messdaten_imp WHERE field_3 || field_2 || field_4 in (SELECT rw || hw || höhe FROM messdaten);"""
|
||
|
self.con.executeSql(query)
|
||
|
query = """DELETE FROM messdaten_imp WHERE field_1 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,
|
||
|
NULL,
|
||
|
field_1,
|
||
|
field_3,
|
||
|
field_2,
|
||
|
field_4,
|
||
|
field_5,
|
||
|
field_6 ,
|
||
|
field_7 ,
|
||
|
field_8 ,
|
||
|
field_9 ,
|
||
|
field_10,
|
||
|
field_11,
|
||
|
field_12,
|
||
|
field_13,
|
||
|
field_14,
|
||
|
field_15,
|
||
|
field_16,
|
||
|
field_17, --ESDV
|
||
|
field_18,
|
||
|
field_19,
|
||
|
DATE('now'),
|
||
|
'',
|
||
|
'Vermessung Bauhof'
|
||
|
FROM messdaten_imp;"""
|
||
|
self.con.executeSql(query)
|
||
|
|
||
|
query = """UPDATE messdaten SET geom = setsrid(makepoint(RW, HW), 25832) WHERE "import_dat" = DATE('now')"""
|
||
|
self.con.executeSql(query)
|
||
|
|
||
|
# Anzahl der Objekte aktuallisieren
|
||
|
query = """DELETE FROM messdaten_imp WHERE field_1 in (SELECT Nummer FROM messdaten);"""
|
||
|
self.con.executeSql(query)
|
||
|
|
||
|
# Import-Tabelle leeren
|
||
|
query = """DELETE FROM messdaten_imp;"""
|
||
|
self.con.executeSql(query)
|
||
|
|
||
|
iface.mapCanvas().refreshAllLayers()
|
||
|
|