Add project files.

This commit is contained in:
Robert 2021-04-14 21:04:08 +02:00
parent 18b81590d3
commit 045f3a8439
15 changed files with 1303019 additions and 0 deletions

View file

@ -0,0 +1,49 @@
#include <osmnode.hpp>
#include <tinyxml2.h>
namespace xml = tinyxml2;
namespace osmp
{
Node::Node(const tinyxml2::XMLElement* node_elem, Object* parent) :
parent(parent)
{
// Get Attribute
id = GetSafeAttributeUint(node_elem, "id");
lat = GetSafeAttributeFloat(node_elem, "lat");
lon = GetSafeAttributeFloat(node_elem, "lon");
user = GetSafeAttributeString(node_elem, "user");
uid = GetSafeAttributeUint(node_elem, "uid");
visible = GetSafeAttributeBool(node_elem, "visible");
version = GetSafeAttributeString(node_elem, "version");
changeset = GetSafeAttributeUint(node_elem, "changeset");
timestamp = GetSafeAttributeString(node_elem, "timestamp");
const xml::XMLElement* tag_element = node_elem->FirstChildElement("tag");
while (tag_element != nullptr)
{
tags.push_back({
GetSafeAttributeString(tag_element, "k"),
GetSafeAttributeString(tag_element, "v")
});
tag_element = tag_element->NextSiblingElement("tag");
}
}
const std::vector<Tag>& Node::GetTags() const
{
return tags;
}
size_t Node::GetTagsSize() const
{
return tags.size();
}
const Tag& Node::GetTag(size_t index) const
{
return tags[index];
}
}

View file

@ -0,0 +1,118 @@
#include <osmobject.hpp>
#include <iostream>
#include <vector>
#include <tinyxml2.h>
#include <osmnode.hpp>
#include <osmway.hpp>
namespace xml = tinyxml2;
namespace osmp
{
Object::Object(const std::string& file) :
bounds({ 0.0f, 0.0f, 0.0f, 0.0f })
{
xml::XMLDocument doc;
xml::XMLError result = doc.LoadFile(file.c_str());
if (result != xml::XML_SUCCESS)
{
std::cerr << "Error: " << result << std::endl;
return;
}
xml::XMLElement* root = doc.FirstChildElement();
// Get bounds
xml::XMLElement* bounds_elem = root->FirstChildElement("bounds");
bounds = {
GetSafeAttributeFloat(bounds_elem, "minlat"),
GetSafeAttributeFloat(bounds_elem, "minlon"),
GetSafeAttributeFloat(bounds_elem, "maxlat"),
GetSafeAttributeFloat(bounds_elem, "maxlon")
};
// Get nodes
xml::XMLElement* node_elem = root->FirstChildElement("node");
while (node_elem != nullptr)
{
Node* new_node = new Node(node_elem, this);
nodes.insert(std::make_pair(new_node->id, new_node));
node_elem = node_elem->NextSiblingElement("node");
}
// Get ways
xml::XMLElement* way_elem = root->FirstChildElement("way");
while (way_elem != nullptr)
{
Way* new_way = new Way(way_elem, this);
ways.insert(std::make_pair(new_way->id, new_way));
way_elem = way_elem->NextSiblingElement("way");
}
}
Object::~Object()
{
for (std::map<unsigned int, Way*>::iterator it = ways.begin(); it != ways.end(); ++it)
{
delete it->second;
}
ways.clear();
for (std::map<unsigned int, Node*>::iterator it = nodes.begin(); it != nodes.end(); ++it)
{
delete it->second;
}
nodes.clear();
}
std::vector<Node*> Object::GetNodes() const
{
std::vector<Node*> vecNodes;
for (std::map<unsigned int, Node*>::const_iterator it = nodes.begin(); it != nodes.end(); it++)
vecNodes.push_back(it->second);
return vecNodes;
}
size_t Object::GetNodesSize() const
{
return nodes.size();
}
const Node* Object::GetNode(unsigned int id) const
{
std::map<unsigned int, Node*>::const_iterator node = nodes.find(id);
if (node != nodes.end())
return node->second;
return nullptr;
}
std::vector<Way*> Object::GetWays() const
{
std::vector<Way*> vecWays;
for (std::map<unsigned int, Way*>::const_iterator it = ways.begin(); it != ways.end(); it++)
vecWays.push_back(it->second);
return vecWays;
}
size_t Object::GetWaysSize() const
{
return ways.size();
}
const Way* Object::GetWay(unsigned int id) const
{
std::map<unsigned int, Way*>::const_iterator way = ways.find(id);
if (way != ways.end())
return way->second;
return nullptr;
}
}

