|
16 | 16 | import io
|
17 | 17 | import os
|
18 | 18 |
|
19 |
| -from PIL import Image |
20 |
| - |
21 | 19 | from .gl2qgis import parse_layers, parse_background, parse_interpolate_list_by_zoom
|
22 | 20 | from .gl2qgis import parse_interpolate_opacity_by_zoom, PropertyType
|
| 21 | +from qgis.PyQt.QtWidgets import QMessageBox |
23 | 22 |
|
24 | 23 |
|
25 | 24 | def get_sources_dict_from_style_json(style_json_data: dict) -> dict:
|
@@ -84,7 +83,7 @@ def get_sources_dict_from_style_json(style_json_data: dict) -> dict:
|
84 | 83 |
|
85 | 84 | def get_style_json(style_json_url: str) -> dict:
|
86 | 85 | url_endpoint = style_json_url.split("?")[0]
|
87 |
| - if url_endpoint.endswith("style.json"): |
| 86 | + if url_endpoint.endswith(".json"): |
88 | 87 | style_json_data = json.loads(requests.get(style_json_url).text)
|
89 | 88 | return style_json_data
|
90 | 89 | elif url_endpoint.endswith(".pbf"):
|
@@ -178,18 +177,25 @@ def write_sprite_imgs_from_style_json(style_json_data: dict, output_path: str):
|
178 | 177 | sprite_url = style_json_data.get("sprite")
|
179 | 178 | if sprite_url is None:
|
180 | 179 | return {}
|
181 |
| - |
182 |
| - sprite_json_dict = json.loads(requests.get(sprite_url + '.json').text) |
183 |
| - sprite_img = Image.open(io.BytesIO(requests.get(sprite_url + '.png').content)) |
184 |
| - sprite_imgs_dict = {} |
185 |
| - |
186 |
| - for key, value in sprite_json_dict.items(): |
187 |
| - left = int(value["x"]) |
188 |
| - top = int(value["y"]) |
189 |
| - right = left + int(value["width"]) |
190 |
| - bottom = top + int(value["height"]) |
191 |
| - cropped = sprite_img.crop((left, top, right, bottom)) |
192 |
| - sprite_imgs_dict[key] = cropped |
193 |
| - |
194 |
| - for key, value in sprite_imgs_dict.items(): |
195 |
| - value.save(os.path.join(output_path, key + ".png")) |
| 180 | + try: |
| 181 | + from PIL import Image |
| 182 | + sprite_json_dict = json.loads(requests.get(sprite_url + '.json').text) |
| 183 | + sprite_img = Image.open(io.BytesIO(requests.get(sprite_url + '.png').content)) |
| 184 | + sprite_imgs_dict = {} |
| 185 | + |
| 186 | + for key, value in sprite_json_dict.items(): |
| 187 | + left = int(value["x"]) |
| 188 | + top = int(value["y"]) |
| 189 | + right = left + int(value["width"]) |
| 190 | + bottom = top + int(value["height"]) |
| 191 | + cropped = sprite_img.crop((left, top, right, bottom)) |
| 192 | + sprite_imgs_dict[key] = cropped |
| 193 | + |
| 194 | + for key, value in sprite_imgs_dict.items(): |
| 195 | + value.save(os.path.join(output_path, key + ".png")) |
| 196 | + except ImportError: |
| 197 | + import_error_message = "You do not have PIL/Pillow library installed on your system. "\ |
| 198 | + "Sprites will not be supported.\n"\ |
| 199 | + "MacOS users: To install Pillow library, run following code in terminal:\n"\ |
| 200 | + "/Applications/QGIS.app/Contents/MacOS/bin/pip3 install pillow -U" |
| 201 | + QMessageBox.warning(None, 'Missing PIL/Pillow library', import_error_message) |
0 commit comments