File: osm_datasource.cpp

package info (click to toggle)
mapnik 2.0.0%2Bds1-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 35,496 kB
  • sloc: cpp: 91,793; python: 6,051; xml: 3,528; sh: 848; makefile: 70; lisp: 10
file content (187 lines) | stat: -rw-r--r-- 5,452 bytes parent folder | download
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*****************************************************************************
 * 
 * This file is part of Mapnik (c++ mapping toolkit)
 *
 * Copyright (C) 2006 Artem Pavlenko
 *
 * 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 2.1 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, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *****************************************************************************/

// mapnik
#include <mapnik/geom_util.hpp>
#include <mapnik/query.hpp>

// boost
#include <boost/make_shared.hpp>

// stl
#include <iostream>
#include <fstream>
#include <stdexcept>
#include <set>

#include "osm_datasource.hpp"
#include "osm_featureset.hpp"
#include "dataset_deliverer.h"
#include "osmtagtypes.h"
#include "osmparser.h"

DATASOURCE_PLUGIN(osm_datasource)

using mapnik::String;
using mapnik::Double;
using mapnik::Integer;
using mapnik::datasource_exception;
using mapnik::filter_in_box;
using mapnik::filter_at_point;
using mapnik::attribute_descriptor;

osm_datasource::osm_datasource(const parameters &params, bool bind)
   : datasource (params),
     type_(datasource::Vector),
     desc_(*params_.get<std::string>("type"), *params_.get<std::string>("encoding","utf-8")) 
{
    if (bind)
    {
        this->bind();
    }
}

void osm_datasource::bind() const
{
    if (is_bound_) return;

    osm_data_ = NULL;
    std::string osm_filename= *params_.get<std::string>("file","");
    std::string parser = *params_.get<std::string>("parser","libxml2");
    std::string url = *params_.get<std::string>("url","");
    std::string bbox = *params_.get<std::string>("bbox","");
    
    bool do_process=false;

    // load the data
    // if we supplied a filename, load from file
    if (url!="" && bbox!="")
    {
        // otherwise if we supplied a url and a bounding box, load from the url
#ifdef MAPNIK_DEBUG
    cerr<<"loading_from_url: url="<<url << " bbox="<<bbox<<endl;
#endif
        if((osm_data_=dataset_deliverer::load_from_url
            (url,bbox,parser))==NULL)
        {
            throw datasource_exception("Error loading from URL");
        }
        do_process=true;
    }
    else if(osm_filename!="")
    {
        if ((osm_data_=
            dataset_deliverer::load_from_file(osm_filename,parser))==NULL)
        {
            throw datasource_exception("Error loading from file");
        }
        do_process=true;
    }

    if(do_process==true)
    {
        osm_tag_types tagtypes;
        tagtypes.add_type("maxspeed",mapnik::Integer);
        tagtypes.add_type("z_order",mapnik::Integer);

        osm_data_->rewind();
        // Need code to get the attributes of all the data
        std::set<std::string> keys= osm_data_->get_keys();

        // Add the attributes to the datasource descriptor - assume they are
        // all of type String
        for(std::set<std::string>::iterator i=keys.begin(); i!=keys.end(); i++)
          desc_.add_descriptor(attribute_descriptor(*i,tagtypes.get_type(*i)));

        // Get the bounds of the data and set extent_ accordingly
        bounds b = osm_data_->get_bounds();
        extent_ =  box2d<double>(b.w,b.s,b.e,b.n);
    }
    
    is_bound_ = true;
}


osm_datasource::~osm_datasource() 
{ 
    // Do not do as is now static variable and cleaned up at exit
    //delete osm_data_; 
}

std::string osm_datasource::name()
{
   return "osm";
}

int osm_datasource::type() const
{
   return type_;
}

layer_descriptor osm_datasource::get_descriptor() const
{
   return desc_;
}

featureset_ptr osm_datasource::features(const query& q) const
{
    if (!is_bound_) bind();  
    
    filter_in_box filter(q.get_bbox());
    // so we need to filter osm features by bbox here...
    
    return boost::make_shared<osm_featureset<filter_in_box> >(filter,
                                              osm_data_,
                                              q.property_names(),
                                              desc_.get_encoding());
}

featureset_ptr osm_datasource::features_at_point(coord2d const& pt) const
{
   if (!is_bound_) bind();
    
   filter_at_point filter(pt);
   // collect all attribute names
   std::vector<attribute_descriptor> const& desc_vector = 
        desc_.get_descriptors();
   std::vector<attribute_descriptor>::const_iterator itr = desc_vector.begin();
   std::vector<attribute_descriptor>::const_iterator end = desc_vector.end();
   std::set<std::string> names;
    
   while (itr != end)
   {    
      names.insert(itr->get_name());
      ++itr;
   }
    
    return boost::make_shared<osm_featureset<filter_at_point> >(filter,
                                                osm_data_,
                                                names,
                                                desc_.get_encoding());
}

box2d<double> osm_datasource::envelope() const
{
   if (!is_bound_) bind();
   
   return extent_;
}