Skip to content

Instantly share code, notes, and snippets.

@lucassmacedo
Created November 12, 2019 15:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucassmacedo/c07f1a2bebefc259830d58f03377e3e1 to your computer and use it in GitHub Desktop.
Save lucassmacedo/c07f1a2bebefc259830d58f03377e3e1 to your computer and use it in GitHub Desktop.
Folium MAP
# import geopy
# import pandas
# from geopy.geocoders import Nominatim, GoogleV3
import pymysql.cursors
import folium
import geopandas as gpd
import pandas as pd
import numpy as np
import os
import webbrowser
cnx = pymysql.connect(host="xx", user="xx", database="xx", password="xx")
cursor = cnx.cursor()
query = ("select municipio, sum(total) as total from vendas_2019 group by municipio")
cursor.execute(query)
records = cursor.fetchall()
df = pd.read_sql(query, cnx)
dataset = gpd.read_file(os.path.abspath("Brasil/MUNICIPIOS_polígonos.shp"))
geo = dataset[['COD_IBGE','NOME_MUNI','geometry']].rename(columns={'NOME_MUNI':'cidade','COD_IBGE':'municipio'})
combined = pd.merge(left = geo,how='outer',right = df, on = 'municipio')
combined['total'] = combined['total'].fillna(0)
combined['total'] = combined['total'].round()
mappy = folium.Map(location = (-16.1237611, -59.9219642), zoom_start = 4)
sat_17 = folium.Choropleth(
geo_data=combined[['cidade', 'geometry', 'total']].to_json(),
data=combined,
columns=['cidade','total'],
fill_color='YlOrRd',
key_on='properties.cidade',
legend_name='Total (%)',
name='SAT Participation, 2017',
highlight=True).add_to(mappy)
sat_17.geojson.add_child(
folium.features.GeoJsonTooltip(fields = ['cidade', 'total'],
aliases= ['Cidade: ', 'Total: '],
sticky=False))
folium.LayerControl(collapsed=False).add_to(mappy)
mappy.save('index.html')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment