- aligned coding style in osm plugin

This commit is contained in:
kunitoki 2011-10-22 16:04:05 +02:00
parent 2a4fe24ea9
commit 05a32a18e5
13 changed files with 705 additions and 491 deletions

View file

@ -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);
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));
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;
}
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);
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);
// fprintf(stderr,"data->nbytes is %d\n", data->nbytes);
return rsize;
}

View file

@ -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

View file

@ -1,55 +1,85 @@
/*****************************************************************************
*
* 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 <sstream>
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)
if (dataset == NULL)
{
dataset = new osm_dataset;
if(dataset->load(file.c_str(),parser)==false)
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)
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)
if (dataset == NULL)
{
dataset = new osm_dataset;
if(dataset->load_from_url(url.c_str(),bbox,parser)==false)
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<<","<<bbox<<endl;
std::cerr << "Osm Plugin: BBOXES ARE DIFFERENT: " << last_bbox << "," << bbox << std::endl;
#endif
// Reload the dataset
dataset->clear();
if(dataset->load_from_url(url.c_str(),bbox,parser)==false)
if (dataset->load_from_url(url.c_str(), bbox, parser) == false)
{
return NULL;
}
last_bbox = bbox;
}
return dataset;

View file

@ -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 <string>
#include <iostream>
@ -12,9 +37,8 @@ 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()
{
@ -22,3 +46,4 @@ public:
}
};
#endif // DATASET_DELIVERER_H

View file

@ -1,23 +1,42 @@
/*****************************************************************************
*
* 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 <libxml/parser.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include "basiccurl.h"
#include <iostream>
using namespace std;
polygon_types osm_way::ptypes;
bool osm_dataset::load(const char* filename,const std::string& parser)
{
if (parser=="libxml2")
if (parser == "libxml2")
{
return osmparser::parse(this,filename);
return osmparser::parse(this, filename);
}
return false;
}
@ -26,12 +45,12 @@ bool osm_dataset::load_from_url(const std::string& url,
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="<<bbox<<endl;
std::clog << "Osm Plugin: osm_dataset::load_from_url url=" << url << " bbox=" << bbox << std::endl;
#endif
std::ostringstream str;
// use curl to grab the data
// fetch all the data we want - probably from osmxpai
@ -39,22 +58,23 @@ bool osm_dataset::load_from_url(const std::string& url,
str << url << "?bbox=" << bbox;
#ifdef MAPNIK_DEBUG
cerr << "FULL URL : " << str.str() << endl;
std::clog << "Osm Plugin: FULL URL " << str.str() << std::endl;
#endif
CURL_LOAD_DATA *resp = grab_http_response(str.str().c_str());
if(resp!=NULL)
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);
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);
bool success = osmparser::parse(this, resp->data, resp->nbytes);
return success;
}
}
@ -69,35 +89,35 @@ osm_dataset::~osm_dataset()
void osm_dataset::clear()
{
#ifdef MAPNIK_DEBUG
cerr<<"osm_dataset::clear()"<<endl;
cerr<<"deleting ways"<<endl;
std::clog << "Osm Plugin: osm_dataset::clear()" << std::endl;
std::clog << "Osm Plugin: deleting ways" << std::endl;
#endif
for(unsigned int count=0; count<ways.size(); count++)
for (unsigned int count = 0; count < ways.size(); ++count)
{
delete ways[count];
ways[count]=NULL;
ways[count] = NULL;
}
#ifdef MAPNIK_DEBUG
cerr<<"deleting nodes"<<endl;
std::clog << "Osm Plugin: deleting nodes" << std::endl;
#endif
for(unsigned int count=0; count<nodes.size(); count++)
for (unsigned int count = 0; count < nodes.size(); ++count)
{
delete nodes[count];
nodes[count]=NULL;
nodes[count] = NULL;
}
#ifdef MAPNIK_DEBUG
cerr<<"Clearing ways/nodes"<<endl;
std::clog << "Osm Plugin: clearing ways/nodes" << std::endl;
#endif
ways.clear();
nodes.clear();
#ifdef MAPNIK_DEBUG
cerr<<"Done"<<endl;
std::clog << "Osm Plugin: done" << std::endl;
#endif
}
@ -105,61 +125,59 @@ std::string osm_dataset::to_string()
{
std::string result;
for(unsigned int count=0; count<nodes.size(); count++)
for (unsigned int count = 0; count < nodes.size(); ++count)
{
result += nodes[count]->to_string();
}
for(unsigned int count=0; count<ways.size(); count++)
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; count<nodes.size();count++)
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;
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())
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())
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)
osm_item* item = NULL;
if (next_item_mode == Node)
{
item = next_node();
if(item==NULL)
if (item == NULL)
{
next_item_mode=Way;
next_item_mode = Way;
rewind_ways();
item = next_way();
}
@ -174,18 +192,19 @@ osm_item *osm_dataset::next_item()
std::set<std::string> osm_dataset::get_keys()
{
std::set<std::string> keys;
for(unsigned int count=0; count<nodes.size(); count++)
for (unsigned int count = 0; count < nodes.size(); ++count)
{
for(std::map<std::string,std::string>::iterator i=
nodes[count]->keyvals.begin(); i!=nodes[count]->keyvals.end(); i++)
for (std::map<std::string, std::string>::iterator i = nodes[count]->keyvals.begin();
i != nodes[count]->keyvals.end(); i++)
{
keys.insert(i->first);
}
}
for(unsigned int count=0; count<ways.size(); count++)
for (unsigned int count = 0; count < ways.size(); ++count)
{
for(std::map<std::string,std::string>::iterator i=
ways[count]->keyvals.begin(); i!=ways[count]->keyvals.end(); i++)
for (std::map<std::string, std::string>::iterator i = ways[count]->keyvals.begin();
i != ways[count]->keyvals.end(); i++)
{
keys.insert(i->first);
}
@ -194,25 +213,24 @@ std::set<std::string> osm_dataset::get_keys()
}
std::string osm_item::to_string()
{
std::ostringstream strm;
strm << "id=" << id << std::endl << "Keyvals: " << std::endl;
for(std::map<std::string,std::string>::iterator i=keyvals.begin();
i!=keyvals.end(); i++)
for (std::map<std::string, std::string>::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=" <<lon << std::endl;
strm << "Node: " << osm_item::to_string() << " lat=" << lat <<" lon=" <<lon << std::endl;
return strm.str();
}
@ -221,43 +239,42 @@ std::string osm_way::to_string()
std::ostringstream strm;
strm << "Way: " << osm_item::to_string() << "Nodes in way:";
for(unsigned int count=0; count<nodes.size(); count++)
for (unsigned int count = 0; count < nodes.size(); ++count)
{
if(nodes[count]!=NULL)
if (nodes[count] != NULL)
{
strm << nodes[count]->id << " ";
}
}
strm << std::endl;
return strm.str();
}
bounds osm_way::get_bounds()
{
bounds b (-180,-90,180,90);
for(unsigned int count=0; count<nodes.size();count++)
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;
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; count<ptypes.ptypes.size(); count++)
for (unsigned int count = 0; count < ptypes.ptypes.size(); ++count)
{
if(keyvals.find(ptypes.ptypes[count].first) != keyvals.end() &&
if (keyvals.find(ptypes.ptypes[count].first) != keyvals.end() &&
keyvals[ptypes.ptypes[count].first] == ptypes.ptypes[count].second)
{
return true;
}
}
return false;
}

View file

@ -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 OSM_H
#define OSM_H
@ -9,9 +31,9 @@
struct bounds
{
double w,s,e,n;
bounds() { w=-180; s=-90; e=180; n=90; }
bounds(double w, double s, double e, double n )
double w, s, e, n;
bounds() { w = -180; s = -90; e = 180; n = 90; }
bounds(double w, double s, double e, double n)
{
this->w = w;
this->s = s;
@ -23,39 +45,35 @@ struct bounds
class polygon_types
{
public:
std::vector<std::pair<std::string,std::string> > ptypes;
std::vector<std::pair<std::string, std::string> > ptypes;
polygon_types()
{
ptypes.push_back(std::pair<std::string,std::string>("natural","wood"));
ptypes.push_back(std::pair<std::string,std::string>("natural","water"));
ptypes.push_back(std::pair<std::string,std::string>("natural","heath"));
ptypes.push_back(std::pair<std::string,std::string>("natural","marsh"));
ptypes.push_back(std::pair<std::string,std::string>("military",
"danger_area"));
ptypes.push_back(std::pair<std::string,std::string>
("landuse","forest"));
ptypes.push_back(std::pair<std::string,std::string>
("landuse","industrial"));
ptypes.push_back(std::pair<std::string, std::string>("natural", "wood"));
ptypes.push_back(std::pair<std::string, std::string>("natural", "water"));
ptypes.push_back(std::pair<std::string, std::string>("natural", "heath"));
ptypes.push_back(std::pair<std::string, std::string>("natural", "marsh"));
ptypes.push_back(std::pair<std::string, std::string>("military", "danger_area"));
ptypes.push_back(std::pair<std::string, std::string>("landuse","forest"));
ptypes.push_back(std::pair<std::string, std::string>("landuse","industrial"));
}
};
struct osm_item
{
long id;
std::map<std::string,std::string> keyvals;
std::map<std::string, std::string> 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<osm_node*> 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<osm_node*>::iterator node_i;
std::vector<osm_way*>::iterator way_i;
std::vector<osm_node*> nodes;
std::vector<osm_way*> ways;
public:
osm_dataset() { node_i=nodes.begin(); way_i=ways.begin();
next_item_mode=Node; }
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");
{
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<std::string> 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

View file

@ -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 <iostream>
#include <fstream>
#include <stdexcept>
#include <set>
// mapnik
#include <mapnik/geom_util.hpp>
#include <mapnik/query.hpp>
@ -27,20 +33,12 @@
// 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;
@ -49,10 +47,12 @@ using mapnik::filter_in_box;
using mapnik::filter_at_point;
using mapnik::attribute_descriptor;
osm_datasource::osm_datasource(const parameters &params, bool bind)
DATASOURCE_PLUGIN(osm_datasource)
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"))
desc_(*params_.get<std::string>("type"), *params_.get<std::string>("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<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","");
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;
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="<<url << " bbox="<<bbox<<endl;
std::clog << "Osm Plugin: loading_from_url: url=" << url << " bbox=" << bbox << std::endl;
#endif
if((osm_data_=dataset_deliverer::load_from_url
(url,bbox,parser))==NULL)
if ((osm_data_ = dataset_deliverer::load_from_url(url, bbox, parser)) == NULL)
{
throw datasource_exception("Error loading from URL");
}
do_process=true;
do_process = true;
}
else if(osm_filename!="")
else if (osm_filename != "")
{
if ((osm_data_=
dataset_deliverer::load_from_file(osm_filename,parser))==NULL)
if ((osm_data_= dataset_deliverer::load_from_file(osm_filename, parser)) == NULL)
{
throw datasource_exception("Error loading from file");
}
do_process=true;
do_process = true;
}
if(do_process==true)
if (do_process == true)
{
osm_tag_types tagtypes;
tagtypes.add_type("maxspeed",mapnik::Integer);
tagtypes.add_type("z_order",mapnik::Integer);
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();
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)));
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);
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
@ -144,7 +146,7 @@ layer_descriptor osm_datasource::get_descriptor() const
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...
@ -157,12 +159,11 @@ featureset_ptr osm_datasource::features(const query& q) const
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<attribute_descriptor> const& desc_vector =
desc_.get_descriptors();
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;
@ -181,7 +182,7 @@ featureset_ptr osm_datasource::features_at_point(coord2d const& pt) const
box2d<double> osm_datasource::envelope() const
{
if (!is_bound_) bind();
if (! is_bound_) bind();
return extent_;
}

View file

@ -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,17 +20,15 @@
*
*****************************************************************************/
// $Id$
#ifndef OSM_DATASOURCE_HPP
#define OSM_DATASOURCE_HPP
// mapnik
#include <mapnik/datasource.hpp>
#include <mapnik/box2d.hpp>
#include "osm.h"
using mapnik::datasource;
using mapnik::parameters;
using mapnik::query;
@ -41,11 +39,9 @@ using mapnik::box2d;
class osm_datasource : public datasource
{
public:
osm_datasource(const parameters &params, bool bind=true);
public:
osm_datasource(const parameters& params, 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;
@ -53,14 +49,14 @@ class osm_datasource : public datasource
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:
private:
mutable box2d<double> extent_;
mutable osm_dataset * osm_data_;
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

View file

@ -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,25 +21,23 @@
*
*****************************************************************************/
// stl
#include <iostream>
// mapnik
#include <mapnik/geometry.hpp>
#include <mapnik/feature_factory.hpp>
#include "osm_featureset.hpp"
// stl
#include <iostream>
using mapnik::Feature;
using mapnik::feature_ptr;
using mapnik::geometry_type;
using mapnik::feature_factory;
using std::cerr;
using std::endl;
template <typename filterT>
osm_featureset<filterT>::osm_featureset(const filterT& filter,
osm_dataset * dataset,
osm_dataset* dataset,
const std::set<std::string>&
attribute_names,
std::string const& encoding)
@ -53,23 +51,23 @@ osm_featureset<filterT>::osm_featureset(const filterT& filter,
dataset_->rewind();
}
template <typename filterT>
feature_ptr osm_featureset<filterT>::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<osm_node*>(cur_item)->lat;
double lon = static_cast<osm_node*>(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<filterT>::next()
bounds b = static_cast<osm_way*>(cur_item)->get_bounds();
// Loop until we find a feature which passes the filter
while(cur_item != NULL &&
!filter_.pass(box2d<double>(b.w,b.s,b.e,b.n)))
while (cur_item != NULL &&
! filter_.pass(box2d<double>(b.w, b.s, b.e, b.n)))
{
cur_item = dataset_->next_item();
if(cur_item!=NULL)
if (cur_item != NULL)
{
b = static_cast<osm_way*>(cur_item)->get_bounds();
}
}
if(cur_item != NULL)
if (cur_item != NULL)
{
if(static_cast<osm_way*>(cur_item)->nodes.size())
if (static_cast<osm_way*>(cur_item)->nodes.size())
{
feature = feature_factory::create(feature_id_);
++feature_id_;
geometry_type *geom;
if(static_cast<osm_way*>(cur_item)->is_polygon())
geom = new geometry_type(mapnik::Polygon);
else
geom = new geometry_type(mapnik::LineString);
geom->set_capacity(static_cast<osm_way*>(cur_item)->
nodes.size());
geom->move_to(static_cast<osm_way*>(cur_item)->
nodes[0]->lon,
static_cast<osm_way*>(cur_item)->
nodes[0]->lat);
for(unsigned int count=1; count<static_cast<osm_way*>(cur_item)
->nodes.size(); count++)
geometry_type* geom;
if (static_cast<osm_way*>(cur_item)->is_polygon())
{
geom->line_to(static_cast<osm_way*>(cur_item)
->nodes[count]->lon,
static_cast<osm_way*>(cur_item)
->nodes[count]->lat);
geom = new geometry_type(mapnik::Polygon);
}
else
{
geom = new geometry_type(mapnik::LineString);
}
geom->set_capacity(static_cast<osm_way*>(cur_item)->nodes.size());
geom->move_to(static_cast<osm_way*>(cur_item)->nodes[0]->lon,
static_cast<osm_way*>(cur_item)->nodes[0]->lat);
for (unsigned int count = 1;
count < static_cast<osm_way*>(cur_item)->nodes.size();
count++)
{
geom->line_to(static_cast<osm_way*>(cur_item)->nodes[count]->lon,
static_cast<osm_way*>(cur_item)->nodes[count]->lat);
}
feature->add_geometry(geom);
success = true;
@ -120,31 +120,31 @@ feature_ptr osm_featureset<filterT>::next()
}
// can feature_ptr be compared to NULL? - no
if(success)
if (success)
{
std::map<std::string,std::string>::iterator i=
cur_item->keyvals.begin();
std::map<std::string,std::string>::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 <typename filterT>
osm_featureset<filterT>::~osm_featureset() {}
template class osm_featureset<mapnik::filter_in_box>;
template class osm_featureset<mapnik::filter_at_point>;

View file

@ -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 <set>
// boost
#include <boost/scoped_ptr.hpp>
// mapnik
#include <mapnik/geom_util.hpp>
#include "osm.h"
#include <mapnik/feature.hpp>
#include <mapnik/query.hpp>
#include <mapnik/unicode.hpp>
#include <mapnik/datasource.hpp>
#include <set>
#include "osm.h"
using mapnik::Featureset;
using mapnik::box2d;
@ -40,6 +46,14 @@ using mapnik::transcoder;
template <typename filterT>
class osm_featureset : public Featureset
{
public:
osm_featureset(const filterT& filter,
osm_dataset* dataset,
const std::set<std::string>& attribute_names,
std::string const& encoding);
virtual ~osm_featureset();
feature_ptr next();
private:
filterT filter_;
box2d<double> query_ext_;
boost::scoped_ptr<transcoder> tr_;
@ -49,18 +63,9 @@ class osm_featureset : public Featureset
mutable int feature_id_;
osm_dataset *dataset_;
std::set<std::string> attribute_names_;
public:
osm_featureset(const filterT& filter,
osm_dataset *dataset,
const std::set<std::string>& attribute_names,
std::string const& encoding);
virtual ~osm_featureset();
feature_ptr next();
private:
// no copying
osm_featureset(const osm_featureset&);
const osm_featureset& operator=(const osm_featureset&);
};
#endif //OSM_FS_HH
#endif // OSM_FS_HH

View file

@ -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 <libxml/xmlreader.h>
#include <cstdio>
#include <cstdlib>
@ -6,24 +31,24 @@
#include "osm.h"
#include <map>
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 osm_dataset* components;
static std::string error;
static std::map<long,osm_node*> tmp_node_store;
static std::map<long, osm_node*> 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);
};
#endif // OSMPARSER_H

View file

@ -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 <mapnik/feature_layer_desc.hpp>
class osm_tag_types
{
private:
std::map<std::string,mapnik::eAttributeType> types;
public:
void add_type(std::string tag, mapnik::eAttributeType type)
{
types[tag]=type;
types[tag] = type;
}
mapnik::eAttributeType get_type(std::string tag)
{
std::map<std::string,mapnik::eAttributeType>::iterator i =
types.find(tag);
return (i==types.end()) ? mapnik::String: i->second;
std::map<std::string, mapnik::eAttributeType>::iterator i = types.find(tag);
return (i == types.end()) ? mapnik::String : i->second;
}
private:
std::map<std::string, mapnik::eAttributeType> types;
};
#endif // OSMTAGTYPES_H

View file

@ -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 <iostream>
#include <cmath>
// mapnik
#include <mapnik/map.hpp>
#include <mapnik/layer.hpp>
#include <mapnik/box2d.hpp>
@ -8,52 +35,41 @@
#include <mapnik/font_engine_freetype.hpp>
#include <mapnik/projection.hpp>
using namespace mapnik;
#include <iostream>
#include <cmath>
using namespace std;
int main(int argc,char *argv[])
{
if(argc < 6)
if (argc < 6)
{
std::cerr<<"Usage: render XMLfile w s e n [OSMfile]" << std::endl;
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");
mapnik::datasource_cache::instance()->register_datasources("/usr/local/lib/mapnik/input");
mapnik::freetype_engine::register_font("/usr/local/lib/mapnik/fonts/DejaVuSans.ttf");
Map m (800,800);
load_map(m,argv[1]);
mapnik::Map m(800, 800);
mapnik::load_map(m, argv[1]);
if(argc>6)
if (argc > 6)
{
parameters p;
mapnik::parameters p;
p["type"] = "osm";
p["file"] = argv[6];
for(int count=0; count<m.layer_count(); count++)
for (int count = 0; count < m.layer_count(); count++)
{
parameters q = m.getLayer(count).datasource()->params();
m.getLayer(count).set_datasource(datasource_cache::instance()->
create(p));
mapnik::parameters q = m.getLayer(count).datasource()->params();
m.getLayer(count).set_datasource(mapnik::datasource_cache::instance()->create(p));
}
}
box2d<double> bbox (atof(argv[2]),atof(argv[3]),
atof(argv[4]),atof(argv[5]));
mapnik::box2d<double> bbox (atof(argv[2]), atof(argv[3]), atof(argv[4]), atof(argv[5]));
m.zoom_to_box(bbox);
image_32 buf (m.width(), m.height());
agg_renderer<image_32> r(m,buf);
mapnik::image_32 buf (m.width(), m.height());
mapnik::agg_renderer<mapnik::image_32> r(m, buf);
r.apply();
save_to_file<image_data_32>(buf.data(),"blah.png","png");
mapnik::save_to_file<mapnik::image_data_32>(buf.data(), "blah.png", "png");
return 0;
}