Try   HackMD

使用QGIS製作面量圖

大綱

內容說明

本教學將一步一步帶領讀者使用開源GIS軟體 — QGIS,製作面量圖。面量圖是主題地圖的一種,其製作方法是根據統計數值的多寡來將不同的地理區域上色,如此可有效呈現地理區域間目標數值的分布情形,常使用於資料視覺化的各種應用場合,例如:縣市長全國選舉時用以呈現各縣市的勝選政黨。本教學包括下列各點:

  1. 使用QGIS開啟shapefile檔。
  2. 插入以逗號分隔檔(.csv)存在的統計資料。
  3. 進行「資料表連接(table join)」,將圖徵與統計資料連結起來。
  4. 調整面量圖參數。
  5. 出圖設定。

教學

安裝QGIS 3.X版

製作此指南時,我安裝的QGIS版本號為3.4,理論上3.X版本都可以適用此指南。請自QGIS官網下載軟體,並依照指示安裝。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

▲ 安裝好後,預設在「開始」選單中會出現這些捷徑。

點選「QGIS Desktop 3.4.3」,開啟QGIS主程式。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

▲ 載入主程式的畫面。

QGIS以專案(project)來管理每一次的地理分析作業,可以想像成每一次開啟新的專案,就等同開始一份新的專題研究。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

▲ 初始介面如上,中間會顯示曾經開啟過的專案。

插入向量圖層

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

▲ 點選「Project/New」,開啟新專案。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

▲ 這是新專案的介面。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

▲ 點選「Layer/Add Layeer/Add Vector Layer」,載入Shapefile。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

▲ 依序設定欲載入的Shapefile:

  1. 選擇「File」。
  2. 選擇編碼格式,一般會是「UTF-8」,但若是台灣產出的Shapefile檔,可能會使用「Big-5」。
  3. 選擇檔案位置。我會建議將所有專案會用到的圖資都放在同一個資料夾內以便搜尋,除非檔案很大,不方便移動。
  4. 點選「Add」,加入圖層。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

▲ 順利載入Shapefile檔。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

▲ 在左下角的「Layer」選單中,左鍵點選剛才加入的圖層,然後點右鍵,在選單中點選「Open Attribute Table」,開啟屬性資料表。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

▲ 順利開啟屬性資料表。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

▲ 上方會顯示該Shapefile檔的欄位名稱。

修改特定欄位

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

▲ 點選屬性資料表上方,左邊第一個按鈕「Toggle editing mode」,開啟編輯模式。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

▲ 左鍵雙擊特定欄位,可以修改該欄位內容。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

▲ 修改後,再次點選「Toggle editing mode」,QGIS會詢問是否要儲存剛才的變更。注意此步驟會直接修改Shapefile檔的內容,建議每次進行新專案時先行備份新的Shapefile檔,利用新建立的檔案進行GIS分析。

插入逗號分隔檔圖層

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

▲ 透過文本編輯器,我們可以先行確認.csv檔中欄位的名稱及其對應的資料。在範例中,第二欄(Third_Name)為我們要用來連結兩資料表的主鍵(Key)。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

▲ 點選加入逗號分隔檔的圖層。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

▲ 依照數字順序依序選擇檔案:

  1. 選擇要插入的.csv檔。
  2. 選擇編碼,一般而言是UTF-8(有時是Big5,可以從下方的預覽功能中看到文字有無正常顯示。)
  3. 選擇.csv格式
  4. 勾選「此檔無地理資訊」。
  5. 點選「Add」加入圖層,然後點選「Close」關閉視窗。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

▲ 完成後可以看到資料表格已經順利讀入QGIS。

資料表連結

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

▲ 點選「Processing/Toolbox」。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

▲ 點選設定。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

▲ 勾選「Processing/General/Use filename as layer name」,以方便之後步驟進行。勾完後點選下方的「OK」。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

▲ 在Processing Toolbox中開啟「join」功能。

  1. 搜尋「join」。
  2. 找到「Vector general/Join attribute by field value」,雙擊左鍵打開。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

▲ 設定資料表連結的選項。

  1. 選擇具地理資訊的圖層(即A檔)。
  2. 選擇該圖層中的主鍵欄位。
  3. 選擇具統計資料的.csv檔(即B檔)。
  4. 選擇該圖層中的主鍵欄位。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

▲ 設定連結好的資料表的存檔格式,請點選「Save to GeoPackage」。接著依照彈出的視窗設定檔名及圖層名,並按下「Run」。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

▲ 確認資料表是否有正常連結。

  1. 確認配對成功的圖徵(feature)數,在本次範例中,台灣共有368個鄉鎮,但是.csv資料檔只有107列。QGIS顯示有105列配對成功,263列未成功(以紅字顯示),表示在資料配對的過程中有些微錯誤,導致結果不符合預期,此時應重新省視.csv檔以確認是否有重複或是與Shapefile檔的主鍵不相符之處。本指南先假設無問題,繼續下方說明。
  2. 點選「Close」關閉視窗。
  3. 可以看到連結好的圖層「joined」已經被加入QGIS專案中。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

▲ 點選「joined」圖層的屬性資料表,可以看到.csv檔中的資料已經配對到相對應的列中。空白處則表示該欄位為配對而呈現缺失值(Missing data, NULL)。

缺失值處理

使用QGIS製作面量圖時,如果屬性資料表中有缺失值,則步驟會較為繁瑣。我們先手動修改「joined」圖層的屬性資料表,將所有的缺失值欄位改成「-999(-999是資料科學上常用於表示缺失值的一個數字)」,以便後續作業。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

