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
from collections import defaultdict
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

Benjamin Jakimow
committed
import timeseriesviewer
MAP_LAYER_STORES = [QgsProject.instance()]
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

Benjamin Jakimow
committed
def findMapLayer(layer)->QgsMapLayer:
"""
Returns the first QgsMapLayer out of all layers stored in MAP_LAYER_STORES that matches layer
:param layer: str layer id or layer name or QgsMapLayer
:return: QgsMapLayer
"""
if isinstance(layer, QgsMapLayer):
return layer
elif isinstance(layer, str):
#check for IDs
for store in MAP_LAYER_STORES:
l = store.mapLayer(layer)
if isinstance(l, QgsMapLayer):
return l
#check for name
for store in MAP_LAYER_STORES:
l = store.mapLayersByName(layer)
if len(l) > 0:
return l[0]
return None
def file_search(rootdir, pattern, recursive=False, ignoreCase=False, directories=False, fullpath=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('.*'))
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
if directories == False:
if entry.is_file():
if fullpath:
name = entry.path
else:
name = os.path.basename(entry.path)
if isinstance(pattern, regType):
if pattern.search(name):
yield entry.path.replace('\\','/')
elif (ignoreCase and fnmatch.fnmatch(name, pattern.lower())) \
or fnmatch.fnmatch(name, pattern):
yield entry.path.replace('\\','/')
elif entry.is_dir() and recursive == True:
for r in file_search(entry.path, pattern, recursive=recursive, directories=directories):
yield r
else:
if entry.is_dir():
if recursive == True:
for d in file_search(entry.path, pattern, recursive=recursive, directories=directories):
yield d
if fullpath:
name = entry.path
else:
name = os.path.basename(entry.path)
if isinstance(pattern, regType):
if pattern.search(name):
yield entry.path.replace('\\','/')
elif (ignoreCase and fnmatch.fnmatch(name, pattern.lower())) \
or fnmatch.fnmatch(name, pattern):

Benjamin Jakimow
committed
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
NEXT_COLOR_HUE_DELTA_CON = 10
NEXT_COLOR_HUE_DELTA_CAT = 100
def nextColor(color, mode='cat'):
"""
Returns another color
:param color: the previous color
:param mode: 'cat' - for categorical color jump (next color looks pretty different to previous)
'con' - for continuous color jump (next color looks similar to previous)
:return:
"""
assert mode in ['cat','con']
assert isinstance(color, QColor)
hue, sat, value, alpha = color.getHsl()
if mode == 'cat':
hue += NEXT_COLOR_HUE_DELTA_CAT
elif mode == 'con':
hue += NEXT_COLOR_HUE_DELTA_CON
if sat == 0:
sat = 255
value = 128
alpha = 255
s = ""
while hue > 360:
hue -= 360
return QColor.fromHsl(hue, sat, value, alpha)
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()

Benjamin Jakimow
committed
def setQgsFieldValue(feature:QgsFeature, field, value):
"""
Loading
Loading full blame...