1
00:00:01,520 --> 00:00:07,589
Hi and welcome again to yet another
lecture on web mapping with Python and

2
00:00:07,589 --> 00:00:15,210
Folium This is the map we built this
far, and it contains a base layer,

3
00:00:15,210 --> 00:00:23,910
a polygon layer, and a marker layer. This is
our code. Now what I want to do in this

4
00:00:23,910 --> 00:00:29,310
lecture, I want to implement a feature
that allows me to turn the custom layers

5
00:00:29,310 --> 00:00:34,700
on and off. With custom layers I mean the
polygon layer and the marker or layer

6
00:00:34,700 --> 00:00:39,290
because it's not possible to turn off
the base map at least as far as I know.

7
00:00:39,290 --> 00:00:45,320
And it wouldn't make sense to turn off
the base layer, you don't want a blank page.

8
00:00:45,320 --> 00:00:53,399
So let's see how to do that the key
feature here is the layer control class of

9
00:00:53,399 --> 00:01:00,570
the folium module which can be added
like this. You want to point to the map

10
00:01:00,570 --> 00:01:06,409
object, then you want to add a child,
so layer control is added as a child.

11
00:01:06,409 --> 00:01:20,189
Folium.LayerControl. Now this will not
work, but I still want to execute this so

12
00:01:20,189 --> 00:01:26,340
that you don't make this mistake later on.
So let me reload the page. You only

13
00:01:26,340 --> 00:01:33,020
see the base map here, no layers at all.
The reason this doesn't work is that

14
00:01:33,020 --> 00:01:38,640
when you added layer control to the map,
you still hadn't added the feature

15
00:01:38,640 --> 00:01:46,710
group which is an object that
contains the JSON layer and the marker

16
00:01:46,710 --> 00:01:52,530
layer, so basically layer control will be
looking for the feature group added to

17
00:01:52,530 --> 00:01:57,990
the map, but it doesn't find it at this
point, therefore things are messed up.

18
00:01:57,990 --> 00:02:04,439
So it's important to put that after you
have added the feature group layer to

19
00:02:04,439 --> 00:02:16,530
the map right here. Save, and execute
again, reload the map, and you see the

20
00:02:16,530 --> 00:02:24,030
layers there and you see this box here,
mapboxbright, this is about the

21
00:02:24,030 --> 00:02:32,069
base map and you cannot turn this off,
and you also have this my map item here

22
00:02:32,069 --> 00:02:36,930
of the layer control panel, so this is
the layer control panel and you can turn

23
00:02:36,930 --> 00:02:43,379
on and off of this layer, but as you see
this is turning off and on both the

24
00:02:43,379 --> 00:02:48,140
polygon and the point layer at the same
time which is something you don't want.

25
00:02:48,140 --> 00:02:54,569
So we go ahead and change that and I'll
explain why this happens so this is

26
00:02:54,569 --> 00:03:00,629
happening because you know layer control
what it does it looks for

27
00:03:00,629 --> 00:03:07,470
objects added to map with add child like
here so you have added a child, and you

28
00:03:07,470 --> 00:03:12,480
have added only one child to the map
object therefore layer control will

29
00:03:12,480 --> 00:03:19,230
consider that object as one single item
of the layer control panel, but that

30
00:03:19,230 --> 00:03:24,930
feature group object
it contains the circle markers but it

31
00:03:24,930 --> 00:03:29,940
also contains the GeoJSON layer.
Therefore you need to change your

32
00:03:29,940 --> 00:03:35,459
strategy here. So how can you do that?
There may be different ways. One way is

33
00:03:35,459 --> 00:03:43,680
to actually create a feature group for
volcanos, let's call it fgv, so you add

34
00:03:43,680 --> 00:03:49,680
fgv here and then you want another
feature group. Let me copy this line

35
00:03:49,680 --> 00:03:52,849
paste it here.
You want another feature group for

36
00:03:55,700 --> 00:04:04,010
population. Let's call this population.
Let's call this volcanoes like that.

37
00:04:04,010 --> 00:04:15,890
Change the variable name fgp, fgp here.
Okay. Fgv there, again.

38
00:04:17,190 --> 00:04:26,670
Add another child which has to be fgp
for population. Yeah, now everything looks

39
00:04:26,670 --> 00:04:36,190
good, so let's see if layer control will
be able to read these two feature groups

40
00:04:36,190 --> 00:04:49,540
now. Okay, go and reload. The map looks
fine. Volcanoes, working well, population

41
00:04:49,540 --> 00:04:59,860
too, so everything is working fine. This is
the map. And yeah, if you were able to build

42
00:04:59,860 --> 00:05:08,800
this as I did, then good job! You can also
choose not to use feature groups

43
00:05:08,800 --> 00:05:17,530
actually, so you could have added
GeoJSON to the map directly, however for

44
00:05:17,530 --> 00:05:23,740
this case for vocals where you are
adding, you are executing the add child

45
00:05:23,740 --> 00:05:29,680
method multiple times, in that case you
would have a while layer for every

46
00:05:29,680 --> 00:05:37,630
volcano, so you'd have added 62 children
to your map object if you did map

47
00:05:37,630 --> 00:05:45,540
without feature group here, so that means
in this control panel here you'd have 62

48
00:05:45,540 --> 00:05:52,180
entries, 62 layers, and that's not what we
want, so feature group comes in handy in here.

49
00:05:52,180 --> 00:05:58,900
For volcanoes it is necessary,
but for GeoJSON you could have used map

50
00:05:58,900 --> 00:06:05,340
here and everything would be fine, but
for consistency it's good to add both to

51
00:06:05,340 --> 00:06:13,930
particular, to specific feature groups.
Volcanoes here, and population here, so this

52
00:06:13,930 --> 00:06:20,130
is the code, and I hope you enjoyed this
program, and feel free to ask questions.

53
00:06:20,130 --> 00:06:23,000
And I'll talk to you later on. See you!

