File: IntersectionPerfTest.cpp

package info (click to toggle)
geos 3.14.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 31,212 kB
  • sloc: cpp: 199,103; xml: 56,065; ansic: 6,162; sh: 287; makefile: 26
file content (166 lines) | stat: -rw-r--r-- 5,160 bytes parent folder | download | duplicates (2)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**********************************************************************
 *
 * GEOS - Geometry Engine Open Source
 * http://geos.osgeo.org
 *
 * Copyright (C) 2019 Daniel Baston <dbaston@gmail.com>
 *
 * This is free software; you can redistribute and/or modify it under
 * the terms of the GNU Lesser General Public Licence as published
 * by the Free Software Foundation.
 * See the COPYING file for more information.
 *
 **********************************************************************/

#include <geos/profiler.h>
#include <geos_c.h>

#include <fstream>
#include <iostream>
#include <sstream>
#include <deque>

using BinaryPredicate = decltype(&GEOSIntersects);
using BinaryOperation = decltype(&GEOSIntersection);

int main(int argc, char** argv) {
    if (argc < 2 || argc > 5) {
        std::cout << "perf_intersection reads geometries from a WKT file and" << std::endl;
        std::cout << "inserts them into an STR-tree. For each input geometry, it" << std::endl;
        std::cout << "queries the tree to find all intersecting geometries and" << std::endl;
        std::cout << "then computes their intersection." << std::endl;
        std::cout << std::endl;
        std::cout << "Usage: perf_intersection [wktfile] [n] [pred] [op]" << std::endl;
        return 0;
    }

    initGEOS(nullptr, nullptr);

    std::string fname{argv[1]};

    long max_geoms;
    if (argc >= 3 && std::string(argv[2]) != "all") {
        max_geoms = std::atol(argv[2]);
        std::cout << "Reading up to " << max_geoms << " geometries from " << fname << std::endl;
    } else {
        std::cout << "Reading geometries from " << fname << std::endl;
        max_geoms = std::numeric_limits<decltype(max_geoms)>::max();
    }

    std::string pred = "intersects";
    if (argc >= 4) {
        pred = argv[3];
    }

    BinaryPredicate predfn = GEOSIntersects;
    if (pred == "intersects") {
        predfn = GEOSIntersects;
    } else if (pred == "contains") {
        predfn = GEOSContains;
    } else if (pred == "covers") {
        predfn = GEOSCovers;
    } else if (pred == "within") {
        predfn = GEOSWithin;
    } else if (pred == "coveredby") {
        predfn = GEOSCoveredBy;
    } else if (pred == "touches") {
        predfn = GEOSTouches;
    } else if (pred == "overlaps") {
        predfn = GEOSOverlaps;
    } else if (pred == "crosses") {
        predfn = GEOSCrosses;
    } else if (pred == "equals") {
        predfn = GEOSEquals;
    } else {
        std::cerr << "Unknown predicate." << std::endl;
        return 1;
    }

    std::string overlay = "intersection";
    if (argc >= 5) {
        overlay = argv[4];
    }
    BinaryOperation overlayfn = GEOSIntersection;
    if (overlay == "intersection") {
        overlayfn = GEOSIntersection;
    } else if (overlay == "none") {
        overlayfn = nullptr;
    }

    std::vector<GEOSGeometry*> geoms;

    std::ifstream f(fname);
    std::string line;
    long i = 0;
    std::deque<long> lineNos;
    while(std::getline(f, line) && i < max_geoms) {
        auto geom = GEOSGeomFromWKT(line.c_str());
        if (geom) {
            i++;
            lineNos.push_back(i);
            GEOSGeom_setUserData(geom, &lineNos.back());
            geoms.push_back(geom);
        }
    }
    f.close();

    std::cout << "Read " << geoms.size() << " geometries." << std::endl;
    std::cout << "Testing according to predicate: " << pred << " and performing operation: " << overlay << std::endl;

    GEOSSTRtree* tree = GEOSSTRtree_create(10);

    for (const auto& g : geoms) {
        GEOSSTRtree_insert(tree, g, g);
    }

    geos::util::Profile sw("Intersection");
    sw.start();

    struct QueryContext {
        GEOSGeometry* queryGeom;
        BinaryPredicate pred;
        BinaryOperation overlay;
        std::size_t* treeHits;
        std::size_t* predHits;
    };

    std::size_t treeHits = 0;
    std::size_t predHits = 0;


    for (const auto& g : geoms) {
        QueryContext ctxt{g, predfn, overlayfn, &treeHits, &predHits};

        GEOSSTRtree_query(tree, g, [](void* item, void* data) {
            auto context = static_cast<QueryContext*>(data);
            ++*context->treeHits;

            GEOSGeometry* g1 = (GEOSGeometry*) context->queryGeom;
            GEOSGeometry* g2 = (GEOSGeometry*) item;

#if 0
            std::cout << "Eval pred btwn geom " << *static_cast<long*>(GEOSGeom_getUserData(g1)) << " and " << *static_cast<long*>(GEOSGeom_getUserData(g2)) << std::endl;
#endif

            if ((context->pred)(g1, g2) == 1) {
                ++*context->predHits;

                if (context->overlay) {
                    GEOSGeometry* g3 = (context->overlay)(g1, g2);
                    GEOSGeom_destroy(g3);
                }
            }
        }, &ctxt);
    }

    sw.stop();
    std::cerr << predHits << " out of " << treeHits << " bounding box intersections satisfied predicate: " << pred << " (" << 100 * static_cast<double>(predHits) / static_cast<double>(treeHits) << "%)" << std::endl;
    std::cout << sw.getTotFormatted() << std::endl;

    GEOSSTRtree_destroy(tree);

    for (auto& g : geoms) {
        GEOSGeom_destroy(g);
    }
}