Newer
Older
if path.endswith('.qrc'):
qrcPathes.append(path)
buffer = io.StringIO() # buffer to store modified XML
buffer.write(doc.toString())
buffer.flush()
buffer.seek(0)
#make resource file directories available to the python path (sys.path)
baseDir = os.path.dirname(pathUi)
tmpDirs = []
for qrcPath in qrcPathes:
d = os.path.dirname(os.path.join(baseDir, os.path.dirname(qrcPath)))
if d not in sys.path:
tmpDirs.append(d)
sys.path.extend(tmpDirs)
#load form class
try:
FORM_CLASS, _ = uic.loadUiType(buffer, resource_suffix=RC_SUFFIX)
except SyntaxError as ex:
FORM_CLASS, _ = uic.loadUiType(pathUi, resource_suffix=RC_SUFFIX)
FORM_CLASSES[pathUi] = FORM_CLASS
#remove temporary added directories from python path
for d in tmpDirs:
sys.path.remove(d)
return FORM_CLASSES[pathUi]
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
def zipdir(pathDir, pathZip):
"""
:param pathDir: directory to compress
:param pathZip: path to new zipfile
"""
#thx to https://stackoverflow.com/questions/1855095/how-to-create-a-zip-archive-of-a-directory
"""
import zipfile
assert os.path.isdir(pathDir)
zipf = zipfile.ZipFile(pathZip, 'w', zipfile.ZIP_DEFLATED)
for root, dirs, files in os.walk(pathDir):
for file in files:
zipf.write(os.path.join(root, file))
zipf.close()
"""
import zipfile
relroot = os.path.abspath(os.path.join(pathDir, os.pardir))
with zipfile.ZipFile(pathZip, "w", zipfile.ZIP_DEFLATED) as zip:
for root, dirs, files in os.walk(pathDir):
# add directory (needed for empty dirs)
zip.write(root, os.path.relpath(root, relroot))
for file in files:
filename = os.path.join(root, file)
if os.path.isfile(filename): # regular files only
arcname = os.path.join(os.path.relpath(root, relroot), file)
zip.write(filename, arcname)

Benjamin Jakimow
committed
def initQgisApplication(pythonPlugins=None, PATH_QGIS=None, qgisDebug=False, qgisResourceDir=None):
"""
Initializes the QGIS Environment
:return: QgsApplication instance of local QGIS installation
"""
import site
if pythonPlugins is None:
pythonPlugins = []
assert isinstance(pythonPlugins, list)
if isinstance(qgisResourceDir, str) and os.path.isdir(qgisResourceDir):

Benjamin Jakimow
committed
import importlib, re
modules = [m for m in os.listdir(qgisResourceDir) if re.search(r'[^_].*\.py', m)]
modules = [m[0:-3] for m in modules]
for m in modules:
mod = importlib.import_module('qgisresources.{}'.format(m))
if "qInitResources" in dir(mod):
mod.qInitResources()
envVar = os.environ.get('QGIS_PLUGINPATH', None)
if isinstance(envVar, list):
pythonPlugins.extend(re.split('[;:]', envVar))
# make plugin paths available to QGIS and Python
os.environ['QGIS_PLUGINPATH'] = ';'.join(pythonPlugins)
os.environ['QGIS_DEBUG'] = '1' if qgisDebug else '0'
for p in pythonPlugins:
sys.path.append(p)
if isinstance(QgsApplication.instance(), QgsApplication):
else:
if PATH_QGIS is None:
# find QGIS Path
if sys.platform == 'darwin':
import qgis, re
assert '.app' in qgis.__file__, 'Can not locate path of QGIS.app'
PATH_QGIS_APP = re.split(r'\.app[\/]', qgis.__file__)[0] + '.app'
PATH_QGIS = os.path.join(PATH_QGIS_APP, *['Contents', 'MacOS'])
os.environ['GDAL_DATA'] = r'/Library/Frameworks/GDAL.framework/Versions/Current/Resources/gdal'
QApplication.addLibraryPath(os.path.join(PATH_QGIS_APP, *['Contents', 'PlugIns']))
QApplication.addLibraryPath(os.path.join(PATH_QGIS_APP, *['Contents', 'PlugIns', 'qgis']))
else:
# assume OSGeo4W startup
PATH_QGIS = os.environ['QGIS_PREFIX_PATH']
assert os.path.exists(PATH_QGIS)
qgsApp = QgsApplication([], True)
qgsApp.setPrefixPath(PATH_QGIS, True)
qgsApp.initQgis()
qgsApp.registerOgrDrivers()
from qgis.gui import QgsGui
QgsGui.editorWidgetRegistry().initEditors()
def printQgisLog(tb, error, level):
if error not in ['Python warning']:
print(tb)
QgsApplication.instance().messageLog().messageReceived.connect(printQgisLog)
def createCRSTransform(src, dst):
assert isinstance(src, QgsCoordinateReferenceSystem)
assert isinstance(dst, QgsCoordinateReferenceSystem)
t = QgsCoordinateTransform()
t.setSourceCrs(src)
t.setDestinationCrs(dst)
return t
if __name__ == '__main__':
#nice predecessors
qgsApp = initQgisApplication()

benjamin.jakimow@geo.hu-berlin.de
committed
se = SpatialExtent.world()
assert nicePredecessor(26) == 25
assert nicePredecessor(25) == 25
assert nicePredecessor(23) == 20
assert nicePredecessor(999) == 950
assert nicePredecessor(1001) == 1000
assert nicePredecessor(1.2) == 1.0 #
assert nicePredecessor(0.8) == 0.5
assert nicePredecessor(0.2) == 0.1
assert nicePredecessor(0.021) == 0.01
assert nicePredecessor(0.0009991) == 0.0005