File: test-output-pgsql-area.cpp

package info (click to toggle)
osm2pgsql 0.96.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,304 kB
  • sloc: cpp: 11,462; python: 543; sh: 98; makefile: 17
file content (106 lines) | stat: -rw-r--r-- 2,841 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/**

Test the area reprojection functionality of osm2pgsql 

The idea behind that functionality is to populate the way_area
column with the area that a polygoun would have in EPSG:3857, 
rather than the area it actually has in the coordinate system
used for importing.

*/
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <cassert>
#include <sstream>
#include <stdexcept>
#include <memory>

#include "osmtypes.hpp"
#include "osmdata.hpp"
#include "middle.hpp"
#include "output-pgsql.hpp"
#include "options.hpp"
#include "middle-pgsql.hpp"
#include "middle-ram.hpp"
#include "taginfo_impl.hpp"

#include <sys/types.h>
#include <unistd.h>

#include <boost/lexical_cast.hpp>

#include "tests/common-pg.hpp"
#include "tests/common.hpp"

void run_test(const char* test_name, void (*testfunc)()) {
    try {
        fprintf(stderr, "%s\n", test_name);
        testfunc();

    } catch (const std::exception& e) {
        fprintf(stderr, "%s\n", e.what());
        fprintf(stderr, "FAIL\n");
        exit(EXIT_FAILURE);
    }

    fprintf(stderr, "PASS\n");
}
#define RUN_TEST(x) run_test(#x, &(x))


void test_area_base(bool latlon, bool reproj, double expect_area_poly, double expect_area_multi) {
    std::unique_ptr<pg::tempdb> db;

    try {
        db.reset(new pg::tempdb);
    } catch (const std::exception &e) {
        std::cerr << "Unable to setup database: " << e.what() << "\n";
        exit(77);
    }

    std::shared_ptr<middle_pgsql_t> mid_pgsql(new middle_pgsql_t());
    options_t options;
    options.database_options = db->database_options;
    options.num_procs = 1;
    options.style = "default.style";
    options.prefix = "osm2pgsql_test";
    if (latlon) {
        options.projection.reset(reprojection::create_projection(PROJ_LATLONG));
    }
    if (reproj) {
        options.reproject_area = true;
    }

    auto out_test = std::make_shared<output_pgsql_t>(mid_pgsql.get(), options);

    osmdata_t osmdata(mid_pgsql, out_test, options.projection);
    testing::parse("tests/test_output_pgsql_area.osm", "xml",
                   options, &osmdata);

    db->check_count(2, "SELECT COUNT(*) FROM osm2pgsql_test_polygon");
    db->check_number(expect_area_poly, "SELECT way_area FROM osm2pgsql_test_polygon WHERE name='poly'");
    db->check_number(expect_area_multi, "SELECT way_area FROM osm2pgsql_test_polygon WHERE name='multi'");

    return;
}

void test_area_classic() {
    test_area_base(false, false, 1.23927e+10, 9.91828e+10);
}

void test_area_latlon() {
    test_area_base(true, false, 1, 8);
}

void test_area_latlon_with_reprojection() {
    test_area_base(true, true, 1.23927e+10, 9.91828e+10);
}

int main(int argc, char *argv[]) {
    RUN_TEST(test_area_latlon);
    RUN_TEST(test_area_classic);
    RUN_TEST(test_area_latlon_with_reprojection);
    return 0;
}