TomSchimansky / TkinterMapView

A python Tkinter widget to display tile based maps like OpenStreetMap or Google Satellite Images.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unable to load overlay layer using "set_overlay_tile_server"

hanselmoovita opened this issue · comments

Example used: map_with_customtkinter.py

I added a 4th option to include overlay option for "Railway". Modifications made were:

In self.map_option_menu:

self.map_option_menu = customtkinter.CTkOptionMenu(self.frame_left, values=["OpenStreetMap", "Google normal", "Google satellite", "railway"]

In def change map:

elif new_map == "railway":
self.map_widget.set_overlay_tile_server("http://a.tiles.openrailwaymap.org/standard/{z}/{x}/{y}.png") # railway infrastructure

However, upon running the program and selecting the "railway" option for Tile Server, the TkinterMapView returned blank map:

Tkintermapview

I used the suggested API link given under "Use other tile servers" in your Readme.

May I ask for help to fix or advice on this issue

Hi @hanselmoovita sorry for late answer. You are probably having an exception raising in the try block at line 494 of the map_widget.py file that results in one of the except blocks which is returning self.empty_tile_image.
Could you please provide further details?

@LorenzoMattia

Can I clarify with you, if I would like to apply overlay layer on top of Main Tile layer, is this the correct way to do it? See screenshot below, right window for highlighted portion:

Screenshot from 2023-12-18 14-13-57

The current problem I faced is that when I added this line, the tkinter application goes blank when I made the selection with overlay (ie: Google satellite)

For this scenario, I have not made modifications to map_widget.py I have tried to modify the definition for but have not been successful either

For your kind advice please

@hanselmoovita

Yes, you are doing right in setting the overlay_tile_server.

The problem I think you are facing is in the block I mentioned in my previous answer.

I suggest you to execute the code in debug mode or trivially to temporarily remove the try except block at line 494 in map_widget.py to see what exception is raised.
My intuition is that a certain problem is causing that block to fall in on of the except blocks in which the empty_tile_image is being returned and that is probably the blank image you are seeing in the screen.

I'm talking about this piece of code

        try:
            url = self.tile_server.replace("{x}", str(x)).replace("{y}", str(y)).replace("{z}", str(zoom))
            image = Image.open(requests.get(url, stream=True, headers={"User-Agent": "TkinterMapView"}).raw)

            if self.overlay_tile_server is not None:
                url = self.overlay_tile_server.replace("{x}", str(x)).replace("{y}", str(y)).replace("{z}", str(zoom))
                image_overlay = Image.open(requests.get(url, stream=True, headers={"User-Agent": "TkinterMapView"}).raw)
                image = image.convert("RGBA")
                image_overlay = image_overlay.convert("RGBA")

                if image_overlay.size is not (self.tile_size, self.tile_size):
                    image_overlay = image_overlay.resize((self.tile_size, self.tile_size), Image.ANTIALIAS)

                image.paste(image_overlay, (0, 0), image_overlay)

            if self.running:
                image_tk = ImageTk.PhotoImage(image)
            else:
                return self.empty_tile_image

            self.tile_image_cache[f"{zoom}{x}{y}"] = image_tk
            return image_tk

        except PIL.UnidentifiedImageError:  # image does not exist for given coordinates
            self.tile_image_cache[f"{zoom}{x}{y}"] = self.empty_tile_image
            return self.empty_tile_image

        except requests.exceptions.ConnectionError:
            return self.empty_tile_image

        except Exception:
            return self.empty_tile_image

Keep me updated once you have tried!

I've removed the entire try-except code block starting from line 494 in map_widget.py, and when the app is launched, the default selection OpenStreetMap shows a blank grey screen, and many exception errors were returned in the debugger in one go:

Screenshot from 2023-12-19 09-08-51

No other modifications were made other than the one you've suggested above. The full log of exception errors are found below:

Error log.txt