▲ 點開「joined」圖層的屬性資料表,在上方工具列中找到、點選「Select/filter features using form」。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

▲ 設定選取規則。

  1. 找到目標欄位「Plant_Area」,在右方「Exclude field」中找到、點選「is missing (NULL)」。
  2. 點選「Select features」。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

▲ 完成後可以發現所有「Plant_Area」為缺失值的圖徵都已經被選取。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

▲ 開啟編輯模式。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

▲ 設定修改數值。

  1. 選擇「Plant_Area」欄位。
  2. 輸入「-999」。
  3. 點選「Update Selected」。
  4. 再次點選「Toggle editing mode」,儲存變更並關閉編輯模式,

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

▲ 點選「Deselect all」。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

▲ 點選「Switch to table view」。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

▲ 可以看到所有的缺失值欄位已經被取代成「-999」。

調整面量圖參數

接下來進入作圖設定,已經可以把「Processing Toolbox」關閉了。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

▲ 選擇「joined」圖層,並點選左上方的「Open the Layer Styling panel」。


▲ 將這個選項改成「Graduated」。


▲ 依序設定參數。

  1. 選擇資料來源欄位。
  2. 選擇使用的顏色色階。
  3. 點選「+」加入分級單位。


▲ 依序設定參數。

  1. 調整小數位數至「Precision -2」。
  2. 雙擊每個級距單位的「Values」與「Legends」,更改為需要的數值。我這邊將第一個級距單位設為「-999-0」,如此便會顯示出缺失值欄位。
  3. 點選「Apply」進行分級著色。


▲ 完成後已可見面量圖之雛形,但我們還可以加以美化。


▲ 選擇所有級距單元後,按下上方的「Symbol/Change」。


▲ 依許點選「Fill/Simple fill」,並點選下方的「Sroke color」右邊的下拉選單,勾選「Transparent stroke」,並按下「Apply」。除此之外,這個繪圖面板還有很多細部設定可以調整,可以自行嘗試。


▲ 完成面量圖設定,剩下出圖面板的設定了。

合併圖徵

此指南使用的Shapefile檔的最小級別是「鄉鎮市」,我們可以透過合併圖徵來獲得「縣市」級別的圖徵。


▲ 點選「Vector/Geoprocessing Tools/Dissolve」。


▲ 依序設定參數:

  1. 選擇欲合併的圖層。
  2. 選擇欲用以合併的欄位(主鍵),QGIS將會依此欄位將較次級的圖徵合併。
  3. 設定是否要另存成新的獨立圖資檔案,或是僅使用暫存圖層,建議儲存成獨立檔案以便下一次載入專案檔時使用。
  4. 點選「Run」。


▲ 此為上述第2步驟的畫面,可以選擇單一用以合併的目標欄位。


▲ QGIS程式合併中。


▲ 完成圖徵合併,後續可以依照上面的說明調整視覺化設定,以另地圖美觀,例如:調整縣市級的邊框粗細。

出圖面板設定

最後一步是設定出圖面板(Print Layout),出圖面板式儲存於專案檔(.qgz)中的,理論上下一次開啟同一個專案檔也可以載入相同的出圖設定,而不用重複調整。


▲ 點選「Project/New Print Layout」,開啟出圖面板。


▲ 出圖面板為一個獨立彈出的視窗。點選「Add Item/Add Map」。


▲ 透過游標拉出要插入地圖的範圍。


▲ 放開游標後,已插入地圖。


▲ 同上,點選「Add Item/Add Scale Bar」,插入比例尺。


▲ 同上,點選「Add Item/Add Legend」,插入圖例。


▲ 同上,點選「Add Item/Add Picture」,插入方向標。


▲ 方向標的選項很多種,位於右方「Item Properties/Search Directories」中,可以自行挑選。


▲ 插入的地圖範圍是根據原本QGIS操作頁面的可視範圍決定的,現在在QGIS專案頁面中縮放(Zoom)到目標位置(金門)。


▲ 回到出圖面板,再次插入地圖,可以發現金門的範圍已被加入出圖面板中,代號為「Map 2」。


▲ 點選「Add Item/Add Label」,插入文字標籤,我們在這邊插入金門與馬祖的標示,以便閱讀者判斷。


▲ 點選「Layout/Export as Image」,挑選欲匯出的圖片格式後,即可匯出完成的地圖。QGIS也提供了SVG檔與PDF檔可供匯出。


▲ 完成!

補充-AgriMap_TW輸出的Shapefile

AgriMap_TW專案所輸出的Shapefile檔也可以透過這個流程開啟,因為在專案中已經做過資料表連結的動作,所以直接從「缺失值處理」這一步開始即可,以下簡單說明載入流程。


▲ 依據上面的流程,指定載入AgriMap_TW所輸出的.shp檔(單一個Shapefile檔由五個子檔案所構成,只須點選副檔名為「.shp」的檔案,電腦會自動載入同一資料夾內的其他子檔案。)


▲ 順利載入Shapefile檔。


▲ 透過圖層選單開啟資料屬性表(Open Attribute Table)。


▲ 資料屬性表的視窗。


▲ 上方第一列可見資料屬性表的表頭。注意2號方框,AgriMap_TW產出的Shapefile檔並未將缺失值補齊,可以利用上述教學補齊缺失值欄位後,再進行出圖設定。

延伸閱讀

  1. QGIS官方指南
    我是參考這個指南來製作本教學的,基本上內容一致。

變更日誌

  • 2020/07/19-18:08 完成第1.0版說明。
  • 2020/07/19-18:46 完成第1.1版說明。

Find this document incomplete? Leave a comment!

tags: GIS Documentation