如何在Matplotlib底图上使用OpenStreetMap背景

时间:2022-02-06 23:07:34

This should be simple, but when I look for it I just find web packages. I need something better than as oriented on This Blog. Maybe using .oms file or shapefiles. Some way to give bbox and get the OpenStreetMap background on Basemap map.

这应该很简单,但是当我寻找它时,我只能找到Web包。我需要的东西比在这个博客上的导向更好。也许使用.oms文件或shapefile。某种方式给bbox并在Basemap地图上获取OpenStreetMap背景。

I found some questions like this on Stack, but the answers directs to, or download the .png file on OpenStreetMap website, or to use some web package.

我在Stack上发现了一些像这样的问题,但答案是指向或下载OpenStreetMap网站上的.png文件,或者使用一些网络包。

3 个解决方案

#1


I would suggest not to try to make something work, which is not made (yet) to work together. There is a simple way to achieve what you want with Mplleaflet. https://github.com/jwass/mplleaflet The library allows you to visualize geographic data on a beautiful interactive openstreetmap. Map projection of data in long lat format is automatically performed.

我建议不要尝试做一些有用的工作,但这些工作还没有完成。有一种简单的方法可以通过Mplleaflet实现您的目标。 https://github.com/jwass/mplleaflet该库允许您在美丽的交互式openstreetmap上可视化地理数据。自动执行长lat格式的数据映射投影。

Installation in windows and ubuntu is easy:

在windows和ubuntu中安装很简单:

pip install mplleaflet

You can start with the provided examples and go from there.

您可以从提供的示例开始,然后从那里开始。

#2


There are many libraries today that can do this for you - smopy, folium and tilemapbase are three examples from my recent use.

今天有很多图书馆可以为你做这些 - smopy,folium和tilemapbase是我最近使用的三个例子。

Each of these tools fetch map tiles from the one of several servers that host OSM or other (Stamen, Carto, etc) map tiles and then allows you to display and plot on them using matplotlib. Tilemapbase also caches the tiles locally so that they are not fetched again the next time.

这些工具中的每一个都从托管OSM或其他(Stamen,Carto等)地图图块的几个服务器中的一个获取地图图块,然后允许您使用matplotlib显示和绘制它们。 Tilemapbase还会在本地缓存切片,以便下次不再提取它们。

But there does not seem to be a readily available tool yet, based on my recent experience, to use offline tilesets (such as a compressed .mbtiles file) as background for matplotlib plotting.

但根据我最近的经验,似乎没有一个现成的工具可以使用离线tileset(例如压缩的.mbtiles文件)作为matplotlib绘图的背景。

This link contains a survey of the above tools and more - https://github.com/ispmarin/maps

此链接包含对上述工具的调查以及更多 - https://github.com/ispmarin/maps

EDIT

I had mentioned in my previous answer that Tilemapbase did not work for some geographical locations in the world, and hence explicitly recommended not to use it. But it turns out I was wrong, and I apologize for that. It actually works great! The problem in my case was embarrassingly simple - I had reversed the order or lat and lon while fetching tiles, and hence it always fetched blank tiles for certain geographical locations, leading me to assume that it did not work for those locations.

我在之前的回答中提到Tilemapbase不适用于世界上的某些地理位置,因此明确建议不要使用它。但事实证明我错了,我为此道歉。它实际上很棒!在我的情况下的问题是令人尴尬的简单 - 我在取出瓷砖时颠倒了顺序或者拉特和lon,因此它总是为某些地理位置获取空白区块,这使我认为它不适用于那些位置。

I had raised the issue in github and it was immediately resolved by the developers. See it here - https://github.com/MatthewDaws/TileMapBase/issues/7

我在github上提出了这个问题,开发人员立即解决了这个问题。在此处查看 - https://github.com/MatthewDaws/TileMapBase/issues/7

Note the responses:

请注意回复:

Coordinates are to be provided in order (1) longitude, (2) latitude. If you copied them from Google Maps, they will be in lat/lon order and you have to flip them. So your map image is not empty, it's just a location in the ocean north of Norway.

坐标顺序为(1)经度,(2)纬度。如果您从Google地图中复制它们,它们将按照纬度/经度顺序排列,您必须将它们翻转。所以你的地图图像不是空的,它只是挪威北部海洋中的一个位置。

And from the developer himself:

从开发者本人:

Yes, when I wrote the code, it seemed that there wasn't a universal standard for ordering. So I chose the one which is different to Google Maps. The method name from_lonlat should give a hint as to the correct ordering...

是的,当我编写代码时,似乎没有一个通用的订购标准。所以我选择了与谷歌地图不同的那个。方法名称from_lonlat应该给出正确排序的提示......

#3


You can download the necessary tiles yourself from one of the tile servers. The OSM wiki explains the technical details behind slippy map tilenames and also includes examples for various programming and scripting languages.

