diff --git a/metadata.txt b/metadata.txt index b592009..e216b58 100644 --- a/metadata.txt +++ b/metadata.txt @@ -12,7 +12,7 @@ about=Plugin which reads vector tiles according to Mapbox' Vector Tiles specific * OpenMapTiles.com with OSM Bright MB GL style (default) * Nextzen.org This Python plugin uses prebuilt C++ binaries for performance reasons. -version=3.0.4 +version=3.0.5 author=Martin Boos email=geometalab@gmail.com @@ -22,6 +22,9 @@ email=geometalab@gmail.com # Uncomment the following line and add your changelog: changelog= + ---3.0.5--- + * Bugfix: Layer names from the TileJSON containing characters invalid for a filepath, would lead to an exception. + * Feature: ArcGIS support via TileJSON ---3.0.4--- * Feature: Loading a TileJSON from the harddisk is now supported * Bugfix: Native library might have prevented deinstallation of plugin diff --git a/plugin/util/file_helper.py b/plugin/util/file_helper.py index bd4b40f..a2b69e6 100644 --- a/plugin/util/file_helper.py +++ b/plugin/util/file_helper.py @@ -1,4 +1,5 @@ import os +import re import shutil import sys import tempfile @@ -121,6 +122,21 @@ def get_geojson_file_name(name): return os.path.join(path, name_with_extension) +def get_valid_filename(s): + """ + Return the given string converted to a string that can be used for a clean + filename. Remove leading and trailing spaces; convert other spaces to + underscores; and remove anything that is not an alphanumeric, dash, + underscore, or dot. + >>> get_valid_filename("john's portrait in 2004.jpg") + 'johns_portrait_in_2004.jpg' + + Source: https://github.com/django/django/blob/master/django/utils/text.py + """ + s = str(s).strip().replace(" ", "_") + return re.sub(r"(?u)[^-\w.]", "", s) + + def is_sqlite_db(path): header_string = "SQLite" chunksize = len(header_string) diff --git a/plugin/vt_reader.py b/plugin/vt_reader.py index ac15362..8fef8a6 100644 --- a/plugin/vt_reader.py +++ b/plugin/vt_reader.py @@ -27,6 +27,7 @@ get_geojson_file_name, get_style_folder, get_styles, + get_valid_filename, is_gzipped, ) from .util.log_helper import critical, debug, info, remove_key @@ -507,7 +508,8 @@ def _process_tiles(self, tiles, layer_filter): self._update_progress(progress=index + 1) def _get_geojson_filename(self, layer_name, geo_type): - return "{}.{}.{}".format(self._source.name().replace(" ", "_"), layer_name, geo_type) + file_name = "{}.{}.{}".format(self._source.name().replace(" ", "_"), layer_name, geo_type) + return get_valid_filename(file_name) def _create_qgis_layers(self, merge_features, apply_styles, clip_tiles): """ @@ -622,7 +624,7 @@ def _create_qgis_layers(self, merge_features, apply_styles, clip_tiles): info("Layer creation complete") @staticmethod - def _update_layer_source(layer_source, feature_collection): + def _update_layer_source(layer_source: str, feature_collection: dict) -> None: """ Updates the layers GeoJSON source file :param layer_source: The path to the geoJSON file that is the source of the layer