查看: 817|回复: 0

osmnx——Python OpenStreetMap库

[复制链接]
发表于 昨天 13:03 | 显示全部楼层 |阅读模式
<section class="_editor" data-support="96编辑器" data-style-id="47562" style="margin-bottom:unset;"><section data-align="title" style="text-align:center;margin:10px 0px;padding:5px 0px;box-sizing:border-box;"><section style="display:inline-block;margin-bottom:unset;"><section style="width:60%;height:1px;background-color:rgb(148, 134, 255);margin-left:auto;margin-bottom:-4px;overflow:hidden;line-height:0;transform:rotate(0deg);-webkit-transform:rotate(0deg);-moz-transform:rotate(0deg);-ms-transform:rotate(0deg);-o-transform:rotate(0deg);" data-width="60%"></section><section style="width:1px;height:25px;background-color:rgb(148, 134, 255);margin-left:auto;margin-bottom:-23px;margin-right:2px;overflow:hidden;line-height:0;transform:rotate(0deg);-webkit-transform:rotate(0deg);-moz-transform:rotate(0deg);-ms-transform:rotate(0deg);-o-transform:rotate(0deg);"></section><section style="background-color:rgb(237, 252, 255);padding:5px 10px;display:flex;justify-content:center;align-items:center;box-sizing:border-box;margin-bottom:unset;"><section style="width:15px;margin-bottom:unset;"><p></p></section><section style="font-size:16px;color:#9486FF;letter-spacing:2px;margin:0px 10px;"><p><strong>点击蓝字 关注我们</strong></p></section><section style="width:5px;height:5px;background-color:rgb(255, 217, 83);border-radius:50%;box-sizing:border-box;margin-bottom:unset;overflow:hidden;line-height:0;"></section></section><section style="width:1px;height:25px;background-color:rgb(148, 134, 255);margin-top:-23px;margin-left:2px;margin-bottom:unset;overflow:hidden;line-height:0;"></section><section style="width:60%;height:1px;background-color:rgb(148, 134, 255);margin-top:-4px;margin-bottom:unset;overflow:hidden;line-height:0;" data-width="60%"></section></section></section></section><p><span style='font-family: 微软雅黑, "Microsoft YaHei";'>《osmnx,一个非常实用但容易被忽视的宝藏Python库》</span></p><br/>

<p> osmnx——Python OpenStreetMap库-7985 </p><br/>

<section class="_editor" data-style-id="49598" data-support="96缂栬緫鍣�">

<section data-align="title" style="margin: 10px 0px; display: flex; justify-content: flex-start; align-items: flex-end;">

<section style="color: #9486ff; line-height: 1; font-size: 32px; z-index: 1;">

<p>

<span style="font-size: 50px;"><strong>1.</strong></span>

</p>

</section>

<section data-width="55%" style="width: 55%; margin-left: -7px;">

<p style="line-height: 1; letter-spacing: 2px; margin-left: 18px;">

<span style="color: #9486ff; font-size: 20px;"><strong>引言</strong></span>

</p>

<section style="height: 6px; line-height: 0; overflow: hidden; background-color: #f4f4f4;"></section>

</section>

<section style="width: 57px; margin-left: 5px;">

<p>

<br/>

</p>

</section>

</section>

</section>

<section class="_editor">

<p>

<br/>

</p>

</section>

<p><span style='font-family: 微软雅黑, "Microsoft YaHei";'>你是否曾经需要获取某个城市的街道地图数据,或者想分析某个地区的交通网络?你或许会想到使用一些商业地图API,但它们往往伴随着使用限制和费用。今天,我要向你介绍一个开源的Python库——osmnx,它能让你轻松地从OpenStreetMap

(OSM) 获取和处理地理空间数据,开启无限可能。</span></p><br/>

<p> osmnx——Python OpenStreetMap库-9924 </p><br/>

<section class="_editor" data-style-id="49598" data-support="96缂栬緫鍣�">

<section data-align="title" style="margin: 10px 0px; display: flex; justify-content: flex-start; align-items: flex-end;">

<section style="color: #9486ff; line-height: 1; font-size: 32px; z-index: 1;">

<p>

<span style="font-size: 50px;"><strong>2.</strong></span>

