显示 OpenStreetMap 文件中的建筑物
本主题演示了如何将 OpenStreetMap® 文件中的建筑轮廓读取到地理空间表中,然后利用颜色和数据提示在二维地图上可视化建筑的属性。有关在三维球体上显示 OpenStreetMap 文件中的建筑物的信息,请参阅addCustomBuildings。
当您使用 readgeotable 函数从 OpenStreetMap 文件中读取建筑物信息时,该函数会从文件中提取建筑物相关信息,并将结果存储在地理空间表中。该地理空间表包含建筑物的占地范围、占地范围的质心,以及建筑高度、颜色和材料的信息。
从 OpenStreetMap 文件中读取建筑物数据
从一个包含日本东京涩谷区多个街区数据的 OpenStreetMap 文件 [1] 中读取建筑图层。该文件中的建筑物已对颜色和材质进行了修改。生成的地理空间表使用地理坐标系中的多边形来表示建筑物。
buildingsLayer = readgeotable("shibuya.osm",Layer="buildings");
可视化建筑物的最大高度
该地理空间表将建筑物的高度存储在 MaxHeight 表变量中。使用 geoplot 函数在地图上显示建筑物的占地范围。通过指定 ColorVariable 名称-值参量,根据高度调整图表颜色。
figure
geoplot(buildingsLayer,ColorVariable="MaxHeight")添加标题,更改颜色图,并添加带标签的颜色栏。
title("Maximum Heights of Buildings") colormap sky c = colorbar; c.Label.String = "Height in Meters";

建筑材料可视化
该地理空间表将建筑材料存储在 Material 表变量中。使用不同的颜色分别表示每种材料的建筑占地范围。
当 OpenStreetMap 文件未指定建筑物的材质时,该表会将 Material 的值存储为 ""。为了便于阅读,请找出表格中未指定建筑材料的行,并将 "" 替换为 "unknown"。
idxNoMaterial = buildingsLayer.Material == ""; buildingsLayer.Material(idxNoMaterial) = "unknown";
找出存储在表格中的独特建筑材料。
materials = unique(buildingsLayer.Material)
materials = 4×1 string
"concrete"
"glass"
"stone"
"unknown"
在道路地图上显示建筑物的占地范围。要根据材料生成图例,请使用循环显示每种材料的建筑占地范围。在循环内部:
从唯一材料列表中获取一种材料。
找出使用该材料的表行。
根据该表的行创建一个子表。
显示子表中的建筑物。
figure geobasemap streets hold on for k = 1:length(materials) m = materials(k); idxm = buildingsLayer.Material == m; rows = buildingsLayer(idxm,:); geoplot(rows,DisplayName=m); end
添加标题和图例,并将建筑物的透明度调高一些。
title("Building Materials")
legend
alpha(1)
可视化建筑色彩
该地理空间表将建筑物的颜色存储在 Color 表变量中。使用文件中存储的颜色来可视化建筑物的占地范围。
当 OpenStreetMap 文件未指定建筑物的颜色时,该表会将颜色存储为 ""。找出表格中未指定建筑材料的行,并将 "" 替换为深灰色。请使用十六进制颜色代码指定颜色。
idxNoColor = buildingsLayer.Color == ""; buildingsLayer.Color(idxNoColor) = "#565656";
查找存储在地理空间表中的唯一颜色代码。
colors = unique(buildingsLayer.Color)
colors = 5×1 string
"#027880"
"#3BAA32"
"#565656"
"#808000"
"#8E4585"
在道路地图上显示建筑物的占地范围。要应用每种颜色,请使用循环显示建筑物的占地范围。在循环内部:
从颜色代码列表中获取一个颜色代码。
找出表格中使用了该颜色代码的行。
根据该表的行创建一个子表。
显示子表中的建筑物。使用颜色代码指定图表颜色。
figure geobasemap streets hold on for k = 1:length(colors) c = colors(k); idxc = buildingsLayer.Color == c; rows = buildingsLayer(idxc,:); geoplot(rows,FaceColor=c); end
添加标题,并提高建筑物的不透明度。
title("Building Colors")
alpha(1)
为建筑物添加数据提示
使用数据提示显示建筑物的名称和高度。
当 OpenStreetMap 文件未指定建筑名称时,该表会将名称存储为 ""。查找表中未指定楼名的行,并将 "" 替换为 "unknown"。
idxNoName = buildingsLayer.Name == ""; buildingsLayer.Name(idxNoName) = "unknown";
在卫星底图上使用多边形显示建筑物。将 Polygon 对象转换为 pg,为建筑物添加数据提示做好准备。让建筑物看起来更不透明一些。
figure geobasemap satellite pg = geoplot(buildingsLayer); title("Buildings in Shibuya") alpha(0.7)
通过在 Polygon 对象的数据提示模板中添加建筑名称和高度,自定义数据提示的内容。有关在多边形上创建自定义数据提示的更多信息,请参阅 Add Data Tips to Point, Line, and Polygon Shapes。
dtRowName = dataTipTextRow("Name",buildingsLayer.Name); pg.DataTipTemplate.DataTipRows(1) = dtRowName; dtRowMaterial = dataTipTextRow("Height (m)",buildingsLayer.MaxHeight); pg.DataTipTemplate.DataTipRows(2) = dtRowMaterial;
找出表格中与最高建筑相对应的那一行。然后,在最高的建筑上添加一个数据提示。将 Polygon 对象和行号作为输入传递给 datatip 函数。
tallest = max(buildingsLayer.MaxHeight); idxTallest = find(buildingsLayer.MaxHeight == tallest); dt = datatip(pg,DataIndex=idxTallest);

[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/.