question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Layer not being displayed in Jupyter using Ipyleaflet but no error thrown

See original GitHub issue

First time ever posting an issue on Github so be gentle, pedants!

I’m trying to create my own custom GeoDataFrame and then display it using Ipyleaflet in a Jupyter notebook. Unfortunately, the basemap displays but my layer isn’t shown and there are no errors thrown to point out any issues.

Here’s my code:

# General python libraries
import json
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
import pandas as pd
import pickle
import re

# Geo mapping libraries
import geopandas as gpd
from shapely.geometry import Point, Polygon
from ipyleaflet import Map, GeoData, basemaps, LayersControl, Choropleth
from ipyleaflet import WidgetControl, GeoJSON 
from ipywidgets import Text, HTML
import geopandas

# Create my own geodataframe using lats/longs to create the cells
def create_geobins(df_lat_ser, df_long_ser, n_lats, n_longs):
    polygons = []
    max_lat = df_lat_ser.max()
    min_lat = df_lat_ser.min()
    max_long = df_long_ser.max()
    min_long = df_long_ser.min()
    lat_step = abs((max_lat - min_lat)) / n_lats
    long_step = abs((max_long - min_long)) / n_longs
    for this_lat in np.arange(min_lat, max_lat, lat_step):
        for this_long in np.arange(min_long, max_long, long_step):
            polygons.append(Polygon([(this_lat, this_long),
                                     (this_lat + lat_step, this_long),
                                     (this_lat + lat_step, this_long + long_step),
                                     (this_lat, this_long + long_step),
                                     (this_lat, this_long)
                                     ]
                                    )
                            )
    return gpd.GeoDataFrame(polygons, columns=['geometry'])

# Create custom GeoDataFrame
geobins = create_geobins(map_df.lat, map_df.long, 10, 6)

# Create basemap, add layer
geobins_layer = GeoData(geo_dataframe=geobins, name='cells')
m = Map(center=(47.5391,-122.070), zoom=9)
m.add_layer(geobins_layer)
m.add_control(LayersControl())
m

The map with base layer is shown and I can even toggle the ‘cells’ layer on the control but no polygons are displayed.

For debugging:

–type(geobins_layer) ipyleaflet.leaflet.GeoData

–geobins_layer.data: {‘type’: ‘FeatureCollection’, ‘features’: [{‘id’: ‘0’, ‘type’: ‘Feature’, ‘properties’: {}, ‘geometry’: {‘type’: ‘Polygon’, ‘coordinates’: [[[47.1559, -122.512], [47.218070000000004, -122.512], [47.218070000000004, -122.3125], [47.1559, -122.3125], [47.1559, -122.512]]]}}, {‘id’: ‘1’, ‘type’: ‘Feature’, ‘properties’: {}, ‘geometry’: {‘type’: ‘Polygon’, ‘coordinates’: [[[47.1559, -122.3125], [47.218070000000004, -122.3125], [47.218070000000004, -122.113], [47.1559, -122.113], [47.1559, -122.3125]]]}}, {‘id’: ‘2’, …

–conda list … ipyleaflet 0.12.3 py_1 conda-forge …

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
martinRenoucommented, Mar 19, 2020

I’ll try to have a look.

In the meantime, did you try switching latitude and longitude when creating your Polygon? It might be that your dataset uses longitude/latitude and not latitude/longitude

2reactions
benroyalalexandercommented, Mar 19, 2020

You were correct. Shapely Polygons are in order (x, y) meaning (Long, Lat) rather than (Lat, Long). Thank you very much for the quick attention though! Excited to use Ipyleaflet more! I’ll post the final fixed result.

# Create my own geodataframe using lats/longs to create the cells, need id
def create_geobins(df_lat_ser, df_long_ser, n_lats, n_longs):
    polygons = []
    max_lat = df_lat_ser.max()
    min_lat = df_lat_ser.min()
    max_long = df_long_ser.max()
    min_long = df_long_ser.min()
    lat_step = abs((max_lat - min_lat)) / n_lats
    long_step = abs((max_long - min_long)) / n_longs
    for this_lat in np.arange(min_lat, max_lat, lat_step):
        for this_long in np.arange(min_long, max_long, long_step):
            polygons.append(Polygon([
                (this_long, this_lat),
                (this_long, this_lat + lat_step),
                (this_long + long_step, this_lat + lat_step),
                (this_long+long_step, this_lat),
                (this_long, this_lat)
            ]))

# Incorrect lat/long order for Shapely Polygon objects
#             polygons.append(Polygon([(this_lat, this_long),
#                                      (this_lat + lat_step, this_long),
#                                      (this_lat + lat_step, this_long + long_step),
#                                      (this_lat, this_long + long_step),
#                                      (this_lat, this_long)
#                                      ]
#                                     )
#                             )
    return gpd.GeoDataFrame(polygons, columns=['geometry'])
                            
geobins = create_geobins(map_df.lat, map_df.long, 10, 6)

# Create basemap, add geobins_layer
m = Map(center=(47.5391,-122.070), zoom=9)
geobins_layer = GeoData(geo_dataframe=geobins,
                       style={'color': 'black'},
#                        hover_style={'fillColor': 'red' , 'fillOpacity': 0.2},
                       name = 'cells')
m.add_layer(geobins_layer)
m.add_control(LayersControl())
m

Screenshot_20200319_100453

Read more comments on GitHub >

github_iconTop Results From Across the Web

Ipyleaflet map object doesn't display in Jupyter Notebook but it ...
I just tried to install ipyleaflet with Jupyter 5.2.3: conda install -c conda-forge ipyleaflet. and also didn't get a map.
Read more >
Can't see USGS WMS layers in ipyleaflet (leaflet.js for python)
Trying to use ipyleaflet (leaflet.js for python/jupyter widgets) to view a WMS layer representing 1m DEM availability.
Read more >
typeerror: cannot read properties of undefined (reading 'lat')
If you want to prevent Map to be "undefined". First declare your map variable as public js variable (in the head of your...
Read more >
python-visualization/folium - Gitter
i am not able to visualise my overlay map using folium..but it is throwing an unexpected error. map.choropleth(geo_path=country_geo, data=plot_data,
Read more >
arXiv:1701.01222v2 [astro-ph.IM] 6 May 2017
Vizic: A Jupyter-based Interactive Visualization Tool for Astronomical ... API to display and interact with sky maps created from cata-.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found