iT邦幫忙

2021 iThome 鐵人賽

DAY 20
1

看看昨天的圖層

我們將圖層輸出(Export)成 GeoJSON,點開來可以看到這些 json 的文字訊息

{
{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {
        "full_id": "n1839325691",
        "osm_id": 1839325691,
        "osm_type": "node",
        "amenity": "kindergarten",
        "short_name": null,
        "name:nan-TL": null,
        "name:nan-POJ": null,
        "name:nan": null,
        "language:zh": null,
        "operator:type": null,
        "full_name": null,
        "alt_name": null,
        "addr:unit": null,
        "toilets": null,
        "wheelchair:description": null,
        "start_date": null,
        "capacity": null,
        "name:th": null,
        "official_name": null,
        "disused:name": null,
        "disused:amenity": null,
        "wheelchair": null,
        "toilets:wheelchair": null,
        "layer": null,
        "opening_hours": null,
        "name:fa": null,
        "facebook": null,
        "email": null,
        "level": null,
        "wikipedia": null,
        "wikidata": null,
        "old_name": null,
        "name:ja": null,
        "preschool": null,
        "name:zh": null,
        "name:en": null,
        "operator": null,
        "religion": null,
        "denomination": null,
        "addr:floor": null,
        "website": null,
        "phone": null,
        "fax": null,
        "branch": null,
        "addr:full": null,
        "addr:street": null,
        "addr:postcode": null,
        "addr:housenumber": null,
        "addr:housename": null,
        "addr:district": null,
        "addr:country": null,
        "addr:city": null,
        "office": null,
        "name": "達人托兒所"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          121.5715472,
          25.0840952
        ]
      }
    },

但其實我們到這邊把剛剛的 GeoJSON 匯入進去會出現:

會是一個很酷的地理位置 JSON 檔。

所以我們到底要怎麼用它

我們來寫一個程式點出(pin) 自己的位置跟某個學校的位置!

import folium # 匯入 folium 套件
import json

self_coordinates = [25.01206450477216, 121.5414458747392]
fitness_coordinates = []

fmap = folium.Map(location=self_coordinates, zoom_start=15)

fmap.save("test.html")

會以自己設定的座標為正中心的位置,如下圖。

這樣看不出來自己到底位置在哪裡,那我們加個東西!

已經可以看出我把自己定位在台科大

import folium # 匯入 folium 套件
import json

self_coordinates = [25.01206450477216, 121.5414458747392]
fitness_coordinates = []

fmap = folium.Map(location=self_coordinates, zoom_start=15)

tooltip ='請點選我檢視該點資訊'

popup = folium.Popup('目前所在地', max_width=500)

folium.Marker(location = self_coordinates,
              popup=popup,
              icon=folium.Icon(color='red')).add_to(fmap)
fmap.save("test.html")
  • folium.map.Popup(html=None, parse_html=False, max_width='100%', show=False, sticky=False, **kwargs)
    • Parameters
      1. html (string or Element) – Popup 裡面的文字
      2. max_width (int for pixels or text for percentages, default '100%') – 整數,Popup 最大寬度。
      3. show (bool, default False) – Loading 這個頁面之後是否直接顯示(default: False

搭配 GeoJSON

import folium # 匯入 folium 套件
import json

self_coordinates = [25.01206450477216, 121.5414458747392]
fitness_coordinates = []

fmap = folium.Map(location=self_coordinates, zoom_start=15)

tooltip ='請點選我檢視該點資訊'

popup = folium.Popup('目前所在地', max_width=500)

folium.Marker(location = self_coordinates,
              popup=popup,
              icon=folium.Icon(color='red')).add_to(fmap)

path = 'school.geojson'
with open(path, 'r', encoding='utf-8') as f:
    school = json.load(f) # taipei_fitness type :dict

school_list = school['features'] # 我們要找的是 key: feature
school_coordinates = []

for mark in school_list:
    mark['geometry']['coordinates'].reverse()

    school_coordinates.append(mark['geometry']['coordinates'])

for m in school_coordinates:
    folium.Marker(location=m,
                  icon=folium.Icon(color='blue')).add_to(fmap)


fmap.save("test.html")


我們也可以透過 程式碼 來把每一個點都點出來


明天可以繼續來講解如何透過這些 feature 來算,可能距離我們最近的學校等等等等。
有很多功能都可以搭配一起進來,也可以透過這個計算位址,傳送回 Line Chatbot


上一篇
【Day 19】QGIS + OSM
下一篇
【Day 21】整合
系列文
陪聊_伃時不候 Line Bot 聊天機器人30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言