</p>

</section>

<section data-width="55%" style="width: 55%; margin-left: -7px;">

<p style="line-height: 1; letter-spacing: 2px; margin-left: 18px;">

<span style="color: #9486ff; font-size: 20px;"><strong>安装和配置osmnx</strong></span>

</p>

<section style="height: 6px; line-height: 0; overflow: hidden; background-color: #f4f4f4;"></section>

</section>

<section style="width: 57px; margin-left: 5px;">

<p>

<br/>

</p>

</section>

</section>

</section>

<section class="_editor">

<p>

<br/>

</p>

</section>

<p><span style='font-family: 微软雅黑, "Microsoft YaHei";'>安装osmnx非常简单,只需使用pip即可:</span></p><br/>

<p> osmnx——Python OpenStreetMap库-9482 osmnx——Python OpenStreetMap库-3837 </p><br/>

<p><span style='font-family: 微软雅黑, "Microsoft YaHei";'>pip install osmnx</span></p><br/>

<p>osmnx依赖于一些关键库,例如<code>geopandas</code>、<code>requests</code>和<code>shapely</code>。上述命令会自动安装这些依赖项。

如果你遇到安装问题,特别是<code>geopandas</code>的安装,请确保你的系统已经安装了必要的地理空间库,例如<code>GDAL</code>、<code>Fiona</code>和<code>pyproj</code>。

可以尝试使用conda安装这些库:</p><br/>

<p> osmnx——Python OpenStreetMap库-7073 osmnx——Python OpenStreetMap库-5723 </p><br/>

<p><span style='font-family: 微软雅黑, "Microsoft YaHei";'>conda install -c conda-forge geopandas</span></p><br/>

<p><span style='font-family: 微软雅黑, "Microsoft YaHei";'>最佳实践是创建一个虚拟环境来安装osmnx及其依赖项,以避免与其他Python项目产生冲突:</span></p><br/>

<p> osmnx——Python OpenStreetMap库-4596 python3 -m venv .venv

    source .venv/bin/activate  # Linux/macOS

    .venv\Scripts\activate  # Windows

    pip install osmnx</p><br/>

<section class="_editor" data-style-id="49598" data-support="96缂栬緫鍣�">

<section data-align="title" style="margin: 10px 0px; display: flex; justify-content: flex-start; align-items: flex-end;">

<section style="color: #9486ff; line-height: 1; font-size: 32px; z-index: 1;">

<p>

<span style="font-size: 50px;"><strong>3.</strong></span>

</p>

</section>

<section data-width="55%" style="width: 55%; margin-left: -7px;">

<p style="line-height: 1; letter-spacing: 2px; margin-left: 18px;">

<span style="color: #9486ff; font-size: 20px;"><strong>基本用法/核心概念</strong></span>

</p>

<section style="height: 6px; line-height: 0; overflow: hidden; background-color: #f4f4f4;"></section>

</section>

<section style="width: 57px; margin-left: 5px;">

<p>

<br/>

</p>

</section>

</section>

</section>

<section class="_editor">

<p>

<br/>

</p>

</section>

<p><span style='font-family: 微软雅黑, "Microsoft YaHei";'>osmnx的核心功能是下载和处理地理空间数据。让我们从一个简单的例子开始,获取北京市的地图数据:import osmnx as ox</span></p><br/>

<pre class="highlight" style="

        background-color: #282c34;

        border: 1px solid #3e4451;

        border-radius: 6px;

        color: #abb2bf;

        font-family: 'Menlo', 'Monaco', 'Courier New', monospace;

        font-size: 14px;

        line-height: 1.6;

        margin-bottom: 1.6em;

        max-width: 100%;

        overflow-x: auto;

        padding: 16px 16px 16px 56px;

        position: relative;

        white-space: pre-wrap;

        word-wrap: break-word;

    "><div style="

        content: '';

        position: absolute;

        top: 0;

        left: 0;

        width: 100%;

        height: 36px;

        background-color: #21252b;

        border-top-left-radius: 6px;

        border-top-right-radius: 6px;

    "><span style="

        content: '';

        position: absolute;

        top: 12px;

        width: 12px;

        height: 12px;

        border-radius: 50%;

    left: 13px; background-color: #ff5f56;"></span><span style="

        content: '';

        position: absolute;

        top: 12px;

        width: 12px;

        height: 12px;

        border-radius: 50%;

    left: 33px; background-color: #ffbd2e;"></span><span style="

        content: '';

        position: absolute;

        top: 12px;

        width: 12px;

        height: 12px;

        border-radius: 50%;

    left: 53px; background-color: #27c93f;"></span></div><div style="padding-top: 33px; overflow-x: auto;"><div style="padding-left: 20px; position: relative; white-space: pre-wrap; word-wrap: break-word;"><span style="position: absolute; left: -40px; color: #636d83; user-select: none;">1</span><code># 获取北京市的路网图</code></div><div style="padding-left: 20px; position: relative; white-space: pre-wrap; word-wrap: break-word;"><span style="position: absolute; left: -40px; color: #636d83; user-select: none;">2</span><code>place = “Beijing, China”</code></div><div style="padding-left: 20px; position: relative; white-space: pre-wrap; word-wrap: break-word;"><span style="position: absolute; left: -40px; color: #636d83; user-select: none;">3</span><code>graph = ox.graph_from_place(place, network_type='drive')</code></div><div style="padding-left: 20px; position: relative; white-space: pre-wrap; word-wrap: break-word;"><span style="position: absolute; left: -40px; color: #636d83; user-select: none;">5</span><code># 将路网图保存为shapefile文件</code></div><div style="padding-left: 20px; position: relative; white-space: pre-wrap; word-wrap: break-word;"><span style="position: absolute; left: -40px; color: #636d83; user-select: none;">6</span><code>ox.save_graph_shapefile(graph, filepath=“./beijing_road_network.shp”)</code></div><div style="padding-left: 20px; position: relative; white-space: pre-wrap; word-wrap: break-word;"><span style="position: absolute; left: -40px; color: #636d83; user-select: none;">8</span><code># 可视化路网图</code></div><div style="padding-left: 20px; position: relative; white-space: pre-wrap; word-wrap: break-word;"><span style="position: absolute; left: -40px; color: #636d83; user-select: none;">9</span><code>fig, ax = ox.plot_graph(graph)</code></div></div></pre>

<p>这段代码首先使用<code>ox.graph_from_place()</code>函数,根据地名“Beijing,

China”下载了北京市的道路网络数据,并将其存储在<code>graph</code>对象中。<code>network_type='drive'</code>参数指定下载的是可驾驶道路网络。使用<code>ox.save_graph_shapefile()</code>函数将路网图保存为shapefile文件,方便在GIS软件中使用。使用<code>ox.plot_graph()</code>函数将路网图可视化。</p><br/>

<section class="_editor" data-style-id="49598" data-support="96缂栬緫鍣�">

<section data-align="title" style="margin: 10px 0px; display: flex; justify-content: flex-start; align-items: flex-end;">

<section style="color: #9486ff; line-height: 1; font-size: 32px; z-index: 1;">

<p>

<span style="font-size: 50px;"><strong>4.</strong></span>

</p>

</section>

<section data-width="55%" style="width: 55%; margin-left: -7px;">

<p style="line-height: 1; letter-spacing: 2px; margin-left: 18px;">

<span style="color: #9486ff; font-size: 20px;"><strong>进阶技巧/最佳实践</strong></span>

</p>

<section style="height: 6px; line-height: 0; overflow: hidden; background-color: #f4f4f4;"></section>

</section>

<section style="width: 57px; margin-left: 5px;">

<p>

<br/>

</p>

</section>

</section>

</section>

<section class="_editor">

<p>

<br/>

</p>

</section>

<p><span style='font-family: 微软雅黑, "Microsoft YaHei";'>除了下载路网图,osmnx还可以下载建筑物、兴趣点(POI)等其他地理空间数据。我们可以下载北京市内的所有学校:import osmnx as ox</span></p><br/>

