User:Haribo/Auswertung/highway maxspeed

From OpenStreetMap Wiki
Jump to navigation Jump to search
// The code in this file is released into the Public Domain.

#include <string>
#include <map>
#include <iostream>

#include <osmium/io/any_input.hpp>
#include <osmium/geom/haversine.hpp>
#include <osmium/visitor.hpp>

#include <osmium/index/map/sparse_mem_array.hpp>
#include <osmium/handler/node_locations_for_ways.hpp>
typedef osmium::index::map::SparseMemArray<osmium::unsigned_object_id_type, osmium::Location> index_type;
typedef osmium::handler::NodeLocationsForWays<index_type> location_handler_type;
const char* highwayValue;

struct RoadLengthHandler : public osmium::handler::Handler {

    double length = 0;
    double count = 0;

		double length_maxspeed = 0;
    std::map<std::string, double> maxspeedValues;

    void way(const osmium::Way& way) {
        const char* highway = way.tags()["highway"];
        const char* maxspeed = way.tags()["maxspeed"];
        if (highway && !strcmp(highway,highwayValue)) {
					  double length_way = osmium::geom::haversine::distance(way.nodes());
						length += length_way;
						count++;
						if (maxspeed) {
							  length_maxspeed += length_way;
								if (maxspeedValues.count(std::string(maxspeed)) == 0) {
											maxspeedValues[std::string(maxspeed)] = length_way;
								}
								else {
										maxspeedValues.at(std::string(maxspeed)) += length_way;
								}

						}
        }
    }

};

int main(int argc, char* argv[]) {
    if (argc != 3) {
        std::cerr << "Usage: " << argv[0] << " PATH/OSMFILE  HIGHWAY_VALUE\n";
        exit(1);
    }
		highwayValue=argv[2];
    osmium::io::Reader reader(argv[1], osmium::osm_entity_bits::node | osmium::osm_entity_bits::way);

    index_type index;
    location_handler_type location_handler(index);
    RoadLengthHandler road_length_handler;

    osmium::apply(reader, location_handler, road_length_handler);

    std::cout << "Count\t" << road_length_handler.count << "\n";
    std::cout << "Length\t" << road_length_handler.length / 1000 << " km\n";
    std::cout << "Maxspeed\t" << road_length_handler.length_maxspeed / 1000 << " km\n";
		std::map<std::string, double>::iterator it;
		for(it = road_length_handler.maxspeedValues.begin(); it != road_length_handler.maxspeedValues.end(); it++) {
			std::cout << it->first << "\t" << it->second / 1000 << " km\n";
		}
}