2011-10-22 14:04:05 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2008-03-01 11:49:37 +00:00
|
|
|
#include "osm.h"
|
2008-03-02 13:23:50 +00:00
|
|
|
#include "osmparser.h"
|
2011-10-22 14:04:05 +00:00
|
|
|
#include "basiccurl.h"
|
|
|
|
|
2012-04-08 00:20:56 +00:00
|
|
|
#include <mapnik/debug.hpp>
|
|
|
|
|
2008-03-01 11:49:37 +00:00
|
|
|
#include <libxml/parser.h>
|
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
|
|
|
|
2008-04-02 21:40:28 +00:00
|
|
|
polygon_types osm_way::ptypes;
|
|
|
|
|
2012-09-03 17:27:48 +00:00
|
|
|
bool osm_dataset::load(const char* filename,std::string const& parser)
|
2008-03-01 11:49:37 +00:00
|
|
|
{
|
2011-10-22 14:04:05 +00:00
|
|
|
if (parser == "libxml2")
|
|
|
|
{
|
|
|
|
return osmparser::parse(this, filename);
|
|
|
|
}
|
|
|
|
return false;
|
2008-03-01 11:49:37 +00:00
|
|
|
}
|
|
|
|
|
2012-09-03 17:27:48 +00:00
|
|
|
bool osm_dataset::load_from_url(std::string const& url,
|
|
|
|
std::string const& bbox,
|
|
|
|
std::string const& parser)
|
2009-02-15 13:51:07 +00:00
|
|
|
{
|
2011-10-22 14:04:05 +00:00
|
|
|
if (parser == "libxml2")
|
|
|
|
{
|
2012-04-09 01:00:51 +00:00
|
|
|
MAPNIK_LOG_DEBUG(osm) << "osm_dataset: load_from_url url=" << url << ",bbox=" << bbox;
|
2011-10-22 14:04:05 +00:00
|
|
|
|
|
|
|
std::ostringstream str;
|
|
|
|
// use curl to grab the data
|
|
|
|
// fetch all the data we want - probably from osmxpai
|
2009-02-15 13:51:07 +00:00
|
|
|
|
|
|
|
str << url << "?bbox=" << bbox;
|
2009-04-28 20:07:18 +00:00
|
|
|
|
2012-04-09 01:00:51 +00:00
|
|
|
MAPNIK_LOG_DEBUG(osm) << "osm_dataset: Full url=" << str.str();
|
2009-04-28 20:07:18 +00:00
|
|
|
|
2011-10-22 14:04:05 +00:00
|
|
|
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';
|
2009-04-28 20:07:18 +00:00
|
|
|
|
2012-04-09 01:00:51 +00:00
|
|
|
MAPNIK_LOG_DEBUG(osm) << "osm_dataset: CURL Response=" << blx;
|
2009-04-28 20:07:18 +00:00
|
|
|
|
2011-10-22 14:04:05 +00:00
|
|
|
delete[] blx;
|
|
|
|
bool success = osmparser::parse(this, resp->data, resp->nbytes);
|
|
|
|
return success;
|
|
|
|
}
|
2011-04-02 03:45:50 +00:00
|
|
|
}
|
2011-10-22 14:04:05 +00:00
|
|
|
return false;
|
2009-02-15 13:51:07 +00:00
|
|
|
}
|
|
|
|
|
2008-03-01 11:49:37 +00:00
|
|
|
osm_dataset::~osm_dataset()
|
|
|
|
{
|
2011-10-22 14:04:05 +00:00
|
|
|
clear();
|
2009-02-16 23:10:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void osm_dataset::clear()
|
|
|
|
{
|
2012-04-09 01:00:51 +00:00
|
|
|
MAPNIK_LOG_DEBUG(osm) << "osm_dataset: Clear";
|
2009-04-28 20:07:18 +00:00
|
|
|
|
2012-04-09 01:00:51 +00:00
|
|
|
MAPNIK_LOG_DEBUG(osm) << "osm_dataset: -- Deleting ways";
|
2011-10-22 14:04:05 +00:00
|
|
|
for (unsigned int count = 0; count < ways.size(); ++count)
|
|
|
|
{
|
|
|
|
delete ways[count];
|
|
|
|
ways[count] = NULL;
|
|
|
|
}
|
2012-04-08 00:20:56 +00:00
|
|
|
ways.clear();
|
2009-04-28 20:07:18 +00:00
|
|
|
|
2012-04-09 01:00:51 +00:00
|
|
|
MAPNIK_LOG_DEBUG(osm) << "osm_dataset: -- Deleting nodes";
|
2011-10-22 14:04:05 +00:00
|
|
|
for (unsigned int count = 0; count < nodes.size(); ++count)
|
|
|
|
{
|
|
|
|
delete nodes[count];
|
|
|
|
nodes[count] = NULL;
|
|
|
|
}
|
|
|
|
nodes.clear();
|
2009-04-28 20:07:18 +00:00
|
|
|
|
2012-04-09 01:00:51 +00:00
|
|
|
MAPNIK_LOG_DEBUG(osm) << "osm_dataset: Clear done";
|
2008-03-01 11:49:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string osm_dataset::to_string()
|
|
|
|
{
|
2011-10-22 14:04:05 +00:00
|
|
|
std::string 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;
|
2008-03-01 11:49:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bounds osm_dataset::get_bounds()
|
|
|
|
{
|
2011-10-22 14:04:05 +00:00
|
|
|
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;
|
2008-03-01 11:49:37 +00:00
|
|
|
}
|
|
|
|
|
2011-10-22 14:04:05 +00:00
|
|
|
osm_node* osm_dataset::next_node()
|
2008-03-01 11:49:37 +00:00
|
|
|
{
|
2011-10-22 14:04:05 +00:00
|
|
|
if (node_i != nodes.end())
|
|
|
|
{
|
|
|
|
return *(node_i++);
|
|
|
|
}
|
|
|
|
return NULL;
|
2008-03-01 11:49:37 +00:00
|
|
|
}
|
2011-10-22 14:04:05 +00:00
|
|
|
|
|
|
|
osm_way* osm_dataset::next_way()
|
2008-03-01 11:49:37 +00:00
|
|
|
{
|
2011-10-22 14:04:05 +00:00
|
|
|
if (way_i != ways.end())
|
|
|
|
{
|
|
|
|
return *(way_i++);
|
|
|
|
}
|
|
|
|
return NULL;
|
2008-03-01 11:49:37 +00:00
|
|
|
}
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2011-10-22 14:04:05 +00:00
|
|
|
osm_item* osm_dataset::next_item()
|
2008-03-01 11:49:37 +00:00
|
|
|
{
|
2011-10-22 14:04:05 +00:00
|
|
|
osm_item* item = NULL;
|
|
|
|
if (next_item_mode == Node)
|
|
|
|
{
|
|
|
|
item = next_node();
|
|
|
|
if (item == NULL)
|
|
|
|
{
|
|
|
|
next_item_mode = Way;
|
|
|
|
rewind_ways();
|
|
|
|
item = next_way();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2011-04-02 03:45:50 +00:00
|
|
|
{
|
2011-10-22 14:04:05 +00:00
|
|
|
item = next_way();
|
2011-04-02 03:45:50 +00:00
|
|
|
}
|
2011-10-22 14:04:05 +00:00
|
|
|
return item;
|
2008-03-01 11:49:37 +00:00
|
|
|
}
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2008-03-01 11:49:37 +00:00
|
|
|
std::set<std::string> osm_dataset::get_keys()
|
|
|
|
{
|
2011-10-22 14:04:05 +00:00
|
|
|
std::set<std::string> keys;
|
|
|
|
for (unsigned int count = 0; count < nodes.size(); ++count)
|
2011-04-02 03:45:50 +00:00
|
|
|
{
|
2011-10-22 14:04:05 +00:00
|
|
|
for (std::map<std::string, std::string>::iterator i = nodes[count]->keyvals.begin();
|
|
|
|
i != nodes[count]->keyvals.end(); i++)
|
|
|
|
{
|
|
|
|
keys.insert(i->first);
|
|
|
|
}
|
2011-04-02 03:45:50 +00:00
|
|
|
}
|
2011-10-22 14:04:05 +00:00
|
|
|
|
|
|
|
for (unsigned int count = 0; count < ways.size(); ++count)
|
2011-04-02 03:45:50 +00:00
|
|
|
{
|
2011-10-22 14:04:05 +00:00
|
|
|
for (std::map<std::string, std::string>::iterator i = ways[count]->keyvals.begin();
|
|
|
|
i != ways[count]->keyvals.end(); i++)
|
|
|
|
{
|
|
|
|
keys.insert(i->first);
|
|
|
|
}
|
2011-04-02 03:45:50 +00:00
|
|
|
}
|
2011-10-22 14:04:05 +00:00
|
|
|
return keys;
|
2008-03-01 11:49:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string osm_item::to_string()
|
|
|
|
{
|
2011-10-22 14:04:05 +00:00
|
|
|
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++)
|
|
|
|
{
|
|
|
|
strm << "Key " << i->first << " Value " << i->second << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
return strm.str();
|
2008-03-01 11:49:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string osm_node::to_string()
|
|
|
|
{
|
2011-10-22 14:04:05 +00:00
|
|
|
std::ostringstream strm;
|
|
|
|
strm << "Node: " << osm_item::to_string() << " lat=" << lat <<" lon=" <<lon << std::endl;
|
|
|
|
return strm.str();
|
2008-03-01 11:49:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string osm_way::to_string()
|
|
|
|
{
|
2011-10-22 14:04:05 +00:00
|
|
|
std::ostringstream strm;
|
|
|
|
strm << "Way: " << osm_item::to_string() << "Nodes in way:";
|
2008-03-01 11:49:37 +00:00
|
|
|
|
2011-10-22 14:04:05 +00:00
|
|
|
for (unsigned int count = 0; count < nodes.size(); ++count)
|
2011-04-02 03:45:50 +00:00
|
|
|
{
|
2011-10-22 14:04:05 +00:00
|
|
|
if (nodes[count] != NULL)
|
|
|
|
{
|
|
|
|
strm << nodes[count]->id << " ";
|
|
|
|
}
|
2011-04-02 03:45:50 +00:00
|
|
|
}
|
2011-10-22 14:04:05 +00:00
|
|
|
|
|
|
|
strm << std::endl;
|
|
|
|
return strm.str();
|
2008-03-01 11:49:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bounds osm_way::get_bounds()
|
|
|
|
{
|
2011-10-22 14:04:05 +00:00
|
|
|
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;
|
2008-03-01 11:49:37 +00:00
|
|
|
}
|
2008-04-02 21:40:28 +00:00
|
|
|
|
|
|
|
bool osm_way::is_polygon()
|
|
|
|
{
|
2011-10-22 14:04:05 +00:00
|
|
|
for (unsigned int count = 0; count < ptypes.ptypes.size(); ++count)
|
2011-04-02 03:45:50 +00:00
|
|
|
{
|
2011-10-22 14:04:05 +00:00
|
|
|
if (keyvals.find(ptypes.ptypes[count].first) != keyvals.end() &&
|
|
|
|
keyvals[ptypes.ptypes[count].first] == ptypes.ptypes[count].second)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2011-04-02 03:45:50 +00:00
|
|
|
}
|
2011-10-22 14:04:05 +00:00
|
|
|
|
|
|
|
return false;
|
2008-04-02 21:40:28 +00:00
|
|
|
}
|