<pre class="highlight" style="

        background-color: #282c34;

        border: 1px solid #3e4451;

        border-radius: 6px;

        color: #abb2bf;

        font-family: 'Menlo', 'Monaco', 'Courier New', monospace;

        font-size: 14px;

        line-height: 1.6;

        margin-bottom: 1.6em;

        max-width: 100%;

        overflow-x: auto;

        padding: 16px 16px 16px 56px;

        position: relative;

        white-space: pre-wrap;

        word-wrap: break-word;

    "><div style="

        content: '';

        position: absolute;

        top: 0;

        left: 0;

        width: 100%;

        height: 36px;

        background-color: #21252b;

        border-top-left-radius: 6px;

        border-top-right-radius: 6px;

    "><span style="

        content: '';

        position: absolute;

        top: 12px;

        width: 12px;

        height: 12px;

        border-radius: 50%;

    left: 13px; background-color: #ff5f56;"></span><span style="

        content: '';

        position: absolute;

        top: 12px;

        width: 12px;

        height: 12px;

        border-radius: 50%;

    left: 33px; background-color: #ffbd2e;"></span><span style="

        content: '';

        position: absolute;

        top: 12px;

        width: 12px;

        height: 12px;

        border-radius: 50%;

    left: 53px; background-color: #27c93f;"></span></div><div style="padding-top: 33px; overflow-x: auto;"><div style="padding-left: 20px; position: relative; white-space: pre-wrap; word-wrap: break-word;"><span style="position: absolute; left: -40px; color: #636d83; user-select: none;">1</span><code># 获取北京市内的所有学校</code></div><div style="padding-left: 20px; position: relative; white-space: pre-wrap; word-wrap: break-word;"><span style="position: absolute; left: -40px; color: #636d83; user-select: none;">2</span><code>tags = {“amenity”: “school”}</code></div><div style="padding-left: 20px; position: relative; white-space: pre-wrap; word-wrap: break-word;"><span style="position: absolute; left: -40px; color: #636d83; user-select: none;">3</span><code>schools = ox.geometries_from_place(place, tags=tags)</code></div><div style="padding-left: 20px; position: relative; white-space: pre-wrap; word-wrap: break-word;"><span style="position: absolute; left: -40px; color: #636d83; user-select: none;">5</span><code># 将学校数据保存为GeoPackage文件</code></div><div style="padding-left: 20px; position: relative; white-space: pre-wrap; word-wrap: break-word;"><span style="position: absolute; left: -40px; color: #636d83; user-select: none;">6</span><code>ox.save_gdf_geopackage(schools, filepath=“./beijing_schools.gpkg”)</code></div><div style="padding-left: 20px; position: relative; white-space: pre-wrap; word-wrap: break-word;"><span style="position: absolute; left: -40px; color: #636d83; user-select: none;">8</span><code># 可视化学校位置</code></div><div style="padding-left: 20px; position: relative; white-space: pre-wrap; word-wrap: break-word;"><span style="position: absolute; left: -40px; color: #636d83; user-select: none;">9</span><code>fig, ax = ox.plot_footprints(schools)</code></div></div></pre>

<p><span style='font-family: 微软雅黑, "Microsoft YaHei";'>在处理大型数据集时,为了提高效率,建议使用缓存机制。osmnx默认使用磁盘缓存,可以避免重复下载相同的数据。</span></p><br/>

<section class="_editor" data-style-id="49598" data-support="96缂栬緫鍣�">

<section data-align="title" style="margin: 10px 0px; display: flex; justify-content: flex-start; align-items: flex-end;">

<section style="color: #9486ff; line-height: 1; font-size: 32px; z-index: 1;">

<p>

<span style="font-size: 50px;"><strong>5.</strong></span>

</p>

</section>

<section data-width="55%" style="width: 55%; margin-left: -7px;">

<p style="line-height: 1; letter-spacing: 2px; margin-left: 18px;">

<span style="color: #9486ff; font-size: 20px;"><strong>实战案例/应用场景</strong></span>

</p>

<section style="height: 6px; line-height: 0; overflow: hidden; background-color: #f4f4f4;"></section>

</section>

<section style="width: 57px; margin-left: 5px;">

<p>

<br/>

</p>

</section>

</section>

</section>

<section class="_editor">

<p>

<br/>

</p>

</section>

<p><span style='font-family: 微软雅黑, "Microsoft YaHei";'>osmnx可以应用于各种地理空间数据分析场景,例如:</span></p><br/>

<ul>

