import pandas as pd
import folium
pm25_url ='http://opendata.epa.gov.tw/webapi/Data/REWIQA/?$orderby=SiteName&$skip=0&$top=1000&format=json'
pm25 = pd.read_json(pm25_url)
m = folium.Map(location=[24, 121], zoom_start=7)
然後怎麼寫
我真的不愛pandas,所以我改用jsonpickle改寫一下
import folium
import jsonpickle
import requests
import re
import os
localhtml = "taiwan.html"
pm25_url ='http://opendata.epa.gov.tw/webapi/Data/REWIQA/?$orderby=SiteName&$skip=0&$top=1000&format=json'
html = requests.get(pm25_url)
json = jsonpickle.decode(html.text)
m = folium.Map(location=[24, 121], zoom_start=7)
isdigit = re.compile(r'^\d+\.*\d*$')
for j in json:
pm2p5 = j["PM2.5"]
if isdigit.match(pm2p5) is not None:
r = int(pm2p5)*500
loc = (j["Latitude"],j["Longitude"])
folium.Circle(loc,radius=r).add_to(m)
currpath = os.path.join( os.getcwd(),localhtml)
m.save(currpath)
os.system('start "{0}"'.format(currpath)) # windows開啟
改一下區分顏色跟填滿......看起來比較順眼
(點圏圈會出現地名跟數值)
寫的不好看還請見諒,今天第一次初學folium,真的靠摸索
借花獻佛...剛好派的上用場
# -*- coding:utf-8 -*-
import folium
import jsonpickle
import requests
import re
import os
from time import gmtime,strftime,localtime
def color_producer(r):
cn="white"
if r<=10 :
cn = 'green'
elif r>10 and r<=15:
cn = 'yellow'
elif r>15 and r<=20:
cn= "red"
else:
cn= "purple"
cr= 0.7
return [cn,cr]
localhtml = "taiwan.html"
pm25_url ='http://opendata.epa.gov.tw/webapi/Data/REWIQA/?orderby=SiteName&skip=0&top=1000&format=json'
html = requests.get(pm25_url)
json = jsonpickle.decode(html.text)
center = (23.5, 121)
m = folium.Map(location=center, zoom_start=8,title="全台PM2.5分布圖")
folium.map.Marker(
location=(23.2,121.4),
icon=folium.DivIcon(
icon_size=(300,36),
icon_anchor=(0,0),
html="<div style='font-size:1.8em;color:red;background-color:yellow;'>全台PM2.5分布圖</div>".format(strftime("%Y年%m月%d日 %H:%M:%S",localtime(8)))),
).add_to(m)
isdigit = re.compile(r'^\d+\.*\d*$')
fg = folium.FeatureGroup(name="PM2.5")
for j in json:
pm2p5 = j["PM2.5_AVG"]
if j["Latitude"]!= "" and j["Longitude"]!="" and isdigit.match(pm2p5) is not None:
r = int(pm2p5)
cc = color_producer(r)
loc = (j["Latitude"],j["Longitude"])
#folium.Circle(loc,radius=r).add_to(m)
fg.add_child(folium.CircleMarker(
location=loc,
radius = r,
tooltip=str(r)+'\n'+j["SiteName"],
fill_color=cc[0],
color = 'grey',
fill_opacity=cc[1]))
m.add_child(fg)
m.save(localhtml)
輸出在下則網址
感恩高手幫忙