diff --git a/plugins/input/osm/basiccurl.cpp b/plugins/input/osm/basiccurl.cpp index 74d6c9880..d0d1fd725 100755 --- a/plugins/input/osm/basiccurl.cpp +++ b/plugins/input/osm/basiccurl.cpp @@ -1,45 +1,70 @@ +/***************************************************************************** + * + * This file is part of Mapnik (c++ mapping toolkit) + * + * Copyright (C) 2011 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 + * + *****************************************************************************/ + #include "basiccurl.h" -CURL_LOAD_DATA *grab_http_response(const char *url) +CURL_LOAD_DATA* grab_http_response(const char* url) { - CURL_LOAD_DATA *data; + CURL_LOAD_DATA* data; - CURL *curl = curl_easy_init(); + CURL* curl = curl_easy_init(); - if(curl) - { - data = do_grab(curl,url); - curl_easy_cleanup(curl); - return data; - } - return NULL; + if(curl) + { + data = do_grab(curl, url); + curl_easy_cleanup(curl); + return data; + } + return NULL; } -CURL_LOAD_DATA *do_grab(CURL *curl,const char *url) +CURL_LOAD_DATA* do_grab(CURL* curl,const char* url) { - CURLcode res; - CURL_LOAD_DATA *data = (CURL_LOAD_DATA *)malloc(sizeof(CURL_LOAD_DATA)); - data->data = NULL; - data->nbytes = 0; + CURLcode res; + CURL_LOAD_DATA* data = (CURL_LOAD_DATA*)malloc(sizeof(CURL_LOAD_DATA)); + data->data = NULL; + data->nbytes = 0; - curl_easy_setopt(curl,CURLOPT_URL,url); - curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,response_callback); - curl_easy_setopt(curl,CURLOPT_WRITEDATA,data); + curl_easy_setopt(curl, CURLOPT_URL, url); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, response_callback); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, data); - res=curl_easy_perform(curl); + res = curl_easy_perform(curl); - return data; + return data; } -size_t response_callback(void *ptr,size_t size,size_t nmemb, void *d) +size_t response_callback(void* ptr, size_t size, size_t nmemb, void* d) { - size_t rsize=size*nmemb; - CURL_LOAD_DATA *data=(CURL_LOAD_DATA *)d; -// fprintf(stderr,"rsize is %d\n", rsize); - data->data=(char *)realloc(data->data,(data->nbytes+rsize) - *sizeof(char)); - memcpy(&(data->data[data->nbytes]),ptr,rsize); - data->nbytes += rsize; -// fprintf(stderr,"data->nbytes is %d\n", data->nbytes); - return rsize; + size_t rsize = size * nmemb; + CURL_LOAD_DATA* data = (CURL_LOAD_DATA*)d; + + // fprintf(stderr,"rsize is %d\n", rsize); + + data->data = (char*)realloc(data->data, (data->nbytes + rsize) * sizeof(char)); + memcpy(&(data->data[data->nbytes]), ptr, rsize); + data->nbytes += rsize; + + // fprintf(stderr,"data->nbytes is %d\n", data->nbytes); + + return rsize; } diff --git a/plugins/input/osm/basiccurl.h b/plugins/input/osm/basiccurl.h index 8981c773a..0736720c7 100755 --- a/plugins/input/osm/basiccurl.h +++ b/plugins/input/osm/basiccurl.h @@ -1,3 +1,25 @@ +/***************************************************************************** + * + * This file is part of Mapnik (c++ mapping toolkit) + * + * Copyright (C) 2011 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 + * + *****************************************************************************/ + #ifndef BASICCURL_H #define BASICCURL_H @@ -12,7 +34,7 @@ typedef struct } CURL_LOAD_DATA; CURL_LOAD_DATA *grab_http_response(const char *url); -CURL_LOAD_DATA *do_grab(CURL *curl,const char *url); -size_t response_callback(void *ptr,size_t size,size_t nmemb, void *data); +CURL_LOAD_DATA *do_grab(CURL *curl, const char *url); +size_t response_callback(void *ptr ,size_t size, size_t nmemb, void *data); -#endif +#endif // BASICCURL_H diff --git a/plugins/input/osm/dataset_deliverer.cpp b/plugins/input/osm/dataset_deliverer.cpp index 1e2b11d8c..e56a52a38 100644 --- a/plugins/input/osm/dataset_deliverer.cpp +++ b/plugins/input/osm/dataset_deliverer.cpp @@ -1,56 +1,86 @@ +/***************************************************************************** + * + * This file is part of Mapnik (c++ mapping toolkit) + * + * Copyright (C) 2011 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 + * + *****************************************************************************/ + #include "dataset_deliverer.h" #include "basiccurl.h" #include -osm_dataset * dataset_deliverer::dataset=NULL; +osm_dataset * dataset_deliverer::dataset = NULL; std::string dataset_deliverer::last_bbox = ""; std::string dataset_deliverer::last_filename = ""; -osm_dataset* dataset_deliverer::load_from_file(const string& file, - const string& parser) +osm_dataset* dataset_deliverer::load_from_file(const string& file, const string& parser) { - // Only actually load from file if we haven't done so already - if(dataset == NULL) - { - dataset = new osm_dataset; - if(dataset->load(file.c_str(),parser)==false) - return NULL; - atexit(dataset_deliverer::release); - last_filename = file; - } - else if(file != last_filename) - { - dataset = new osm_dataset; - if(dataset->load(file.c_str(),parser)==false) - return NULL; - last_filename = file; - } - return dataset; + // Only actually load from file if we haven't done so already + if (dataset == NULL) + { + dataset = new osm_dataset; + if (dataset->load(file.c_str(), parser) == false) + { + return NULL; + } + + atexit(dataset_deliverer::release); + last_filename = file; + } + else if(file != last_filename) + { + dataset = new osm_dataset; + if (dataset->load(file.c_str(), parser) == false) + { + return NULL; + } + last_filename = file; + } + return dataset; } -osm_dataset* dataset_deliverer::load_from_url - (const string& url,const string& bbox,const string& parser) +osm_dataset* dataset_deliverer::load_from_url(const string& url, const string& bbox, const string& parser) { - - if(dataset==NULL) - { - dataset = new osm_dataset; - if(dataset->load_from_url(url.c_str(),bbox,parser)==false) - return NULL; - atexit(dataset_deliverer::release); - last_bbox = bbox; - } - else if (bbox != last_bbox) - { + if (dataset == NULL) + { + dataset = new osm_dataset; + if (dataset->load_from_url(url.c_str(), bbox, parser) == false) + { + return NULL; + } + + atexit(dataset_deliverer::release); + last_bbox = bbox; + } + else if (bbox != last_bbox) + { #ifdef MAPNIK_DEBUG - cerr<<"BBOXES ARE DIFFERENT: " << last_bbox<<","<clear(); - if(dataset->load_from_url(url.c_str(),bbox,parser)==false) - return NULL; - last_bbox = bbox; - } - return dataset; + // Reload the dataset + dataset->clear(); + if (dataset->load_from_url(url.c_str(), bbox, parser) == false) + { + return NULL; + } + + last_bbox = bbox; + } + return dataset; } diff --git a/plugins/input/osm/dataset_deliverer.h b/plugins/input/osm/dataset_deliverer.h index cdcbdee43..82ea029cf 100644 --- a/plugins/input/osm/dataset_deliverer.h +++ b/plugins/input/osm/dataset_deliverer.h @@ -1,3 +1,28 @@ +/***************************************************************************** + * + * This file is part of Mapnik (c++ mapping toolkit) + * + * Copyright (C) 2011 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 + * + *****************************************************************************/ + +#ifndef DATASET_DELIVERER_H +#define DATASET_DELIVERER_H + #include "osm.h" #include #include @@ -12,13 +37,13 @@ private: static std::string last_filename; public: - static osm_dataset *load_from_file(const string&,const string&); - static osm_dataset *load_from_url - (const string&,const string&,const string&); + static osm_dataset *load_from_file(const string&, const string&); + static osm_dataset *load_from_url(const string&, const string&, const string&); static void release() { - delete dataset; + delete dataset; } }; +#endif // DATASET_DELIVERER_H diff --git a/plugins/input/osm/osm.cpp b/plugins/input/osm/osm.cpp index b15ca98b2..e93b5b128 100644 --- a/plugins/input/osm/osm.cpp +++ b/plugins/input/osm/osm.cpp @@ -1,263 +1,280 @@ +/***************************************************************************** + * + * This file is part of Mapnik (c++ mapping toolkit) + * + * Copyright (C) 2011 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 + * + *****************************************************************************/ + #include "osm.h" #include "osmparser.h" +#include "basiccurl.h" + #include #include #include #include #include -#include "basiccurl.h" - - -#include -using namespace std; polygon_types osm_way::ptypes; bool osm_dataset::load(const char* filename,const std::string& parser) { - if (parser=="libxml2") - { - return osmparser::parse(this,filename); - } - return false; + if (parser == "libxml2") + { + return osmparser::parse(this, filename); + } + return false; } bool osm_dataset::load_from_url(const std::string& url, - const std::string& bbox, - const std::string& parser) + const std::string& bbox, + const std::string& parser) { - if(parser=="libxml2") - { + if (parser == "libxml2") + { #ifdef MAPNIK_DEBUG - cerr<<"osm_dataset::load_from_url: url=" << url << - " bbox="<nbytes+1]; - memcpy(blx,resp->data,resp->nbytes); - blx[resp->nbytes] = '\0'; + CURL_LOAD_DATA* resp = grab_http_response(str.str().c_str()); + + if (resp != NULL) + { + char *blx = new char[resp->nbytes + 1]; + memcpy(blx, resp->data, resp->nbytes); + blx[resp->nbytes] = '\0'; #ifdef MAPNIK_DEBUG - cerr<< " CURL RESPONSE: " << blx << endl; + std::clog << "Osm Plugin: CURL RESPONSE: " << blx << std::endl; #endif - delete[] blx; - bool success= osmparser::parse(this,resp->data,resp->nbytes); - return success; + delete[] blx; + bool success = osmparser::parse(this, resp->data, resp->nbytes); + return success; + } } - } - return false; + return false; } osm_dataset::~osm_dataset() { - clear(); + clear(); } void osm_dataset::clear() { #ifdef MAPNIK_DEBUG - cerr<<"osm_dataset::clear()"<to_string(); - } - for(unsigned int count=0; countto_string(); - } - return result; + for (unsigned int count = 0; count < nodes.size(); ++count) + { + result += nodes[count]->to_string(); + } + + for (unsigned int count = 0; count < ways.size(); ++count) + { + result += ways[count]->to_string(); + } + + return result; } bounds osm_dataset::get_bounds() { - bounds b (-180,-90,180,90); - for(unsigned int count=0; countlon > b.w) - b.w=nodes[count]->lon; - if(nodes[count]->lon < b.e) - b.e=nodes[count]->lon; - if(nodes[count]->lat > b.s) - b.s=nodes[count]->lat; - if(nodes[count]->lat < b.n) - b.n=nodes[count]->lat; - } - return b; + bounds b (-180, -90, 180, 90); + for (unsigned int count = 0; count < nodes.size(); ++count) + { + if(nodes[count]->lon > b.w) b.w = nodes[count]->lon; + if(nodes[count]->lon < b.e) b.e = nodes[count]->lon; + if(nodes[count]->lat > b.s) b.s = nodes[count]->lat; + if(nodes[count]->lat < b.n) b.n = nodes[count]->lat; + } + return b; } -osm_node *osm_dataset::next_node() +osm_node* osm_dataset::next_node() { - if(node_i!=nodes.end()) - { - return *(node_i++); - } - return NULL; - + if (node_i != nodes.end()) + { + return *(node_i++); + } + return NULL; } -osm_way *osm_dataset::next_way() + +osm_way* osm_dataset::next_way() { - if(way_i!=ways.end()) - { - return *(way_i++); - } - return NULL; + if (way_i != ways.end()) + { + return *(way_i++); + } + return NULL; } -osm_item *osm_dataset::next_item() +osm_item* osm_dataset::next_item() { - osm_item *item=NULL; - if(next_item_mode==Node) - { - item = next_node(); - if(item==NULL) + osm_item* item = NULL; + if (next_item_mode == Node) { - next_item_mode=Way; - rewind_ways(); - item = next_way(); + item = next_node(); + if (item == NULL) + { + next_item_mode = Way; + rewind_ways(); + item = next_way(); + } } - } - else - { - item = next_way(); - } - return item; + else + { + item = next_way(); + } + return item; } std::set osm_dataset::get_keys() { - std::set keys; - for(unsigned int count=0; count::iterator i= - nodes[count]->keyvals.begin(); i!=nodes[count]->keyvals.end(); i++) + std::set keys; + for (unsigned int count = 0; count < nodes.size(); ++count) { - keys.insert(i->first); + for (std::map::iterator i = nodes[count]->keyvals.begin(); + i != nodes[count]->keyvals.end(); i++) + { + keys.insert(i->first); + } } - } - for(unsigned int count=0; count::iterator i= - ways[count]->keyvals.begin(); i!=ways[count]->keyvals.end(); i++) + + for (unsigned int count = 0; count < ways.size(); ++count) { - keys.insert(i->first); + for (std::map::iterator i = ways[count]->keyvals.begin(); + i != ways[count]->keyvals.end(); i++) + { + keys.insert(i->first); + } } - } - return keys; + return keys; } - - std::string osm_item::to_string() { - std::ostringstream strm; - strm << "id=" << id << std::endl << "Keyvals: " << std::endl; - for(std::map::iterator i=keyvals.begin(); - i!=keyvals.end(); i++) - { - strm << "Key " << i->first << " Value " << i->second << std::endl; - } - return strm.str(); + std::ostringstream strm; + strm << "id=" << id << std::endl << "Keyvals: " << std::endl; + + for (std::map::iterator i = keyvals.begin(); + i != keyvals.end(); i++) + { + strm << "Key " << i->first << " Value " << i->second << std::endl; + } + + return strm.str(); } std::string osm_node::to_string() { - std::ostringstream strm; - strm << "Node: "<< osm_item::to_string() << - " Lat=" << lat <<" lon=" <id << " "; + if (nodes[count] != NULL) + { + strm << nodes[count]->id << " "; + } } - } - strm << std::endl; - return strm.str(); + + strm << std::endl; + return strm.str(); } bounds osm_way::get_bounds() { - bounds b (-180,-90,180,90); - for(unsigned int count=0; countlon > b.w) - b.w=nodes[count]->lon; - if(nodes[count]->lon < b.e) - b.e=nodes[count]->lon; - if(nodes[count]->lat > b.s) - b.s=nodes[count]->lat; - if(nodes[count]->lat < b.n) - b.n=nodes[count]->lat; - } - return b; + bounds b (-180, -90, 180, 90); + + for (unsigned int count = 0; count < nodes.size(); ++count) + { + if(nodes[count]->lon > b.w) b.w = nodes[count]->lon; + if(nodes[count]->lon < b.e) b.e = nodes[count]->lon; + if(nodes[count]->lat > b.s) b.s = nodes[count]->lat; + if(nodes[count]->lat < b.n) b.n = nodes[count]->lat; + } + return b; } bool osm_way::is_polygon() { - for(unsigned int count=0; countw = w; this->s = s; @@ -23,39 +45,35 @@ struct bounds class polygon_types { public: - std::vector > ptypes; + std::vector > ptypes; polygon_types() { - ptypes.push_back(std::pair("natural","wood")); - ptypes.push_back(std::pair("natural","water")); - ptypes.push_back(std::pair("natural","heath")); - ptypes.push_back(std::pair("natural","marsh")); - ptypes.push_back(std::pair("military", - "danger_area")); - ptypes.push_back(std::pair - ("landuse","forest")); - ptypes.push_back(std::pair - ("landuse","industrial")); + ptypes.push_back(std::pair("natural", "wood")); + ptypes.push_back(std::pair("natural", "water")); + ptypes.push_back(std::pair("natural", "heath")); + ptypes.push_back(std::pair("natural", "marsh")); + ptypes.push_back(std::pair("military", "danger_area")); + ptypes.push_back(std::pair("landuse","forest")); + ptypes.push_back(std::pair("landuse","industrial")); } }; struct osm_item { long id; - std::map keyvals; + std::map keyvals; virtual std::string to_string(); - virtual ~osm_item() { } + virtual ~osm_item() {} }; - -struct osm_node: public osm_item +struct osm_node : public osm_item { double lat, lon; std::string to_string(); }; -struct osm_way: public osm_item +struct osm_way : public osm_item { std::vector nodes; std::string to_string(); @@ -68,36 +86,48 @@ class osm_dataset { private: int next_item_mode; - enum {Node, Way }; + enum { Node, Way }; std::vector::iterator node_i; std::vector::iterator way_i; std::vector nodes; std::vector ways; public: - osm_dataset() { node_i=nodes.begin(); way_i=ways.begin(); - next_item_mode=Node; } - osm_dataset(const char* name) - { node_i=nodes.begin(); way_i=ways.begin(); - next_item_mode=Node; load(name); } - bool load(const char* name,const std::string& parser="libxml2"); - bool load_from_url(const std::string&,const std::string&, - const std::string& parser="libxml2"); + osm_dataset() + { + node_i = nodes.begin(); + way_i = ways.begin(); + next_item_mode = Node; + } + + osm_dataset(const char* name) + { + node_i = nodes.begin(); + way_i = ways.begin(); + next_item_mode = Node; + load(name); + } + ~osm_dataset(); + + bool load(const char* name, const std::string& parser = "libxml2"); + bool load_from_url(const std::string&, + const std::string&, + const std::string& parser = "libxml2"); void clear(); void add_node(osm_node* n) { nodes.push_back(n); } void add_way(osm_way* w) { ways.push_back(w); } std::string to_string(); bounds get_bounds(); std::set get_keys(); - void rewind_nodes() { node_i=nodes.begin(); } - void rewind_ways() { way_i=ways.begin(); } - void rewind() { rewind_nodes(); rewind_ways(); next_item_mode=Node; } + void rewind_nodes() { node_i = nodes.begin(); } + void rewind_ways() { way_i = ways.begin(); } + void rewind() { rewind_nodes(); rewind_ways(); next_item_mode = Node; } osm_node * next_node(); osm_way * next_way(); osm_item * next_item(); - bool current_item_is_node() { return next_item_mode==Node; } - bool current_item_is_way() { return next_item_mode==Way; } + bool current_item_is_node() { return next_item_mode == Node; } + bool current_item_is_way() { return next_item_mode == Way; } }; #endif // OSM_H diff --git a/plugins/input/osm/osm_datasource.cpp b/plugins/input/osm/osm_datasource.cpp index f286c1f55..f2f64b545 100644 --- a/plugins/input/osm/osm_datasource.cpp +++ b/plugins/input/osm/osm_datasource.cpp @@ -2,7 +2,7 @@ * * This file is part of Mapnik (c++ mapping toolkit) * - * Copyright (C) 2006 Artem Pavlenko + * Copyright (C) 2011 Artem Pavlenko * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -20,6 +20,12 @@ * *****************************************************************************/ +// stl +#include +#include +#include +#include + // mapnik #include #include @@ -27,20 +33,12 @@ // boost #include -// stl -#include -#include -#include -#include - #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; @@ -49,10 +47,12 @@ using mapnik::filter_in_box; using mapnik::filter_at_point; using mapnik::attribute_descriptor; -osm_datasource::osm_datasource(const parameters ¶ms, bool bind) - : datasource (params), - type_(datasource::Vector), - desc_(*params_.get("type"), *params_.get("encoding","utf-8")) +DATASOURCE_PLUGIN(osm_datasource) + +osm_datasource::osm_datasource(const parameters& params, bool bind) + : datasource (params), + type_(datasource::Vector), + desc_(*params_.get("type"), *params_.get("encoding", "utf-8")) { if (bind) { @@ -65,62 +65,64 @@ void osm_datasource::bind() const if (is_bound_) return; osm_data_ = NULL; - std::string osm_filename= *params_.get("file",""); - std::string parser = *params_.get("parser","libxml2"); - std::string url = *params_.get("url",""); - std::string bbox = *params_.get("bbox",""); + std::string osm_filename = *params_.get("file", ""); + std::string parser = *params_.get("parser", "libxml2"); + std::string url = *params_.get("url", ""); + std::string bbox = *params_.get("bbox", ""); - bool do_process=false; + bool do_process = false; // load the data // if we supplied a filename, load from file - if (url!="" && bbox!="") + 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="<rewind(); + // Need code to get the attributes of all the data - std::set keys= osm_data_->get_keys(); + std::set keys = osm_data_->get_keys(); // Add the attributes to the datasource descriptor - assume they are // all of type String - for(std::set::iterator i=keys.begin(); i!=keys.end(); i++) - desc_.add_descriptor(attribute_descriptor(*i,tagtypes.get_type(*i))); + for (std::set::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(b.w,b.s,b.e,b.n); + extent_ = box2d(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 @@ -129,59 +131,58 @@ osm_datasource::~osm_datasource() std::string osm_datasource::name() { - return "osm"; + return "osm"; } int osm_datasource::type() const { - return type_; + return type_; } layer_descriptor osm_datasource::get_descriptor() const { - return desc_; + return desc_; } featureset_ptr osm_datasource::features(const query& q) const { - if (!is_bound_) bind(); + 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 >(filter, - osm_data_, - q.property_names(), - desc_.get_encoding()); + osm_data_, + q.property_names(), + desc_.get_encoding()); } featureset_ptr osm_datasource::features_at_point(coord2d const& pt) const { - if (!is_bound_) bind(); + if (! is_bound_) bind(); - filter_at_point filter(pt); - // collect all attribute names - std::vector const& desc_vector = - desc_.get_descriptors(); - std::vector::const_iterator itr = desc_vector.begin(); - std::vector::const_iterator end = desc_vector.end(); - std::set names; + filter_at_point filter(pt); + // collect all attribute names + std::vector const& desc_vector = desc_.get_descriptors(); + std::vector::const_iterator itr = desc_vector.begin(); + std::vector::const_iterator end = desc_vector.end(); + std::set names; - while (itr != end) - { - names.insert(itr->get_name()); - ++itr; - } + while (itr != end) + { + names.insert(itr->get_name()); + ++itr; + } return boost::make_shared >(filter, - osm_data_, - names, - desc_.get_encoding()); + osm_data_, + names, + desc_.get_encoding()); } box2d osm_datasource::envelope() const { - if (!is_bound_) bind(); + if (! is_bound_) bind(); return extent_; } diff --git a/plugins/input/osm/osm_datasource.hpp b/plugins/input/osm/osm_datasource.hpp index 44b826870..600ccf406 100644 --- a/plugins/input/osm/osm_datasource.hpp +++ b/plugins/input/osm/osm_datasource.hpp @@ -1,8 +1,8 @@ /***************************************************************************** - * + * * This file is part of Mapnik (c++ mapping toolkit) * - * Copyright (C) 2006 Artem Pavlenko + * Copyright (C) 2011 Artem Pavlenko * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -20,17 +20,15 @@ * *****************************************************************************/ -// $Id$ - #ifndef OSM_DATASOURCE_HPP #define OSM_DATASOURCE_HPP +// mapnik #include #include #include "osm.h" - using mapnik::datasource; using mapnik::parameters; using mapnik::query; @@ -41,26 +39,24 @@ using mapnik::box2d; class osm_datasource : public datasource { - public: - osm_datasource(const parameters ¶ms, bool bind=true); - virtual ~osm_datasource(); - - // these must be overridden - int type() const; - featureset_ptr features(const query& q) const; - featureset_ptr features_at_point(coord2d const& pt) const; - box2d envelope() const; - layer_descriptor get_descriptor() const; +public: + osm_datasource(const parameters& params, bool bind = true); + virtual ~osm_datasource(); + int type() const; + featureset_ptr features(const query& q) const; + featureset_ptr features_at_point(coord2d const& pt) const; + box2d envelope() const; + layer_descriptor get_descriptor() const; static std::string name(); void bind() const; - private: - osm_datasource(const osm_datasource&); - osm_datasource& operator=(const osm_datasource&); - private: - mutable box2d extent_; - mutable osm_dataset * osm_data_; +private: + mutable box2d extent_; + mutable osm_dataset* osm_data_; int type_; mutable layer_descriptor desc_; + // no copying + osm_datasource(const osm_datasource&); + osm_datasource& operator=(const osm_datasource&); }; -#endif //OSM_DATASOURCE_HPP +#endif // OSM_DATASOURCE_HPP diff --git a/plugins/input/osm/osm_featureset.cpp b/plugins/input/osm/osm_featureset.cpp index 3cf7b7b58..e16202046 100644 --- a/plugins/input/osm/osm_featureset.cpp +++ b/plugins/input/osm/osm_featureset.cpp @@ -3,7 +3,7 @@ * * This file is part of Mapnik (c++ mapping toolkit) * - * Copyright (C) 2006 Artem Pavlenko + * Copyright (C) 2011 Artem Pavlenko * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -21,55 +21,53 @@ * *****************************************************************************/ +// stl +#include + // mapnik #include #include #include "osm_featureset.hpp" -// stl -#include - using mapnik::Feature; using mapnik::feature_ptr; using mapnik::geometry_type; using mapnik::feature_factory; -using std::cerr; -using std::endl; template osm_featureset::osm_featureset(const filterT& filter, - osm_dataset * dataset, + osm_dataset* dataset, const std::set& attribute_names, std::string const& encoding) - : filter_(filter), - query_ext_(), - tr_(new transcoder(encoding)), - feature_id_(1), - dataset_ (dataset), - attribute_names_ (attribute_names) + : filter_(filter), + query_ext_(), + tr_(new transcoder(encoding)), + feature_id_(1), + dataset_ (dataset), + attribute_names_ (attribute_names) { dataset_->rewind(); } - template feature_ptr osm_featureset::next() { - osm_item * cur_item = dataset_->next_item(); feature_ptr feature; - bool success=false; - if(cur_item != NULL) + bool success = false; + + osm_item* cur_item = dataset_->next_item(); + if (cur_item != NULL) { - if(dataset_->current_item_is_node()) + if (dataset_->current_item_is_node()) { feature = feature_factory::create(feature_id_); ++feature_id_; double lat = static_cast(cur_item)->lat; double lon = static_cast(cur_item)->lon; - geometry_type * point = new geometry_type(mapnik::Point); - point->move_to(lon,lat); + geometry_type* point = new geometry_type(mapnik::Point); + point->move_to(lon, lat); feature->add_geometry(point); success = true; } @@ -78,40 +76,42 @@ feature_ptr osm_featureset::next() bounds b = static_cast(cur_item)->get_bounds(); // Loop until we find a feature which passes the filter - while(cur_item != NULL && - !filter_.pass(box2d(b.w,b.s,b.e,b.n))) + while (cur_item != NULL && + ! filter_.pass(box2d(b.w, b.s, b.e, b.n))) { cur_item = dataset_->next_item(); - if(cur_item!=NULL) + if (cur_item != NULL) + { b = static_cast(cur_item)->get_bounds(); + } } - if(cur_item != NULL) + if (cur_item != NULL) { - if(static_cast(cur_item)->nodes.size()) + if (static_cast(cur_item)->nodes.size()) { feature = feature_factory::create(feature_id_); ++feature_id_; - geometry_type *geom; - if(static_cast(cur_item)->is_polygon()) - geom = new geometry_type(mapnik::Polygon); - else - geom = new geometry_type(mapnik::LineString); - - geom->set_capacity(static_cast(cur_item)-> - nodes.size()); - geom->move_to(static_cast(cur_item)-> - nodes[0]->lon, - static_cast(cur_item)-> - nodes[0]->lat); - - for(unsigned int count=1; count(cur_item) - ->nodes.size(); count++) + geometry_type* geom; + if (static_cast(cur_item)->is_polygon()) { - geom->line_to(static_cast(cur_item) - ->nodes[count]->lon, - static_cast(cur_item) - ->nodes[count]->lat); + geom = new geometry_type(mapnik::Polygon); + } + else + { + geom = new geometry_type(mapnik::LineString); + } + + geom->set_capacity(static_cast(cur_item)->nodes.size()); + geom->move_to(static_cast(cur_item)->nodes[0]->lon, + static_cast(cur_item)->nodes[0]->lat); + + for (unsigned int count = 1; + count < static_cast(cur_item)->nodes.size(); + count++) + { + geom->line_to(static_cast(cur_item)->nodes[count]->lon, + static_cast(cur_item)->nodes[count]->lat); } feature->add_geometry(geom); success = true; @@ -120,31 +120,31 @@ feature_ptr osm_featureset::next() } // can feature_ptr be compared to NULL? - no - if(success) + if (success) { - std::map::iterator i= - cur_item->keyvals.begin(); + std::map::iterator i = cur_item->keyvals.begin(); // add the keyvals to the feature. the feature seems to be a map // of some sort so this should work - see dbf_file::add_attribute() - while(i != cur_item->keyvals.end()) + while (i != cur_item->keyvals.end()) { - //only add if in the specified set of attribute names - if(attribute_names_.find(i->first) != attribute_names_.end()) + // only add if in the specified set of attribute names + if (attribute_names_.find(i->first) != attribute_names_.end()) + { (*feature)[i->first] = tr_->transcode(i->second.c_str()); + } + i++; } + return feature; } } return feature_ptr(); } - template osm_featureset::~osm_featureset() {} template class osm_featureset; template class osm_featureset; - - diff --git a/plugins/input/osm/osm_featureset.hpp b/plugins/input/osm/osm_featureset.hpp index a1251e221..cefd63e7d 100644 --- a/plugins/input/osm/osm_featureset.hpp +++ b/plugins/input/osm/osm_featureset.hpp @@ -2,7 +2,7 @@ * * This file is part of Mapnik (c++ mapping toolkit) * - * Copyright (C) 2006 Artem Pavlenko + * Copyright (C) 2011 Artem Pavlenko * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -23,14 +23,20 @@ #ifndef OSM_FS_HH #define OSM_FS_HH +// stl +#include + +// boost #include + +// mapnik #include -#include "osm.h" #include #include #include #include -#include + +#include "osm.h" using mapnik::Featureset; using mapnik::box2d; @@ -40,27 +46,26 @@ using mapnik::transcoder; template class osm_featureset : public Featureset { - filterT filter_; - box2d query_ext_; - boost::scoped_ptr tr_; - std::vector attr_ids_; - mutable box2d feature_ext_; - mutable int total_geom_size; - mutable int feature_id_; - osm_dataset *dataset_; - std::set attribute_names_; - - public: - osm_featureset(const filterT& filter, - osm_dataset *dataset, - const std::set& attribute_names, - std::string const& encoding); - virtual ~osm_featureset(); - feature_ptr next(); - private: - osm_featureset(const osm_featureset&); - const osm_featureset& operator=(const osm_featureset&); - +public: + osm_featureset(const filterT& filter, + osm_dataset* dataset, + const std::set& attribute_names, + std::string const& encoding); + virtual ~osm_featureset(); + feature_ptr next(); +private: + filterT filter_; + box2d query_ext_; + boost::scoped_ptr tr_; + std::vector attr_ids_; + mutable box2d feature_ext_; + mutable int total_geom_size; + mutable int feature_id_; + osm_dataset *dataset_; + std::set attribute_names_; + // no copying + osm_featureset(const osm_featureset&); + const osm_featureset& operator=(const osm_featureset&); }; -#endif //OSM_FS_HH +#endif // OSM_FS_HH diff --git a/plugins/input/osm/osmparser.h b/plugins/input/osm/osmparser.h index 5ffa0477f..5cc00e24a 100644 --- a/plugins/input/osm/osmparser.h +++ b/plugins/input/osm/osmparser.h @@ -1,3 +1,28 @@ +/***************************************************************************** + * + * This file is part of Mapnik (c++ mapping toolkit) + * + * Copyright (C) 2011 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 + * + *****************************************************************************/ + +#ifndef OSMPARSER_H +#define OSMPARSER_H + #include #include #include @@ -6,24 +31,24 @@ #include "osm.h" #include - class osmparser { +public: + static void processNode(xmlTextReaderPtr reader); + static void startElement(xmlTextReaderPtr reader, const xmlChar* name); + static void endElement(const xmlChar* name); + static bool parse(osm_dataset* ds, const char* filename); + static bool parse(osm_dataset* ds, char* data, int nbytes); + private: static osm_item *cur_item; static long curID; - static bool in_node, in_way; + static bool in_node, in_way; static osm_dataset* components; static std::string error; - static std::map tmp_node_store; + static std::map tmp_node_store; - static int do_parse(xmlTextReaderPtr); - -public: - static void processNode(xmlTextReaderPtr reader); - static void startElement(xmlTextReaderPtr reader, const xmlChar *name); - static void endElement(const xmlChar* name); - static bool parse(osm_dataset *ds, const char* filename); - static bool parse(osm_dataset *ds, char* data,int nbytes); + static int do_parse(xmlTextReaderPtr); }; +#endif // OSMPARSER_H diff --git a/plugins/input/osm/osmtagtypes.h b/plugins/input/osm/osmtagtypes.h index d486946a8..191514757 100644 --- a/plugins/input/osm/osmtagtypes.h +++ b/plugins/input/osm/osmtagtypes.h @@ -1,28 +1,50 @@ +/***************************************************************************** + * + * This file is part of Mapnik (c++ mapping toolkit) + * + * Copyright (C) 2011 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 + * + *****************************************************************************/ + #ifndef OSMTAGTYPES_H #define OSMTAGTYPES_H // osmtagtypes.h // for finding the types of particular tags +// mapnik #include class osm_tag_types { -private: - std::map types; - public: - void add_type(std::string tag, mapnik::eAttributeType type) - { - types[tag]=type; - } + void add_type(std::string tag, mapnik::eAttributeType type) + { + types[tag] = type; + } - mapnik::eAttributeType get_type(std::string tag) - { - std::map::iterator i = - types.find(tag); - return (i==types.end()) ? mapnik::String: i->second; - } + mapnik::eAttributeType get_type(std::string tag) + { + std::map::iterator i = types.find(tag); + return (i == types.end()) ? mapnik::String : i->second; + } + +private: + std::map types; }; #endif // OSMTAGTYPES_H diff --git a/plugins/input/osm/render.cpp b/plugins/input/osm/render.cpp index b55e8dd53..30583346a 100644 --- a/plugins/input/osm/render.cpp +++ b/plugins/input/osm/render.cpp @@ -1,3 +1,30 @@ +/***************************************************************************** + * + * This file is part of Mapnik (c++ mapping toolkit) + * + * Copyright (C) 2011 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 + * + *****************************************************************************/ + +// stl +#include +#include + +// mapnik #include #include #include @@ -8,52 +35,41 @@ #include #include -using namespace mapnik; - -#include -#include -using namespace std; - - int main(int argc,char *argv[]) { - if(argc < 6) - { - std::cerr<<"Usage: render XMLfile w s e n [OSMfile]" << std::endl; - exit(0); - } - - datasource_cache::instance()->register_datasources - ("/usr/local/lib/mapnik/input"); - freetype_engine::register_font - ("/usr/local/lib/mapnik/fonts/DejaVuSans.ttf"); - - Map m (800,800); - load_map(m,argv[1]); - - if(argc>6) - { - parameters p; - p["type"] = "osm"; - p["file"] = argv[6]; - for(int count=0; countparams(); - m.getLayer(count).set_datasource(datasource_cache::instance()-> - create(p)); + std::cerr << "Usage: render XMLfile w s e n [OSMfile]" << std::endl; + exit(0); } - } - box2d bbox (atof(argv[2]),atof(argv[3]), - atof(argv[4]),atof(argv[5])); + mapnik::datasource_cache::instance()->register_datasources("/usr/local/lib/mapnik/input"); + mapnik::freetype_engine::register_font("/usr/local/lib/mapnik/fonts/DejaVuSans.ttf"); + + mapnik::Map m(800, 800); + mapnik::load_map(m, argv[1]); + + if (argc > 6) + { + mapnik::parameters p; + p["type"] = "osm"; + p["file"] = argv[6]; + for (int count = 0; count < m.layer_count(); count++) + { + mapnik::parameters q = m.getLayer(count).datasource()->params(); + m.getLayer(count).set_datasource(mapnik::datasource_cache::instance()->create(p)); + } + } + + mapnik::box2d bbox (atof(argv[2]), atof(argv[3]), atof(argv[4]), atof(argv[5])); - m.zoom_to_box(bbox); + m.zoom_to_box(bbox); - image_32 buf (m.width(), m.height()); - agg_renderer r(m,buf); - r.apply(); + mapnik::image_32 buf (m.width(), m.height()); + mapnik::agg_renderer r(m, buf); + r.apply(); - save_to_file(buf.data(),"blah.png","png"); + mapnik::save_to_file(buf.data(), "blah.png", "png"); - return 0; + return 0; }