<li><strong style="color: #2a5eff; background-color: #ffffff;">交通网络分析:</strong> 计算最短路径、分析交通流量、识别交通瓶颈等。</li>

<li><strong style="color: #2a5eff; background-color: #ffffff;">城市规划:</strong> 分析城市空间结构、评估可达性、优化公共交通线路等。</li>

<li><strong style="color: #2a5eff; background-color: #ffffff;">物流配送:</strong> 规划配送路线、优化仓库选址等。</li>

<li><strong style="color: #2a5eff; background-color: #ffffff;">灾害应急:</strong> 分析受灾区域、规划救援路线等。</li>

</ul><br/>

<section class="_editor" data-style-id="49598" data-support="96缂栬緫鍣�">

<section data-align="title" style="margin: 10px 0px; display: flex; justify-content: flex-start; align-items: flex-end;">

<section style="color: #9486ff; line-height: 1; font-size: 32px; z-index: 1;">

<p>

<span style="font-size: 50px;"><strong>6.</strong></span>

</p>

</section>

<section data-width="55%" style="width: 55%; margin-left: -7px;">

<p style="line-height: 1; letter-spacing: 2px; margin-left: 18px;">

<span style="color: #9486ff; font-size: 20px;"><strong>总结/展望</strong></span>

</p>

<section style="height: 6px; line-height: 0; overflow: hidden; background-color: #f4f4f4;"></section>

</section>

<section style="width: 57px; margin-left: 5px;">

<p>

<br/>

</p>

</section>

</section>

</section>

<section class="_editor">

<p>

<br/>

</p>

</section>

<p><span style='font-family: 微软雅黑, "Microsoft YaHei";'>osmnx是一个功能强大且易于使用的Python库,它为我们提供了一种便捷的方式来获取和处理OpenStreetMap数据。它在城市规划、交通分析、物流配送等领域具有广泛的应用前景。

虽然它是一个相对小众的库,但它绝对是一个值得你探索的宝藏。</span></p><br/>

<section class="_editor" data-style-id="49598" data-support="96缂栬緫鍣�">

<section data-align="title" style="margin: 10px 0px; display: flex; justify-content: flex-start; align-items: flex-end;">

<section style="color: #9486ff; line-height: 1; font-size: 32px; z-index: 1;">

<p>

<span style="font-size: 50px;"><strong>7.</strong></span>

</p>

</section>

<section data-width="55%" style="width: 55%; margin-left: -7px;">

<p style="line-height: 1; letter-spacing: 2px; margin-left: 18px;">

<span style="color: #9486ff; font-size: 20px;"><strong>彩蛋/额外福利</strong></span>

</p>

<section style="height: 6px; line-height: 0; overflow: hidden; background-color: #f4f4f4;"></section>

</section>

<section style="width: 57px; margin-left: 5px;">

<p>

<br/>

</p>

</section>

</section>

</section>

<section class="_editor">

<p>

<br/>

</p>

</section>

<p><span style='font-family: 微软雅黑, "Microsoft YaHei";'>除了官方文档,你还可以参考一些优秀的博客和教程来学习osmnx的更多用法。 你也可以加入osmnx的社区论坛,与其他用户交流经验和心得。

