GEOS 3.14.0dev
HilbertEncoder.h
1/**********************************************************************
2 *
3 * GEOS - Geometry Engine Open Source
4 * http://geos.osgeo.org
5 *
6 * Copyright (C) 2020 Paul Ramsey <pramsey@cleverelephant.ca>
7 *
8 * This is free software; you can redistribute and/or modify it under
9 * the terms of the GNU Lesser General Public Licence as published
10 * by the Free Software Foundation.
11 * See the COPYING file for more information.
12 *
13 **********************************************************************/
14
15
16#pragma once
17
18#include <geos/export.h>
19#include <geos/geom/Geometry.h>
20#include <string>
21#include <vector>
22#include <cstdint>
23
24// Forward declarations
25namespace geos {
26namespace geom {
27class Coordinate;
28}
29}
30
31namespace geos {
32namespace shape { // geos.shape
33namespace fractal { // geos.shape.fractal
34
35
36class GEOS_DLL HilbertEncoder {
37
38public:
39
40 HilbertEncoder(uint32_t p_level, geom::Envelope& extent);
41 uint32_t encode(const geom::Envelope* env);
42 static void sort(std::vector<geom::Geometry*>& geoms);
43
44 template<typename T>
45 static geom::Envelope getEnvelope(T begin, T end) {
46 geom::Envelope extent;
47 for (auto it = begin; it != end; ++it) {
48 const auto* g = *it;
49 if (extent.isNull())
50 extent = *(g->getEnvelopeInternal());
51 else
52 extent.expandToInclude(*(g->getEnvelopeInternal()));
53 }
54
55 return extent;
56 }
57
58 template<typename T>
59 static void sort(T begin, T end) {
60 auto extent = getEnvelope(begin, end);
61 HilbertEncoder encoder(12, extent);
62 HilbertComparator hilbertCompare(encoder);
63 std::sort(begin, end, hilbertCompare);
64 }
65
66private:
67
68 uint32_t level;
69 double minx;
70 double miny;
71 double strideX;
72 double strideY;
73
74 struct HilbertComparator {
75
76 HilbertEncoder& enc;
77
78 HilbertComparator(HilbertEncoder& e)
79 : enc(e) {};
80
81 bool
82 operator()(const geom::Geometry* a, const geom::Geometry* b)
83 {
84 return enc.encode(a->getEnvelopeInternal()) > enc.encode(b->getEnvelopeInternal());
85 }
86 };
87
88};
89
90
91} // namespace geos.shape.fractal
92} // namespace geos.shape
93} // namespace geos
94
95
96
Basic namespace for all GEOS functionalities.
Definition geos.h:39