Layer not being displayed in Jupyter using Ipyleaflet but no error thrown
See original GitHub issueFirst 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:
- Created 4 years ago
- Comments:8 (5 by maintainers)
Top GitHub Comments
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
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.