希望osmnx能帮助你更好地利用OpenStreetMap数据,创造更多价值!</span></p><br/><section class="_editor" data-support="96编辑器" data-style-id="48822" style="margin-bottom:unset;"><section style="margin:10px 0px;"><section style="background-color:rgb(250, 247, 255);padding:15px 10px;border-radius:15px;margin-bottom:unset;box-sizing:border-box;"><section style="display:flex;justify-content:flex-start;align-items:center;margin-bottom:unset;"><section style="background-color:rgb(148, 134, 255);padding:5px 25px;border-radius:30px;margin-bottom:unset;box-sizing:border-box;"><section style="font-size:16px;letter-spacing:2px;color:#fefefe;margin-bottom:unset;"><p><strong>往期回顾</strong></p></section></section><section style="font-size:12px;color:#9486ff;letter-spacing:5px;margin-left:10px;margin-bottom:unset;"><p><br></p></section></section><section style="background-color:rgb(254, 254, 254);padding:10px;margin-top:10px;margin-bottom:unset;box-sizing:border-box;"><section style="display:flex;justify-content:flex-start;align-items:center;margin-bottom:unset;"><section style="width:10%;margin-bottom:unset;" data-width="10%"><section style="text-align:center;width:37px;height:37px;background-color:rgb(148, 134, 255);border-radius:50%;margin:auto;box-sizing:border-box;transform:rotate(0deg);-webkit-transform:rotate(0deg);-moz-transform:rotate(0deg);-ms-transform:rotate(0deg);-o-transform:rotate(0deg);"><section style="font-size:15px;line-height:37px;color:#fefefe;margin-bottom:unset;"><p><strong><span style="font-size:13px;">01</span></strong></p></section></section></section><section style="width:80%;margin-bottom:unset;" data-width="80%"><section style="font-size:14px;letter-spacing:1px;line-height:1.75;margin-bottom:unset;"><p><br></p></section></section></section><section style="display:flex;justify-content:flex-start;align-items:center;margin-top:11px;margin-bottom:unset;" class="_li"><section style="width:10%;margin-bottom:unset;" data-width="10%"><section style="text-align:center;width:37px;height:37px;background-color:rgb(148, 134, 255);border-radius:50%;margin:auto;box-sizing:border-box;transform:rotate(0deg);-webkit-transform:rotate(0deg);-moz-transform:rotate(0deg);-ms-transform:rotate(0deg);-o-transform:rotate(0deg);"><section style="font-size:15px;line-height:37px;color:#fefefe;margin-bottom:unset;"><p><span style="font-size:13px;"><strong>02</strong></span></p></section></section></section><section style="width:80%;margin-bottom:unset;" data-width="80%"><section style="font-size:14px;letter-spacing:2px;line-height:1.75;margin-bottom:unset;"><p><br></p></section></section></section><section style="display:flex;justify-content:flex-start;align-items:center;margin-top:11px;margin-bottom:unset;" class="_li"><section style="width:10%;margin-bottom:unset;" data-width="10%"><section style="text-align:center;width:37px;height:37px;background-color:rgb(148, 134, 255);border-radius:50%;margin:auto;box-sizing:border-box;transform:rotate(0deg);-webkit-transform:rotate(0deg);-moz-transform:rotate(0deg);-ms-transform:rotate(0deg);-o-transform:rotate(0deg);"><section style="font-size:15px;line-height:37px;color:#fefefe;margin-bottom:unset;"><p><span style="font-size:13px;"><strong>03</strong></span></p></section></section></section><section style="width:80%;margin-bottom:unset;" data-width="80%"><section style="font-size:14px;letter-spacing:2px;line-height:1.75;margin-bottom:unset;"><p><br></p></section></section></section></section></section></section></section><section class="_editor" data-support="96编辑器" data-style-id="47567" style="margin-bottom:unset;"><section style="margin:10px 0px;"><section style="display:flex;width:70%;margin-left:auto;justify-content:space-around;align-items:flex-end;margin-bottom:unset;" data-width="70%"><section style="text-align:center;margin-bottom:unset;"><section style="width:25px;margin:auto;margin-bottom:8px;"><p> osmnx——Python OpenStreetMap库-1 </p></section><section style="font-size:13px;letter-spacing:2px;margin-bottom:unset;"><p>分享</p></section></section><section style="text-align:center;margin-bottom:unset;"><section style="width:25px;margin:auto;margin-bottom:8px;"><p> osmnx——Python OpenStreetMap库-6246 </p></section><section style="font-size:13px;letter-spacing:2px;margin-bottom:unset;"><p>收藏</p></section></section><section style="text-align:center;margin-bottom:unset;"><section style="width:27px;margin:auto;margin-bottom:8px;"><p> osmnx——Python OpenStreetMap库-7499 </p></section><section style="font-size:13px;letter-spacing:2px;margin-bottom:unset;"><p>在看</p></section></section><section style="text-align:center;margin-bottom:unset;"><section style="width:22px;margin:auto;margin-bottom:8px;"><p></p></section><section style="font-size:13px;letter-spacing:2px;margin-bottom:unset;"><p>点赞</p></section></section></section></section></section>
您需要登录后才可以回帖 登录 | 加入联盟

本版积分规则

快速回复 返回顶部 返回列表