Skip to content

Commit

Permalink
add add_timestamp utility
Browse files Browse the repository at this point in the history
  • Loading branch information
woodpeck committed Jul 30, 2013
1 parent 6edb146 commit 8ec98a8
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ LDFLAGS = $(LIB_EXPAT) $(LIB_PBF)
PROGRAMS = \
count_addresses \
count \
osmgrep
osmgrep \
add_timestamp

.PHONY: all clean

Expand All @@ -51,6 +52,9 @@ count: count.cpp
osmgrep: osmgrep.cpp
$(CXX) $(CXXFLAGS) $(CXXFLAGS_LIBXML2) -o $@ $< $(LDFLAGS) $(LIB_XML2)

add_timestamp: add_timestamp.cpp
$(CXX) $(CXXFLAGS) $(CXXFLAGS_LIBXML2) -o $@ $< $(LDFLAGS) $(LIB_XML2)

clean:
rm -f *.o core $(PROGRAMS)

103 changes: 103 additions & 0 deletions add_timestamp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
Add a synthesized timestamp tag to all objects in the output file.
This makes little sense *except* if you are using some kind of
OSM file processor that cannot use the timestamp attribute;
using this program you can transform the timestamp attributo
into a proper tag which can then be used.
Written by Frederik Ramm <[email protected]>, public domain.
*/

#include <iostream>
#include <getopt.h>

#define OSMIUM_WITH_PBF_INPUT
#define OSMIUM_WITH_XML_INPUT
#include <osmium.hpp>
#include <osmium/output/xml.hpp>
#include <osmium/output/pbf.hpp>

void print_help()
{
std::cout << "add_timestamp <inputfile> <outputfile>\n\nadds a synthesized timestamp tag to all OSM objects.\n";
}

class AddTimestampHandler : public Osmium::Output::Handler
{
public:

AddTimestampHandler(Osmium::OSMFile& outfile) : Osmium::Output::Handler(outfile)
{
}

void node(const shared_ptr<Osmium::OSM::Node >& node)
{
node->tags().add("timestamp", node->timestamp_as_string().c_str());
Osmium::Output::Handler::node(node);
}
void way(const shared_ptr<Osmium::OSM::Way >& way)
{
way->tags().add("timestamp", way->timestamp_as_string().c_str());
Osmium::Output::Handler::way(way);
}
void relation(const shared_ptr<Osmium::OSM::Relation >& relation)
{
relation->tags().add("timestamp", relation->timestamp_as_string().c_str());
Osmium::Output::Handler::relation(relation);
}
};


int main(int argc, char* argv[])
{
static struct option long_options[] =
{
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0}
};

std::string input_format;
std::string output_format;

while (true)
{
int c = getopt_long(argc, argv, "h", long_options, 0);
if (c == -1)
{
break;
}

switch (c)
{
case 'h':
print_help();
exit(0);
default:
exit(1);
}
}

std::string input;
std::string output;
int remaining_args = argc - optind;
if (remaining_args != 2)
{
print_help();
exit(1);
}

input = argv[optind];
output = argv[optind+1];

Osmium::OSMFile infile(input);
Osmium::OSMFile outfile(output);

AddTimestampHandler ats(outfile);
ats.set_generator("add_timestamp");

Osmium::Input::read(infile, ats);
}

0 comments on commit 8ec98a8

Please sign in to comment.