Permalink
Browse files

Use output database ref instead of ptr

Now that the output database is not optional any more, we can use a reference
instead of a pointer to it and remove all existence checks.
  • Loading branch information...
1 parent 27821cb commit f35e30604f6d3a24a00fbdc6b19e7096305b5ca5 @joto joto committed Apr 24, 2012
Showing with 80 additions and 84 deletions.
  1. +9 −11 coastline_handlers.hpp
  2. +9 −13 coastline_ring_collection.cpp
  3. +1 −1 coastline_ring_collection.hpp
  4. +55 −58 osmcoastline.cpp
  5. +4 −1 output_database.cpp
  6. +2 −0 output_database.hpp
View
@@ -69,11 +69,11 @@ class CoastlineHandlerPass2 : public Osmium::Handler::Base {
* thereafter used for each node coming in.
*/
posmap_t m_posmap;
- OutputDatabase* m_output;
+ OutputDatabase& m_output;
public:
- CoastlineHandlerPass2(CoastlineRingCollection& coastline_rings, OutputDatabase* output) :
+ CoastlineHandlerPass2(CoastlineRingCollection& coastline_rings, OutputDatabase& output) :
m_coastline_rings(coastline_rings),
m_posmap(),
m_output(output)
@@ -82,15 +82,13 @@ class CoastlineHandlerPass2 : public Osmium::Handler::Base {
}
void node(const shared_ptr<Osmium::OSM::Node const>& node) {
- if (m_output) {
- const char* natural = node->tags().get_tag_by_key("natural");
- if (natural && !strcmp(natural, "coastline")) {
- try {
- Osmium::Geometry::Point point(*node);
- m_output->add_error_point(point.create_ogr_geometry(), "tagged_node", node->id());
- } catch (Osmium::Exception::IllegalGeometry) {
- std::cerr << "Ignoring illegal geometry for node " << node->id() << ".\n";
- }
+ const char* natural = node->tags().get_tag_by_key("natural");
+ if (natural && !strcmp(natural, "coastline")) {
+ try {
+ Osmium::Geometry::Point point(*node);
+ m_output.add_error_point(point.create_ogr_geometry(), "tagged_node", node->id());
+ } catch (Osmium::Exception::IllegalGeometry) {
+ std::cerr << "Ignoring illegal geometry for node " << node->id() << ".\n";
}
}
@@ -162,7 +162,7 @@ unsigned int CoastlineRingCollection::output_rings(OutputDatabase& output) {
return warnings;
}
-void CoastlineRingCollection::close_rings(OutputDatabase* output, bool debug, double max_distance) {
+void CoastlineRingCollection::close_rings(OutputDatabase& output, bool debug, double max_distance) {
std::vector<Connection> connections;
// Create vector with all possible combinations of connections between rings.
@@ -200,16 +200,14 @@ void CoastlineRingCollection::close_rings(OutputDatabase* output, bool debug, do
CoastlineRing* e = eit->second->get();
CoastlineRing* s = sit->second->get();
- if (output) {
- output->add_error_point(e->ogr_last_point(), "fixed_end_point", e->last_node_id());
- output->add_error_point(s->ogr_first_point(), "fixed_end_point", s->first_node_id());
+ output.add_error_point(e->ogr_last_point(), "fixed_end_point", e->last_node_id());
+ output.add_error_point(s->ogr_first_point(), "fixed_end_point", s->first_node_id());
- if (e->last_position() != s->first_position()) {
- OGRLineString* linestring = new OGRLineString;
- linestring->addPoint(e->last_position().lon(), e->last_position().lat());
- linestring->addPoint(s->first_position().lon(), s->first_position().lat());
- output->add_error_line(linestring, "added_line");
- }
+ if (e->last_position() != s->first_position()) {
+ OGRLineString* linestring = new OGRLineString;
+ linestring->addPoint(e->last_position().lon(), e->last_position().lat());
+ linestring->addPoint(s->first_position().lon(), s->first_position().lat());
+ output.add_error_line(linestring, "added_line");
}
if (e == s) {
@@ -223,9 +221,7 @@ void CoastlineRingCollection::close_rings(OutputDatabase* output, bool debug, do
e->join_over_gap(*s);
if (e->first_position() == e->last_position()) {
- if (output) {
- output->add_error_point(e->ogr_first_point(), "double_node", e->first_node_id());
- }
+ output.add_error_point(e->ogr_first_point(), "double_node", e->first_node_id());
m_start_nodes.erase(e->first_node_id());
m_end_nodes.erase(eit);
m_start_nodes.erase(sit);
@@ -95,7 +95,7 @@ class CoastlineRingCollection {
unsigned int output_rings(OutputDatabase& output);
- void close_rings(OutputDatabase* output, bool debug, double max_distance);
+ void close_rings(OutputDatabase& output, bool debug, double max_distance);
private:
View
@@ -51,7 +51,7 @@ bool debug;
/**
* This function assembles all the coastline rings into one huge multipolygon.
*/
-polygon_vector_t* create_polygons(CoastlineRingCollection coastline_rings, OutputDatabase* output) {
+polygon_vector_t* create_polygons(CoastlineRingCollection coastline_rings, OutputDatabase& output) {
std::vector<OGRGeometry*> all_polygons;
coastline_rings.add_polygons_to_vector(all_polygons);
@@ -70,7 +70,7 @@ polygon_vector_t* create_polygons(CoastlineRingCollection coastline_rings, Outpu
if (p->IsValid()) {
polygons->push_back(p);
} else {
- output->add_error_line(static_cast<OGRLineString*>(p->getExteriorRing()->clone()), "invalid");
+ output.add_error_line(static_cast<OGRLineString*>(p->getExteriorRing()->clone()), "invalid");
OGRGeometry* buf0 = p->Buffer(0);
if (buf0 && buf0->getGeometryType() == wkbPolygon && buf0->IsValid()) {
buf0->assignSpatialReference(srs.wgs84());
@@ -208,7 +208,7 @@ int main(int argc, char *argv[]) {
} else {
vout << "Will NOT create geometry index (because you told me to using --no-index/-i).\n";
}
- OutputDatabase* output_database = new OutputDatabase(options.output_database, options.create_index);
+ OutputDatabase output_database(options.output_database, options.create_index);
// The collection of all coastline rings we will be filling and then
// operating on.
@@ -240,72 +240,69 @@ int main(int argc, char *argv[]) {
vout << memory_usage();
- if (output_database) {
- output_database->set_options(options);
+ output_database.set_options(options);
- if (options.close_rings) {
- vout << "Close broken rings... (Use --close-distance/-c 0 if you do not want this.)\n";
- vout << " Closing if distance between nodes smaller than " << options.close_distance << ". (Set this with --close-distance/-c.)\n";
- coastline_rings.close_rings(output_database, options.debug, options.close_distance);
- stats.rings_fixed = coastline_rings.num_fixed_rings();
- vout << " Closed " << coastline_rings.num_fixed_rings() << " rings. This left "
- << coastline_rings.num_unconnected_nodes() << " nodes where the coastline could not be closed.\n";
- } else {
- vout << "Not closing broken rings (because you used the option --close-distance/-c 0).\n";
- }
+ if (options.close_rings) {
+ vout << "Close broken rings... (Use --close-distance/-c 0 if you do not want this.)\n";
+ vout << " Closing if distance between nodes smaller than " << options.close_distance << ". (Set this with --close-distance/-c.)\n";
+ coastline_rings.close_rings(output_database, options.debug, options.close_distance);
+ stats.rings_fixed = coastline_rings.num_fixed_rings();
+ vout << " Closed " << coastline_rings.num_fixed_rings() << " rings. This left "
+ << coastline_rings.num_unconnected_nodes() << " nodes where the coastline could not be closed.\n";
+ } else {
+ vout << "Not closing broken rings (because you used the option --close-distance/-c 0).\n";
+ }
- if (options.output_rings) {
- vout << "Writing out rings... (Because you gave the --output-rings/-r option.)\n";
- warnings += coastline_rings.output_rings(*output_database);
- } else {
- vout << "Not writing out rings. (Use option --output-rings/-r if you want the rings.)\n";
- }
+ if (options.output_rings) {
+ vout << "Writing out rings... (Because you gave the --output-rings/-r option.)\n";
+ warnings += coastline_rings.output_rings(output_database);
+ } else {
+ vout << "Not writing out rings. (Use option --output-rings/-r if you want the rings.)\n";
+ }
- if (options.output_polygons) {
- vout << "Create polygons...\n";
- CoastlinePolygons coastline_polygons(create_polygons(coastline_rings, output_database), *output_database, options.bbox_overlap, options.max_points_in_polygon);
- stats.land_polygons_before_split = coastline_polygons.num_polygons();
+ if (options.output_polygons) {
+ vout << "Create polygons...\n";
+ CoastlinePolygons coastline_polygons(create_polygons(coastline_rings, output_database), output_database, options.bbox_overlap, options.max_points_in_polygon);
+ stats.land_polygons_before_split = coastline_polygons.num_polygons();
- vout << "Fixing coastlines going the wrong way...\n";
- stats.rings_turned_around = coastline_polygons.fix_direction();
- vout << " Turned " << stats.rings_turned_around << " polygons around.\n";
- warnings += stats.rings_turned_around;
+ vout << "Fixing coastlines going the wrong way...\n";
+ stats.rings_turned_around = coastline_polygons.fix_direction();
+ vout << " Turned " << stats.rings_turned_around << " polygons around.\n";
+ warnings += stats.rings_turned_around;
- if (options.epsg != 4326) {
- vout << "Transforming polygons to EPSG " << options.epsg << "...\n";
- coastline_polygons.transform();
- }
+ if (options.epsg != 4326) {
+ vout << "Transforming polygons to EPSG " << options.epsg << "...\n";
+ coastline_polygons.transform();
+ }
- if (options.simplify) {
- vout << "Simplifying polygons with tolerance " << options.tolerance << " (because you used --simplify/-S).\n";
- coastline_polygons.simplify(options.tolerance);
- }
+ if (options.simplify) {
+ vout << "Simplifying polygons with tolerance " << options.tolerance << " (because you used --simplify/-S).\n";
+ coastline_polygons.simplify(options.tolerance);
+ }
- if (options.split_large_polygons || options.water) {
- vout << "Split polygons with more than " << options.max_points_in_polygon << " points... (Use --max-points/-m to change this. Set to 0 not to split at all.)\n";
- vout << " Using overlap of " << options.bbox_overlap << " (Set this with --bbox-overlap/-b).\n";
- coastline_polygons.split();
- stats.land_polygons_after_split = coastline_polygons.num_polygons();
- }
- if (options.water) {
- vout << "Writing out water polygons...\n";
- coastline_polygons.output_water_polygons();
- } else {
- coastline_polygons.output_land_polygons();
- }
+ if (options.split_large_polygons || options.water) {
+ vout << "Split polygons with more than " << options.max_points_in_polygon << " points... (Use --max-points/-m to change this. Set to 0 not to split at all.)\n";
+ vout << " Using overlap of " << options.bbox_overlap << " (Set this with --bbox-overlap/-b).\n";
+ coastline_polygons.split();
+ stats.land_polygons_after_split = coastline_polygons.num_polygons();
+ }
+ if (options.water) {
+ vout << "Writing out water polygons...\n";
+ coastline_polygons.output_water_polygons();
} else {
- vout << "Not creating polygons (Because you set the --no-polygons/-p option).\n";
+ coastline_polygons.output_land_polygons();
}
+ } else {
+ vout << "Not creating polygons (Because you set the --no-polygons/-p option).\n";
+ }
- vout << memory_usage();
-
- vout << "Commiting database transactions...\n";
- output_database->set_meta(vout.runtime(), get_memory_usage().second, stats);
- delete output_database;
+ vout << memory_usage();
- vout << "All done.\n";
- vout << memory_usage();
- }
+ vout << "Commiting database transactions...\n";
+ output_database.set_meta(vout.runtime(), get_memory_usage().second, stats);
+ output_database.commit();
+ vout << "All done.\n";
+ vout << memory_usage();
vout << "There were " << warnings << " warnings.\n";
vout << "There were " << errors << " errors.\n";
View
@@ -119,11 +119,14 @@ void OutputDatabase::set_meta(int runtime, int memory_usage, const Stats& stats)
}
OutputDatabase::~OutputDatabase() {
+ OGRDataSource::DestroyDataSource(m_data_source);
+}
+
+void OutputDatabase::commit() {
m_layer_polygons->commit();
m_layer_rings->commit();
m_layer_error_lines->commit();
m_layer_error_points->commit();
- OGRDataSource::DestroyDataSource(m_data_source);
}
void OutputDatabase::add_error_point(OGRPoint* point, const char* error, osm_object_id_t id) {
View
@@ -88,6 +88,8 @@ class OutputDatabase {
void set_options(const Options& options);
void set_meta(int runtime, int memory_usage, const Stats& stats);
+ void commit();
+
};
#endif // OUTPUT_DATABASE_HPP

0 comments on commit f35e306

Please sign in to comment.