prettymaps

库

marceloprates/prettymaps

用matplotlib绘制漂亮的OpenStreetMap地图。

项目简介

一个基于OpenStreetMap数据绘制定制地图的极简Python库,支持图层、样式和预设,适用于生成艺术与制图。

README 预览

# prettymaps\n\nA minimal Python library to draw customized maps from [OpenStreetMap](https://www.openstreetmap.org/#map=12/11.0733/106.3078) created using the [osmnx](https://github.com/gboeing/osmnx), [matplotlib](https://matplotlib.org/), [shapely](https://shapely.readthedocs.io/en/stable/index.html) and [vsketch](https://github.com/abey79/vsketch) packages.\n\n\n\n# [](https://marceloprates.github.io/prettymaps/) [](https://pypi.org/project/prettymaps/) [](https://www.python.org/) [](LICENSE)\n\n\nThis work is [licensed](LICENSE) under a GNU Affero General Public License v3.0 (you can make commercial use, distribute and modify this project, but must **disclose** the source code with the license and copyright notice)\n\n## Note about crediting and NFTs:\n- Please keep the printed message on the figures crediting my repository and OpenStreetMap ([mandatory by their license](https://www.openstreetmap.org/copyright)).\n- I am personally **against** NFTs for their [environmental impact](https://earth.org/nfts-environmental-impact/), the fact that they're a [giant money-laundering pyramid scheme](https://twitter.com/smdiehl/status/1445795667826208770) and the structural incentives they create for [theft](https://twitter.com/NFTtheft) in the open source and generative art communities.\n- **I do not authorize in any way this project to be used for selling NFTs**, although I cannot legally enforce it. **Respect the creator**.\n- The [AeternaCivitas](https://magiceden.io/marketplace/aeterna_civitas) and [geoartnft](https://www.geo-nft.com/) projects have used this work to sell NFTs and refused to credit it. See how they reacted after being exposed: [AeternaCivitas](https://github.com/marceloprates/prettymaps/raw/main/pictures/NFT_theft_AeternaCivitas.jpg), [geoartnft](https://github.com/marceloprates/prettymaps/raw/main/pictures/NFT_theft_geoart.jpg).\n- **I have closed my other generative art projects on Github and won't be sharing new ones as open source to protect me f

常见问题 (5)

故障排除
如何修复在 prettymaps 的 plotter 模式下使用 `save_as` 时生成的 SVG 文件为空白的问题?

这是 prettymaps 的 plotter 模式中的一个 bug,而不是你的绘图参数有问题。该 bug 已在提交 3f3b8be 中修复。请更新到包含该修复的版本,然后重新运行绘图。在 Google Colab 中,使用以下命令安装修复版本:

!pip install git+https://github.com/marceloprates/prettymaps.git@3f3b8be

如果提示重启运行时,请重启,然后再次运行你的代码,例如:

prettymaps.plot(..., mode='plotter', save_as='barcelona_plot.svg')

生成的 SVG 应该不再是空白的。

来源 Issue #138
故障排除
为什么 Prettymaps 的首次渲染需要大约 10 分钟,而且几乎没有 CPU 占用?

延迟是由 OSMnx 等待公共 Overpass API 造成的,该 API 会对请求进行限流。使用 ox.config(log_file=True) 启用日志,并查找类似 "Pausing 56 seconds before making HTTP POST request" 的消息。因此,首次获取属于网络受限,而非 CPU 受限。OSM 响应会缓存在磁盘上,因此后续渲染同一区域时会快得多。请在多次运行之间保留缓存,更新到包含磁盘缓存修复的版本,并缩小地图范围或减少 OSM 图层数量,以尽量减少 Overpass 请求。

来源 Issue #22
故障排除
如何修复在 prettymaps 中按名称绘制城市时河流或水域几何图形缺失的问题?

这是由于几何图形与边界(perimeter)的相交操作丢弃了跨越命名区域边界的 OSM 要素所致。作为变通方法,请使用坐标和半径(而非地名)进行绘图:

plot((48.8534, 2.3397), radius=7000, ...)

若要保留命名边界,请修改 prettymaps/fetch.py 中的 get_geometries()。将:

geometries = geometries.intersection(perimeter)

替换为:

geometries = [geo.intersection(perimeter).buffer(0) for geo in geometries.geometry]

.buffer(0) 调用可修复无效的相交结果并保留多个多边形。或者,升级到包含该修复的 prettymaps 版本,该修复在 issue #51 / "Fix geometry-perimeter intersection" PR 中进行跟踪。

来源 Issue #46
故障排除
如何防止一个具有大量依赖的 Python 包破坏我现有的 Python 安装?

在隔离的虚拟环境中安装软件包,而不是安装到系统 Python 中。使用 venv 时,运行:python3 -m venv .venv,然后在 Linux/macOS 上使用 source .venv/bin/activate 激活它,在 Windows 上使用 .venv\Scripts\activate 激活,再使用 pip install <package-name> 安装软件包。使用 Conda 时,运行:conda create -n myenv python=3.11 -y,然后运行 conda activate myenv,再使用 pip install <package-name> 或 conda install <package-name> 安装软件包。这样可以使依赖版本与操作系统的 Python 相互隔离,避免版本冲突。

来源 Issue #16
故障排除
如何在不改变道路宽度的情况下,为桥梁或特定的 OSM 街道标签设置不同的颜色?

将桥梁绘制为独立的图层,并应用逐图层的样式。对于桥梁结构,定义一个图层,例如 "bridge": {"tags": {"man_made": "bridge"}}。对于带有桥梁标签的 highway 路段,使用一个单独的道路图层,通过 custom_filter 筛选出 bridge=yes 的 ways。为该图层设置不同的颜色和更高的 zorder/z-height,使其渲染在普通道路图层之上,同时保持基础 highway 图层的宽度配置不变。如果样式 API 要求显式值,则将桥梁图层设置为与道路图层相同的线宽映射。

来源 Issue #12