-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscipy_filters.py
137 lines (103 loc) · 4.28 KB
/
scipy_filters.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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
130
131
132
133
134
135
136
137
# -*- coding: utf-8 -*-
"""
/***************************************************************************
SciPyFilters
A QGIS plugin
Filter collection implemented with SciPy
Generated by Plugin Builder: http://g-sherman.github.io/Qgis-Plugin-Builder/
-------------------
begin : 2024-03-03
copyright : (C) 2024 by Florian Neukirchen
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
"""
__author__ = 'Florian Neukirchen'
__date__ = '2024-03-03'
__copyright__ = '(C) 2024 by Florian Neukirchen'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
import os
import sys
import inspect
from qgis.core import QgsApplication
from qgis.PyQt.QtCore import QSettings, QTranslator, QCoreApplication
from scipy_filters.helpers import tr
# This fails if scipy is not installed
try:
from .scipy_filters_provider import SciPyFiltersProvider
except ModuleNotFoundError:
# If scipy is not installed. We check below.
pass
# Uncomment for debugging
# from .scipy_filters_provider import SciPyFiltersProvider
cmd_folder = os.path.split(inspect.getfile(inspect.currentframe()))[0]
if cmd_folder not in sys.path:
sys.path.insert(0, cmd_folder)
class SciPyFiltersPlugin(object):
def __init__(self):
# super().__init__()
self.provider = None
# initialize locale
self.plugin_dir = os.path.dirname(__file__)
locale = QSettings().value('locale/userLocale')[0:2]
locale_path = os.path.join(
self.plugin_dir,
'i18n',
'scipy_filters_{}.qm'.format(locale))
if os.path.exists(locale_path):
self.translator = QTranslator()
self.translator.load(locale_path)
QCoreApplication.installTranslator(self.translator)
def initProcessing(self):
"""Init Processing provider for QGIS >= 3.8."""
try:
self.provider = SciPyFiltersProvider()
scipy_is_installed = True
except NameError:
scipy_is_installed = False
if not scipy_is_installed:
scipy_is_installed = self.install_scipy()
if scipy_is_installed:
QgsApplication.processingRegistry().addProvider(self.provider)
def initGui(self):
self.initProcessing()
def unload(self):
QgsApplication.processingRegistry().removeProvider(self.provider)
def install_scipy(self):
from qgis.PyQt.QtWidgets import QMessageBox
choice = QMessageBox.question(
None,
tr('SciPy Filters: SciPy is not installed'),
tr('SciPy is not installed. Do you want to install it automatically (using pip)?'),
QMessageBox.Yes | QMessageBox.No
)
if choice == QMessageBox.Yes:
msg = None
res = ""
import subprocess
import sys
try:
res = subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'scipy'])
except subprocess.CalledProcessError:
msg = tr('Installing SciPy failed. This probably means that pip is not installed.')
# Test if it worked
try:
import scipy
except ModuleNotFoundError:
msg = tr('Installing SciPy failed.')
if not msg:
return True
QMessageBox.warning(
None,
tr('SciPy Filters: Installing SciPy failed.'),
msg + ' ' + res
)
return False