1
00:00:01,429 --> 00:00:07,470
Hi again! In this lecture I'll show you
how to make a country by population map.

2
00:00:07,470 --> 00:00:14,519
I will be showing population of each
country through graduated colors so

3
00:00:14,519 --> 00:00:19,920
we'll think about a range of colors to
represent population from low to high.

4
00:00:19,920 --> 00:00:28,650
And we have that piece of information in
our world.json file which is stored in

5
00:00:28,650 --> 00:00:37,770
this pop 2005 attribute, so pop 2005
means population from 2005 which is

6
00:00:37,770 --> 00:00:44,460
quite old, but it shouldn't need
problem for educational purposes.

7
00:00:44,460 --> 00:00:51,420
So let's get back to the code and the way
to change the color of those polygons

8
00:00:51,420 --> 00:00:58,350
because by default we have this green
color and there's also a transparency on the

9
00:00:58,350 --> 00:01:05,129
polygons, you can see, you can see
the base map on the background and so to

10
00:01:05,129 --> 00:01:12,000
change that you need to access the style
function argument, but I have messed

11
00:01:12,000 --> 00:01:16,790
something here from a previous lecture
which could cause some problems later.

12
00:01:16,790 --> 00:01:26,960
This doesn't have to be in round
brackets so delete that, and one here as well.

13
00:01:26,960 --> 00:01:35,610
So we have 1, 2, 3 open brackets and 3
closing brackets. Now it should be fun.

14
00:01:35,610 --> 00:01:40,470
We need to add another argument to GeoJSON.
This is the first argument, so data

15
00:01:40,470 --> 00:01:48,740
is the first argument which ends in here.
So add there another style function

16
00:01:48,740 --> 00:01:56,399
augment and you can actually break the
line in here. You are free to break

17
00:01:56,399 --> 00:02:03,990
lines in python when the expression is
inside brackets, round brackets, square,

18
00:02:03,990 --> 00:02:09,780
or curly brackets. In this case this
expression is inside brackets so we are

19
00:02:09,780 --> 00:02:13,760
able to break it and it's good to break
it at the comma.

20
00:02:13,760 --> 00:02:21,590
So style function equals to…
Now this expects a lambda function.

21
00:02:21,590 --> 00:02:29,790
Lambda functions are just like functions,
normal functions, but they are written in

22
00:02:29,790 --> 00:02:36,390
single lines of code. For instance let's
say l equals lambda, this is the lambda

23
00:02:36,390 --> 00:02:48,500
function, X, X in power of 2, sorry I had
a typo thee not landba, but lambda, okay.

24
00:02:48,500 --> 00:02:55,380
Another typo. All right.
So L is a function here just like you'd

25
00:02:55,380 --> 00:03:01,950
have a normal function and then you can
call L, you can pass a value like five,

26
00:03:01,950 --> 00:03:09,180
and that will return five in power of 2.
As you can see you can write the

27
00:03:09,180 --> 00:03:14,670
function in one single line and this can
also be useful in other cases like this

28
00:03:14,670 --> 00:03:25,170
one in here that you'll see in just a bit.
So lambda X. This also allows you to

29
00:03:25,170 --> 00:03:29,300
write anonymous functions so this
function now it doesn't have a name like

30
00:03:29,300 --> 00:03:36,720
here I assigned a name to this function
because I needed to call it later, but

31
00:03:36,720 --> 00:03:41,690
this one here we don't need to call it
later so you don't need to give a name.

32
00:03:41,690 --> 00:03:48,620
So here the syntax would be correct
if I do just like that.

33
00:03:48,620 --> 00:03:59,820
So let's go ahead and do this, fillColor
with a capital C lowercase f, uppercase C

34
00:03:59,820 --> 00:04:06,950
and that would be in quotes.
Let's say yellow.

35
00:04:11,270 --> 00:04:21,739
Now if you want you can leave it like
that, so save the script, exit the final

36
00:04:21,739 --> 00:04:31,040
shell, and execute the script.
The execution went fine.

