LoginSignup
1
3

緯度・経度を使って地図にプロット

Last updated at Posted at 2023-11-10

【東京のカーディーラーWebスクレイピングの続編】

前回GeoCodingで取得した緯度・経度を使って地図にプロットします。
Google DriveにCSVファイルを置いて、Google Colaboratoryで実行しています。
出力はhtmlです。

今回は10種類に色分けしましたが分かりづらいので、凝集性の高い場合は3種類くらいまでの方が良さそうですね。
僕の雇用契約先会社の店舗は200店以上あってセンターも常低温で10センターくらいあるので、管轄センターごとに店舗を色分けして可視化して使ったりしています。

次回はこの地図の応用編!

import pandas as pd
import folium
from chardet.universaldetector import UniversalDetector

csv_file_path = 'Google Driveのファイルパス'

detector = UniversalDetector()
with open(csv_file_path, 'rb') as file:
    for line in file:
        detector.feed(line)
        if detector.done:
            break
detector.close()
encoding = detector.result['encoding']

df = pd.read_csv(csv_file_path, encoding='shift_jis')

m = folium.Map(location=[df['Latitude1'].mean(), df['Longitude1'].mean()], zoom_start=10)

colors = ['green', 'blue', 'gray', 'red', 'purple', 'lightblue', 'pink', 'orange', 'lightgray', 'lightgreen']

for i in range(10):
    lat_col = f'Latitude{i+1}'
    lon_col = f'Longitude{i+1}'
    filtered_df = df.dropna(subset=[lat_col, lon_col])
    for _, row in filtered_df.iterrows():
        lat = row[lat_col]
        lon = row[lon_col]
        
        popup_text = row[f'Shop Name{i+1}']
        folium.Marker(
            [lat, lon],
            tooltip=row[f'Address{i+1}'],
            icon=folium.Icon(color=colors[i]),
            popup=popup_text
        ).add_to(m)

html = '<div style="position: absolute; left: 20px; top: -300px; white-space: nowrap; font-size: 15px; color: black;">GREEN=外車<br>BLUE=レクサス<br>GRAY=スバル<br>PURPLE=マツダ<br>LIGHTBLUE=スズキ<br>PINK=ダイハツ<br>ORANGE=トヨタ<br>RED=三菱<br>lightgray=ホンダ<br>lightgreen=日産</div>'
marker = folium.Marker(
    [df['Latitude1'].mean(), df['Longitude1'].mean()],
    icon=folium.DivIcon(html=html)
)

marker.add_to(m)

m.save('生成するファイル名.html')

スクリーンショット 2023-11-10 110451.png

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3