Pydeck การแสดงข้อมูลด้วย Python Column Layer บนแผนที่ Carto

Feb 18 2023
ฉันจะสำรวจความสามารถในการสร้างภาพข้อมูลทางภูมิศาสตร์ของ deck.gl ด้วยไลบรารี Python และ pandas

ฉันจะสำรวจ ความสามารถในการสร้างภาพข้อมูลทางภูมิศาสตร์ ของ deck.glด้วยไลบรารี Python และ pandas Deck.glเป็นเฟรมเวิร์กการแสดงภาพที่ขับเคลื่อนโดย WebGL สำหรับชุดข้อมูลขนาดใหญ่ และpydeckเป็นการแสดงผลเชิงพื้นที่ระดับสูงสำหรับ Python (ขับเคลื่อนโดย deck.gl)

สำหรับโปรเจ็กต์นี้ ฉันใช้Jupyter Notebookซึ่งเป็นเว็บแอปพลิเคชันดั้งเดิมสำหรับสร้างและแชร์เอกสารการคำนวณ มันมอบประสบการณ์ที่เรียบง่าย คล่องตัว และเน้นเอกสารเป็นหลัก

ไลบรารี Python ที่สำคัญสำหรับโปรเจ็กต์นี้คือpydeck ตามค่าเริ่มต้น pydeck 0.6 จัดเตรียมชิ้นส่วนแผนที่ฐานผ่าน Carto คุณสามารถใช้คีย์ Mapbox API โดยการลงทะเบียนสำหรับ Mapbox แต่แผนที่ Carto ก็เพียงพอแล้วสำหรับโครงการนี้

มานำเข้า pydeck ผ่าน pip:

pip install pydeck

  1. เราเริ่มต้นด้วยการนำเข้าไลบรารี Python ที่จำเป็นสำหรับการดำเนินการของเรา: pandas, pydeck, osและตัวเลือกmatplotlib.pyplot (ฉันอาจพัฒนาโพสต์นี้ในอนาคต)
  2. import pandas as pd
    import pydeck as pdk
    from pydeck.types import String
    import matplotlib.pyplot as plt
    import os
    

    os.listdir() ## checking the current file composition of the folder
    df = pd.read_excel('stores.xlsx') ## reading .xlsx file to Pandas DataFrame
    df.set_index('Location Name') ## setting indexing to store's name
    
           
                    

    df.columns
    
    # output
    # Index(['Location Name', 'Address line 1', 'Address line 2', 'Address line 3',
    #    'City Name', 'Latitude', 'Longitude', 'Lat', 'Long', 'Postal Code',
    #      'Results'],
    #     dtype='object')
    

    df2 = df[['Location Name', 'City Name', 'Latitude', 'Longitude', 'Lat', 'Long', 'Postal Code', 'Results']]
    df2
    

df2["Long"] = df2["Long"].astype(float)
df2["Lat"] = df2["Lat"].astype(float)
df2

layer = pdk.Layer(
    'ColumnLayer',
    df2,
    get_position=['Long', 'Lat'],
    get_elevation=["Results"],
    auto_highlight=True,
    elevation_scale=5,
    pickable=True,
    extruded=True,
    get_radius=100,
    get_fill_color=["Results * 10", "Results", "Results * 40", 220],
    coverage=1
)

# Set the viewport location
view_state = pdk.ViewState(
    longitude=25.336900,
    latitude=54.734235,
    zoom=2,
    min_zoom=5,
    max_zoom=15,
    pitch=40.5,
    bearing=-27.36)

r = pdk.Deck(layers=[layer], initial_view_state=view_state)
r.to_html('stores.html')