View file

@ -0,0 +1,73 @@
#include <osmway.hpp>
#include <tinyxml2.h>
#include <osmobject.hpp>
namespace xml = tinyxml2;
namespace osmp
{
Way::Way(const tinyxml2::XMLElement* way_elem, Object* parent) :
parent(parent)
{
// Attributes
id = GetSafeAttributeUint(way_elem, "id");
user = GetSafeAttributeString(way_elem, "user");
uid = GetSafeAttributeUint(way_elem, "uid");
visible = GetSafeAttributeBool(way_elem, "visible");
version = GetSafeAttributeString(way_elem, "version");
changeset = GetSafeAttributeUint(way_elem, "changeset");
timestamp = GetSafeAttributeString(way_elem, "timestamp");
const xml::XMLElement* nd_elem = way_elem->FirstChildElement("nd");
while (nd_elem != nullptr)
{
nodes.push_back(
parent->GetNode(GetSafeAttributeUint(nd_elem, "ref"))
);
nd_elem = nd_elem->NextSiblingElement("nd");
}
const xml::XMLElement* tag_elem = way_elem->FirstChildElement("tag");
while (tag_elem != nullptr)
{
tags.push_back({
GetSafeAttributeString(tag_elem, "k"),
GetSafeAttributeString(tag_elem, "v")
});
tag_elem = tag_elem->NextSiblingElement("tag");
}
}
const std::vector<Tag>& Way::GetTags() const
{
return tags;
}
size_t Way::GetTagsSize() const
{
return tags.size();
}
const Tag& Way::GetTag(size_t index) const
{
return tags[index];
}
const std::vector<const Node*>& Way::GetNodes() const
{
return nodes;
}
size_t Way::GetNodesSize() const
{
return nodes.size();
}
const Node& Way::GetNode(size_t index) const
{
return *(nodes[index]);
}
}

View file

@ -0,0 +1,59 @@
#include <util.hpp>
#include <iostream>
#include <tinyxml2.h>
namespace xml = tinyxml2;
namespace osmp
{
#define FAILED(err) (err != xml::XML_SUCCESS)
std::string GetSafeAttributeString(const tinyxml2::XMLElement* elem, const std::string& name)
{
const char* buffer;
xml::XMLError result = elem->QueryStringAttribute(name.c_str(), &buffer);
if (FAILED(result))
{
std::cerr << "Failed to fetch string attribute \"" << name << "\"" << std::endl;
return "";
}
std::string returnStr(buffer);
return returnStr;
}
float GetSafeAttributeFloat(const tinyxml2::XMLElement* elem, const std::string& name)
{
float returnVal = 0.0f;
xml::XMLError result = elem->QueryFloatAttribute(name.c_str(), &returnVal);
if (FAILED(result))
std::cerr << "Failed to fetch float attribute \"" << name << "\"" << std::endl;
return returnVal;
}
unsigned int GetSafeAttributeUint(const tinyxml2::XMLElement* elem, const std::string& name)
{
unsigned int returnVal = 0;
xml::XMLError result = elem->QueryUnsignedAttribute(name.c_str(), &returnVal);
if (FAILED(result))
std::cerr << "Failed to fetch uint attribute \"" << name << "\"" << std::endl;
return returnVal;
}
bool GetSafeAttributeBool(const tinyxml2::XMLElement* elem, const std::string& name)
{
bool returnVal = false;
xml::XMLError result = elem->QueryBoolAttribute(name.c_str(), &returnVal);
if (FAILED(result))
std::cerr << "Failed to fetch bool attribute \"" << name << "\"" << std::endl;
return returnVal;
}
}