37
00:04:31,040 --> 00:04:38,060
Let me check the map, reload and you'll
see that the colors of the polygons changed

38
00:04:38,060 --> 00:04:46,190
to yellow, so that's how you can change
the attributes of your JSON file so to say.

39
00:04:46,190 --> 00:04:52,330
Basically you add another,
Basically you add in this attribute

40
00:04:52,330 --> 00:05:03,110
which is not in the JSON file. If you
search for it, you'll find nothing and so now

41
00:05:03,110 --> 00:05:09,470
you can play around with this by adding
conditionals inside this dictionary, such

42
00:05:09,470 --> 00:05:31,390
as if x properties POP2005 is less than,
let's say 10 million, so 10 1, 2, 3, 1, 2, 3.

43
00:05:31,390 --> 00:05:41,120
So what I'm doing here is I'm saying
that set yellow to fill color if X

44
00:05:41,120 --> 00:05:47,420
represents the feature, a JSON feature
here, so if the properties, actually the

45
00:05:47,420 --> 00:05:59,000
pop 2005 attribute of this attribute of
this attribute which is here, this is

46
00:05:59,000 --> 00:06:09,620
pop 2005, I know this is a bit complicated.
Let me Toggle Soft Wrap so

47
00:06:09,620 --> 00:06:18,470
now I have all the text wraped here in
the view and here pop 2005

48
00:06:18,470 --> 00:06:22,030
is actually part of this
dictionary up to here, so this dictionary,

49
00:06:28,100 --> 00:06:35,260
these are a set of properties, this
dictionary is the value of this key

50
00:06:35,260 --> 00:06:42,610
of the properties key.
So what that means is that you know we

51
00:06:42,610 --> 00:06:48,040
are accessing the value of the properties
key using this syntax which is known

52
00:06:48,040 --> 00:06:58,830
in Python, meanwhile X properties, what that
is, is this is X here represents features,

53
00:06:58,830 --> 00:07:07,330
so you are at your accessing properties
from features and on the background,

54
00:07:07,330 --> 00:07:12,880
and this is a bit of a black box but
Folium what Folium will do, is it

55
00:07:12,880 --> 00:07:17,680
will go through all the features of all
the polygons and it will check if the

56
00:07:17,680 --> 00:07:23,680
value of pop 2005 is less than 10
million and if it is, then it will set

57
00:07:23,680 --> 00:07:30,100
this yellow, this color to fill color
attribute. That's more or less about it.

58
00:07:30,100 --> 00:07:35,860
I think it's better to put this green,
so if it's less than 10 million, put it

59
00:07:35,860 --> 00:07:51,570
green, else orange if between 10 million
do the same thing here properties,

60
00:07:55,150 --> 00:08:11,110
actually you can break the code in here.
So properties pop 2005, and let's put here

61
00:08:11,110 --> 00:08:19,180
20 million, so these are just
arbitrary numbers. You can put everything

62
00:08:19,180 --> 00:08:24,850
that you want but that will affect how
your polygons are classified, are

63
00:08:24,850 --> 00:08:30,100
colored so for population between 10
million and 20 million, we will get orange

64
00:08:30,100 --> 00:08:41,380
colors, else red, so everything about
10 million will be colored as a red. Okay,

65
00:08:41,380 --> 00:08:47,760
let me save this
and I hope this will work!

66
00:08:48,870 --> 00:09:00,220
No errors. Reload. We have US colored in
red because it has a population of

67
00:09:00,220 --> 00:09:13,000
higher than 20 million people, and yeah
this seems to be working fine. Portugal.

68
00:09:13,000 --> 00:09:17,860
Yeah. Portugal has a population of
around 12 or 15 million people. I'm not

69
00:09:17,860 --> 00:09:25,630
sure. So it's in the middle range of our
classification algorithm and yeah, that's

70
00:09:25,630 --> 00:09:31,060
more or less about it, and if you like
you can ask questions I'll be happy to

71
00:09:31,060 --> 00:09:34,000
help you and I'll talk to you later.
Thanks!

