analyze_pbf.cc 4.46 KB
Newer Older
Jean-Benoist Leger's avatar
Jean-Benoist Leger committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
/*
    This file is part of the osmpbf library.

    Copyright(c) 2014 Daniel Bahrdt.

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 3 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, see
    <http://www.gnu.org/licenses/>.
 */
#include "parse_way.h"
#include <iostream>
#include <osmpbf/osmfile.h>
#include <osmpbf/inode.h>
#include <osmpbf/iway.h>
#include <osmpbf/irelation.h>
#include <osmpbf/filter.h>
#include <osmpbf/primitiveblockinputadaptor.h>
#include <set>

struct srtm_tile_t
{
    int lon;
    int lat;

    srtm_tile_t(const int & lon_i, const int & lat_i) : lon(lon_i), lat(lat_i) {}
    srtm_tile_t(const srtm_tile_t & orig) : lon(orig.lon), lat(orig.lat) {}
    bool operator<(const srtm_tile_t & rhs) const
    {
        if(lon<rhs.lon)
            return true;
        if(lon>rhs.lon)
            return false;
        if(lat<rhs.lat)
            return true;
        return false;
    }

    bool operator==(const srtm_tile_t & rhs) const
    {
        return (lon == rhs.lon && lat == rhs.lat);
    }
};


void parseBlock_ways(
        osmpbf::PrimitiveBlockInputAdaptor & pbi,
        uint64_t & count,
        std::set<srtm_tile_t> & srtm)
{
		
	if (pbi.waysSize())
    {
		for (osmpbf::IWayStream way = pbi.getWayStream(); !way.isNull(); way.next())
        {
            std::string tag_highway, tag_cycleway, tag_bicycle, tag_oneway, tag_junction;
			for(uint32_t i = 0, s = way.tagsSize();  i < s; ++i)
            {
                if( way.key(i) == "highway" )
                    tag_highway = way.value(i);
                if( way.key(i) == "cycleway" )
                    tag_cycleway = way.value(i);
                if( way.key(i) == "bicycle" )
                    tag_bicycle = way.value(i);
                if( way.key(i) == "oneway" )
                    tag_oneway = way.value(i);
                if( way.key(i) == "junction" )
                    tag_junction = way.value(i);
			}

            bool we_use_it;
            way_kind_t way_kind;
            int oneway;

            parse_way(
                    we_use_it,
                    way_kind,
                    oneway,
                    tag_highway,
                    tag_cycleway,
                    tag_bicycle,
                    tag_oneway,
                    tag_junction);
    
            if(we_use_it)
            {
                for(osmpbf::IWay::RefIterator refIt(way.refBegin()), refEnd(way.refEnd()); refIt != refEnd; ++refIt)
                {
                    count++;
                }
            }
		}
	}	

	if (pbi.nodesSize())
    {
		for (osmpbf::INodeStream node = pbi.getNodeStream(); !node.isNull(); node.next())
        {
            double lon = node.lond();
            double lat = node.latd();

            int lon1 = (int)((lon*1200-3)/1200 + 360)-360;
            int lon2 = (int)((lon*1200+3)/1200 + 360)-360;
            int lat1 = (int)((lat*1200-3)/1200 + 360)-360;
            int lat2 = (int)((lat*1200+3)/1200 + 360)-360;

            srtm.insert(srtm_tile_t(lon1,lat1));
            srtm.insert(srtm_tile_t(lon1,lat2));
            srtm.insert(srtm_tile_t(lon2,lat1));
            srtm.insert(srtm_tile_t(lon2,lat2));
        }
    }
}

int main(int argc, char ** argv)
{
	if (argc < 1)
    {
		fprintf(stderr,"Usage: %s pbf\n",argv[0]);
        abort();
	}
    
    uint64_t count=0;
    std::set<srtm_tile_t> srtm;
        
    std::string inputFileName(argv[1]);


    osmpbf::OSMFileIn inFile(inputFileName, false);

    if (!inFile.open())
    {
        std::cout << "Failed to open " <<  inputFileName << std::endl;
        return -1;
    }

    osmpbf::PrimitiveBlockInputAdaptor pbi;
    while (inFile.parseNextBlock(pbi))
    {
        if (pbi.isNull())
            continue;
        parseBlock_ways(pbi, count, srtm);
    }


    for(auto srtm_it = srtm.begin();
            srtm_it != srtm.end();
            srtm_it++)
    {
        fprintf(stdout,"%i\t%i\n",srtm_it->lon,srtm_it->lat);
    }

    fprintf(stderr,"number of (way,node)s: %lu\n",count);
	return 0;
}