主要内容

本页采用了机器翻译。点击此处可查看英文原文。

从 OpenStreetMap 文件中读取数据

自 R2023b 起

本示例演示了如何将 OpenStreetMap® 文件导入地理空间表,然后根据标签查找表中存储的位置。

OpenStreetMap 文件通过标签 提供有关位置的信息。每个标签由一个键和一个值组成。

  • 为该位置提供了上下文信息。OpenStreetMap 支持数千个键,例如 highwaybuildingaddress

  • 提供了有关该键的详细信息。每个键可以支持数百个值。例如,highway 键的值包括 "yes""primary""footway""cycleway"

地理空间表是一种 tabletimetable 对象,其中包含一个 Shape 表变量和属性表变量。当您使用 readgeotable 函数将 OpenStreetMap 文件中的数据读入地理空间表时,该地理空间表会通过属性表变量来存储这些标签。对于常见标签,变量的名称与键名一致。对于不常见的标签,该表会将键和值存储在 other_tags_dictionary 变量中。存储在 OpenStreetMap 文件中的大多数位置并非所有标签都有值。

从文件中读取数据

指定一个包含日本东京涩谷区多个街区数据的 OpenStreetMap 文件名称 [1]

filename = "shibuya.osm";

将文件中的线和点图层读入地理空间表中。

  • 线条图层表示诸如道路、人行道和铁路轨道等特征。该表格通过线形在地理坐标系中表示线条。

  • 点图层表示诸如交通信号灯、公交站和地铁入口等要素。该表格通过点形状在地理坐标系中表示点位。

linesLayer = readgeotable(filename,Layer="lines");
pointsLayer = readgeotable(filename,Layer="points");

按标签查找地点

显示 OpenStreetMap 文件中带有 railway 标签的线和点。OpenStreetMap 文件使用 railway 标签来指定有关铁路的信息。例如,railway 标签可将一条线标识为地铁线路,将一个点标识为车站入口。

查询存储在表变量中的标签

“lines”图层将 railway 键作为表变量包含其中。您可以使用 matches 函数来验证该表是否包含 railway 变量。

matches("railway",linesLayer.Properties.VariableNames)
ans = logical
   1

查找包含 railway 标签的表格行。如果某行数据中没有 railway 标签,则该行 railway 键的值是 ""。根据带有 railway 标签的表行创建一个新的地理空间表。

idxRailways = ~ismissing(linesLayer.railway,"");
railways = linesLayer(idxRailways,:);

在地图上显示带有 railway 标签的线形要素。

figure
geoplot(railways)
title("Railway Lines")

Figure contains an axes object with type geoaxes. The geoaxes object contains an object of type line.

存储在 other_tags_dictionary 中的查询标签

由于 railway 标签在点图层中较少见,因此点图层会将 railway 标签的实例存储在 other_tags_dictionary 变量中。您可以使用 matches 函数来验证该表中是否不存在 railway 变量。

matches("railway",pointsLayer.Properties.VariableNames)
ans = logical
   0

变量 other_tags_dictionary 将标签存储在字典的元胞数组中。使用循环查找表中包含 railway 标签的行。对于表格的每一行,从存储在 other_tags_dictionary 变量中的元胞数组中获取字典。然后,判断该字典中是否包含 railway 标签。

numRows = height(pointsLayer);
hasRailwayTag = false(numRows,1);
for row = 1:numRows
    dict = pointsLayer.other_tags_dictionary{row};
    hasRailwayTag(row) = isKey(dict,"railway");
end

请确认表中至少有一行包含 railway 标签。

any(hasRailwayTag)
ans = logical
   1

根据带有 railway 标签的表行创建一个新的地理空间表。

railpoints = pointsLayer(hasRailwayTag,:);

将带有 railway 标签的点与线显示在同一张地图上。

hold on
geoplot(railpoints,"*")
title("Railway Lines and Points")

Figure contains an axes object with type geoaxes. The geoaxes object contains 2 objects of type line, point.

查找具有指定值的标签

显示代表人行道的线条和代表地铁入口的点。OpenStreetMap 文件使用 highway 键和 "footway" 值来标识人行道,使用 railway 键和 "subway_entrance" 值来标识地铁入口。

查询表变量中存储的标签和值

“lines”图层将 highway 键作为表变量包含其中。您可以使用 matches 函数来验证该表是否包含 highway 变量。

matches("highway",linesLayer.Properties.VariableNames)
ans = logical
   1

找出代表人行道的表格行。根据“footpath”表中的行创建一个新的地理空间表。

isFootpath = linesLayer.highway == "footway";
footway = linesLayer(isFootpath,:);

在地图上显示人行道。放大查看涩谷站周围的人行道。

figure
geoplot(footway)
geolimits([35.6583 35.6602],[139.698 139.7015])
title("Footpaths")

Figure contains an axes object with type geoaxes. The geoaxes object contains an object of type line.

存储在 other_tags_dictionary 中的查询标签和值

other_tags_dictionary 变量中提取 railway 标签的实例,并使用一个新的表变量存储这些值。

“点”图层将 railway 标签的实例存储在 other_tags_dictionary 变量中。您可以使用 matches 函数来验证该表中是否不存在 railway 变量。

matches("railway",pointsLayer.Properties.VariableNames)
ans = logical
   0

变量 other_tags_dictionary 将标签存储在字典的元胞数组中。创建一个名为 railway 的新表变量,其中包含 "" 的值。然后,使用循环将数据赋值给该变量。对于表格的每一行,从存储在 other_tags_dictionary 变量中的元胞数组中获取字典。如果字典中包含 railway 标签,则将该值添加到 railway 变量中。

numRows = height(pointsLayer);
pointsLayer.railway = strings(numRows,1);
for row = 1:numRows
    dict = pointsLayer.other_tags_dictionary{row};
    hasTag = isKey(dict,"railway");
    if hasTag
        pointsLayer.railway(row) = dict("railway");
    end
end

通过查询 railway 变量,找出代表地铁入口的表行。根据表中的行创建一个新的地理空间表。

idxEntrance = pointsLayer.railway == "subway_entrance";
entrance = pointsLayer(idxEntrance,:);

从表格中提取纬度和经度坐标。然后,在同一张地图上,使用图钉图标显示地铁入口。

lat = entrance.Shape.Latitude;
lon = entrance.Shape.Longitude;

hold on
geoiconchart(lat,lon,SizeData=30)
title("Footpaths and Subway Entrances")

Figure contains an axes object with type geoaxes. The geoaxes object contains 2 objects of type line, iconchart.

[1] You can download OpenStreetMap files from https://www.openstreetmap.org, which provides access to crowd-sourced map data all over the world.The data is licensed under the Open Data Commons Open Database License (ODbL), https://opendatacommons.org/licenses/odbl/.

另请参阅

函数

对象

主题