now distinguishes between lines and polygons for ways

This commit is contained in:
Nick Whitelegg 2008-04-02 21:40:28 +00:00
parent e128d51534
commit 49208998c6
5 changed files with 105 additions and 57 deletions

View file

@ -1,4 +1,4 @@
CXXFLAGS = `xml2-config --cflags` -I/usr/local/include/mapnik -I/usr/include/boost -I/usr/include/freetype2 -fPIC -g
CXXFLAGS = `xml2-config --cflags` -I/usr/local/include/mapnik -I/usr/include/boost -I/usr/include/freetype2 -I/home/nick/mapnik-osm/agg/include -fPIC -g
MAPNIK_OSM_OBJ = osmparser.o osm.o osm_datasource.o osm_featureset.o
osm.input: $(MAPNIK_OSM_OBJ)
g++ -shared -o osm.input $(MAPNIK_OSM_OBJ)

View file

@ -10,6 +10,8 @@
#include <iostream>
using namespace std;
polygon_types osm_way::ptypes;
bool osm_dataset::load(const char* filename,const std::string& parser)
{
if (parser=="libxml2")
@ -174,3 +176,16 @@ bounds osm_way::get_bounds()
}
return b;
}
bool osm_way::is_polygon()
{
for(int count=0; count<ptypes.ptypes.size(); count++)
{
if(keyvals.find(ptypes.ptypes[count].first) != keyvals.end() &&
keyvals[ptypes.ptypes[count].first] == ptypes.ptypes[count].second)
{
return true;
}
}
return false;
}

View file

@ -5,72 +5,93 @@
#include <string>
#include <map>
#include <set>
#include <utility>
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 )
{
this->w = w;
this->s = s;
this->e = e;
this->n = 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;
this->e = e;
this->n = n;
}
};
class polygon_types
{
public:
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>
("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;
virtual std::string to_string();
long id;
std::map<std::string,std::string> keyvals;
virtual std::string to_string();
};
struct osm_node: public osm_item
{
double lat, lon;
std::string to_string();
double lat, lon;
std::string to_string();
};
struct osm_way: public osm_item
{
std::vector<osm_node*> nodes;
std::string to_string();
bounds get_bounds();
std::vector<osm_node*> nodes;
std::string to_string();
bounds get_bounds();
bool is_polygon();
static polygon_types ptypes;
};
class osm_dataset
{
private:
int next_item_mode;
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;
int next_item_mode;
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(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");
~osm_dataset();
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; }
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; }
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");
~osm_dataset();
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; }
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; }
};
#endif // OSM_H

View file

@ -30,6 +30,10 @@ using mapnik::feature_ptr;
using mapnik::geometry2d;
using mapnik::point_impl;
using mapnik::line_string_impl;
using mapnik::polygon_impl;
using std::cerr;
using std::endl;
template <typename filterT>
osm_featureset<filterT>::osm_featureset(const filterT& filter,
@ -84,10 +88,15 @@ feature_ptr osm_featureset<filterT>::next()
if(static_cast<osm_way*>(cur_item)->nodes.size())
{
feature=feature_ptr(new Feature(count_++));
geometry2d *line = new line_string_impl;
line->set_capacity(static_cast<osm_way*>(cur_item)->
geometry2d *geom;
if(static_cast<osm_way*>(cur_item)->is_polygon())
geom=new polygon_impl;
else
geom=new line_string_impl;
geom->set_capacity(static_cast<osm_way*>(cur_item)->
nodes.size());
line->move_to(static_cast<osm_way*>(cur_item)->
geom->move_to(static_cast<osm_way*>(cur_item)->
nodes[0]->lon,
static_cast<osm_way*>(cur_item)->
nodes[0]->lat);
@ -95,12 +104,12 @@ feature_ptr osm_featureset<filterT>::next()
for(int count=1; count<static_cast<osm_way*>(cur_item)
->nodes.size(); count++)
{
line->line_to(static_cast<osm_way*>(cur_item)
geom->line_to(static_cast<osm_way*>(cur_item)
->nodes[count]->lon,
static_cast<osm_way*>(cur_item)
->nodes[count]->lat);
}
feature->add_geometry(line);
feature->add_geometry(geom);
success=true;
}
}

View file

@ -19,7 +19,7 @@ int main(int argc,char *argv[])
{
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);
}
@ -31,14 +31,17 @@ int main(int argc,char *argv[])
Map m (800,800);
load_map(m,argv[1]);
parameters p;
p["type"] = "osm";
p["file"] = argv[6];
for(int count=0; count<m.layerCount(); count++)
if(argc>6)
{
parameters q = m.getLayer(count).datasource()->params();
m.getLayer(count).set_datasource(datasource_cache::instance()->
create(p));
parameters p;
p["type"] = "osm";
p["file"] = argv[6];
for(int count=0; count<m.layerCount(); count++)
{
parameters q = m.getLayer(count).datasource()->params();
m.getLayer(count).set_datasource(datasource_cache::instance()->
create(p));
}
}
Envelope<double> bbox (atof(argv[2]),atof(argv[3]),