1 00:00:00,000 --> 00:00:02,218 [No audio] 2 00:00:02,220 --> 00:00:04,320 Hi again, in this lecture, I'll show you 3 00:00:04,320 --> 00:00:07,650 how to make a country by population map. 4 00:00:08,195 --> 00:00:10,230 I'll be showing population of each 5 00:00:10,230 --> 00:00:12,472 country through graduated colors. 6 00:00:14,276 --> 00:00:17,340 So we'll think about a range of colors to 7 00:00:17,340 --> 00:00:20,280 represent population from low to high. 8 00:00:21,360 --> 00:00:24,270 And we have that piece of information in 9 00:00:24,330 --> 00:00:28,470 a world.json file, which is stored 10 00:00:28,500 --> 00:00:34,680 in this POP2005 attribute. So POP2005 11 00:00:34,680 --> 00:00:37,800 means population from 2005, which is 12 00:00:37,800 --> 00:00:40,170 quite old, but it shouldn't be a 13 00:00:40,200 --> 00:00:44,490 problem for educational purposes. So 14 00:00:44,490 --> 00:00:47,610 let's get back to the code, and the way 15 00:00:47,610 --> 00:00:51,480 to change the color of those polygons, 16 00:00:51,480 --> 00:00:54,120 because by default, we have this green 17 00:00:54,120 --> 00:00:58,320 color, and there's also transparency on the 18 00:00:58,320 --> 00:01:01,710 polygons. You can see the 19 00:01:01,710 --> 00:01:05,520 base map on the background, and so to change 20 00:01:05,520 --> 00:01:08,880 that, you need to access the style 21 00:01:08,880 --> 00:01:12,030 function argument. But I have missed 22 00:01:12,030 --> 00:01:13,200 something here from the previous 23 00:01:13,200 --> 00:01:15,810 lecture, we check could cause some 24 00:01:15,810 --> 00:01:18,900 problems later. This doesn't have to be 25 00:01:18,900 --> 00:01:21,485 in a round brackets, delete that. 26 00:01:21,486 --> 00:01:24,452 [No audio] 27 00:01:24,454 --> 00:01:25,484 One here as well. 28 00:01:25,485 --> 00:01:27,728 [No audio] 29 00:01:27,730 --> 00:01:31,260 So we have 1, 2, 3 open brackets 30 00:01:31,740 --> 00:01:33,660 and 3 closing brackets. Now it 31 00:01:33,695 --> 00:01:36,695 should be fine. So we need to add another 32 00:01:36,720 --> 00:01:39,570 arguments to GeoJSON, this is the first 33 00:01:39,570 --> 00:01:41,760 argument. So data is the first argument 34 00:01:41,796 --> 00:01:45,936 which ends in here. So add there another 35 00:01:46,410 --> 00:01:51,570 style_function argument, and you can 36 00:01:51,570 --> 00:01:55,080 actually break the line in here. You are 37 00:01:55,080 --> 00:01:59,580 free to break lines in Python, when the 38 00:01:59,580 --> 00:02:02,820 expression is inside brackets, round 39 00:02:02,820 --> 00:02:06,840 brackets, square, or curly brackets. So in 40 00:02:06,840 --> 00:02:08,640 this case, this expression is inside 41 00:02:08,640 --> 00:02:11,310 brackets, so we are able to break it 42 00:02:11,700 --> 00:02:13,440 and it's going to break it at the comma. 43 00:02:14,490 --> 00:02:16,351 So style_function equals to. 44 00:02:16,353 --> 00:02:18,533 [No audio] 45 00:02:18,535 --> 00:02:22,710 Now this expects a lambda function. Lambda 46 00:02:22,710 --> 00:02:25,350 functions are just like functions, 47 00:02:25,800 --> 00:02:30,240 normal functions, but they are written in 48 00:02:30,240 --> 00:02:32,910 single lines of code. For instance, 49 00:02:33,480 --> 00:02:36,180 let's say l equals to lambda. This is a 50 00:02:36,180 --> 00:02:40,636 lambda function x, x a power of 2. 51 00:02:40,638 --> 00:02:42,659 [No audio] 52 00:02:42,660 --> 00:02:45,263 Sorry, I had a typo there. Not lamdba 53 00:02:45,265 --> 00:02:51,300 but lambda, okay, another typo. All 54 00:02:51,300 --> 00:02:54,360 right. So l is a function here, just 55 00:02:54,360 --> 00:02:57,720 like you'd have a normal function, and 56 00:02:57,720 --> 00:03:00,810 then you can call l, you can pass a 57 00:03:00,810 --> 00:03:04,110 value like 5 and that will return 58 00:03:04,304 --> 00:03:05,653 5 in power of 2. 59 00:03:05,678 --> 00:03:07,678 [No audio] 60 00:03:07,703 --> 00:03:08,730 So as you can see 61 00:03:08,730 --> 00:03:10,620 you can write the function in one single 62 00:03:10,620 --> 00:03:13,620 line and this can also be useful in 63 00:03:13,620 --> 00:03:15,540 other cases like this one in here that 64 00:03:15,540 --> 00:03:21,396 you'll see in just a bit. So lambda x. 65 00:03:21,421 --> 00:03:23,878 [No audio] 66 00:03:23,880 --> 00:03:26,160 This also allows you to write anonymous 67 00:03:26,160 --> 00:03:27,960 functions. So this function now it 68 00:03:27,960 --> 00:03:30,990 doesn't have a name. Like here, I 69 00:03:31,320 --> 00:03:33,270 assigned a name to this function, 70 00:03:34,560 --> 00:03:36,720 because I needed to call it later. But 71 00:03:36,720 --> 00:03:38,730 this one here, we don't need to call it 72 00:03:38,755 --> 00:03:41,635 later. So you don't need to give a name. 73 00:03:42,450 --> 00:03:46,860 So here, the syntax would be correct if 74 00:03:46,860 --> 00:03:50,881 I do just like that. So let's go ahead and do this. 75 00:03:50,882 --> 00:03:54,500 [No audio] 76 00:03:54,502 --> 00:03:58,898 fillColor with a capital C, lowercase f, 77 00:03:58,991 --> 00:04:05,109 uppercase C, and that should be in quotes, let's say 78 00:04:05,110 --> 00:04:08,532 [No audio] 79 00:04:08,534 --> 00:04:09,534 yellow. 80 00:04:09,536 --> 00:04:11,536 [No audio] 81 00:04:11,538 --> 00:04:13,407 Now, if you want, you can leave it like that. 82 00:04:14,370 --> 00:04:15,842 So save the script. 83 00:04:15,843 --> 00:04:20,809 [No audio] 84 00:04:20,811 --> 00:04:23,880 exit the python shell and execute the script. 85 00:04:23,881 --> 00:04:29,488 [No audio] 86 00:04:29,490 --> 00:04:32,996 The execution went fine. Let me check the map, reload 87 00:04:32,997 --> 00:04:35,199 [No audio] 88 00:04:35,201 --> 00:04:38,850 and you'll see that colors of the polygons change to yellow. 89 00:04:38,852 --> 00:04:41,098 [No audio] 90 00:04:41,100 --> 00:04:42,600 So that's how you can change the 91 00:04:42,600 --> 00:04:46,230 attributes of your JSON file. So this 92 00:04:46,230 --> 00:04:49,530 say basically you add another, basically 93 00:04:49,560 --> 00:04:51,333 you add in these attributes, 94 00:04:53,140 --> 00:04:56,242 which is not in the JSON file if you search for it. 95 00:04:56,243 --> 00:04:59,248 [No audio] 96 00:04:59,273 --> 00:05:01,796 You'll find nothing and save. 97 00:05:02,890 --> 00:05:05,580 Now you can play around with this by adding 98 00:05:05,580 --> 00:05:07,950 conditionals inside this dictionary, 99 00:05:09,210 --> 00:05:11,831 such as if x 100 00:05:11,832 --> 00:05:14,554 [No audio] 101 00:05:14,556 --> 00:05:15,556 properties 102 00:05:15,558 --> 00:05:17,952 [No audio] 103 00:05:17,953 --> 00:05:20,455 POP2005 104 00:05:20,456 --> 00:05:23,586 [No audio] 105 00:05:23,588 --> 00:05:27,154 is less than, let's say 10 million. 106 00:05:27,638 --> 00:05:30,868 So 10 1, 2, 3, 1, 2, 3. 107 00:05:32,206 --> 00:05:35,370 So, what I'm doing here is 108 00:05:35,370 --> 00:05:39,296 I'm saying that set yellow to fillColor 109 00:05:39,882 --> 00:05:42,840 if x represent the feature, 110 00:05:43,110 --> 00:05:46,740 the JSON feature here. So if the property 111 00:05:46,740 --> 00:05:51,210 is actually at the POP2005 attribute 112 00:05:51,240 --> 00:05:54,973 of this attribute, of this attribute, which is 113 00:05:54,975 --> 00:05:57,088 [No audio] 114 00:05:57,090 --> 00:06:03,069 here, this is POP2005. I know this is a bit complicated 115 00:06:03,070 --> 00:06:05,399 [No audio] 116 00:06:05,401 --> 00:06:10,920 to Toggle Soft Wrap, so now I have all the text 117 00:06:10,950 --> 00:06:15,148 wraps here in the view. And here, 118 00:06:15,150 --> 00:06:20,340 POP2005, it's actually part of this 119 00:06:20,341 --> 00:06:24,208 [No audio] 120 00:06:24,210 --> 00:06:27,720 dictionary, up to here. So this 121 00:06:27,750 --> 00:06:29,580 dictionary, these are a set of 122 00:06:29,580 --> 00:06:33,240 properties. This dictionary is the 123 00:06:33,240 --> 00:06:37,680 value of this key of the properties key. 124 00:06:37,681 --> 00:06:40,138 [No audio] 125 00:06:40,140 --> 00:06:42,450 So, what that means is that, you know, 126 00:06:42,450 --> 00:06:45,473 we are accessing the value of a properties 127 00:06:45,475 --> 00:06:48,150 key using this syntax, which is known 128 00:06:48,150 --> 00:06:54,030 in Python. Meanwhile, x properties or 129 00:06:54,030 --> 00:06:57,360 that is, x here represents 130 00:06:57,390 --> 00:07:01,980 features. So you are accessing 131 00:07:02,160 --> 00:07:06,330 properties from features. And all 132 00:07:06,360 --> 00:07:09,327 background, this is a bit of a blackbox. 133 00:07:10,702 --> 00:07:12,990 But folium, what folium will do is it 134 00:07:12,990 --> 00:07:14,790 will go through all the features of all 135 00:07:14,790 --> 00:07:17,820 the polygons, and it will check if the 136 00:07:17,820 --> 00:07:20,880 value of POP2005 is less than 10 137 00:07:20,880 --> 00:07:23,820 million. And if it is, then it will set 138 00:07:23,850 --> 00:07:27,030 this color to fillColor attributes. 139 00:07:28,205 --> 00:07:30,012 That's more or less about it. 140 00:07:30,700 --> 00:07:33,203 I think it's better to put this green, so if 141 00:07:33,204 --> 00:07:38,775 it's less than 10 million, put it green, else, 142 00:07:38,777 --> 00:07:41,981 [No audio] 143 00:07:41,983 --> 00:07:46,867 orange, if between 144 00:07:46,868 --> 00:07:48,884 [No audio] 145 00:07:48,886 --> 00:07:49,886 10 million. 146 00:07:49,887 --> 00:07:52,508 [No audio] 147 00:07:52,510 --> 00:07:53,674 Do the same thing here. 148 00:07:54,510 --> 00:07:59,253 Properties, actually, you can break the code in here. 149 00:07:59,254 --> 00:08:01,876 [No audio] 150 00:08:01,878 --> 00:08:06,600 So properties POP2005. 151 00:08:06,601 --> 00:08:09,988 [No audio] 152 00:08:09,990 --> 00:08:12,517 Let's put here 20 million. 153 00:08:12,518 --> 00:08:16,087 [No audio] 154 00:08:16,089 --> 00:08:18,750 So these are just arbitrary numbers, you can put 155 00:08:18,750 --> 00:08:20,730 everything that you want, but that will 156 00:08:20,730 --> 00:08:23,970 affect how your polygons are 157 00:08:23,970 --> 00:08:26,370 classified are colored. So for 158 00:08:26,370 --> 00:08:28,590 population between 10 million and 20 159 00:08:28,592 --> 00:08:31,062 million, we'll get orange colors. 160 00:08:31,063 --> 00:08:33,221 [No audio] 161 00:08:33,223 --> 00:08:37,770 else red, so everything above the new will be 162 00:08:37,770 --> 00:08:43,200 call it as a read. Okay, let me save 163 00:08:43,200 --> 00:08:45,962 this and so I hope this will work. 164 00:08:45,963 --> 00:08:49,408 [No audio] 165 00:08:49,410 --> 00:08:52,653 No errors. Reload. 166 00:08:52,654 --> 00:08:56,475 [No audio] 167 00:08:56,477 --> 00:08:58,558 We have US color in red 168 00:08:58,560 --> 00:09:00,990 because it has a population of higher 169 00:09:00,990 --> 00:09:04,830 than 20 million people. And yeah, it 170 00:09:04,830 --> 00:09:06,270 seems to be working fine. 171 00:09:06,271 --> 00:09:12,238 [No audio] 172 00:09:12,240 --> 00:09:14,850 Yep, Portugal here. Portugal as a population of 173 00:09:14,850 --> 00:09:18,090 around 12 or 15 million people, I'm not 174 00:09:18,090 --> 00:09:21,390 sure. So it's in the middle range of our 175 00:09:21,390 --> 00:09:23,062 classification algorithm. 176 00:09:23,063 --> 00:09:25,251 [No audio] 177 00:09:25,253 --> 00:09:26,937 and it has more or less about it. 178 00:09:26,938 --> 00:09:28,950 And I'll talk to you later. Thanks. 179 00:09:28,951 --> 00:09:31,328 [No audio]