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,28 @@
cmake_minimum_required(VERSION 3.10)
project(osmp)
find_package(tinyxml2 CONFIG REQUIRED)
file(GLOB_RECURSE CPP_FILES
"src/*.cpp"
)
file(GLOB_RECURSE HPP_FILES
"include/*.hpp"
)
get_target_property(TINYXML2_INCLUDE_DIR tinyxml2::tinyxml2 INTERFACE_INCLUDE_DIRECTORIES)
add_library(osmp STATIC
${CPP_FILES} ${HPP_FILES}
)
target_include_directories(osmp PUBLIC
include
${TINYXML2_INCLUDE_DIR}
)
target_link_libraries(osmp PRIVATE
tinyxml2::tinyxml2
)

View file

@ -0,0 +1,34 @@
#pragma once
#include <vector>
#include "util.hpp"
namespace osmp
{
class Object;
class Node
{
public:
Node(const tinyxml2::XMLElement* xml, Object* parent);
const std::vector<Tag>& GetTags() const;
size_t GetTagsSize() const;
const Tag& GetTag(size_t index) const;
private:
Object* parent;
std::vector<Tag> tags;
public:
unsigned int id;
float lat, lon;
std::string user;
unsigned int uid;
bool visible;
std::string version;
unsigned int changeset;
std::string timestamp;
};
}

View file

@ -0,0 +1,37 @@
#pragma once
#include <string>
#include <map>
#include <vector>
#include <util.hpp>
namespace osmp
{
class Node;
class Way;
class Object
{
public:
Object(const std::string& file);
~Object();
std::vector<Node*> GetNodes() const;
size_t GetNodesSize() const;
const Node* GetNode(unsigned int id) const;
std::vector<Way*> GetWays() const;
size_t GetWaysSize() const;
const Way* GetWay(unsigned int id) const;
public:
const std::string version;
const std::string generator;
Bounds bounds;
private:
std::map<unsigned int, Node*> nodes;
std::map<unsigned int, Way*> ways;
};
}

View file

@ -0,0 +1,5 @@
#pragma once
#include <osmobject.hpp>
#include <osmnode.hpp>
#include <osmway.hpp>

View file

@ -0,0 +1,39 @@
#pragma once
#include <vector>
#include <util.hpp>
namespace osmp
{
class Object;
class Node;
class Way
{
public:
Way(const tinyxml2::XMLElement* way_elem, Object* parent);
const std::vector<Tag>& GetTags() const;
size_t GetTagsSize() const;
const Tag& GetTag(size_t index) const;
const std::vector<const Node*>& GetNodes() const;
size_t GetNodesSize() const;
const Node& GetNode(size_t index) const;
public:
unsigned int id;
std::string user;
unsigned int uid;
bool visible;
std::string version;
unsigned int changeset;
std::string timestamp;
private:
Object* parent;
std::vector<const Node*> nodes;
std::vector<Tag> tags;
};
}

View file

@ -0,0 +1,27 @@
#pragma once
#include <string>
namespace tinyxml2
{
class XMLElement;
}
namespace osmp
{
typedef struct sBounds
{
float minlat, minlon, maxlat, maxlon;
} Bounds;
typedef struct sTag
{
std::string k; // TODO: Should/could be an enum
std::string v;
} Tag;
std::string GetSafeAttributeString(const tinyxml2::XMLElement* elem, const std::string& name);
float GetSafeAttributeFloat(const tinyxml2::XMLElement* elem, const std::string& name);
unsigned int GetSafeAttributeUint(const tinyxml2::XMLElement* elem, const std::string& name);
bool GetSafeAttributeBool(const tinyxml2::XMLElement* elem, const std::string& name);
}

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;
}
}