1 00:00:00,000 --> 00:00:02,070 Hello, in the previous lecture, you were 2 00:00:02,070 --> 00:00:04,470 able to build this map with multiple 3 00:00:04,470 --> 00:00:07,230 markers. This map happens to have only 4 00:00:07,230 --> 00:00:09,540 two markers, but you get the idea, and 5 00:00:09,540 --> 00:00:12,930 the script was this one here, so you can 6 00:00:12,955 --> 00:00:16,125 add as many coordinate pairs as you want in here. 7 00:00:16,893 --> 00:00:19,225 But in this lecture, I want to show you how to add 8 00:00:19,635 --> 00:00:22,287 pairs of coordinates out of a text file, 9 00:00:23,914 --> 00:00:26,704 which is this one here, a Volcanoes.txt. 10 00:00:26,705 --> 00:00:30,538 [No audio] 11 00:00:30,540 --> 00:00:33,270 If you like, you can just rename the name of this 12 00:00:33,480 --> 00:00:36,894 from Volcanoes.txt to Volcanoes.csv 13 00:00:36,964 --> 00:00:39,060 then you can open it in a 14 00:00:39,150 --> 00:00:41,850 program such as Excel, if you like, you 15 00:00:41,850 --> 00:00:44,700 can show it on a plain text file, as I'm 16 00:00:44,700 --> 00:00:48,330 doing in here. Python can read both CSV 17 00:00:48,330 --> 00:00:50,520 and text files. So what do we have here, here 18 00:00:50,520 --> 00:00:53,370 are some data about volcanoes. 19 00:00:53,735 --> 00:00:56,345 Basically, we have a number of the 20 00:00:56,370 --> 00:00:59,610 volcano, which is some sort of ID. So 21 00:00:59,610 --> 00:01:03,864 VOLCANX020, we have yet another NUMBER then. 22 00:01:03,865 --> 00:01:06,319 [No audio] 23 00:01:06,343 --> 00:01:09,702 And as you see every column, so we have columns 24 00:01:09,703 --> 00:01:12,660 in this data file, and every column is separated 25 00:01:12,660 --> 00:01:15,570 by a comma. This is the name of the 26 00:01:15,570 --> 00:01:17,940 first column, which is which the 27 00:01:17,940 --> 00:01:19,950 first value of this first column is this 28 00:01:19,950 --> 00:01:22,350 one here, then is the name of the second 29 00:01:22,350 --> 00:01:24,840 column. The first value of the second 30 00:01:24,840 --> 00:01:26,910 column is this one here, then the second 31 00:01:26,910 --> 00:01:29,940 value of the second column is this, and so 32 00:01:29,940 --> 00:01:31,860 no, and then we have the name column, 33 00:01:32,160 --> 00:01:37,200 the name of the volcano in here. Sorry, 34 00:01:37,530 --> 00:01:40,980 the name is here, Baker, and then the comma 35 00:01:40,980 --> 00:01:43,680 starts here, which means that 36 00:01:44,303 --> 00:01:47,903 location is the some near US-Washington, 37 00:01:48,620 --> 00:01:50,885 and then you have the STATUS, STATUS, 38 00:01:50,910 --> 00:01:53,130 Historical for the first volcano 39 00:01:54,060 --> 00:01:56,790 and then we have the elevation, which is 40 00:01:56,790 --> 00:01:59,490 this one in here. I'm not sure whether 41 00:01:59,490 --> 00:02:01,800 these are in feet or meters. But you 42 00:02:01,800 --> 00:02:03,960 can find that out. If you're curious, 43 00:02:03,995 --> 00:02:07,025 you can do a research for these particular 44 00:02:07,050 --> 00:02:09,240 volcano and compare the elevation. 45 00:02:09,510 --> 00:02:12,033 Anyway, then we have a TYPE 46 00:02:12,034 --> 00:02:14,972 [No audio] 47 00:02:14,997 --> 00:02:19,343 of the volcano, and we have a TIMEFRAME, 48 00:02:21,000 --> 00:02:23,100 which is D3, I'm not sure what 49 00:02:23,100 --> 00:02:27,750 this means a D4, and so, and then 50 00:02:27,750 --> 00:02:30,000 lastly, this is where we are interested 51 00:02:30,000 --> 00:02:33,120 about, we have the LAT and LON columns. 52 00:02:33,690 --> 00:02:36,660 So this is the lat which means latitude, and 53 00:02:36,660 --> 00:02:38,484 this is longitude of the volcano. 54 00:02:38,485 --> 00:02:40,669 [No audio] 55 00:02:40,671 --> 00:02:44,100 So you need these two numeric values in order 56 00:02:44,100 --> 00:02:48,240 to map the features of a volcanoes into 57 00:02:48,270 --> 00:02:50,910 a map, in this case into a folium.Map. 58 00:02:51,630 --> 00:02:55,410 So we have as around 63, actually 59 00:02:55,410 --> 00:03:00,510 62, 63, including the header of the data. 60 00:03:01,290 --> 00:03:05,070 Also how do we load this file into Python? 61 00:03:05,507 --> 00:03:07,758 Well, to load that file into Python, 62 00:03:09,219 --> 00:03:13,830 let me clear the terminal and you would 63 00:03:13,830 --> 00:03:16,050 first need to install a very useful 64 00:03:16,050 --> 00:03:19,920 library called pandas. We're going to 65 00:03:19,920 --> 00:03:21,780 use this library later on. So I'm not 66 00:03:21,780 --> 00:03:25,080 going to explain pandas in detail for 67 00:03:25,080 --> 00:03:27,870 now, because we were just using one or 68 00:03:27,870 --> 00:03:30,990 two of its functions, which I'll explain 69 00:03:30,990 --> 00:03:32,790 in this lecture. So go ahead and install 70 00:03:32,815 --> 00:03:34,385 pandas, if you don't have pandas, 71 00:03:34,938 --> 00:03:38,188 pip install pandas or pip3 install pandas, 72 00:03:38,462 --> 00:03:39,720 depending on how you have 73 00:03:39,720 --> 00:03:43,830 configured Python, and then you can go 74 00:03:43,830 --> 00:03:46,678 ahead and open a Python session. 75 00:03:47,311 --> 00:03:51,870 import pandas and the way to load a file with 76 00:03:51,870 --> 00:03:56,213 pandas is by pointing to pandas.read_csv 77 00:03:56,214 --> 00:03:58,465 [No audio] 78 00:03:58,467 --> 00:04:00,870 Volcanoes.txt, make sure 79 00:04:00,870 --> 00:04:04,078 that your Python session has been opened 80 00:04:04,080 --> 00:04:08,318 in the directory where your Volcanoes.txt 81 00:04:08,320 --> 00:04:11,756 file is. So for instance, my session is 82 00:04:11,757 --> 00:04:14,617 in the same mapping folder, which is this 83 00:04:14,618 --> 00:04:16,335 one here. So I can just pass the file name. 84 00:04:16,336 --> 00:04:18,826 [No audio] 85 00:04:18,828 --> 00:04:21,296 Otherwise, you may have to pass absolute path 86 00:04:21,297 --> 00:04:26,280 of the file which starts from like C// 87 00:04:26,280 --> 00:04:28,200 and so on. Depending on what operating 88 00:04:28,200 --> 00:04:31,068 system you're in, and execute that 89 00:04:32,870 --> 00:04:35,591 and call data and you get the data printed out. 90 00:04:36,691 --> 00:04:37,705 Just like that. 91 00:04:37,707 --> 00:04:40,289 [No audio] 92 00:04:40,291 --> 00:04:43,717 And so pandas is able, actually the read_csv method 93 00:04:43,718 --> 00:04:49,140 is able to distinguish between these commas where 94 00:04:49,140 --> 00:04:52,200 the commas are, and so it creates a well 95 00:04:52,200 --> 00:04:57,570 structured data format with columns. So 96 00:04:57,600 --> 00:05:00,900 if you go down here, so you have some 97 00:05:00,900 --> 00:05:02,970 columns here, column, column, column, and 98 00:05:02,970 --> 00:05:07,860 then up to the end, and then you have other 99 00:05:07,860 --> 00:05:10,350 columns, because Python cannot print 100 00:05:10,350 --> 00:05:13,440 out. Actually, the terminal doesn't 101 00:05:13,890 --> 00:05:16,140 allow you to have all the columns in one, 102 00:05:16,470 --> 00:05:18,990 inside all of this area here, because it'd be 103 00:05:18,990 --> 00:05:21,060 too much. Anyway, you have STATUS, 104 00:05:21,090 --> 00:05:23,668 ELEVATION, TYPE, TIMEFRAME, LAT, 105 00:05:23,670 --> 00:05:25,679 [No audio] 106 00:05:25,680 --> 00:05:30,030 and LON. So that's about the data. 107 00:05:30,600 --> 00:05:33,270 Now you need to figure out a way to 108 00:05:33,300 --> 00:05:38,100 iterate through that dataframe. So this 109 00:05:38,100 --> 00:05:39,379 is called a dataframe. 110 00:05:39,380 --> 00:05:42,247 [No audio] 111 00:05:42,249 --> 00:05:45,156 So the data object is called a dataframe, 112 00:05:45,157 --> 00:05:47,491 you can check its type, actually, 113 00:05:47,492 --> 00:05:50,774 [No audio] 114 00:05:50,776 --> 00:05:53,115 pandas.core.frame.DataFrame. 115 00:05:54,881 --> 00:05:59,656 So what I'm thinking of is to actually create 116 00:05:59,657 --> 00:06:05,370 two lists out of this dataframe columns. So to put 117 00:06:05,460 --> 00:06:08,076 the latitude column in a Python list, 118 00:06:08,295 --> 00:06:10,020 and the longitude column in another 119 00:06:10,020 --> 00:06:13,200 Python list, and so that we have a native 120 00:06:13,200 --> 00:06:15,300 Python object, which is a list, and then 121 00:06:15,300 --> 00:06:17,130 we can iterate through that list using 122 00:06:17,130 --> 00:06:21,060 the for loop. So let me try to do that. 123 00:06:21,061 --> 00:06:23,206 [No audio] 124 00:06:23,208 --> 00:06:24,208 import 125 00:06:24,210 --> 00:06:26,900 [Author typing] 126 00:06:26,901 --> 00:06:27,901 pandas 127 00:06:27,902 --> 00:06:30,051 [No audio] 128 00:06:30,053 --> 00:06:32,850 and then create the dataframe 129 00:06:32,875 --> 00:06:38,515 object. So loading the data, read_csv 130 00:06:40,320 --> 00:06:43,650 Volcanoes.txt and yeah, that's it. 131 00:06:44,460 --> 00:06:47,610 So how do we convert a dataframe column 132 00:06:47,612 --> 00:06:49,802 into list. Well, the way to do that is 133 00:06:50,250 --> 00:06:54,090 by just doing, let's say, l or lat 134 00:06:54,276 --> 00:06:58,235 equals to data and then the name of 135 00:06:58,260 --> 00:07:00,006 the column, which is lat. 136 00:07:00,007 --> 00:07:02,836 [No audio] 137 00:07:02,838 --> 00:07:05,177 So you can get a list of columns, by the way, 138 00:07:05,271 --> 00:07:07,839 by doing data.columns. 139 00:07:07,840 --> 00:07:11,434 [No audio] 140 00:07:11,436 --> 00:07:13,971 Here, you see you have a LAT and a LON column, 141 00:07:14,775 --> 00:07:20,916 then you do lat equals to list and data 142 00:07:20,941 --> 00:07:22,941 [No audio] 143 00:07:22,943 --> 00:07:23,736 LON 144 00:07:23,738 --> 00:07:25,738 [No audio] 145 00:07:25,740 --> 00:07:30,810 in square brackets, close it, and so 146 00:07:30,810 --> 00:07:33,750 what you did here is data with a LON 147 00:07:33,780 --> 00:07:36,930 attribute there, will return a series 148 00:07:36,960 --> 00:07:39,600 object, but that series object, we are 149 00:07:39,600 --> 00:07:42,360 converting it into a native list. So 150 00:07:42,360 --> 00:07:44,057 that will be a Python list. 151 00:07:44,058 --> 00:07:47,606 [No audio] 152 00:07:47,608 --> 00:07:51,497 This will also show up as a similar object. 153 00:07:51,498 --> 00:07:53,548 [No audio] 154 00:07:53,550 --> 00:07:57,990 So this is a series object and this is 155 00:07:57,990 --> 00:08:00,150 a list. But I prefer to work on a list 156 00:08:00,175 --> 00:08:02,305 because working on lists is faster 157 00:08:03,420 --> 00:08:06,750 than working on dataframe series. So the 158 00:08:06,766 --> 00:08:09,327 idea is to have two lists here. 159 00:08:09,352 --> 00:08:11,357 [No audio] 160 00:08:11,381 --> 00:08:18,270 lat equals to list data LON, copy that 161 00:08:18,750 --> 00:08:21,960 paste it here, and the same thing goes for 162 00:08:23,220 --> 00:08:26,556 longitude. So lon, lat and lon list. 163 00:08:26,588 --> 00:08:31,499 [No audio] 164 00:08:31,500 --> 00:08:33,270 So now we need to be careful here 165 00:08:33,450 --> 00:08:35,640 on how we iterate through these two 166 00:08:35,640 --> 00:08:39,420 lists. Because you know, what we have is 167 00:08:39,480 --> 00:08:41,730 we have two lists, and each of them has 168 00:08:41,760 --> 00:08:45,150 62 items, you can check that using 169 00:08:45,175 --> 00:08:47,867 len here, down here, lat. 170 00:08:49,436 --> 00:08:52,980 So it has 62 items, and the same goes for the 171 00:08:53,040 --> 00:08:56,550 longitude list. That means that the 172 00:08:56,550 --> 00:08:59,250 first item of the latitude list 173 00:08:59,490 --> 00:09:01,410 corresponds to the second item of the 174 00:09:01,410 --> 00:09:03,630 latitude list, sorry, of the longitude 175 00:09:03,630 --> 00:09:06,570 list. So the first two items make up the 176 00:09:06,570 --> 00:09:10,740 first location of the first marker. That 177 00:09:10,740 --> 00:09:13,590 means a lat, in the first iteration, we 178 00:09:13,590 --> 00:09:16,530 need to extract both locations. So 179 00:09:16,530 --> 00:09:19,350 actually, this sort of structure won't 180 00:09:19,560 --> 00:09:21,930 be appropriate anymore. What you need to 181 00:09:21,930 --> 00:09:24,390 do is you need to go through, let's say 182 00:09:24,420 --> 00:09:29,310 lat and lon variables in, when you 183 00:09:29,310 --> 00:09:31,920 iterate through two lists at the same 184 00:09:31,920 --> 00:09:35,820 time, you need to use the zip function, and 185 00:09:35,820 --> 00:09:38,790 then goes lat and lon. So basically what this 186 00:09:38,790 --> 00:09:41,688 function does is like this, and let's say 187 00:09:41,712 --> 00:09:45,555 for i, j in zip. 188 00:09:46,597 --> 00:09:48,451 Let's say the first list is 1, 2, 3, 189 00:09:48,452 --> 00:09:52,880 [No audio] 190 00:09:52,881 --> 00:09:55,444 and the second is 4, 5, 6. 191 00:09:55,445 --> 00:09:59,787 [Author typing] 192 00:09:59,788 --> 00:10:06,209 print i and j. 193 00:10:06,210 --> 00:10:08,910 [Author typing] 194 00:10:08,912 --> 00:10:11,730 So this is what happens, this loop 195 00:10:11,760 --> 00:10:15,390 will go through this to lists at the same 196 00:10:15,390 --> 00:10:17,790 time. So what happens is that I will go 197 00:10:17,790 --> 00:10:20,040 through the first item, or the first 198 00:10:20,040 --> 00:10:22,920 list, while j will go through the first 199 00:10:22,920 --> 00:10:26,190 item of the second list, and so in the first 200 00:10:26,190 --> 00:10:30,150 iteration 1 and 4 are extracted, 201 00:10:30,600 --> 00:10:33,104 so, 1 here and 4 here. 202 00:10:33,105 --> 00:10:35,660 [No audio] 203 00:10:35,662 --> 00:10:38,409 And you can do such an extraction by using 204 00:10:38,410 --> 00:10:41,040 the zip function. Otherwise, you won't be able 205 00:10:41,040 --> 00:10:43,980 to do that if you don't use the zip 206 00:10:43,980 --> 00:10:46,740 function. So, zip sort of distributes 207 00:10:46,770 --> 00:10:49,358 the items one by one, and so the same goes here, 208 00:10:51,036 --> 00:10:54,425 lt will get the first item of this 209 00:10:54,450 --> 00:10:56,400 list, lon will get the first item 210 00:10:56,400 --> 00:10:58,770 of this list and then here, what you need 211 00:10:58,770 --> 00:11:03,330 to do is in the constructor list with lt 212 00:11:03,390 --> 00:11:09,690 and ln, and that's it, save the script, go 213 00:11:09,720 --> 00:11:14,610 to the terminal to execute. Let's type here 214 00:11:14,610 --> 00:11:16,770 in the terminal, I'll just exit that. 215 00:11:18,300 --> 00:11:22,818 python3 map1.py execute. 216 00:11:23,811 --> 00:11:27,870 No errors, go to Firefox and reload. 217 00:11:27,871 --> 00:11:33,538 [No audio] 218 00:11:33,540 --> 00:11:36,150 And I have got no markers for some reason but 219 00:11:36,150 --> 00:11:38,700 I didn't get an error. So this is one of 220 00:11:38,700 --> 00:11:40,890 those scenarios where you don't get an 221 00:11:40,890 --> 00:11:42,960 error in the terminal. So in that case, 222 00:11:42,960 --> 00:11:44,970 you need to double check your code 223 00:11:45,750 --> 00:11:51,720 carefully. So data pandas.read_csv, lat 224 00:11:51,750 --> 00:11:56,130 list data LON here is there. So what's 225 00:11:56,130 --> 00:11:59,760 happening is that I'm assigning the 226 00:11:59,760 --> 00:12:04,170 wrong column to the latitude list. 227 00:12:04,980 --> 00:12:08,958 So let me change the order and try again. 228 00:12:10,294 --> 00:12:12,570 So don't get intimidated by 229 00:12:12,570 --> 00:12:15,000 errors, errors happen all the time. You 230 00:12:15,000 --> 00:12:18,540 just need to be cold blooded and read 231 00:12:18,540 --> 00:12:20,070 the errors if you get any error here, 232 00:12:20,310 --> 00:12:22,424 and also look at the code carefully. 233 00:12:22,425 --> 00:12:24,688 [No audio] 234 00:12:24,690 --> 00:12:28,124 Let me reload. I've got some markers now. 235 00:12:28,125 --> 00:12:30,406 [No audio] 236 00:12:30,408 --> 00:12:33,390 So these are the locations of volcanoes 237 00:12:33,810 --> 00:12:39,134 in North in US actually, United States, okay. 238 00:12:39,135 --> 00:12:42,636 [No audio] 239 00:12:42,638 --> 00:12:44,910 They also have this popup, which 240 00:12:44,910 --> 00:12:47,400 is working well, but I assume you don't 241 00:12:47,400 --> 00:12:48,810 like it very much because it's not 242 00:12:48,810 --> 00:12:52,170 showing any information. So what we need 243 00:12:52,200 --> 00:12:55,465 to do here is to actually make this popup 244 00:12:55,467 --> 00:12:57,810 dynamic so that it shows some actual 245 00:12:57,810 --> 00:13:00,388 information, and I'll show you how to 246 00:13:00,390 --> 00:13:02,399 [No audio] 247 00:13:02,400 --> 00:13:05,100 create dynamic popups even next video. 248 00:13:05,790 --> 00:13:07,008 Talk to you there.