GCGM
GCGM

Reputation: 1073

Polygon overlay in leaflet map

I am creating an interactive map using ipyleaflet using the following code:

from ipyleaflet import Map, Polygon


polygon = Polygon(
    locations=[[(38.844185,-4.804621),(39.241299,-1.899833),(40.74308,-2.205491),(40.34742,-5.17429),(38.844185,-4.804621)],[(39.365192,-1.941078),(40.867912,-1.567062),(41.276688,-4.670904),(39.775406,-4.976737),(39.365192,-1.941078)],[(39.706161,-1.849863),(41.207623,-1.465817),(41.617561,-4.594476),(40.117233,-4.908839),(39.706161,-1.849863)],[(39.702591,-5.033657),(40.101254,-2.077048),(41.602196,-2.389729),(41.204681,-5.413605),(39.702591,-5.033657)]],
    color="green",
    fill_opacity= 0.5,
    fill_color="green"
)

m = Map(center=(38.5531, -4.6914), zoom=6)
m.add_layer(polygon);

m

The ouptut looks like this: enter image description here

I am wondering how can I make the intersection of the polygons not be fully transparent. Looking at the attributes https://ipyleaflet.readthedocs.io/en/latest/api_reference/polygon.html in the documentation, I don't see any option?

An example of the desired output can be seen in the image below:

enter image description here

Upvotes: 1

Views: 1273

Answers (1)

True do day
True do day

Reputation: 308

You've got bad results because ipyleaflet subtracts ovellaped polygons (you can see it in second example in the documentation link you posted, "Polygon with hole")

You need to add each polygon separately, I changed your code a bit, now it creates and applies polygons in loop:

from ipyleaflet import Map, Polygon

poligons = [[(38.844185,-4.804621),(39.241299,-1.899833),(40.74308,-2.205491),(40.34742,-5.17429),(38.844185,-4.804621)],
            [(39.365192,-1.941078),(40.867912,-1.567062),(41.276688,-4.670904),(39.775406,-4.976737),(39.365192,-1.941078)],
            [(39.706161,-1.849863),(41.207623,-1.465817),(41.617561,-4.594476),(40.117233,-4.908839),(39.706161,-1.849863)],
            [(39.702591,-5.033657),(40.101254,-2.077048),(41.602196,-2.389729),(41.204681,-5.413605),(39.702591,-5.033657)]]

m = Map(center=(38.5531, -4.6914), zoom=6)

for poly in poligons:
    polygon = Polygon(
        locations= poly,
        color="green",
        fill_opacity= 0.5,
        fill_color="green"
    )
    m.add_layer(polygon);

m

Result: enter image description here

Upvotes: 4

Related Questions