-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpp_strtree.cpp
181 lines (147 loc) · 5.17 KB
/
cpp_strtree.cpp
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/**
* # GEOS C++ example 2
*
* Using a custom object, generates multiple random
* objects and then allows searching on them.
*
* This uses the TemplateSTRtree, a relatively new API.
*
* Program fills a 100x100 grid with random "Person" objects,
* builds an index on those objects, and then queries the
* index with a query shape. The default shape is a square.
* Supply WKT on the commandline for alternate shapes.
*
* ./cpp_strtree 'POLYGON((30 30, 50 30, 50 70, 30 30))'
*/
#include <iostream>
#include <cstdlib>
/* For geometry operations */
#include <geos/geom/Coordinate.h>
#include <geos/geom/Envelope.h>
#include <geos/geom/GeometryFactory.h>
#include <geos/geom/Geometry.h>
#include <geos/geom/Point.h>
/* For indexing */
#include <geos/index/strtree/TemplateSTRtree.h>
/* For WKT read/write */
#include <geos/io/WKTReader.h>
#include <geos/io/WKTWriter.h>
/* Geometry */
using geos::geom::Coordinate;
using geos::geom::Envelope;
using geos::geom::Geometry;
using geos::geom::GeometryFactory;
using geos::geom::Point;
using geos::index::strtree::TemplateSTRtree;
/* WKTReader/WKTWriter */
using geos::io::WKTReader;
using geos::io::WKTWriter;
/*
* An example of a class encapsulating some spatial and
* non-spatial information. A Person has a name and
* a location.
*/
class Person {
public:
Person(const std::string& name, unsigned int x, unsigned int y, const GeometryFactory* gf)
: m_location(gf->createPoint(Coordinate(x, y)))
, m_name(name)
{};
// For the index to work, the class must have a
// getEnvelopeInternal() method.
const Envelope* getEnvelopeInternal() const
{
return m_location->getEnvelopeInternal();
}
const std::string& getName() const {
return m_name;
}
const std::string getWkt() const {
WKTWriter writer;
writer.setTrim(true);
return writer.write(m_location.get());
}
const bool intersects(const Geometry* geom) const {
return m_location->intersects(geom);
}
private:
std::unique_ptr<Point> m_location;
std::string m_name;
};
// For fun we build up the Person name automatically.
static std::string
number_to_name(unsigned int num)
{
std::vector<std::string> first = {"John", "Paul", "Peter", "Matthew", "James", "Mary", "Ruth", "Eliza", "Margaret", "Enid"};
std::vector<std::string> last = {"Smith", "John", "Ng", "Wong", "Kim", "Singh", "Ono", "Woo", "Cage", "Chandra"};
unsigned int onenum = num % 10;
unsigned int tennum = num / 10;
std::string word = first[tennum] + " " + last[onenum];
return word;
}
int
main(int argc, char **argv)
{
// New factory with default (float) precision model
GeometryFactory::Ptr factory = GeometryFactory::create();
// A place to store our Person objects
std::vector<std::unique_ptr<Person>> people;
// STRtree index using Person* as the indexed item
TemplateSTRtree<Person*> index;
// Populate the index with Persons
for (unsigned int i = 0; i < 100; i++) {
// Calculate the name and location of this Person
unsigned int x = std::rand() % 100;
unsigned int y = std::rand() % 100;
std::string name = number_to_name(i);
// Create the object, store it, and add to the index
Person *person = new Person(name, x, y, factory.get());
people.emplace_back(person);
index.insert(person);
// Log what we did
std::cerr << " LOG: Inserted " << person->getName() << " at " << x << "," << y << std::endl;
}
// Query shape
std::unique_ptr<Geometry> query(nullptr);
// Read query WKT string, if provided on the commandline.
WKTReader reader(*factory);
std::string wkt("POLYGON((10 10, 10 35, 35 35, 35 10, 10 10))");
if (argc == 2) {
wkt = std::string(argv[1]);
}
try {
query = reader.read(wkt);
}
catch (std::exception& e) {
std::cerr << "ERROR: Unable to parse WKT: " << wkt << std::endl;
return 1;
}
// Log
std::cerr << " LOG: Querying index with " << wkt << std::endl;
// Place to store query result
std::vector<const Person*> query_result;
// Lambda for the STRtree index search.
// The visited index entry is returned as a parameter.
// Since our entry type is Person*, so is this parameter type.
// We capture the query and query_result so we can use the query
// shape in the exact test for intersection, and push the
// return results into the query_result vector.
auto visitor = [&query, &query_result](const Person* person) {
if (person->intersects(query.get())) {
query_result.push_back(person);
}
};
// Actually run the index query. STRtree query takes an
// Envelope as the query key.
const Envelope* query_env = query->getEnvelopeInternal();
index.query(*query_env, visitor);
// Read back the results
for (auto* person: query_result) {
const std::string& person_name = person->getName();
std::string person_wkt = person->getWkt();
std::cerr << " LOG: Found entry '" << person_name << "' at " << person_wkt << std::endl;
}
// That's it!
std::cerr << " DONE: Done" << std::endl;
return 0;
}