gpt4 book ai didi

php - OSM 数据解析以获取带有子节点的节点

转载 作者:可可西里 更新时间:2023-11-01 00:14:52 28 4
gpt4 key购买 nike

我下载了一个小区域的 Open Street Map 数据,我想过滤数据以获得具有特殊类别的节点。

这是 OSM 数据的示例

 <node id="505126369" lat="31.2933856" lon="34.2687443" user="JumpStart International" uid="125156" visible="true" version="1" changeset="2568758" timestamp="2009-09-22T13:05:10Z"/>
<node id="505126372" lat="31.2682934" lon="34.2745680" user="JumpStart International" uid="125156" visible="true" version="1" changeset="2568758" timestamp="2009-09-22T13:05:10Z"/>
<node id="505126375" lat="31.2953082" lon="34.3471630" user="JumpStart International" uid="125156" visible="true" version="1" changeset="2568758" timestamp="2009-09-22T13:05:10Z"/>
<node id="505126378" lat="31.2807872" lon="34.2757999" user="JumpStart International" uid="125156" visible="true" version="1" changeset="2568758" timestamp="2009-09-22T13:05:11Z">
<tag k="amenity" v="school"/>
<tag k="name" v="Al Aqqad Basic &amp; Secondary Female School"/>
<tag k="name:ar" v="مدرسة العقاد الأساسية والثانوية للبنات"/>
</node>

我想获取整个学校、医院的数据。

如果有人用 PHP 或 Java 进行了 XML 解析,我将非常感谢与我和所有感兴趣的人分享它。

编辑这是我刚刚的一个简单的开始

$dataFile = base_url() . 'media/files/osmdata/map_3.xml';
//echo ($dataFile);

$xml = simplexml_load_file($dataFile);

// $countTotal = count($xml->node);
// echo 'here'.$countTotal;
foreach ($xml as $key => $val) {
var_dump($val);
// can't manage things overs here

}

最佳答案

以下是我编译的带有 PHP SimpleXML 的 OSM Overpass API 小示例,因为我们这里没有它用于 PHP,而且我喜欢 OSM,所以让我们展示一些有用的示例。

第一部分展示了如何使用标准 PHP 查询立交桥端点。您不需要该部分,因为您已经将数据保存在硬盘上:

<?php
/**
* OSM Overpass API with PHP SimpleXML / XPath
*
* PHP Version: 5.4 - Can be back-ported to 5.3 by using 5.3 Array-Syntax (not PHP 5.4's square brackets)
*/


//
// 1.) Query an OSM Overpass API Endpoint
//

$query = 'node
["amenity"~".*"]
(38.415938460513274,16.06338500976562,39.52205163048525,17.51220703125);
out;';

$context = stream_context_create(['http' => [
'method' => 'POST',
'header' => ['Content-Type: application/x-www-form-urlencoded'],
'content' => 'data=' . urlencode($query),
]]);

# please do not stress this service, this example is for demonstration purposes only.
$endpoint = 'http://overpass-api.de/api/interpreter';
libxml_set_streams_context($context);
$start = microtime(true);

$result = simplexml_load_file($endpoint);
printf("Query returned %2\$d node(s) and took %1\$.5f seconds.\n\n", microtime(true) - $start, count($result->node));

对你来说,第二部分更有趣。那就是查询您已有的 XML 数据。这使用 xpath 最容易完成,所使用的 PHP XML 库基于支持 XPath 1.0 的 libxml。很好的涵盖了各种查询需求。

以下示例列出所有学校 并尝试获取它们的名称。我还没有涉及翻译,因为我的示例数据没有这些,但您也可以查找包括翻译在内的所有类型的名称,并且只喜欢特定的名称):

//
// 2.) Work with the XML Result
//

# get all school nodes with xpath
$xpath = '//node[tag[@k = "amenity" and @v = "school"]]';
$schools = $result->xpath($xpath);
printf("%d School(s) found:\n", count($schools));
foreach ($schools as $index => $school)
{
# Get the name of the school (if any), again with xpath
list($name) = $school->xpath('tag[@k = "name"]/@v') + ['(unnamed)'];
printf("#%02d: ID:%' -10s [%s,%s] %s\n", $index, $school['id'], $school['lat'], $school['lon'], $name);
}

这里的关键点是 xpath 查询。使用了两个,第一个用于获取具有特定标签的节点。我认为这对您来说是最有趣的:

//node[tag[@k = "amenity" and @v = "school"]]

这一行说:给我所有 node 元素,这些元素有一个 tag 元素,里面有 k 属性值 "amenity "v 属性值 "school"。这是您必须过滤掉那些标记有舒适学校的节点的条件。

进一步再次使用xpath,现在相对于那些学校节点,查看是否有名称,如果有则获取它:

tag[@k = "name"]/@v'

这一行说:相对于当前节点,给我 tag 元素中的 v 属性作为 k 属性值 < em>“名字”。如您所见,某些部分再次与之前的行相似。我认为你们都可以根据自己的需要采用它们。

因为不是所有的 school 节点都有名称,所以通过将默认字符串添加到(然后为空的)结果数组来提供用于显示目的的默认字符串:

list($name) = $school->xpath('tag[@k = "name"]/@v') + ['(unnamed)'];
^^^^^^^^^^^^^^^
Provide Default Value

这里是我对该代码示例的结果:

Query returned 907 node(s) and took 1.10735 seconds.
10 School(s) found:
#00: ID:332534486 [39.5017565,16.2721899] Scuola Primaria
#01: ID:1428094278 [39.3320912,16.1862820] (unnamed)
#02: ID:1822746784 [38.9075566,16.5776597] (unnamed)
#03: ID:1822755951 [38.9120272,16.5713431] (unnamed)
#04: ID:1903859699 [38.6830409,16.5522243] Liceo Scientifico Statale A. Guarasci
#05: ID:2002566438 [39.1347698,16.0736924] (unnamed)
#06: ID:2056891127 [39.4106679,16.8254844] (unnamed)
#07: ID:2056892999 [39.4124687,16.8286119] (unnamed)
#08: ID:2272010226 [39.4481717,16.2894353] SCUOLA DELL'INFANZIA SAN FRANCESCO
#09: ID:2272017152 [39.4502366,16.2807664] SCUOLA MEDIA

我希望这已经有用了,如果您有更多需要说明的问题,请告诉我。


(by rbwilkinson): This is how you could add additional parameters to find other values. the following example finds other properties within one kilometer:

$query = 'node
["addr:postcode"~"RM12"]
(51.5557914,0.2118915,51.5673083,0.2369398);
node
(around:1000)
["amenity"~"fast_food"];
out;';

$context = stream_context_create(['http' => [
'method' => 'POST',
'header' => ['Content-Type: application/x-www-form-urlencoded'],
'content' => 'data=' . urlencode($query),
]]);

$endpoint = 'http://overpass-api.de/api/interpreter';
libxml_set_streams_context($context);

$result = simplexml_load_file($endpoint);
printf("Query returned %2\$d node(s) and took %1\$.5f seconds.\n\n", microtime(true) - $start, count($result->node));
}

关于php - OSM 数据解析以获取带有子节点的节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16129184/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com