osrm-backend/third_party/libosmium/examples/osmium_toogr2.cpp

332 lines
12 KiB
C++
Raw Normal View History

/*
This is an example tool that converts OSM data to some output format
like Spatialite or Shapefiles using the OGR library.
This version does multipolygon handling (in contrast to the osmium_toogr
example which doesn't).
The code in this example file is released into the Public Domain.
*/
#include <iostream>
#include <getopt.h>
// usually you only need one or two of these
#include <osmium/index/map/dummy.hpp>
Squashed 'third_party/libosmium/' changes from 6522da5..f074d94 f074d94 Use shorter path names in Doxygen doc. 5117d5a Make Doxygen-generated HTML docs work better on small screens. 41607ac Appveyor config: Use lowercase command names everywhere. Filter 7z output. 9e77a39 Remove debug output. 3416c4b Revert "Comment out test to see if there are dependencies between tests." fa49dde Comment out test to see if there are dependencies between tests. 5add75d Fix typo in appveyor conf: nul. 53b3778 Use different file names for tests. 1091c0b Fix Windows munmap() to return the same as the posix one. 14fdc4e Revert "CMake: Remove unneeded build type setting." 0270087 Workaround for MSVC. 56c2120 Another try... 8e96a6f Trying to fix test on Windows... 11a64c2 CMake workaround: Set CMAKE_CONFIGURATION_TYPES before project(). As per http://www.cmake.org/pipermail/cmake/2012-January/048856.html 3847e7a CMake: Remove unneeded build type setting. a1bcaf1 CMake: Make CMAKE_CONFIGURATION_TYPES a cache variable. ddab3e3 Create test file in test_typed_mmap in current dir instead of tmp. ce1d3bd Build "RelWithDebInfo" on appveyor instead of "Release". b6db34f Add README showing source of FindGem.cmake. 2c86c55 Better error message and some comments in CMake for sparsetable check. b16a5a3 Fix ctest on Windows. 2203bc8 Fix FindGem.cmake. 5d1f81d Another try to fix travis build. ddd3f5e Hopefully fix problem where travis doesn't find osm-testdata. 0e673ea More debug output to find travis problem. eb01107 Updated data-tests README. 8a971c8 Remove special case of the multipolygon test in travis/appveyor config. dce792c CMake: Check dependencies of multipolygon test and run as CMake script. b967677 Add tests for thread pool. 400e9b3 Bugfix: Handle exception in one pool threads properly. a1ba489 More detailed error reporting from zlib. 95cf621 Disable SparseMemTable if sparsetable size_type is smaller than 8. 7b601b5 Add missing overload to cast_with_assert() function. 60a7d86 Giving up on trying to remove LNK4221 warning. 35ed5df Try another way to get rid of MSV warning... f9c7d92 Disable a warning about changed behaviour on MSVC. c3c6b2d CTest: Only output failed tests. 9c99996 Try setting option in a different way for getting rid of LNK4221 on MSVC. 4ac563c Another ssize_t fix for Windows. 11db84f Fix mmap for windows. Convert macro to function. 95d8f75 Fix test on Windows (which doesn't have ssize_t). df51aa4 Do not write huge files in one system call. 9c4f772 Change mmap() implementation for Windows. 5be817c Rewrote function to work on Windows without warnings. ea84f73 Fix warnings on Windows. 572d692 Fixed the static_cast_with_assert function and added tests. 89ef86b Use SparseMemArray instead of SparseMemTable in examples. git-subtree-dir: third_party/libosmium git-subtree-split: f074d949a5585a81578d682035f2163de971beb3
2015-03-04 06:50:42 -05:00
#include <osmium/index/map/sparse_mem_array.hpp>
#include <osmium/handler/node_locations_for_ways.hpp>
#include <osmium/visitor.hpp>
#include <osmium/area/multipolygon_collector.hpp>
#include <osmium/area/assembler.hpp>
#include <osmium/geom/mercator_projection.hpp>
//#include <osmium/geom/projection.hpp>
#include <osmium/geom/ogr.hpp>
#include <osmium/io/any_input.hpp>
#include <osmium/handler.hpp>
typedef osmium::index::map::Dummy<osmium::unsigned_object_id_type, osmium::Location> index_neg_type;
Squashed 'third_party/libosmium/' changes from 6522da5..f074d94 f074d94 Use shorter path names in Doxygen doc. 5117d5a Make Doxygen-generated HTML docs work better on small screens. 41607ac Appveyor config: Use lowercase command names everywhere. Filter 7z output. 9e77a39 Remove debug output. 3416c4b Revert "Comment out test to see if there are dependencies between tests." fa49dde Comment out test to see if there are dependencies between tests. 5add75d Fix typo in appveyor conf: nul. 53b3778 Use different file names for tests. 1091c0b Fix Windows munmap() to return the same as the posix one. 14fdc4e Revert "CMake: Remove unneeded build type setting." 0270087 Workaround for MSVC. 56c2120 Another try... 8e96a6f Trying to fix test on Windows... 11a64c2 CMake workaround: Set CMAKE_CONFIGURATION_TYPES before project(). As per http://www.cmake.org/pipermail/cmake/2012-January/048856.html 3847e7a CMake: Remove unneeded build type setting. a1bcaf1 CMake: Make CMAKE_CONFIGURATION_TYPES a cache variable. ddab3e3 Create test file in test_typed_mmap in current dir instead of tmp. ce1d3bd Build "RelWithDebInfo" on appveyor instead of "Release". b6db34f Add README showing source of FindGem.cmake. 2c86c55 Better error message and some comments in CMake for sparsetable check. b16a5a3 Fix ctest on Windows. 2203bc8 Fix FindGem.cmake. 5d1f81d Another try to fix travis build. ddd3f5e Hopefully fix problem where travis doesn't find osm-testdata. 0e673ea More debug output to find travis problem. eb01107 Updated data-tests README. 8a971c8 Remove special case of the multipolygon test in travis/appveyor config. dce792c CMake: Check dependencies of multipolygon test and run as CMake script. b967677 Add tests for thread pool. 400e9b3 Bugfix: Handle exception in one pool threads properly. a1ba489 More detailed error reporting from zlib. 95cf621 Disable SparseMemTable if sparsetable size_type is smaller than 8. 7b601b5 Add missing overload to cast_with_assert() function. 60a7d86 Giving up on trying to remove LNK4221 warning. 35ed5df Try another way to get rid of MSV warning... f9c7d92 Disable a warning about changed behaviour on MSVC. c3c6b2d CTest: Only output failed tests. 9c99996 Try setting option in a different way for getting rid of LNK4221 on MSVC. 4ac563c Another ssize_t fix for Windows. 11db84f Fix mmap for windows. Convert macro to function. 95d8f75 Fix test on Windows (which doesn't have ssize_t). df51aa4 Do not write huge files in one system call. 9c4f772 Change mmap() implementation for Windows. 5be817c Rewrote function to work on Windows without warnings. ea84f73 Fix warnings on Windows. 572d692 Fixed the static_cast_with_assert function and added tests. 89ef86b Use SparseMemArray instead of SparseMemTable in examples. git-subtree-dir: third_party/libosmium git-subtree-split: f074d949a5585a81578d682035f2163de971beb3
2015-03-04 06:50:42 -05:00
typedef osmium::index::map::SparseMemArray<osmium::unsigned_object_id_type, osmium::Location> index_pos_type;
typedef osmium::handler::NodeLocationsForWays<index_pos_type, index_neg_type> location_handler_type;
class MyOGRHandler : public osmium::handler::Handler {
OGRDataSource* m_data_source;
OGRLayer* m_layer_point;
OGRLayer* m_layer_linestring;
OGRLayer* m_layer_polygon;
// Choose one of the following:
// 1. Use WGS84, do not project coordinates.
//osmium::geom::OGRFactory<> m_factory {};
// 2. Project coordinates into "Web Mercator".
osmium::geom::OGRFactory<osmium::geom::MercatorProjection> m_factory;
// 3. Use any projection that the proj library can handle.
// (Initialize projection with EPSG code or proj string).
// In addition you need to link with "-lproj" and add
// #include <osmium/geom/projection.hpp>.
//osmium::geom::OGRFactory<osmium::geom::Projection> m_factory {osmium::geom::Projection(3857)};
public:
MyOGRHandler(const std::string& driver_name, const std::string& filename) {
OGRRegisterAll();
OGRSFDriver* driver = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(driver_name.c_str());
if (!driver) {
std::cerr << driver_name << " driver not available.\n";
exit(1);
}
CPLSetConfigOption("OGR_SQLITE_SYNCHRONOUS", "FALSE");
const char* options[] = { "SPATIALITE=TRUE", nullptr };
m_data_source = driver->CreateDataSource(filename.c_str(), const_cast<char**>(options));
if (!m_data_source) {
std::cerr << "Creation of output file failed.\n";
exit(1);
}
OGRSpatialReference sparef;
sparef.importFromProj4(m_factory.proj_string().c_str());
m_layer_point = m_data_source->CreateLayer("postboxes", &sparef, wkbPoint, nullptr);
if (!m_layer_point) {
std::cerr << "Layer creation failed.\n";
exit(1);
}
OGRFieldDefn layer_point_field_id("id", OFTReal);
layer_point_field_id.SetWidth(10);
if (m_layer_point->CreateField(&layer_point_field_id) != OGRERR_NONE) {
std::cerr << "Creating id field failed.\n";
exit(1);
}
OGRFieldDefn layer_point_field_operator("operator", OFTString);
layer_point_field_operator.SetWidth(30);
if (m_layer_point->CreateField(&layer_point_field_operator) != OGRERR_NONE) {
std::cerr << "Creating operator field failed.\n";
exit(1);
}
/* Transactions might make things faster, then again they might not.
Feel free to experiment and benchmark and report back. */
m_layer_point->StartTransaction();
m_layer_linestring = m_data_source->CreateLayer("roads", &sparef, wkbLineString, nullptr);
if (!m_layer_linestring) {
std::cerr << "Layer creation failed.\n";
exit(1);
}
OGRFieldDefn layer_linestring_field_id("id", OFTReal);
layer_linestring_field_id.SetWidth(10);
if (m_layer_linestring->CreateField(&layer_linestring_field_id) != OGRERR_NONE) {
std::cerr << "Creating id field failed.\n";
exit(1);
}
OGRFieldDefn layer_linestring_field_type("type", OFTString);
layer_linestring_field_type.SetWidth(30);
if (m_layer_linestring->CreateField(&layer_linestring_field_type) != OGRERR_NONE) {
std::cerr << "Creating type field failed.\n";
exit(1);
}
m_layer_linestring->StartTransaction();
m_layer_polygon = m_data_source->CreateLayer("buildings", &sparef, wkbMultiPolygon, nullptr);
if (!m_layer_polygon) {
std::cerr << "Layer creation failed.\n";
exit(1);
}
OGRFieldDefn layer_polygon_field_id("id", OFTInteger);
layer_polygon_field_id.SetWidth(10);
if (m_layer_polygon->CreateField(&layer_polygon_field_id) != OGRERR_NONE) {
std::cerr << "Creating id field failed.\n";
exit(1);
}
OGRFieldDefn layer_polygon_field_type("type", OFTString);
layer_polygon_field_type.SetWidth(30);
if (m_layer_polygon->CreateField(&layer_polygon_field_type) != OGRERR_NONE) {
std::cerr << "Creating type field failed.\n";
exit(1);
}
m_layer_polygon->StartTransaction();
}
~MyOGRHandler() {
m_layer_polygon->CommitTransaction();
m_layer_linestring->CommitTransaction();
m_layer_point->CommitTransaction();
OGRDataSource::DestroyDataSource(m_data_source);
OGRCleanupAll();
}
void node(const osmium::Node& node) {
const char* amenity = node.tags()["amenity"];
if (amenity && !strcmp(amenity, "post_box")) {
OGRFeature* feature = OGRFeature::CreateFeature(m_layer_point->GetLayerDefn());
std::unique_ptr<OGRPoint> ogr_point = m_factory.create_point(node);
feature->SetGeometry(ogr_point.get());
feature->SetField("id", static_cast<double>(node.id()));
feature->SetField("operator", node.tags()["operator"]);
if (m_layer_point->CreateFeature(feature) != OGRERR_NONE) {
std::cerr << "Failed to create feature.\n";
exit(1);
}
OGRFeature::DestroyFeature(feature);
}
}
void way(const osmium::Way& way) {
const char* highway = way.tags()["highway"];
if (highway) {
try {
std::unique_ptr<OGRLineString> ogr_linestring = m_factory.create_linestring(way);
OGRFeature* feature = OGRFeature::CreateFeature(m_layer_linestring->GetLayerDefn());
feature->SetGeometry(ogr_linestring.get());
feature->SetField("id", static_cast<double>(way.id()));
feature->SetField("type", highway);
if (m_layer_linestring->CreateFeature(feature) != OGRERR_NONE) {
std::cerr << "Failed to create feature.\n";
exit(1);
}
OGRFeature::DestroyFeature(feature);
} catch (osmium::geometry_error&) {
std::cerr << "Ignoring illegal geometry for way " << way.id() << ".\n";
}
}
}
void area(const osmium::Area& area) {
const char* building = area.tags()["building"];
if (building) {
try {
std::unique_ptr<OGRMultiPolygon> ogr_polygon = m_factory.create_multipolygon(area);
OGRFeature* feature = OGRFeature::CreateFeature(m_layer_polygon->GetLayerDefn());
feature->SetGeometry(ogr_polygon.get());
feature->SetField("id", static_cast<int>(area.id()));
feature->SetField("type", building);
std::string type = "";
if (area.from_way()) {
type += "w";
} else {
type += "r";
}
feature->SetField("type", type.c_str());
if (m_layer_polygon->CreateFeature(feature) != OGRERR_NONE) {
std::cerr << "Failed to create feature.\n";
exit(1);
}
OGRFeature::DestroyFeature(feature);
} catch (osmium::geometry_error&) {
std::cerr << "Ignoring illegal geometry for area " << area.id() << " created from " << (area.from_way() ? "way" : "relation") << " with id=" << area.orig_id() << ".\n";
}
}
}
};
/* ================================================== */
void print_help() {
std::cout << "osmium_toogr [OPTIONS] [INFILE [OUTFILE]]\n\n" \
<< "If INFILE is not given stdin is assumed.\n" \
<< "If OUTFILE is not given 'ogr_out' is used.\n" \
<< "\nOptions:\n" \
<< " -h, --help This help message\n" \
<< " -d, --debug Enable debug output\n" \
<< " -f, --format=FORMAT Output OGR format (Default: 'SQLite')\n";
}
int main(int argc, char* argv[]) {
static struct option long_options[] = {
{"help", no_argument, 0, 'h'},
{"debug", no_argument, 0, 'd'},
{"format", required_argument, 0, 'f'},
{0, 0, 0, 0}
};
std::string output_format("SQLite");
bool debug = false;
while (true) {
int c = getopt_long(argc, argv, "hdf:", long_options, 0);
if (c == -1) {
break;
}
switch (c) {
case 'h':
print_help();
exit(0);
case 'd':
debug = true;
break;
case 'f':
output_format = optarg;
break;
default:
exit(1);
}
}
std::string input_filename;
std::string output_filename("ogr_out");
int remaining_args = argc - optind;
if (remaining_args > 2) {
std::cerr << "Usage: " << argv[0] << " [OPTIONS] [INFILE [OUTFILE]]" << std::endl;
exit(1);
} else if (remaining_args == 2) {
input_filename = argv[optind];
output_filename = argv[optind+1];
} else if (remaining_args == 1) {
input_filename = argv[optind];
} else {
input_filename = "-";
}
osmium::area::Assembler::config_type assembler_config;
assembler_config.enable_debug_output(debug);
osmium::area::MultipolygonCollector<osmium::area::Assembler> collector(assembler_config);
std::cerr << "Pass 1...\n";
osmium::io::Reader reader1(input_filename);
collector.read_relations(reader1);
reader1.close();
std::cerr << "Pass 1 done\n";
index_pos_type index_pos;
index_neg_type index_neg;
location_handler_type location_handler(index_pos, index_neg);
location_handler.ignore_errors();
MyOGRHandler ogr_handler(output_format, output_filename);
std::cerr << "Pass 2...\n";
osmium::io::Reader reader2(input_filename);
osmium::apply(reader2, location_handler, ogr_handler, collector.handler([&ogr_handler](const osmium::memory::Buffer& area_buffer) {
osmium::apply(area_buffer, ogr_handler);
}));
reader2.close();
std::cerr << "Pass 2 done\n";
std::vector<const osmium::Relation*> incomplete_relations = collector.get_incomplete_relations();
if (!incomplete_relations.empty()) {
std::cerr << "Warning! Some member ways missing for these multipolygon relations:";
for (const auto* relation : incomplete_relations) {
std::cerr << " " << relation->id();
}
std::cerr << "\n";
}
}