[No audio]
Hi again, in this lecture, I'll show you
how to make a country by population map.
I'll be showing population of each
country through graduated colors.
So we'll think about a range of colors to
represent population from low to high.
And we have that piece of information in
a world.json file, which is stored
in this POP2005 attribute. So POP2005
means population from 2005, which is
quite old, but it shouldn't be a
problem for educational purposes. So
let's get back to the code, and the way
to change the color of those polygons,
because by default, we have this green
color, and there's also transparency on the
polygons. You can see the
base map on the background, and so to change
that, you need to access the style
function argument. But I have missed
something here from the previous
lecture, we check could cause some
problems later. This doesn't have to be
in a round brackets, delete that.
[No audio]
One here as well.
[No audio]
So we have 1, 2, 3 open brackets
and 3 closing brackets. Now it
should be fine. So we need to add another
arguments to GeoJSON, this is the first
argument. So data is the first argument
which ends in here. So add there another
style_function argument, and you can
actually break the line in here. You are
free to break lines in Python, when the
expression is inside brackets, round
brackets, square, or curly brackets. So in
this case, this expression is inside
brackets, so we are able to break it
and it's going to break it at the comma.
So style_function equals to.
[No audio]
Now this expects a lambda function. Lambda
functions are just like functions,
normal functions, but they are written in
single lines of code. For instance,
let's say l equals to lambda. This is a
lambda function x, x a power of 2.
[No audio]
Sorry, I had a typo there. Not lamdba
but lambda, okay, another typo. All
right. So l is a function here, just
like you'd have a normal function, and
then you can call l, you can pass a
value like 5 and that will return
5 in power of 2.
[No audio]
So as you can see
you can write the function in one single
line and this can also be useful in
other cases like this one in here that
you'll see in just a bit. So lambda x.
[No audio]
This also allows you to write anonymous
functions. So this function now it
doesn't have a name. Like here, I
assigned a name to this function,
because I needed to call it later. But
this one here, we don't need to call it
later. So you don't need to give a name.
So here, the syntax would be correct if
I do just like that. So let's go ahead and do this.
[No audio]
fillColor with a capital C, lowercase f,
uppercase C, and that should be in quotes, let's say
[No audio]
yellow.
[No audio]
Now, if you want, you can leave it like that.
So save the script.
[No audio]
exit the python shell and execute the script.
[No audio]
The execution went fine. Let me check the map, reload
[No audio]
and you'll see that colors of the polygons change to yellow.
[No audio]
So that's how you can change the
attributes of your JSON file. So this
say basically you add another, basically
you add in these attributes,
which is not in the JSON file if you search for it.
[No audio]
You'll find nothing and save.
Now you can play around with this by adding
conditionals inside this dictionary,
such as if x
[No audio]
properties
[No audio]
POP2005
[No audio]
is less than, let's say 10 million.
So 10 1, 2, 3, 1, 2, 3.
So, what I'm doing here is
I'm saying that set yellow to fillColor
if x represent the feature,
the JSON feature here. So if the property
is actually at the POP2005 attribute
of this attribute, of this attribute, which is
[No audio]
here, this is POP2005. I know this is a bit complicated
[No audio]
to Toggle Soft Wrap, so now I have all the text
wraps here in the view. And here,
POP2005, it's actually part of this
[No audio]
dictionary, up to here. So this
dictionary, these are a set of
properties. This dictionary is the
value of this key of the properties key.
[No audio]
So, what that means is that, you know,
we are accessing the value of a properties
key using this syntax, which is known
in Python. Meanwhile, x properties or
that is, x here represents
features. So you are accessing
properties from features. And all
background, this is a bit of a blackbox.
But folium, what folium will do is it
will go through all the features of all
the polygons, and it will check if the
value of POP2005 is less than 10
million. And if it is, then it will set
this color to fillColor attributes.
That's more or less about it.
I think it's better to put this green, so if
it's less than 10 million, put it green, else,
[No audio]
orange, if between
[No audio]
10 million.
[No audio]
Do the same thing here.
Properties, actually, you can break the code in here.
[No audio]
So properties POP2005.
[No audio]
Let's put here 20 million.
[No audio]
So these are just arbitrary numbers, you can put
everything that you want, but that will
affect how your polygons are
classified are colored. So for
population between 10 million and 20
million, we'll get orange colors.
[No audio]
else red, so everything above the new will be
call it as a read. Okay, let me save
this and so I hope this will work.
[No audio]
No errors. Reload.
[No audio]
We have US color in red
because it has a population of higher
than 20 million people. And yeah, it
seems to be working fine.
[No audio]
Yep, Portugal here. Portugal as a population of
around 12 or 15 million people, I'm not
sure. So it's in the middle range of our
classification algorithm.
[No audio]
and it has more or less about it.
And I'll talk to you later. Thanks.
[No audio]