File: GEOSClusterTest.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 (148 lines) | stat: -rw-r--r-- 5,311 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
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
//
// Test Suite for C-API GEOSCluster*

#include <tut/tut.hpp>
// geos
#include <geos_c.h>

#include "capi_test_utils.h"

namespace tut {
//
// Test Group
//

// Common data used in test cases.
struct test_capicluster_data : public capitest::utility {
    static GEOSGeometry* construct_clusters(const GEOSGeometry* input,
                                            const GEOSClusterInfo* clusters)
    {
        auto ngeoms = static_cast<std::size_t>(GEOSGetNumGeometries(input));
        auto numClusters = GEOSClusterInfo_getNumClusters(clusters);

        std::vector<GEOSGeometry*> cluster_components(ngeoms);
        std::vector<GEOSGeometry*> cluster_geoms(numClusters);

        for (std::size_t cluster_id = 0; cluster_id < numClusters; cluster_id++) {
            auto sz = GEOSClusterInfo_getClusterSize(clusters, cluster_id);
            const std::size_t* indices = GEOSClusterInfo_getInputsForClusterN(clusters, cluster_id);
            for (std::size_t i = 0; i < sz; i++) {
                cluster_components[i] = GEOSGeom_clone(GEOSGetGeometryN(input, static_cast<int>(indices[i])));
            }
            cluster_geoms[cluster_id] = GEOSGeom_createCollection(GEOS_GEOMETRYCOLLECTION,
                                                                  cluster_components.data(),
                                                                  static_cast<unsigned>(sz));
        }

        // combine the clusters into a single nested GeometryCollection
        return GEOSGeom_createCollection(GEOS_GEOMETRYCOLLECTION,
                                         cluster_geoms.data(),
                                         static_cast<unsigned>(cluster_geoms.size()));
    }
};

typedef test_group<test_capicluster_data> group;
typedef group::object object;

group test_capicluster_group("capi::GEOSCluster");

//
// Test Cases
//
template<>
template<> void object::test<1>
()
{
    input_ = fromWKT(
                 "GEOMETRYCOLLECTION ("
                 "POINT (0 1),"
                 "LINESTRING (0 0, 0 0.1),"
                 "LINESTRING (0 0, 1.0 1.0),"
                 "POINT (0.9 1.0),"
                 "POINT (0 7))");

    {
        GEOSClusterInfo* clusters = GEOSClusterEnvelopeIntersects(input_);
        ensure_equals("two clusters by envelope intersection", GEOSClusterInfo_getNumClusters(clusters), 2u);

        GEOSGeometry* geom_result = construct_clusters(input_, clusters);
        GEOSGeometry* geom_expected = fromWKT("GEOMETRYCOLLECTION ("
                                              "  GEOMETRYCOLLECTION ("
                                              "    POINT (0 1),"
                                              "    LINESTRING (0 0, 0 0.1),"
                                              "    LINESTRING (0 0, 1.0 1.0),"
                                              "    POINT (0.9 1.0)),"
                                              "  GEOMETRYCOLLECTION ("
                                              "    POINT (0 7)))");

        ensure_geometry_equals_identical(geom_expected, geom_result);
        GEOSGeom_destroy(geom_result);
        GEOSGeom_destroy(geom_expected);

        GEOSClusterInfo_destroy(clusters);
    }

    {
        GEOSClusterInfo* clusters = GEOSClusterEnvelopeDistance(input_, 6);
        ensure_equals("one cluster by envelope distance", GEOSClusterInfo_getNumClusters(clusters), 1u);
        GEOSClusterInfo_destroy(clusters);
    }

    {
        GEOSClusterInfo* clusters = GEOSClusterGeometryIntersects(input_);
        ensure_equals("four clusters by geometry intersection", GEOSClusterInfo_getNumClusters(clusters), 4u);
        GEOSClusterInfo_destroy(clusters);
    }

    {
        GEOSClusterInfo* clusters = GEOSClusterGeometryDistance(input_, 0.2);
        ensure_equals("three clusters by distance", GEOSClusterInfo_getNumClusters(clusters), 3u);
        GEOSClusterInfo_destroy(clusters);
    }
}

template<>
template<>
void object::test<2>()
{
    input_ = fromWKT(
                 "GEOMETRYCOLLECTION ("
                 "POINT (0 0),"
                 "POINT (-1 0),"
                 "POINT (-1 -0.1),"
                 "POINT (-1 0.1),"
                 "POINT (1 0),"
                 "POINT (2 0),"
                 "POINT (3  0),"
                 "POINT ( 3 -0.1),"
                 "POINT ( 3 0.1)"
                 ")");

    {
        GEOSClusterInfo* clusters = GEOSClusterDBSCAN(input_, 1.01, 5);
        ensure_equals("two clusters with minPoints = 5", GEOSClusterInfo_getNumClusters(clusters), 2u);
        GEOSClusterInfo_destroy(clusters);
    }

    {
        GEOSClusterInfo* clusters = GEOSClusterDBSCAN(input_, 1.01, 2);
        ensure_equals("one cluster with minPoints = 2", GEOSClusterInfo_getNumClusters(clusters), 1u);
        GEOSClusterInfo_destroy(clusters);
    }

    {
        GEOSClusterInfo* clusters = GEOSClusterDBSCAN(input_, 1.01, 20);
        ensure_equals("no clusters with minPoints = 20", GEOSClusterInfo_getNumClusters(clusters), 0u);

        auto* cluster_ids = GEOSClusterInfo_getClustersForInputs(clusters);
        for (int i = 0; i < GEOSGetNumGeometries(input_); i++) {
            ensure_equals(cluster_ids[i], GEOS_CLUSTER_NONE);
        }
        GEOSClusterInfo_destroy(clusters);
        GEOSFree(cluster_ids);
    }
}


} // namespace tut