Showing shapely MultiPolygon on top of ipyleaflet map

Viewed 691

I don't know if there are other packages that are doing what I would like to do easily, but is there a way to add a layer on shaply MultiPolygon on top of a ipyleadfet map?

Here is an example for a code:

import numpy as np
import matplotlib.pyplot as plt
from shapely.geometry import Point
from shapely.ops import unary_union
from ipyleaflet import Map, basemaps, basemap_to_tiles,AntPath,Marker, MarkerCluster
%matplotlib inline

c1 = Point(33.1,32.8).buffer(0.1)
c2 = Point(33.1,34.8).buffer(1)
c3 = Point(33.1,36.8).buffer(1)
C = [c1,c2,c3]
D=unary_union(C)
type(D)

shapely.geometry.multipolygon.MultiPolygon

D

enter image description here

# Testing ipyleaflet
Tel_aviv_lat_lon = (33.109333, 34.855499)
m = Map(center=Tel_aviv_lat_lon, zoom=10)

dark_matter_layer = basemap_to_tiles(basemaps.OpenStreetMap.Mapnik)
m.add_layer(dark_matter_layer)
m

How do I add the D (shapely.geometry.multipolygon.MultiPolygon object) on top of the map?

1 Answers

hey I stumbled upon this question an easy way is to transform the shapely polygon into WKT and then add a WKTLayer

like so:

from shapely.geometry import Point
from shapely.ops import unary_union
from ipyleaflet import Map, WKTLayer

c1 = Point(32.8,33.1).buffer(0.1)
c2 = Point(33.1,34.8).buffer(1)
c3 = Point(33.1,36.8).buffer(1)
C = [c1,c2,c3]
D=unary_union(C)

m = Map(center=(35, 35), zoom=6)

wlayer = WKTLayer(
    wkt_string=D.wkt,
)

m.add_layer(wlayer)

m

I played a bit with the coordiantes so they would make sens

Related