-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapcomponentizer_algorithm.py
180 lines (138 loc) · 5.97 KB
/
mapcomponentizer_algorithm.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
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
173
174
175
176
177
178
179
180
# -*- coding: utf-8 -*-
"""
/***************************************************************************
MapComponentizer
A QGIS plugin
Convert a QGIS project into a React Web Application
Generated by Plugin Builder: http://g-sherman.github.io/Qgis-Plugin-Builder/
-------------------
begin : 2024-03-08
copyright : (C) 2024 by WhereGroup GmbH
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__ = 'WhereGroup GmbH'
__date__ = '2024-03-08'
__copyright__ = '(C) 2024 by WhereGroup GmbH'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
import inspect
import os
import platform
from qgis.PyQt.QtCore import QCoreApplication
from qgis.core import (
QgsProcessingAlgorithm,
QgsProcessingParameterFolderDestination,
QgsProcessingParameterEnum,
QgsProcessingParameterBoolean,
QgsProject
)
import subprocess
import shutil
from qgis.core import QgsApplication, QgsProject
from .ProjectUtils import *
from .LayersExporter import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import os
from qgis.PyQt.QtGui import QIcon
class MapComponentizerAlgorithm(QgsProcessingAlgorithm):
# Constants used to refer to parameters and outputs. They will be
# used when calling the algorithm from another algorithm, or when
# calling from the QGIS console.
OUTPUT = 'OUTPUT'
TEMPLATE = 'TEMPLATE'
plugin_path = os.path.dirname(os.path.realpath(__file__))
templatesPath = f'{plugin_path}/templates'
OPEN = 'OPEN'
def initAlgorithm(self, config):
"""
Here we define the inputs and output of the algorithm.
"""
self.addParameter(
QgsProcessingParameterFolderDestination(
self.OUTPUT,
self.tr('Output folder'),
self.plugin_path + '/output'
)
)
self.addParameter(
QgsProcessingParameterEnum(
self.TEMPLATE,
self.tr('Create from template: '),
options=self.get_folder_names(self.templatesPath),
defaultValue=0,
optional=False)
)
self.addParameter(
QgsProcessingParameterBoolean(
self.OPEN,
self.tr('Open export folder when finished'),
True
)
)
def processAlgorithm(self, parameters, context, feedback):
# Send some information to the user
plugin_path = os.path.dirname(os.path.realpath(__file__))
templateOptions = self.get_folder_names(self.templatesPath)
BASE_OUTPUT_DIRECTORY = parameters[self.OUTPUT]
TEMP_DIRECTORY = f'{plugin_path}/tmp'
TEMPLATE_PATH = f'{plugin_path}/templates/{templateOptions[parameters[self.TEMPLATE]]}'
#create tmp folder if it doesn't exist, empty it if previously failed to do it
os.makedirs(TEMP_DIRECTORY, exist_ok=True)
shutil.rmtree(TEMP_DIRECTORY)
os.makedirs(TEMP_DIRECTORY, exist_ok=True)
# Get the project instance
project = QgsProject.instance()
projectName = project.baseName()
projectFolder, exportFolder = ProjectUtils.create_project_directory(
projectName, BASE_OUTPUT_DIRECTORY)
# export project details and layers
ProjectUtils.export_project_details(project, exportFolder)
LayersExporter.reproject_layers(project, TEMP_DIRECTORY, feedback)
for layer in project.mapLayers().values():
LayersExporter.export_layer(project, layer, exportFolder, feedback)
def ignore_paths(directory, contents):
return ['node_modules', 'exported']
# Create the MapComponents project using the selected template
shutil.copytree(TEMPLATE_PATH, f'{projectFolder}', dirs_exist_ok=True, ignore=ignore_paths)
if parameters[self.OPEN]:
subprocess.Popen(['xdg-open', projectFolder])
feedback.pushInfo(f'The project was successfully created in {projectFolder}')
shutil.rmtree(TEMP_DIRECTORY)
os.mkdir(TEMP_DIRECTORY)
return {"status": "SUCCESSS" }
def name(self):
return 'QGIS to MapComponents'
def displayName(self):
"""
Returns the translated algorithm name.
"""
return self.tr(self.name())
# Group Methods create a group inside the Provider folder in the toolbox
# def group(self):
# return self.tr(self.groupId())
# def groupId(self):
# return 'MapComponents'
def shortHelpString(self):
return self.tr("Make a React WebApp from your actual project and run it on a dev server")
def tr(self, string):
return QCoreApplication.translate('Processing', string)
def icon(self):
cmd_folder = os.path.split(inspect.getfile(inspect.currentframe()))[0]
icon = QIcon(os.path.join(os.path.join(cmd_folder, 'logo.svg')))
return icon
def get_folder_names(self, directory_path):
folder_names = [folder for folder in os.listdir(
directory_path) if os.path.isdir(os.path.join(directory_path, folder))]
return folder_names
def createInstance(self):
return MapComponentizerAlgorithm()