您可以从其中一个磁贴服务器自行下载必要的磁贴。 OSM wiki解释了slippy map tilenames背后的技术细节,还包括各种编程和脚本语言的示例。

Please also read about the tile usage policy and keep in mind that different tile serves may have different policies.

另请阅读有关磁贴使用政策的信息,请注意不同的磁贴服务可能有不同的策略。

#1


I would suggest not to try to make something work, which is not made (yet) to work together. There is a simple way to achieve what you want with Mplleaflet. https://github.com/jwass/mplleaflet The library allows you to visualize geographic data on a beautiful interactive openstreetmap. Map projection of data in long lat format is automatically performed.

我建议不要尝试做一些有用的工作,但这些工作还没有完成。有一种简单的方法可以通过Mplleaflet实现您的目标。 https://github.com/jwass/mplleaflet该库允许您在美丽的交互式openstreetmap上可视化地理数据。自动执行长lat格式的数据映射投影。

Installation in windows and ubuntu is easy:

在windows和ubuntu中安装很简单:

pip install mplleaflet

You can start with the provided examples and go from there.

您可以从提供的示例开始,然后从那里开始。

#2


There are many libraries today that can do this for you - smopy, folium and tilemapbase are three examples from my recent use.

今天有很多图书馆可以为你做这些 - smopy,folium和tilemapbase是我最近使用的三个例子。

Each of these tools fetch map tiles from the one of several servers that host OSM or other (Stamen, Carto, etc) map tiles and then allows you to display and plot on them using matplotlib. Tilemapbase also caches the tiles locally so that they are not fetched again the next time.

这些工具中的每一个都从托管OSM或其他(Stamen,Carto等)地图图块的几个服务器中的一个获取地图图块,然后允许您使用matplotlib显示和绘制它们。 Tilemapbase还会在本地缓存切片,以便下次不再提取它们。

But there does not seem to be a readily available tool yet, based on my recent experience, to use offline tilesets (such as a compressed .mbtiles file) as background for matplotlib plotting.

但根据我最近的经验,似乎没有一个现成的工具可以使用离线tileset(例如压缩的.mbtiles文件)作为matplotlib绘图的背景。

This link contains a survey of the above tools and more - https://github.com/ispmarin/maps

此链接包含对上述工具的调查以及更多 - https://github.com/ispmarin/maps

EDIT

I had mentioned in my previous answer that Tilemapbase did not work for some geographical locations in the world, and hence explicitly recommended not to use it. But it turns out I was wrong, and I apologize for that. It actually works great! The problem in my case was embarrassingly simple - I had reversed the order or lat and lon while fetching tiles, and hence it always fetched blank tiles for certain geographical locations, leading me to assume that it did not work for those locations.

我在之前的回答中提到Tilemapbase不适用于世界上的某些地理位置,因此明确建议不要使用它。但事实证明我错了,我为此道歉。它实际上很棒!在我的情况下的问题是令人尴尬的简单 - 我在取出瓷砖时颠倒了顺序或者拉特和lon,因此它总是为某些地理位置获取空白区块,这使我认为它不适用于那些位置。

I had raised the issue in github and it was immediately resolved by the developers. See it here - https://github.com/MatthewDaws/TileMapBase/issues/7

我在github上提出了这个问题,开发人员立即解决了这个问题。在此处查看 - https://github.com/MatthewDaws/TileMapBase/issues/7

Note the responses:

请注意回复:

Coordinates are to be provided in order (1) longitude, (2) latitude. If you copied them from Google Maps, they will be in lat/lon order and you have to flip them. So your map image is not empty, it's just a location in the ocean north of Norway.

坐标顺序为(1)经度,(2)纬度。如果您从Google地图中复制它们,它们将按照纬度/经度顺序排列,您必须将它们翻转。所以你的地图图像不是空的,它只是挪威北部海洋中的一个位置。

And from the developer himself:

从开发者本人:

Yes, when I wrote the code, it seemed that there wasn't a universal standard for ordering. So I chose the one which is different to Google Maps. The method name from_lonlat should give a hint as to the correct ordering...

是的,当我编写代码时,似乎没有一个通用的订购标准。所以我选择了与谷歌地图不同的那个。方法名称from_lonlat应该给出正确排序的提示......

#3


You can download the necessary tiles yourself from one of the tile servers. The OSM wiki explains the technical details behind slippy map tilenames and also includes examples for various programming and scripting languages.

您可以从其中一个磁贴服务器自行下载必要的磁贴。 OSM wiki解释了slippy map tilenames背后的技术细节,还包括各种编程和脚本语言的示例。

Please also read about the tile usage policy and keep in mind that different tile serves may have different policies.

另请阅读有关磁贴使用政策的信息,请注意不同的磁贴服务可能有不同的策略。