包含具有无效位置的 NodeRef 的方式
Way containing NodeRefs with invalid location
我正在解析 mayotte pbf with osmium, and my handler is looking for ways. When I find one I process its barycentre and print it. The issue I ran into is that all of the ways I process have invalid location
。如果打印位置,我会得到纬度和经度的 undefined
。
我的 PBF 文件或我对 osmium library 的理解有问题吗?
这是一个 mcve:
/**
* To compile this script, you should first install `libosmium` and its
* dependencies. Then:
* g++ -std=c++11 -lz -lexpat -lbz2 mcve.cpp -o mcve
*/
#include <iostream>
#include <osmium/handler.hpp>
#include <osmium/io/any_input.hpp>
#include <osmium/osm/node.hpp>
#include <osmium/osm/way.hpp>
#include <osmium/visitor.hpp>
class ParkingAndCarpoolingAreasHandler : public osmium::handler::Handler {
public:
void way(const osmium::Way& way) {
double lng;
double lat;
double count = 0.0;
for (const osmium::NodeRef& nr : way.nodes()) {
if (!nr.location().valid()) {
std::cerr << "Way (id=" << way.id()
<< " version=" << way.version()
<< " timestamp=" << way.timestamp()
<< " visible=" << (way.visible() ? "true" : "false")
<< " changeset=" << way.changeset()
<< " uid=" << way.uid()
<< " user=" << way.user() << ")\n";
std::cerr << "NodeRef (ref=" << nr.ref() << " location=" << nr.location() << ")\n";
std::cerr << std::endl;
return;
}
count++;
lng += nr.location().lon();
lat += nr.location().lat();
}
lng /= count;
lat /= count;
std::cout << "POINT(" << lat << ' ' << lng << ")\n";
}
};
int main() {
auto otypes = osmium::osm_entity_bits::node | osmium::osm_entity_bits::way;
osmium::io::Reader reader{"tmp/mayotte-latest.osm.pbf", otypes};
ParkingAndCarpoolingAreasHandler handler;
osmium::apply(reader, handler);
reader.close();
}
在 OSM 中,一种方式通常只存储对它所包含的节点的引用。这些引用仅包含节点 ID,但不包含其他信息(例如坐标和标签)。要获得节点坐标,您必须查看实际节点,而不仅仅是它们的参考。
有关详细信息,请参阅 OSM XML and PBF Format。
由于我没有使用 osmium 的经验,所以我无法告诉您如何通过 ID 检索相应的节点。但是根据 Osmium Concepts Manual you can use a NodeLocationsForWays
handler to populate your NodeRef
objects with locations. examples/osmium_road_length.cpp 包含一个示例。
我正在解析 mayotte pbf with osmium, and my handler is looking for ways. When I find one I process its barycentre and print it. The issue I ran into is that all of the ways I process have invalid location
。如果打印位置,我会得到纬度和经度的 undefined
。
我的 PBF 文件或我对 osmium library 的理解有问题吗?
这是一个 mcve:
/**
* To compile this script, you should first install `libosmium` and its
* dependencies. Then:
* g++ -std=c++11 -lz -lexpat -lbz2 mcve.cpp -o mcve
*/
#include <iostream>
#include <osmium/handler.hpp>
#include <osmium/io/any_input.hpp>
#include <osmium/osm/node.hpp>
#include <osmium/osm/way.hpp>
#include <osmium/visitor.hpp>
class ParkingAndCarpoolingAreasHandler : public osmium::handler::Handler {
public:
void way(const osmium::Way& way) {
double lng;
double lat;
double count = 0.0;
for (const osmium::NodeRef& nr : way.nodes()) {
if (!nr.location().valid()) {
std::cerr << "Way (id=" << way.id()
<< " version=" << way.version()
<< " timestamp=" << way.timestamp()
<< " visible=" << (way.visible() ? "true" : "false")
<< " changeset=" << way.changeset()
<< " uid=" << way.uid()
<< " user=" << way.user() << ")\n";
std::cerr << "NodeRef (ref=" << nr.ref() << " location=" << nr.location() << ")\n";
std::cerr << std::endl;
return;
}
count++;
lng += nr.location().lon();
lat += nr.location().lat();
}
lng /= count;
lat /= count;
std::cout << "POINT(" << lat << ' ' << lng << ")\n";
}
};
int main() {
auto otypes = osmium::osm_entity_bits::node | osmium::osm_entity_bits::way;
osmium::io::Reader reader{"tmp/mayotte-latest.osm.pbf", otypes};
ParkingAndCarpoolingAreasHandler handler;
osmium::apply(reader, handler);
reader.close();
}
在 OSM 中,一种方式通常只存储对它所包含的节点的引用。这些引用仅包含节点 ID,但不包含其他信息(例如坐标和标签)。要获得节点坐标,您必须查看实际节点,而不仅仅是它们的参考。
有关详细信息,请参阅 OSM XML and PBF Format。
由于我没有使用 osmium 的经验,所以我无法告诉您如何通过 ID 检索相应的节点。但是根据 Osmium Concepts Manual you can use a NodeLocationsForWays
handler to populate your NodeRef
objects with locations. examples/osmium_road_length.cpp 包含一个示例。