{"id":1390,"date":"2022-05-04T16:10:08","date_gmt":"2022-05-04T16:10:08","guid":{"rendered":"https:\/\/marcel-jan.eu\/datablog\/?p=1390"},"modified":"2022-05-25T18:33:44","modified_gmt":"2022-05-25T18:33:44","slug":"making-my-video-location-map-even-better-with-folium","status":"publish","type":"post","link":"https:\/\/marcel-jan.eu\/datablog\/2022\/05\/04\/making-my-video-location-map-even-better-with-folium\/","title":{"rendered":"Making my video location map even better with Folium"},"content":{"rendered":"<p>Yesterday I<a href=\"https:\/\/marcel-jan.eu\/datablog\/2022\/05\/03\/plotting-video-locations-from-my-sony-camera-in-python\/\"> shared how I plotted locations of videos shot with my Sony FDR-X3000<\/a> camera on a map. I was already pretty happy. Then I got a tip from Twitter user Bob Haffner (<a href=\"https:\/\/twitter.com\/bobhaffner\">@bobhaffner<\/a>): why not use Folium to create my maps?<\/p>\n<p>Huh? I already got a working map now, didn&#8217;t I? Well creating a map with matplotlib is a bit of a hassle. You&#8217;ve got to download a base map from <a href=\"https:\/\/www.openstreetmap.org\">Openstreetmap.org<\/a>. And if it&#8217;s too big, like the map of all my rides in the Vercors and Dr\u00f4me (France) last year, you might not get it.<\/p>\n<p>&nbsp;<\/p>\n<h1>Folium<\/h1>\n<p>A quick look <a href=\"https:\/\/www.analyticsvidhya.com\/blog\/2020\/06\/guide-geospatial-analysis-folium-python\/\">at blogs about Folium<\/a> tell me you don&#8217;t need to download a map. It even does zoomable maps. Okay. Wasn&#8217;t exactly looking for that. But sounds great.<\/p>\n<p>And I do like the markers you can create. Even with popup texts. Yes please!<\/p>\n<p>&nbsp;<\/p>\n<h1>Changing the code<\/h1>\n<p>Of course, the new version of this code is on Github in my repo: <a href=\"https:\/\/github.com\/Marcel-Jan\/media_gpsplot\">https:\/\/github.com\/Marcel-Jan\/media_gpsplot<\/a>.<\/p>\n<p>First of all we need to import Folium:<\/p>\n<pre>import folium<\/pre>\n<p>So I already have my dataframe with geo data. Don&#8217;t need to change that. But I&#8217;m going to change all the plotting stuff.<\/p>\n<p>For Matplotlib we needed to define a boundary box. For Folium we only have to have the center point of the map. And you don&#8217;t need to load map images or anything here. So that is very nice.<\/p>\n<pre># Find center of folium map\r\nlatitude_mean = geodf['latitude'].mean()\r\nlongitude_mean = geodf['longitude'].mean()<\/pre>\n<p>Now I define a map:<\/p>\n<pre>my_map = folium.Map(location=[latitude_mean, longitude_mean], zoom_start=12)<\/pre>\n<p>And for each point in my Pandas dataframe I want a marker. Folium markers allow you to add popup text, which you can make nice with HTML tags. I decided I wanted to have my filename and creation date in here.<\/p>\n<pre>for index, georow in geodf.iterrows():\r\n    folium.Marker([georow['latitude'], georow['longitude']], popup=f\"filename: {georow['xmlfilename']}&lt;\/br&gt;creationdate: {georow['creationdate']}\").add_to(my_map)<\/pre>\n<p>&nbsp;<\/p>\n<h1>Showing the map<\/h1>\n<p>But why didn&#8217;t PyCharm show my map? Well, it turns out that Folium is more notebook (Jupyter) oriented. Not to worry. You can save the html:<\/p>\n<pre>my_map.save('videogps_folium.html')<\/pre>\n<p>And when you <a href=\"https:\/\/github.com\/Marcel-Jan\/media_gpsplot\/blob\/main\/videogps_folium.html\">open this file<\/a>, there you have it: a wonderful, zoomable map with markers for all my video locations.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1391\" src=\"https:\/\/marcel-jan.eu\/datablog\/wp-content\/uploads\/2022\/05\/VDM2021_Sony_GPS_folium1-300x208.png\" alt=\"\" width=\"783\" height=\"543\" \/><\/p>\n<p>And it turns out that now the map is zoomable, this is actually very useful. Remember last blogpost that I would probably create my maps per day? Not anymore. I will just zoom in to that particular ride now.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1392\" src=\"https:\/\/marcel-jan.eu\/datablog\/wp-content\/uploads\/2022\/05\/2022-05-04_17-40-40-300x173.gif\" alt=\"\" width=\"529\" height=\"305\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>As mentioned, you can find my new version of this code in my Github repo:<\/p>\n<p><a href=\"https:\/\/github.com\/Marcel-Jan\/media_gpsplot\">https:\/\/github.com\/Marcel-Jan\/media_gpsplot<\/a><\/p>\n<p>You can also find an example output file:<\/p>\n<p><a href=\"https:\/\/github.com\/Marcel-Jan\/media_gpsplot\/blob\/main\/videogps_folium.html\">https:\/\/github.com\/Marcel-Jan\/media_gpsplot\/blob\/main\/videogps_folium.html<\/a><\/p>\n<p>&nbsp;<\/p>\n<p>Other blogposts I wrote about geo data in Python:<\/p>\n<p><a href=\"https:\/\/marcel-jan.eu\/datablog\/2022\/05\/23\/adding-the-track-of-my-bike-ride-on-a-folium-map\/\">Adding the track of my bike ride in Folium (Antpaths and Polylines)<\/a><\/p>\n<p><a href=\"https:\/\/marcel-jan.eu\/datablog\/2022\/05\/21\/digging-into-video-files-for-geolocations\/\">Digging into video files for geolocations (Exif data in video files, running OS commands from Python, processing XML)<\/a><\/p>\n<p><a href=\"https:\/\/marcel-jan.eu\/datablog\/2022\/05\/11\/photo-locations-marker-icons-and-displaying-photos-on-my-map\/\">Photo location markers and displaying photos on a map (Accessing exif data in JPGs with Python, projecting photos on a Folium map).<\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Yesterday I shared how I plotted locations of videos shot with my Sony FDR-X3000 camera on a map. I was already pretty happy. Then I got a tip from Twitter user Bob Haffner (@bobhaffner): why not use Folium to create my maps? Huh? I already got a working map now, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[191,55,75],"tags":[319,77,314,320],"class_list":["post-1390","post","type-post","status-publish","format-standard","hentry","category-data-engineering","category-howto","category-python","tag-folium","tag-pandas","tag-sony-fdr-x3000","tag-zoomable-map"],"_links":{"self":[{"href":"https:\/\/marcel-jan.eu\/datablog\/wp-json\/wp\/v2\/posts\/1390","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/marcel-jan.eu\/datablog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/marcel-jan.eu\/datablog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/marcel-jan.eu\/datablog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/marcel-jan.eu\/datablog\/wp-json\/wp\/v2\/comments?post=1390"}],"version-history":[{"count":7,"href":"https:\/\/marcel-jan.eu\/datablog\/wp-json\/wp\/v2\/posts\/1390\/revisions"}],"predecessor-version":[{"id":1444,"href":"https:\/\/marcel-jan.eu\/datablog\/wp-json\/wp\/v2\/posts\/1390\/revisions\/1444"}],"wp:attachment":[{"href":"https:\/\/marcel-jan.eu\/datablog\/wp-json\/wp\/v2\/media?parent=1390"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/marcel-jan.eu\/datablog\/wp-json\/wp\/v2\/categories?post=1390"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/marcel-jan.eu\/datablog\/wp-json\/wp\/v2\/tags?post=1390"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}