{"id":1618,"date":"2018-10-13T19:44:05","date_gmt":"2018-10-13T23:44:05","guid":{"rendered":"http:\/\/www.xavignu.com\/?p=1618"},"modified":"2018-10-14T18:04:37","modified_gmt":"2018-10-14T22:04:37","slug":"mapping-pirenaica-with-folium","status":"publish","type":"post","link":"https:\/\/www.xavignu.com\/?p=1618","title":{"rendered":"Mapping Pirenaica with Folium"},"content":{"rendered":"<p>So back in September I cycled through the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Pyrenees\" target=\"_blank\" rel=\"noopener\">Pyrenees<\/a> and decided to map it with <a href=\"https:\/\/github.com\/python-visualization\/folium\" target=\"_blank\" rel=\"noopener\">Folium<\/a>. Latitude and longitude info was retrieved from <a href=\"https:\/\/www.strava.com\" target=\"_blank\" rel=\"noopener\">Strava<\/a> gpx files and cleaned up using sed, grep and awk. Result is a file as below.<\/p>\n<pre id=\"terminal\">head Pirenaica_stage_1.csv\r\nlat,lon\r\n43.3057130,-1.9778780\r\n43.3057130,-1.9778770\r\n43.3057140,-1.9778770\r\n43.3057230,-1.9778880\r\n43.3060960,-1.9783310\r\n43.3064990,-1.9788360\r\n43.3065760,-1.9789890\r\n43.3067630,-1.9791790\r\n43.3069250,-1.9791730\r\n<\/pre>\n<p><!--more--><\/p>\n<p>Below code will create the html with the complete Pirenaica course.<br \/>\n[python]<br \/>\nimport random<br \/>\nimport folium<br \/>\nfrom pyspark.sql import SparkSession<br \/>\nfrom pyspark.sql.functions import col<br \/>\nspark = SparkSession.builder.master(&#8220;local&#8221;).getOrCreate()<\/p>\n<p># Change Spark loglevel<br \/>\nspark.sparkContext.setLogLevel(&#8216;FATAL&#8217;)<\/p>\n<p># Load the rides and ride_routes data from local instead of HDFS<br \/>\nposition1 = spark.read.load(&#8220;\/tmp\/Pirenaica_stage_1.csv&#8221;, format=&#8221;csv&#8221;, sep=&#8221;,&#8221;, inferSchema=&#8221;true&#8221;, header=&#8221;true&#8221;)<br \/>\nposition2 = spark.read.load(&#8220;\/tmp\/Pirenaica_stage_2.csv&#8221;, format=&#8221;csv&#8221;, sep=&#8221;,&#8221;, inferSchema=&#8221;true&#8221;, header=&#8221;true&#8221;)<br \/>\nposition3 = spark.read.load(&#8220;\/tmp\/Pirenaica_stage_3.csv&#8221;, format=&#8221;csv&#8221;, sep=&#8221;,&#8221;, inferSchema=&#8221;true&#8221;, header=&#8221;true&#8221;)<br \/>\nposition4 = spark.read.load(&#8220;\/tmp\/Pirenaica_stage_4.csv&#8221;, format=&#8221;csv&#8221;, sep=&#8221;,&#8221;, inferSchema=&#8221;true&#8221;, header=&#8221;true&#8221;)<br \/>\nposition5 = spark.read.load(&#8220;\/tmp\/Pirenaica_stage_5.csv&#8221;, format=&#8221;csv&#8221;, sep=&#8221;,&#8221;, inferSchema=&#8221;true&#8221;, header=&#8221;true&#8221;)<br \/>\nposition6 = spark.read.load(&#8220;\/tmp\/Pirenaica_stage_6.csv&#8221;, format=&#8221;csv&#8221;, sep=&#8221;,&#8221;, inferSchema=&#8221;true&#8221;, header=&#8221;true&#8221;)<br \/>\nposition7 = spark.read.load(&#8220;\/tmp\/Pirenaica_stage_7.csv&#8221;, format=&#8221;csv&#8221;, sep=&#8221;,&#8221;, inferSchema=&#8221;true&#8221;, header=&#8221;true&#8221;)<br \/>\nposition8 = spark.read.load(&#8220;\/tmp\/Pirenaica_stage_8.csv&#8221;, format=&#8221;csv&#8221;, sep=&#8221;,&#8221;, inferSchema=&#8221;true&#8221;, header=&#8221;true&#8221;)<br \/>\nposition9 = spark.read.load(&#8220;\/tmp\/Pirenaica_stage_9.csv&#8221;, format=&#8221;csv&#8221;, sep=&#8221;,&#8221;, inferSchema=&#8221;true&#8221;, header=&#8221;true&#8221;)<\/p>\n<p>position = [position1, position2, position3, position4, position5, position6, position7, position8, position9]<\/p>\n<p>#m = folium.Map()<br \/>\nINIT_COORDINATES = (46.00, -0.50)<br \/>\nm = folium.Map(location=INIT_COORDINATES, tiles=&#8217;openstreetmap&#8217;, zoom_start=1)<\/p>\n<p>col=0<br \/>\ncolArray=[&#8216;red&#8217;,&#8217;blue&#8217;,&#8217;green&#8217;,&#8217;yellow&#8217;, &#8216;brown&#8217;, &#8216;purple&#8217;, &#8216;salmon&#8217;, &#8216;olive&#8217;, &#8216;orange&#8217;]<\/p>\n<p># Check file was correctly loaded<br \/>\nfor x in position:<br \/>\n#   x.printSchema()<br \/>\n#   x.show(2)<\/p>\n<p># Map position<br \/>\n   coordinates = [[float(i.lat), float(i.lon)] for i in x.collect()]<\/p>\n<p># Make a Folium map<br \/>\n#m = folium.Map()<br \/>\n   m.fit_bounds(coordinates, padding=(25, 25))<br \/>\n   folium.PolyLine(locations=coordinates, weight=5, color=colArray[random.randrange(8)]).add_to(m)<br \/>\n   folium.Marker(coordinates[1], icon=folium.Icon(color=&#8217;blue&#8217;), popup=&#8221;Origin&#8221;).add_to(m)<br \/>\n   folium.Marker(coordinates[-1], popup=&#8221;Destination&#8221;, icon=folium.Icon(color=&#8217;red&#8217;)).add_to(m)<br \/>\n   col = col + 1<\/p>\n<p># Adding cols we crossed<br \/>\nfolium.Marker([43.366944, -1.820556], icon=folium.Icon(color=&#8217;green&#8217;), popup=&#8217;Col du Jaizkibel, alt: 455m&#8217;).add_to(m)<br \/>\nfolium.Marker([43.175556, -1.402222], icon=folium.Icon(color=&#8217;green&#8217;), popup=&#8217;Col du Izpegi, alt: 672m&#8217;).add_to(m)<br \/>\nfolium.Marker([43.0381288, -1.0287726], icon=folium.Icon(color=&#8217;green&#8217;), popup=&#8217;Col de Bagargi, alt: 1327m&#8217;).add_to(m)<br \/>\nfolium.Marker([42.9966, -0.9871], icon=folium.Icon(color=&#8217;green&#8217;), popup=&#8217;Col d\\&#8217;Erroymendi, alt: 1362m&#8217;).add_to(m)<br \/>\nfolium.Marker([42.973889, -0.993889], icon=folium.Icon(color=&#8217;green&#8217;), popup=&#8217;Col de Larrau, alt: 1578m&#8217;).add_to(m)<br \/>\nfolium.Marker([42.9212905, -0.989741], icon=folium.Icon(color=&#8217;green&#8217;), popup=&#8217;Alto de Laza, alt: 1106m&#8217;).add_to(m)<br \/>\nfolium.Marker([42.970278, -0.765833], icon=folium.Icon(color=&#8217;green&#8217;), popup=&#8217;Col de La Pierre de St Martin, alt: 1766m&#8217;).add_to(m)<br \/>\nfolium.Marker([42.976667, -0.339722], icon=folium.Icon(color=&#8217;green&#8217;), popup=&#8217;Col d\\&#8217;Aubisque, alt: 1709m&#8217;).add_to(m)<br \/>\nfolium.Marker([42.908194, 0.145111], icon=folium.Icon(color=&#8217;green&#8217;), popup=&#8217;Col du Tourmalet, alt: 2115m&#8217;).add_to(m)<br \/>\nfolium.Marker([42.942306, 0.327389], icon=folium.Icon(color=&#8217;green&#8217;), popup=&#8217;Col d\\&#8217;Aspin, alt: 1489m&#8217;).add_to(m)<br \/>\nfolium.Marker([42.802222, 0.462778], icon=folium.Icon(color=&#8217;green&#8217;), popup=&#8217;Col de Peyresourde, alt: 1596m&#8217;).add_to(m)<br \/>\nfolium.Marker([42.79, 0.445], icon=folium.Icon(color=&#8217;green&#8217;), popup=&#8217;Station de Peyragudes, alt: 1592m&#8217;).add_to(m)<br \/>\nfolium.Marker([42.769167, 0.66], icon=folium.Icon(color=&#8217;green&#8217;), popup=&#8217;Col du Portillon, alt: 1293m&#8217;).add_to(m)<br \/>\nfolium.Marker([42.663889, 0.981944], icon=folium.Icon(color=&#8217;green&#8217;), popup=&#8217;Port de la Bonaigua, alt: 2072m&#8217;).add_to(m)<br \/>\nfolium.Marker([42.371444, 1.238953], icon=folium.Icon(color=&#8217;green&#8217;), popup=&#8217;Port del Canto, alt: 1730m&#8217;).add_to(m)<br \/>\nfolium.Marker([42.3324755, 1.9450939], icon=folium.Icon(color=&#8217;green&#8217;), popup=&#8217;La Molina, alt: 1780m&#8217;).add_to(m)<br \/>\nfolium.Marker([42.2111825,2.3321633], icon=folium.Icon(color=&#8217;green&#8217;), popup=&#8217;Coll de Santigosa, alt: 1064m&#8217;).add_to(m)<\/p>\n<p># Save to an html file<br \/>\nm.save(&#8216;pirenaica.html&#8217;)<\/p>\n<p># Cleanup<br \/>\nspark.stop()<br \/>\n[\/python]<br \/>\nAnd output is an html file we will call pirenaica.html which we can see below.<br \/>\n<iframe loading=\"lazy\" style=\"border: 1px solid #ccc\" width=\"100%\" height=\"400\" src=\"wp-content\/uploads\/2018\/10\/pirenaica.html?inline=true\"><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"<p>So back in September I cycled through the Pyrenees and decided to map it with Folium. Latitude and longitude info was retrieved from Strava gpx files and cleaned up using sed, grep and awk. Result is a file as below. head Pirenaica_stage_1.csv lat,lon 43.3057130,-1.9778780 43.3057130,-1.9778770 43.3057140,-1.9778770 43.3057230,-1.9778880 43.3060960,-1.9783310 43.3064990,-1.9788360 43.3065760,-1.9789890 43.3067630,-1.9791790 43.3069250,-1.9791730<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[92],"tags":[97,99,67,98],"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/pTQgt-q6","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.xavignu.com\/index.php?rest_route=\/wp\/v2\/posts\/1618"}],"collection":[{"href":"https:\/\/www.xavignu.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xavignu.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xavignu.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xavignu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1618"}],"version-history":[{"count":6,"href":"https:\/\/www.xavignu.com\/index.php?rest_route=\/wp\/v2\/posts\/1618\/revisions"}],"predecessor-version":[{"id":1676,"href":"https:\/\/www.xavignu.com\/index.php?rest_route=\/wp\/v2\/posts\/1618\/revisions\/1676"}],"wp:attachment":[{"href":"https:\/\/www.xavignu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1618"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xavignu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1618"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xavignu.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1618"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}