Newer
Older
# -*- coding: utf-8 -*-
"""
/***************************************************************************

Benjamin Jakimow
committed
EO Time Series Viewer
-------------------
begin : 2015-08-20
git sha : $Format:%H$
copyright : (C) 2017 by HU-Berlin
email : benjamin.jakimow@geo.hu-berlin.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. *
* *
***************************************************************************/
"""
# noinspection PyPep8Naming
import os, sys, math, re, io, fnmatch
from collections import defaultdict
from qgis.core import QgsPointXY, QgsCoordinateReferenceSystem, QgsCoordinateTransform, QgsApplication, QgsRectangle, \
QgsMapLayer, QgsRasterLayer, QgsVectorLayer, QgsRasterRenderer, QgsFeatureRenderer, QgsRasterDataProvider, QgsUnitTypes, QgsSingleBandPseudoColorRenderer
#from qgis.gui import *
from qgis.gui import QgsMapCanvas, QgisInterface
from PyQt5.QtGui import *
from PyQt5.QtXml import QDomDocument
from PyQt5 import uic
import weakref
import numpy as np

benjamin.jakimow@geo.hu-berlin.de
committed
jp = os.path.join
dn = os.path.dirname
from timeseriesviewer import DIR_UI, DIR_REPO
from timeseriesviewer import messageLog
def qgisInstance():
"""
If existent, returns the QGIS Instance.
:return: QgisInterface | None
"""
from timeseriesviewer.main import TimeSeriesViewer
if isinstance(qgis.utils.iface, QgisInterface) and \
not isinstance(qgis.utils.iface, TimeSeriesViewer):
return qgis.utils.iface
else:
return None
def file_search(rootdir, pattern, recursive=False, ignoreCase=False, directories=False):
"""
Searches for files
:param rootdir: root directory to search for files.
:param pattern: wildcard ("my*files.*") or regular expression to describe the file name.
:param recursive: set True to search recursively.
:param ignoreCase: set True to ignore character case.
:param directories: set True to search for directories instead of files.
:return: [list-of-paths]
"""
assert os.path.isdir(rootdir), "Path is not a directory:{}".format(rootdir)
regType = type(re.compile('.*'))
results = []
for root, dirs, files in os.walk(rootdir):
if directories:
files = dirs
for file in files:
if isinstance(pattern, regType):
if pattern.search(file):
path = os.path.join(root, file)
results.append(path)
elif (ignoreCase and fnmatch.fnmatch(file.lower(), pattern.lower())) \
or fnmatch.fnmatch(file, pattern):
path = os.path.join(root, file)
results.append(path)
if not recursive:
break
pass
return results
def createQgsField(name : str, exampleValue, comment:str=None):
"""
Create a QgsField using a Python-datatype exampleValue
:param name: field name
:param exampleValue: value, can be any type
:param comment: (optional) field comment.
:return: QgsField
"""
t = type(exampleValue)
if t in [str]:
return QgsField(name, QVariant.String, 'varchar', comment=comment)
elif t in [bool]:
return QgsField(name, QVariant.Bool, 'int', len=1, comment=comment)
elif t in [int, np.int32, np.int64]:
return QgsField(name, QVariant.Int, 'int', comment=comment)
elif t in [float, np.double, np.float16, np.float32, np.float64]:
return QgsField(name, QVariant.Double, 'double', comment=comment)
elif isinstance(exampleValue, np.ndarray):
return QgsField(name, QVariant.String, 'varchar', comment=comment)
elif isinstance(exampleValue, list):
assert len(exampleValue) > 0, 'need at least one value in provided list'
v = exampleValue[0]
prototype = createQgsField(name, v)
subType = prototype.type()
typeName = prototype.typeName()
return QgsField(name, QVariant.List, typeName, comment=comment, subType=subType)
else:
raise NotImplemented()
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
def setQgsFieldValue(feature:QgsFeature, field, value):
"""
Wrties the Python value v into a QgsFeature field, taking care of required conversions
:param feature: QgsFeature
:param field: QgsField | field name (str) | field index (int)
:param value: any python value
"""
if isinstance(field, int):
field = feature.fields().at(field)
elif isinstance(field, str):
field = feature.fields().at(feature.fieldNameIndex(field))
assert isinstance(field, QgsField)
if value is None:
value = QVariant.NULL
if field.type() == QVariant.String:
value = str(value)
elif field.type() in [QVariant.Int, QVariant.Bool]:
value = int(value)
elif field.type() in [QVariant.Double]:
value = float(value)
else:
raise NotImplementedError()
# i = feature.fieldNameIndex(field.name())
feature.setAttribute(field.name(), value)
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
def appendItemsToMenu(menu, itemsToAdd):
"""
Appends items to QMenu "menu"
:param menu: the QMenu to be extended
:param itemsToAdd: QMenu or [list-of-QActions-or-QMenus]
:return: menu
"""
assert isinstance(menu, QMenu)
if isinstance(itemsToAdd, QMenu):
itemsToAdd = itemsToAdd.children()
if not isinstance(itemsToAdd, list):
itemsToAdd = [itemsToAdd]
for item in itemsToAdd:
if isinstance(item, QAction):
#item.setParent(menu)
a = menu.addAction(item.text(), item.triggered, item.shortcut())
a.setEnabled(item.isEnabled())
a.setIcon(item.icon())
menu.addAction(a)
s = ""
elif isinstance(item, QMenu):
item.setParent(menu)
menu.addMenu(menu)
else:
s = ""
return menu
def allSubclasses(cls):
"""
Returns all subclasses of class 'cls'
Thx to: http://stackoverflow.com/questions/3862310/how-can-i-find-all-subclasses-of-a-class-given-its-name
:param cls:
:return:
Loading
Loading full blame...