Plotting Google Map using folium package?

PythonServer Side ProgrammingProgramming

Folium is a very powerful python library which let you create seveal kind of Leaflet maps. As Leaflet/folium maps are interactive, so they are ideal for making dashborad building.

Installation

Installing folium is very easy using pip −

$pip install folium


Like you can see from the below screenshot, you just need to type above command in your console/cmd and pip will install the folium as well as dependencies for your python installation.


Basic Map

#Import library
import folium

#Uses lat then lon. & zoomlevel 4.The bigger the zoom number, the closer in you get.
mapOBJ = folium.Map(location=[17.3616, 78.4747], zoom_start = 4, tiles = 'Stamen Terrain')

# save method of Map object, will create a map

mapOBJ.save('map2.html')

Output

Case#2: GeoJson

import folium
import os
from folium import features
import numpy as np
import pandas as pd

N = 1000

lons = +5 - np.random.normal(size=N)
lats = 48 - np.random.normal(size=N)

data = {
   'type': 'FeatureCollection',
   'features': [
      {
         'type': 'Feature',
         'geometry': {
            'type': 'MultiPoint',
            'coordinates': [[lon, lat] for (lat, lon) in zip(lats, lons)],
         },
         'properties': {'prop0': 'value0'}
      },
   ],
}
m = folium.Map([17.3616, 78.4747], zoom_start = 10)
m.add_child(features.GeoJson(data))
m.save('Features.html')

Output:

Case#3: Marker, Icon, Popup

import folium
from folium import features

m = folium.Map([17.3616, 78.4747], zoom_start = 15)
mk = features.Marker([17.3616, 78.4747])
pp = folium.Popup('Charminar')
ic = features.Icon(color='red')

mk.add_child(ic)
mk.add_child(pp)
m.add_child(mk)

m.save('Features_1.html')

Output

Case#4

I have used one new library- vincent, Vincent takes Python data structures (tuples, lists, dicts, and Pandas DataFrames) and translates them into Vega visualization grammar.

Using pip, its very easy to install vincent.

$pip install vincent
C:\WINDOWS\system32>pip install vincent
Collecting vincent
Downloading https://files.pythonhosted.org/packages/11/bf/a12ecaa21a2e376a16de67e09f64a38a4acd95e04e5dc35ad2f13a6f0bfd/vincent-0.4.4.tar.gz
Requirement already satisfied: pandas in c:\python\python361\lib\site-packages (from vincent) (0.20.2)
Requirement already satisfied: pytz >= 2011k in c:\python\python361\lib\site-packages (from pandas -> vincent) (2017.2)
Requirement already satisfied: python-dateutil >= 2 in c:\python\python361\lib\site-packages (from pandas -> vincent) (2.6.0)
Requirement already satisfied: numpy >= 1.7.0 in c:\python\python361\lib\site-packages (from pandas -> vincent) (1.16.0)
Requirement already satisfied: six >= 1.5 in c:\python\python361\lib\site-packages (from python-dateutil >= 2 -> pandas -> vincent) (1.10.0)
Building wheels for collected packages: vincent
Running setup.py bdist_wheel for vincent ... done
Stored in directory: C:\Users\rajesh\AppData\Local\pip\Cache\wheels\4c\0d\8a\65f34c765c6094a71cce3e42a49a26533eef61695e9b505f03
Successfully built vincent
Installing collected packages: vincent
Successfully installed vincent-0.4.4


import folium
from folium import features
import numpy as np
import json
import vincent

N = 100

multi_iter2 = {
   'x': np.random.uniform(size=(N,)),
   'y': np.random.uniform(size=(N,)),
}

scatter = vincent.Scatter(multi_iter2, iter_idx='x', height=100, width=200)
data = json.loads(scatter.to_json())

m = folium.Map([17.3616, 78.4747], zoom_start=10)
mk = features.Marker([17.3616, 78.4747])
p = folium.Popup('Charminar')
v = features.Vega(data, width='100%', height='100%')

mk.add_child(p)
p.add_child(v)
m.add_child(mk)

m.save('Features_2.html')

Output

raja
Published on 27-Mar-2019 15:50:35
Advertisements