Issue with displaying folium map in pyQt

52 views Asked by At

I'm working on a project where I'm trying to load and display map data using PyQt and folium.**

Problem Description

Note: Content larger than 2 MB cannot be displayed, because setHtml() converts the provided HTML to percent-encoding and places data: in front of it to create the URL it navigates to. Thereby, the provided code becomes a URL that exceeds the 2 MB limit set by Chromium. If the content is too large, the loadFinished() signal is triggered with success=false. - https://doc.qt.io/qt-5/qwebengineview.html#setHtml

I'm using PyQt6.5.3 to convert map data into HTML and trying to display it through QWebEngineView. However, as the data size increases, I'm getting an error that says, "Content larger than 2 MB cannot be displayed" when using the setHtml() function.

SOURCE CODE

def display_map(self, df, selected_states, selected_livestocks, color_mapping):
    if 'total' not in selected_states:
        df = df[df['sig_kor_nm'].isin(selected_states)]
    if 'total' not in selected_livestocks:
        df = df[df['prt_type_nm'].isin(selected_livestocks)]
    
    df_copy = df.copy()
    df_copy.loc[:, 'geometry'] = df_copy['geom_coordinates'].apply(lambda x: Polygon(ast.literal_eval(x)[0]) if pd.notnull(x) else None)
    df = df_copy
    gdf = gpd.GeoDataFrame(df, geometry='geometry')
       
    m = folium.Map(location=[36.429, 127.977], scrollWheelZoom=True, tiles='cartodbdark_matter', zoom_start=6.5)

    geojson_data = gdf.to_json()
    folium.GeoJson(geojson_data,
                       style_function=lambda x: {
                           'color': color_mapping.get(x['properties']['prt_type_nm'], 'gray'),
                           'fillColor': color_mapping.get(x['properties']['prt_type_nm'], 'gray')
                           }
                           ).add_to(m)
    m.save('map.html')

    html_string = m.get_root().render()
    self.web_view.setHtml(html_string) 

Attempted Solution(not working well)

temp_file_name = os.path.join(os.getcwd(), "temp_map.html")
m.save(temp_file_name)
self.web_view.load(html_map)

error : js: Uncaught ReferenceError: L is not defined

I'm looking for an alternative to the setHtml() function or any other method to handle it.

0

There are 0 answers