示例#1
0
    /***
     * Union linestrings to multilinestring and insert them into table
     * relations.
     */
    void create_relation(const osmium::Relation &relation,
                         const osmium::object_id_type relation_id,
                         bool &contains_nowaterway_ways,
                         vector<geos::geom::Geometry *> *linestrings) {

        if (!(linestrings->size())) {
            return;
        }
        const geos::geom::GeometryFactory geom_factory =
                geos::geom::GeometryFactory();
        geos::geom::GeometryCollection *geom_collection = nullptr;
        try {
            geom_collection = geom_factory.createGeometryCollection(
                              linestrings);
        } catch (...) {
            cerr << "Failed to create geometry collection at relation: "
                 << relation_id << endl;
            delete linestrings;
            return;
        }
        geos::geom::Geometry *geos_geom = nullptr;
        try {
            geos_geom = geom_collection->Union().release();
        } catch (...) {
            cerr << "Failed to union linestrings at relation: "
                 << relation_id << endl;
            delete geom_collection;
            return;
        }
        OGRGeometry *ogr_multilinestring = nullptr;
        ogr_multilinestring = geos2ogr(geos_geom);
        if (!strcmp(ogr_multilinestring->getGeometryName(),"LINESTRING")) {
            try {
                ogr_multilinestring =
                        OGRGeometryFactory::forceToMultiLineString(
                        ogr_multilinestring);
            } catch (...) {
                delete geom_collection;
                delete geos_geom;
                return;
            }
        }
        try {
            ds.insert_relation_feature(ogr_multilinestring, relation,
                                       contains_nowaterway_ways);
        } catch (osmium::geometry_error&) {
            OGRGeometryFactory::destroyGeometry(ogr_multilinestring);
            cerr << "Inserting to table failed for relation: "
                 << relation_id << endl;
        } catch (...) {
            OGRGeometryFactory::destroyGeometry(ogr_multilinestring);
            cerr << "Inserting to table failed for relation: "
                 << relation_id << endl;
            cerr << "  Unexpected error" << endl;
        }
        delete geom_collection;
        delete geos_geom;
        OGRGeometryFactory::destroyGeometry(ogr_multilinestring);
    }