搜尋和下載開放街圖資料 (QGIS3)

對於任何 GIS 任務來說,取得高品質的資料是相當基本的事情,有個相當棒而且是免費與開放授權的資料是`開放街圖(OpenStreetMap, OSM) <http://www.openstreetmap.org/>`_。開放街圖資料有所有型能的畫地圖資料 - 道路、在地資料、建築外框、行政邊界等。要使用 QGIS 來取得 GIS 格式的開放街圖資料,能透過 * QuickOSM** 套件做到。這份教學將解釋搜尋、下載和使用這套套件的整個過程。

任務概況

我們會從開放街圖資料庫截取倫敦所有的酒吧和 pubs 的位置,然後存成向量圖層。

其他你會學到的技能

  • 如何合併多個向量圖層。

過程

  1. 從 QGIS 官方套件庫搜尋和安裝 QuickOSM 套件,請見 使用附加元件 取得如何下載套件的指引。注意在撰寫教學的時候,這個套件標示為 實驗性,所以在 Settings 下的 Plugins 視窗,確定有勾選``同時顯示實驗性套件``才能安裝。

../../_images/140.png
  1. 一旦安裝,透過 Vector ‣ QuickOSM -> QuickOSM.... 來啟用。

../../_images/227.png
  1. In the Quick query tab, you can set a filter to select a subset. The attributes of the map features in the OSM database are stored as Tags. Tags are represented with a key and a value. The key is a topic and a value is a specific form. See this page for a comprehensive list of tags for various types of features. Bars are represented using the tag amenity:bar and pubs with the tag amenity:pub. We will first extract the bars. Select amenity as the Key from the drop-down menu.
../../_images/319.png
  1. Select bar from the Value drop-down menu.
../../_images/49.png
  1. Enter London as the In to restrict the search within the city boundary.
../../_images/510.png
  1. Expand the Advanced section. In the OSM data model, features are represented using nodes, ways and relations. As we are interested in the point features, you can select only Node and Points . Click Run query.
../../_images/610.png
  1. The plugin with query the OpenStreetMap database using the Overpass API and convert the data into a QGIS vector layer.
../../_images/710.png
  1. Once the query finishes, switch to the main QGIS window. You will see a new layer called amenity_bar_london added to the Layers panel. The canvas will show the locations of the bars that were extracted.
../../_images/810.png
  1. Switch back to the QuickOSM window, and edit the query to select pub as the Value. Click Run query.
../../_images/910.png
  1. This time the plugin will fetch all the points tagged with amenity:pub from the OSM database.
../../_images/1010.png
  1. Once the query is complete, a new layer amenity_pub_london will be added to the Layers panel. We now have 2 vector layers. These are temporary memory layers that will get lost after we exit QGIS. Let’s merge these to a single vector layer and save it to the disk. Go to Processing ‣ Toolbox.
../../_images/1113.png
  1. Search and locate the Vector general ‣ Merge vector layers tool. Double-click to launch it.
../../_images/1211.png
  1. In the Merge Vector Layers dialog, click the ... button next to Input layers. Select both the amenity_bar_london and amenity_pub_london layers. Click OK.
../../_images/1310.png
  1. Click ... button next to Merged and select Save to GeoPackage.
../../_images/148.png
  1. Browse to the directory where you want to save the data and name the output longon.gpkg.
../../_images/158.png
  1. Enter bars_and_pubs as the Layer name.
../../_images/167.png
  1. Click Run to execute the merge process.
../../_images/178.png
  1. Once the processing finishes, you will see a new layer bars_and_pubs added in the Layers panel. You will see that this layer is the union of all features from both the previous layers. Right-click the bars_and_pubs layer and select Open Attribute Table.
../../_images/187.png
  1. In the Attribute Table, you will see that the layer contains both pub and bar amenity types along with the names of these establishments and other attributes.
../../_images/197.png
  1. We have achieved the objective of extracting the bars and pub locations in London. We had to perform 2 separate queries to get the relevant data and merge it. This is fine for our task, but you maybe in a situation where you need to perform a complex query to get the right set of data for your project. Fortunately, the QuickOSM plugin provides a way to write and execute custom queries. Let’s see how we can write a singel query for the task at hand. Switch to the QuickOSM window and click Show query.
../../_images/206.png
  1. The plugin will switch to the Query tab. The Overpass query section will show the query that was constructed based on the user input. This field is editable and one can enter any query. The format of the query is in the Overpass Query Language (QL). For our purpose, select the section between the <query> ... </query> XML tags and copy it.
../../_images/2111.png
  1. Paste it after the existing query section and change the value from pub to bar. Below is the full query that will fetch values from both the tags in a single query. Click Run query.
<osm-script output="xml" timeout="25">
<id-query {{geocodeArea:London}} into="area_0"/>
<union>
    <query type="node">
        <has-kv k="amenity" v="pub"/>
        <area-query from="area_0"/>
    </query>
    <query type="node">
        <has-kv k="amenity" v="bar"/>
        <area-query from="area_0"/>
    </query>
</union>
<union>
    <item/>
    <recurse type="down"/>
</union>
<print mode="body"/>
</osm-script>
../../_images/228.png
  1. Once the query finishes, you will see a new layer OsmQuery added to the Layers panel. This layer contains points representing both bars and pubs in London.
../../_images/234.png
comments powered by Disqus

This work is licensed under a Creative Commons Attribution 4.0 International License