1

I have the following code that works fine in Jupyter notebook:

import os
import json
import random
import requests

from ipyleaflet import Map, GeoJSON, LayersControl

def handle_click(**kwargs):
    print(kwargs.get('feature')['properties']['ID1'])

with open('roadway.geojson', 'r') as f:
    data = json.load(f)

m = Map(center=(39.7, -86), zoom=10)

geo_json = GeoJSON(
    data=data,
   
)
m.add_layer(geo_json)
geo_json.on_click(handle_click)
m.add_control(LayersControl())

m

I'd like to display it in a window using pyqt5. I think I need to write an equivalent code using pyqtlet and I tried doing that but I can't figure out how to add my geojson file and then of course handle mouse clicks on my geojson layer using pyqtlet. Here is my trial with pyqtlet:

import sys
from PyQt5.QtWidgets import QApplication, QVBoxLayout, QWidget
from pyqtlet import L, MapWidget


class MapWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.mapWidget = MapWidget()
        self.layout = QVBoxLayout()
        self.layout.addWidget(self.mapWidget)
        self.setLayout(self.layout)

        self.map = L.map(self.mapWidget)
        self.map.setView([39.7, -86], 10)
        L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png').addTo(self.map)
        self.data = L.layerGroup("roadway.geojson") ### This is where I get stuck and of course, how to handle clicks on this layer

        self.show()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    widget = MapWindow()
    sys.exit(app.exec_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
AkbarB
  • 460
  • 7
  • 24
  • Note: It seems that you have an XY problem, even translating to pyqt5 the handle_click method will not work since that logic is implemented using the notebook API. On the other hand pyqtlet and ipyleaflet are 2 libraries that do the same with different views: notebook and pyqt5 window. – eyllanesc Nov 06 '20 at 19:15
  • I was going to propose a solution for your [other post](https://stackoverflow.com/questions/64576899/click-on-a-geojson-layer-feature-shown-on-python-folium-map) but since you removed everything from pyqt5 then my answer would never be valid. – eyllanesc Nov 06 '20 at 19:20
  • I understand that ipyleaflet and pyqtlet do the same so that's why I am trying to translate my ipyleaflet to pyqt5. but I got stuck in adding the geojson and hence the question. – AkbarB Nov 06 '20 at 19:39
  • Well I kept changing the other post according to your comments. I managed to switch to leaflet and html/javascript, but then I got stuck at returning click results to my python code. Can you still propose that answer for my other post? I can again edit it to original question. – AkbarB Nov 06 '20 at 19:44
  • 1
    This is the problem: There is no such translation. ipyleaflet is a much more mature SW with more functionalities, on the other hand pyqtlet is a SW that lacks to implement many functionalities such as callbacks before events of the view. So that's why there is no solution for your case. – eyllanesc Nov 06 '20 at 19:45
  • It is that it is the other problem: It is that when you edit your post you radically change the problem, at the beginning it was about the interaction with events associated with a geojson in a window implemented with QWebEngineView, and currently it is not. You realize that folium only generates HTML but the one that makes sense of the events is also the view. My initial comments are for you to improve your MRE; not just a copy-paste of a previous question, but instead you deleted all trace of pyqt5 – eyllanesc Nov 06 '20 at 19:49
  • @eyllanesc Did you find out how to display your ipyleaflet Map in PyQt5 yet? I'm also looking for the way, since pyqtlet is too restricted. – Laurent Jan 12 '21 at 17:00

0 Answers0