Skip to content

Commit

Permalink
0.5
Browse files Browse the repository at this point in the history
0.5
  • Loading branch information
fzahner authored Jun 30, 2022
2 parents 232afac + d595f0e commit 0d69516
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 14 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Additionally, the plugin can create a Worldfile and/or Lexocad sidecar file if d
1. choose the input layer (if not already selected)
2. set the extent by clicking on "Map Canvas Extent".
3. Set the path and name of the output and the output format (GeoTIFF, PNG or JPG).
4. If desired, check the "Create LexoCAD" checkbox to generate LexoCAD georeference files.
6. Set additional parameters if necessary (for advanced users): CRS and Output resolution.
7. finished (load and view with QGIS or Lexocad).

Expand Down
2 changes: 1 addition & 1 deletion metadata.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
name=Raster Cutter
qgisMinimumVersion=3.0
description=Export a raster layer as an image. Select extent, reproject, create Worldfile or Lexocad sidecar files.
version=0.4
version=0.5
author=IFS Institute for Software
email=feedback.ifs@ost.ch

Expand Down
52 changes: 40 additions & 12 deletions raster_cutter.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@
gdal.UseExceptions()


# TODO help button
# TODO imports?
# TODO Clicking on the icon twice acts like pressing "run"??
# TODO add QgsSubTask for splitting tasks (report progress?)

class RasterCutter:
Expand Down Expand Up @@ -213,15 +212,20 @@ def run(self):
self.first_start = False
self.dlg = RasterCutterDialog()
self.dlg.file_dest_field.setFilePath(default_filepath()) # set path to user home
# self.dlg.setWindowFlags(QtCore.Qt.Popup)
widget_init(self)

if self.dlg.isVisible(): # if window is already open, just bring it to foreground
self.dlg.activateWindow()
return

layers = [layer for layer in QgsProject.instance().mapLayers().values()]
if layers: # if there are layers in the project, we can set extent box extents and crs's
# extentbox init
self.dlg.extent_box.setOriginalExtent(originalExtent=self.dlg.layer_combobox.currentLayer().extent(),
originalCrs=self.dlg.layer_combobox.currentLayer().crs())
self.dlg.extent_box.setOutputCrs(self.dlg.layer_combobox.currentLayer().crs())
self.dlg.proj_selection.setCrs(self.dlg.layer_combobox.currentLayer().crs())
self.dlg.extent_box.setOutputCrs(QgsProject.instance().crs())
self.dlg.proj_selection.setCrs(QgsProject.instance().crs())
self.dlg.extent_box.setCurrentExtent(currentExtent=self.iface.mapCanvas().extent(),
currentCrs=QgsProject.instance().crs())
on_lexocad_toggled(
Expand Down Expand Up @@ -289,6 +293,7 @@ def run(self):
options_string=options_string,
extent_win_string=get_extent_win(self),
generate_lexocad=self.dlg.lexocad_checkbox.isChecked(),
layer_name=selected_layer.name(),
add_to_map=self.dlg.add_to_map_checkbox.isChecked(),
target_resolution=get_target_resolution(self),
resampling_method=get_resampling_method(self))
Expand Down Expand Up @@ -329,11 +334,12 @@ def on_lexocad_toggled(self):
else:
self.dlg.proj_selection.setEnabled(True)


def on_tif_selected(self):
# enables/disables the lexocad checkbox depending on if output file is a geotiff
path = self.dlg.file_dest_field.filePath()
filename, file_extension = os.path.splitext(path)
if file_extension == ".tif":
if file_extension == ".tif" or file_extension == ".tiff":
self.dlg.lexocad_checkbox.setChecked(False)
self.dlg.lexocad_checkbox.setEnabled(False)
else:
Expand All @@ -350,14 +356,17 @@ def select_current_layer(self):
def get_target_projection(self):
return self.dlg.proj_selection.crs()


def get_source_projection(self):
return self.dlg.extent_box.outputCrs()


# returns extent window as a string for use in gdal
def get_extent_win(self):
e = self.dlg.extent_box.outputExtent()
return f"{e.xMinimum()} {e.yMaximum()} {e.xMaximum()} {e.yMinimum()}"


def get_resampling_method(self):
if self.dlg.nearest_neighbour_radio_button.isChecked():
return "near"
Expand All @@ -367,10 +376,9 @@ def get_resampling_method(self):
error_message("Could not get resampling method.")



