-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspatial_provider.py
More file actions
172 lines (145 loc) · 5.68 KB
/
spatial_provider.py
File metadata and controls
172 lines (145 loc) · 5.68 KB
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
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
166
167
168
169
170
171
172
# -*- coding: utf-8 -*-
"""
/***************************************************************************
SpatialAnalyzer
A QGIS plugin
This plugin provides data and tools for analyzing space
Generated by Plugin Builder: http://g-sherman.github.io/Qgis-Plugin-Builder/
-------------------
begin : 2018-05-05
git sha : $Format:%H$
copyright : (C) 2018 by D.J Paek
email : dj.paek1@gmail.com
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
"""
import os
from qgis.core import (
QgsProcessingProvider,
QgsProcessingAlgorithm,
QgsProcessingOutputVectorLayer,
QgsProcessingOutputRasterLayer,
QgsVectorLayer,
QgsRasterLayer,
QgsProject,
QgsProcessingException,
)
from qgis.PyQt.QtGui import QIcon
from .algs import (
CentralTendency,
MeanCenterTracker,
MedianCenterTracker,
CentralFeatureTracker,
StandardDistance,
StandardDeviationEllipse,
Gravity,
Kmeans,
Hierarchical,
Dbscan,
Hotspot,
LocalMoransI,
SpatialRegression,
GeographicallyWeightedRegression,
Pca,
Tsne
)
pluginPath = os.path.split(os.path.dirname(__file__))[0]
class SampleFileAlgorithm(QgsProcessingAlgorithm):
"""Algorithm representing a bundled sample data file."""
def __init__(self, file_path):
super().__init__()
self._path = file_path
base_name = os.path.basename(file_path)
# Use the filename (with extension) to ensure uniqueness of algorithm IDs
self._alg_id = base_name.replace(".", "_")
# Layer name should exclude the extension
self._layer_name = os.path.splitext(base_name)[0]
self._is_vector = file_path.lower().endswith(
(".shp", ".gpkg", ".geojson", ".json", ".csv")
)
def name(self):
return self._alg_id
def icon(self):
return QIcon(os.path.join(pluginPath, 'spatial_analysis', 'icons', 'sample_folder.svg'))
def svgIconPath(self):
return os.path.join(pluginPath, 'spatial_analysis', 'sample_folder.svg')
def displayName(self):
return os.path.basename(self._path)
def group(self):
return "Sample Data"
def groupId(self):
return "sample_data"
def initAlgorithm(self, config=None):
if self._is_vector:
self.addOutput(QgsProcessingOutputVectorLayer("OUTPUT", "Layer"))
else:
self.addOutput(QgsProcessingOutputRasterLayer("OUTPUT", "Layer"))
def processAlgorithm(self, parameters, context, feedback):
if self._is_vector:
layer = QgsVectorLayer(self._path, self._layer_name, "ogr")
else:
layer = QgsRasterLayer(self._path, self._layer_name)
if not layer.isValid():
raise QgsProcessingException(f"Could not load sample file: {self._path}")
QgsProject.instance().addMapLayer(layer)
try:
from qgis.utils import iface
if iface is not None:
iface.setActiveLayer(layer)
iface.zoomToActiveLayer()
except Exception:
pass
return {"OUTPUT": layer}
def createInstance(self):
return SampleFileAlgorithm(self._path)
class SpatialProvider(QgsProcessingProvider):
def __init__(self):
super().__init__()
self.alglist = [
CentralTendency.CentralTendency(),
MeanCenterTracker.MeanCenterTracker(),
MedianCenterTracker.MedianCenterTracker(),
CentralFeatureTracker.CentralFeatureTracker(),
StandardDistance.StandardDistance(),
StandardDeviationEllipse.StandardDeviationEllipse(),
Gravity.Gravity(),
Kmeans.Kmeans(),
Hierarchical.Hierarchical(),
Dbscan.Dbscan(),
Hotspot.Hotspot(),
LocalMoransI.LocalMoransI(),
SpatialRegression.SpatialRegression(),
GeographicallyWeightedRegression.GeographicallyWeightedRegression(),
Pca.Pca(),
Tsne.Tsne()
]
def getAlgs(self):
return self.alglist
def id(self, *args, **kwargs):
return 'spatialAnalyzer'
def name(self, *args, **kwargs):
return 'SpatialAnalyzer - Spatial Analysis Toolbox'
def icon(self):
return QIcon(os.path.join(pluginPath, 'spatial_analysis', 'icons', 'icon.svg'))
def svgIconPath(self):
return os.path.join(pluginPath, 'spatial_analysis', 'icon.svg')
def loadAlgorithms(self, *args, **kwargs):
for alg in self.alglist:
self.addAlgorithm(alg)
self._loadSampleDataAlgorithms()
def _loadSampleDataAlgorithms(self):
data_path = os.path.join(os.path.dirname(__file__), "sample_data")
if not os.path.exists(data_path):
return
for filename in sorted(os.listdir(data_path)):
path = os.path.join(data_path, filename)
if not os.path.isfile(path):
continue
self.addAlgorithm(SampleFileAlgorithm(path))