# 地圖專題 ## 套件安裝 ```python= # 安裝套件 !pip install -q folium # 載入模組 import folium # 輸出地圖 m = folium.Map() m # 存成互動式地圖網頁, 輸出map.html檔案, 可下達指令 # m.save('map.html') ``` ![image](https://hackmd.io/_uploads/r1-rcvzIJe.png) ## 劃出高雄市政府 ```python= # 給定城市名稱 city = "高雄市" # 高雄市的 gps 座標 city_gps = [22.6203348, 120.3120375] # 用 folium 畫地圖,並放到變數m裡面去。 m = folium.Map(location=city_gps, zoom_start=16) # 在地圖上的location的座標加上一個 Marker。 folium.Marker(location=city_gps,tooltip='高雄市政府',popup=city).add_to(m) # 顯示出地圖 m ``` ![image](https://hackmd.io/_uploads/SJOwivM8kl.png) ## 練習1 找出你家的GPS座標, 畫出地圖, 並標示圖標 附上Python程式碼及執行結果擷圖 ```python= city = "我家" city_gps = [23.215249, 120.095447] m = folium.Map(location=city_gps, zoom_start=16) folium.Marker(location=city_gps,tooltip='我家',popup=city).add_to(m) m ``` ![image](https://hackmd.io/_uploads/ByFaivzIJx.png) ## 高雄市政府地圖轉衛星雲圖和畫一個圓 ```python= # 給定城市名稱 city = "高雄市" # 高雄市的 gps 座標 city_gps = [22.6203348, 120.3120375] # 切換衛星空照圖資 attr = "default" tiles="http://mt0.google.com/vt/lyrs=s&hl=en&x={x}&y={y}&z={z}" # 用 folium 畫地圖,並放到變數m裡面去。 m = folium.Map(location=city_gps, zoom_start=12,tiles=tiles, attr=attr,) #在地圖上設定座標, 畫一個圓 folium.Circle(location=city_gps,radius=100,color='yellow',fill=True,fill_color='green',fill_opacity=0.3).add_to(m) # 顯示出地圖 m ``` ## 練習2 切換你家的地圖為==衛星空照圖==, 並以你家為圓心畫出==半徑1,3,5公里的3個圓==( 請用3種不同顏色) 加上量測工具, 確認半徑長度無誤 在方圓5公里內的範圍, ==加註5個地標==, 建立你的一日景點生活圈。 ```python= city = "我家" a="XX溝3D彩X村" b="X軍XX溝XX宮" c="XX市XX區公所" d="XX溝-XX茶飲" e="XX溝XX壇" city_gps = [23.215249, 120.095447] s=[23.21725, 120.09531] d=[23.216641, 120.0944843] f=[23.2168883, 120.0942697] g=[23.2159270, 120.0939478] h=[23.2149903, 120.09413026] # 切換衛星空照圖資 attr = "default" tiles="http://mt0.google.com/vt/lyrs=s&hl=en&x={x}&y={y}&z={z}" # 用 folium 畫地圖,並放到變數m裡面去。 m = folium.Map(location=city_gps, zoom_start=16,tiles=tiles, attr=attr) folium.Marker(location=s,tooltip='XX溝3D彩X村',popup=a).add_to(m) folium.Marker(location=d,tooltip='X軍XX溝XX宮',popup=b).add_to(m) folium.Marker(location=f,tooltip='XX市XX區公所',popup=c).add_to(m) folium.Marker(location=g,tooltip='XX溝-XX茶飲',popup=d).add_to(m) folium.Marker(location=h,tooltip='XX溝XX壇',popup=e).add_to(m) folium.Circle(location=city_gps,radius=1000,color='yellow',fill=True,fill_color='green',fill_opacity=0.3).add_to(m) folium.Circle(location=city_gps,radius=3000,color='red',fill=True,fill_color='blue',fill_opacity=0.3).add_to(m) folium.Circle(location=city_gps,radius=5000,color='white',fill=True,fill_color='perple',fill_opacity=0.3).add_to(m) # 顯示出地圖 m ``` ![image](https://hackmd.io/_uploads/H1Km6PMI1x.png) ![image](https://hackmd.io/_uploads/HyjVTwG8yl.png) ## 練習3 將上述一日景點生活圈, 試著用以下的方式呈現==雙地圖及迷你地圖== ```python= # 由 folium plugin 來引入迷你地圖 from folium.plugins import MiniMap import folium.plugins as plugins tiles="http://mt0.google.com/vt/lyrs=m&hl=en&x={x}&y={y}&z={z}" tiles1="http://mt0.google.com/vt/lyrs=y&hl=en&x={x}&y={y}&z={z}" m = folium.Map(location=city_gps, zoom_start=16,tiles=tiles, attr=attr) m = folium.plugins.DualMap([23.215249, 120.095447], tiles= tiles, attr="default", zoom_start=15, control_scale=True, ) minimap = MiniMap() minimap.add_to(m) folium.TileLayer().add_to(m.m1) folium.TileLayer(tiles1,attr="default").add_to(m.m2) folium.LayerControl(collapsed=True).add_to(m) m city = "我家" a="XX溝3DXX村" b="X軍XX溝XX宮" c="XX市XX區公所" d="XX溝-XX茶飲" e="XX溝XX壇" city_gps = [23.215249, 120.095447] s=[23.21725, 120.09531] d=[23.216641, 120.0944843] f=[23.2168883, 120.0942697] g=[23.2159270, 120.0939478] h=[23.2149903, 120.09413026] # 切換衛星空照圖資 attr = "default" tiles="http://mt0.google.com/vt/lyrs=s&hl=en&x={x}&y={y}&z={z}" folium.Marker(location=s,tooltip='XX溝3D彩X村',popup=a).add_to(m) folium.Marker(location=d,tooltip='X軍XX溝XX宮',popup=b).add_to(m) folium.Marker(location=f,tooltip='XX市XX區公所',popup=c).add_to(m) folium.Marker(location=g,tooltip='XX溝-XX茶飲',popup=d).add_to(m) folium.Marker(location=h,tooltip='XX溝XX壇',popup=e).add_to(m) folium.Circle(location=city_gps,radius=1000,color='yellow',fill=True,fill_color='green',fill_opacity=0.3).add_to(m) folium.Circle(location=city_gps,radius=3000,color='red',fill=True,fill_color='blue',fill_opacity=0.3).add_to(m) folium.Circle(location=city_gps,radius=5000,color='white',fill=True,fill_color='perple',fill_opacity=0.3).add_to(m) # 顯示出地圖 m ``` ![image](https://hackmd.io/_uploads/rJHzCDGIke.png) ## 加入測量功能 ```python= from folium.plugins import MeasureControl mCtrl = MeasureControl() # 量測地圖功能加入 mCtrl.add_to(m) m ``` ## 安裝GeoCoder 上面的範例是已知座標經緯度情況下繪製地圖, 通常可利用Google搜尋而得, 但若要在程式中查詢地標/地址或城市名稱來求得經緯度, 可用GeoCoder套件 使用arcgis函式來查詢地標名稱之座標經緯度, 其傳入參數為地標名稱字串 latlng 屬性即為查詢之結果 (是一個 [緯度, 經度] 串列) ```python= # 安裝套件 !pip install -q folium !pip install geocoder # 載入需要的模組 import geocoder import folium # 給定地址 address = "台南市佳里區忠孝路5號" # 使用geocoder取得特定住址的GPS座標 address_gps = geocoder.arcgis(address).latlng # 用 folium 畫地圖,並放到變數m裡面去。 m = folium.Map(location=address_gps, zoom_start=20) # 在地圖上的location的座標加上一個 Marker。 folium.Marker(location=address_gps, popup=address).add_to(m) # 顯示出地圖 m ``` ![image](https://hackmd.io/_uploads/HyVOJuf81g.png) ## 練習4 請設計一個程式, ==輸入地址, 輸出對應的GPS座標==再畫出地址對應的地圖 ```python= import folium.plugins as plugins # 安裝需要的套件 !pip install -q folium !pip install geocoder # 載入需要的模組 import geocoder import folium # 輸入地址 address = input("請輸入地址: ") address_gps = geocoder.arcgis(address).latlng print(f"地址: {address}") print(f"GPS 座標: {address_gps}") m = folium.Map(location=address_gps, zoom_start=15) folium.Marker(location=address_gps, popup=address).add_to(m) m ``` ![image](https://hackmd.io/_uploads/HyDTMOzLke.png) ![image](https://hackmd.io/_uploads/r1LKGuGUyl.png)