# this is where all calculations actually happen
def process(task, src, iface, directory_url, src_srs, dest_srs, format_string, extent_win_string, options_string,
generate_lexocad: bool,
generate_lexocad: bool, layer_name,
add_to_map: bool, target_resolution: {"x": float, "y": float}, resampling_method):
# Crop raster, so that only the needed parts are reprojected, saving processing time
QgsMessageLog.logMessage('Cropping raster (possibly downloading)...', MESSAGE_CATEGORY, Qgis.Info)
Expand Down Expand Up @@ -408,7 +416,11 @@ def process(task, src, iface, directory_url, src_srs, dest_srs, format_string, e

manage_files(generate_lexocad, add_to_map, directory_url)

return {"ds": translated, "iface": iface, "path": translated.GetDescription(), "file_name": file_name}
return {"ds": translated,
"iface": iface,
"path": translated.GetDescription(),
"file_name": file_name,
"layer_name": layer_name}


# generate lexocad file and delete worldfile and .aux.xml if needed
Expand All @@ -429,6 +441,7 @@ def open_dataset(data_provider):
type_string = None
url = None
args = data_provider.dataSourceUri().split("&")
layer = None

for arg in args:
if arg.find("type=") is not -1:
Expand All @@ -437,21 +450,29 @@ def open_dataset(data_provider):
if arg.find("url=") is not -1:
url = arg
url = url.replace("url=", "")
if arg.find("layers=") is not -1:
layer = arg
layer = layer.replace("layers=", "")
if url is None:
raise Exception("Could not find type parameter in data source")
# if the wms datasource contains a "type=xyz", a different approach is required
if type_string == "xyz":
xml_file_path = generate_tms_xml(url)
return xml_file_path, None
else:
gdal_string = "WMS:" + url + "?" + data_provider.dataSourceUri()
if 'WMTSCapabilities' in url:
if layer is None:
raise Exception(f"No layers argument found in source string: {data_provider.dataSourceUri()}")
gdal_string = f"<GDAL_WMTS><GetCapabilitiesUrl>{url}</GetCapabilitiesUrl><Layer>{layer}</Layer></GDAL_WMTS>"
else:
gdal_string = f"WMS:{url}?{data_provider.dataSourceUri()}"



elif data_provider.name() == "gdal":
gdal_string = data_provider.dataSourceUri()
else:
return None, "Could not open given dataset: %s" % data_provider.name()

return gdal.Open(gdal_string, gdal.GA_ReadOnly), None


Expand All @@ -466,11 +487,13 @@ def generate_tms_xml(url):
file.write(data)
return temp_file_path


def delete_tms_xml():
temp_file_path = get_file_path('xyz_tms_tmp.xml')
if os.path.exists(temp_file_path):
os.remove(temp_file_path)


def delete_aux_xml_file(path):
aux_xml_file_path = path + '.aux.xml'
os.remove(aux_xml_file_path)
Expand All @@ -479,8 +502,11 @@ def delete_aux_xml_file(path):
def get_file_path(file_name):
return os.path.join(os.path.dirname(__file__), file_name)


def crop(out, src, extent_win_string, extent_srs, resampling_method):
return gdal.Translate(out, src, options=f"-projwin {extent_win_string}, -projwin_srs {extent_srs}, -outsize 2000 0, -r {resampling_method}")
return gdal.Translate(out, src,
options=f"-projwin {extent_win_string}, -projwin_srs {extent_srs}, -outsize 2000 0, -r {resampling_method}")


def warp(out, src, dst_srs, target_resolution, resampling_method):
options_string = f"-t_srs {dst_srs}, "
Expand All @@ -503,7 +529,9 @@ def completed(exception, result=None):
QgsMessageLog.logMessage('Adding file to map...', MESSAGE_CATEGORY, Qgis.Info)
add_file_to_map(result['iface'], result['path'], result['file_name'])
QgsMessageLog.logMessage('Done!', MESSAGE_CATEGORY, Qgis.Info)
globals()['self'].iface.messageBar().pushMessage("Success", f"Layer exported to {result['path']}", level=Qgis.Info)
globals()['self'].iface.messageBar().pushMessage("Success",
f"Layer \"{result['layer_name']}\" exported to {result['path']}",
level=Qgis.Info) # TODO layer name
else:
error_message("Exception: {}".format(exception))

Expand Down
2 changes: 1 addition & 1 deletion raster_cutter_dialog_base.ui
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
<string/>
</property>
<property name="text">
<string>Create LexoCAD (EPSG:2057, .jpgl / .pngl)</string>
<string>Create LexoCAD (EPSG:2057, .jpgl/.pngl)</string>
</property>
</widget>
</item>
Expand Down

0 comments on commit 0d69516

Please sign in to comment.