Skip to content

Commit

Permalink
Recreate layer hierachy when loading layers to project (#37)
Browse files Browse the repository at this point in the history
* Make sure to only try to load data files as map layers, not ancilliary
files like .txt files

* Recreate layer hierachy when loading layers to project

Fixes #35
  • Loading branch information
nyalldawson committed Dec 17, 2021
1 parent ed2cfd1 commit fe0322d
Showing 1 changed file with 42 additions and 24 deletions.
66 changes: 42 additions & 24 deletions qgreenland_dowload.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def __init__(self, parent=None):
self.folder_path.setText(self.saving_path)
# also enable the download button
self.download_button.setEnabled(True)

self.explore_files_button.clicked.connect(self.open_folder)

self.add_to_project_button.clicked.connect(self.load_layers)
Expand All @@ -164,7 +164,7 @@ def get_server_url(self):
qgreenland_server = QGreenlandServer()

# if no server URL hae been chosen warnin the user and open the config
# settings
# settings
if not self.settings.value("/QGreenland/server-chosen"):
# get an useful QMesasgeBox
message_box = QMessageBox()
Expand Down Expand Up @@ -233,7 +233,7 @@ def on_page_changed(self):
self.download_label.setText("")
else:
self.next_button.setEnabled(True)

if page_name == 'manage_data':
self.prev_button.setVisible(True)
self.next_button.setVisible(False)
Expand Down Expand Up @@ -403,10 +403,10 @@ def _fill_manage_tree(self):
message_box.exec()
self.stackedWidget.setCurrentIndex(0)
return

# loop into the hierarchies and fill the QTreeView with them
for layer in self.downloaded_layers:
layer_hierarchy = layer['hierarchy']
layer_hierarchy = layer['hierarchy'][:]
parent_item = None

while layer_hierarchy:
Expand Down Expand Up @@ -767,7 +767,7 @@ def download_data(self):
files_to_download.append(asset['file'])
total_size+=asset['size_bytes']
layer_to_download[parent] = files_to_download

# get the bytes in megabytes
total_size = total_size / 1_000_000

Expand Down Expand Up @@ -803,15 +803,15 @@ def download_data(self):
# write the reply to a file
with open(saving_path, 'wb') as f:
f.write(reply_content)

self.progressBar.setValue(int((current + 1) * total))

# set the final progress bar text with the amount of megabytes downloaded
self.progressBar.setFormat(f"{total_size:,.2f} MB have been downloaded")

# fill the treeView with the json information
self._fill_manage_tree()


def browse_folder(self):
"""
Expand Down Expand Up @@ -863,37 +863,55 @@ def load_layers(self):
# get the folder path from the settings
folder_path = self.settings.value("/QGreenland/saving_folder")

# create the QgsProject instance
project = QgsProject.instance()

# initialize the empty list
layer_list = []
respect_structure = self.folder_structure_check.isChecked()

# loop into the downloaded layer (json file)
for layer in self.downloaded_layers:
# get only the match between the checked items and the layers
if layer['id'] in items:
if layer['id'] not in items:
continue

layer_hierarchy = layer['hierarchy'][:]
parent_group = QgsProject.instance().layerTreeRoot()

while layer_hierarchy and respect_structure:
group_text = layer_hierarchy[0]
del layer_hierarchy[0]

# try to find existing layer tree group for this hierarchy level
child_group = parent_group.findGroup(group_text)

if not child_group:
# need to create a group
parent_group = parent_group.addGroup(group_text)
else:
parent_group = child_group

# find data assets only, not ancillary files and other types
spatial_data_assets = [asset for asset in layer['assets'] if asset['type'] == 'data']

for asset in spatial_data_assets:
# get the file path of the file
file_path = os.path.join(folder_path, layer['id'], layer['assets'][0]['file'])
file_path = os.path.join(folder_path, layer['id'], asset['file'])
# get the extension of the file to correctly load vector or raster files
_, file_extension = os.path.splitext(file_path)

map_layer = None
# vector layers
if file_extension not in raster_extension:
vl = QgsVectorLayer(
map_layer = QgsVectorLayer(
file_path,
layer['id'],
'ogr'
)
layer_list.append(vl)
# raster layers
else:
rl = QgsRasterLayer(
map_layer = QgsRasterLayer(
file_path,
layer['id']
)

layer_list.append(rl)

# load the layer list in the project
project.addMapLayers(layer_list)

# add to the project, without automatically creating a legend node for the layer...
QgsProject.instance().addMapLayer(map_layer, addToLegend=False)
# ...because we want to manually insert it into the correct parent group
parent_group.addLayer(map_layer)

0 comments on commit fe0322d

Please sign in to comment.