Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spatial join area #1713

Open
wants to merge 50 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
c7a1ea8
untested changes
Jonathan24680 Dec 17, 2024
a266201
continued development
Jonathan24680 Dec 18, 2024
2cc8504
added all areas to the dataset and some tests
Jonathan24680 Dec 23, 2024
d784513
make file_buffer_size in the test index be a parameter
Jonathan24680 Dec 28, 2024
f6e1647
bugfix area dataset
Jonathan24680 Dec 28, 2024
1a4ff8f
backup commit
Jonathan24680 Dec 30, 2024
e3a2644
area test with points and areas working
Jonathan24680 Dec 31, 2024
f38cb1d
moved some stuff from dev space into the project
Jonathan24680 Jan 2, 2025
4d71c4e
new tests
Jonathan24680 Jan 4, 2025
38c9585
first test works now for the areas as well
Jonathan24680 Jan 11, 2025
ddea6d3
all tests are now executed and passed
Jonathan24680 Jan 12, 2025
1902d44
clang format
Jonathan24680 Jan 12, 2025
31e7054
merge master
Jonathan24680 Jan 12, 2025
cba4324
backup with removed filebuffersize compiling but not testing
Jonathan24680 Jan 17, 2025
d5c934c
make buffer size of test index variable
Jonathan24680 Jan 17, 2025
c1dc963
clean up code
Jonathan24680 Jan 17, 2025
c5284ef
sonarqube
Jonathan24680 Jan 20, 2025
fc1a1fe
correct bug for function without inline in header file
Jonathan24680 Jan 21, 2025
ec75e89
arbitrary geometry types and areas dont need to be approximated as po…
Jonathan24680 Jan 25, 2025
4fd698c
PR feedback
Jonathan24680 Jan 31, 2025
173e646
PR feedback
Jonathan24680 Jan 31, 2025
7dd8156
backup
Jonathan24680 Feb 1, 2025
1c60bf9
PR feedback
Jonathan24680 Feb 1, 2025
3b8ea1a
spelling mistake
Jonathan24680 Feb 1, 2025
a121ef2
Sonarqube issues
Jonathan24680 Feb 4, 2025
1168f32
PR changes
Jonathan24680 Feb 4, 2025
ca7f08e
PR changes
Jonathan24680 Feb 4, 2025
9d2956e
PR changes
Jonathan24680 Feb 4, 2025
14032bd
PR changes
Jonathan24680 Feb 4, 2025
29171b5
Sonarqube issues
Jonathan24680 Feb 4, 2025
457f8d4
format check
Jonathan24680 Feb 4, 2025
bf886c4
backup
Jonathan24680 Feb 7, 2025
10bdf7b
strange bug
Jonathan24680 Feb 7, 2025
5f12f22
solved strange bug
Jonathan24680 Feb 7, 2025
978b512
PR feedback
Jonathan24680 Feb 7, 2025
4132701
Sonarqube issue
Jonathan24680 Feb 7, 2025
d711586
Merge branch 'master' into SpatialJoinArea
Jonathan24680 Feb 7, 2025
f3c5b01
consistent naming of QueryBox and BoundingBox
Jonathan24680 Feb 8, 2025
a46fccd
add test for distance between GeoPoints and Areas
Jonathan24680 Feb 8, 2025
2cc0fef
improve true area distance calculation
Jonathan24680 Feb 8, 2025
96a903a
backup
Jonathan24680 Feb 10, 2025
47ba454
backup
Jonathan24680 Feb 10, 2025
61c6b98
PR changes
Jonathan24680 Feb 10, 2025
c0bf282
clang format
Jonathan24680 Feb 10, 2025
988ba8d
Sonarqube issus and bugfix
Jonathan24680 Feb 12, 2025
e42d9d0
format check
Jonathan24680 Feb 12, 2025
5b88f47
Merge branch 'master' into SpatialJoinArea
Jonathan24680 Feb 14, 2025
5a99080
Sonarqube issue remove unused operator
Jonathan24680 Feb 14, 2025
a7f4695
PR feedback
Jonathan24680 Feb 15, 2025
e5121f6
suggested parentheses around and condition
Jonathan24680 Feb 15, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/engine/SpatialJoin.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ class SpatialJoin : public Operation {
return childRight_;
}

PreparedSpatialJoinParams onlyForTestingGetPrepareJoin() const {
return prepareJoin();
}

private:
// helper function to generate a variable to column map from `childRight_`
// that only contains the columns selected by `config_.payloadVariables_`
Expand Down
273 changes: 213 additions & 60 deletions src/engine/SpatialJoinAlgorithms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
#include <s2/util/units/length-units.h>

#include <cmath>
#include <set>

#include "engine/ExportQueryExecutionTrees.h"
#include "engine/SpatialJoin.h"
#include "util/GeoSparqlHelpers.h"

Expand All @@ -38,18 +40,100 @@
};

// ____________________________________________________________________________
Id SpatialJoinAlgorithms::computeDist(const IdTable* idTableLeft,
const IdTable* idTableRight,
size_t rowLeft, size_t rowRight,
ColumnIndex leftPointCol,
ColumnIndex rightPointCol) const {
auto point1 = getPoint(idTableLeft, rowLeft, leftPointCol);
auto point2 = getPoint(idTableRight, rowRight, rightPointCol);
if (!point1.has_value() || !point2.has_value()) {
return Id::makeUndefined();
}
return Id::makeFromDouble(
ad_utility::detail::wktDistImpl(point1.value(), point2.value()));
std::string_view SpatialJoinAlgorithms::betweenQuotes(
std::string_view extractFrom) const {
size_t pos1 = extractFrom.find("\"", 0);
size_t pos2 = extractFrom.find("\"", pos1 + 1);
if (pos1 != std::string::npos && pos2 != std::string::npos) {
return extractFrom.substr(pos1 + 1, pos2 - pos1 - 1);
} else {
return extractFrom;
}

Check warning on line 51 in src/engine/SpatialJoinAlgorithms.cpp

View check run for this annotation

Codecov / codecov/patch

src/engine/SpatialJoinAlgorithms.cpp#L50-L51

Added lines #L50 - L51 were not covered by tests
}

std::optional<size_t> SpatialJoinAlgorithms::getAnyGeometry(
const IdTable* idtable, size_t row, size_t col) {
auto printWarning = [this, &spatialJoin = spatialJoin_]() {
if (this->numFailedParsedGeometries_ == 0) {
std::string warning =
"The input to a spatial join contained at least one element, "
"that is not a Point, Linestring, Polygon, MultiPoint, "
"MultiLinestring or MultiPolygon geometry and is thus skipped. Note "
"that QLever currently only accepts those geometries for "
Jonathan24680 marked this conversation as resolved.
Show resolved Hide resolved
"the spatial joins";
AD_LOG_WARN << warning << std::endl;
this->numFailedParsedGeometries_ += 1;
if (spatialJoin.has_value()) {
AD_CORRECTNESS_CHECK(spatialJoin.value() != nullptr);
spatialJoin.value()->addWarning(warning);
}
}
};

// unfortunately, the current implementation requires the fully materialized
// string. In the future this might get changed. When only the bounding box
// is needed, one could store it in an ID similar to GeoPoint (but with less
// precision), and then the full geometry would only need to be read, when
// the exact distance is wanted
std::string str(betweenQuotes(ExportQueryExecutionTrees::idToStringAndType(
Jonathan24680 marked this conversation as resolved.
Show resolved Hide resolved
qec_->getIndex(), idtable->at(row, col), {})
.value()
.first));
AnyGeometry geometry;
try {
bg::read_wkt(str, geometry);
geometries_.push_back(std::move(geometry));
} catch (...) {
printWarning();
return std::nullopt;
}
return geometries_.size() - 1; // index of the last element
}

// ____________________________________________________________________________
double SpatialJoinAlgorithms::computeDist(const size_t geometryIndex1,
const size_t geometryIndex2) const {
return boost::apply_visitor(ClosestPointVisitor(),
geometries_.at(geometryIndex1),
geometries_.at(geometryIndex2));
};

// ____________________________________________________________________________
size_t SpatialJoinAlgorithms::convertGeoPointToPoint(GeoPoint point) {
geometries_.emplace_back(Point(point.getLng(), point.getLat()));
return geometries_.size() - 1; // index of the last element
};

// ____________________________________________________________________________
Id SpatialJoinAlgorithms::computeDist(RtreeEntry& geo1, RtreeEntry& geo2) {
auto convertPoint = [&](RtreeEntry& entry) {
if (entry.geoPoint_) {
return entry.geoPoint_.value();
}
if (!entry.boundingBox_.has_value()) {
entry.boundingBox_ = boost::apply_visitor(
BoundingBoxVisitor(), geometries_.at(entry.geometryIndex_.value()));
}

Check warning on line 116 in src/engine/SpatialJoinAlgorithms.cpp

View check run for this annotation

Codecov / codecov/patch

src/engine/SpatialJoinAlgorithms.cpp#L114-L116

Added lines #L114 - L116 were not covered by tests
Point p = calculateMidpointOfBox(entry.boundingBox_.value());
return GeoPoint(p.get<1>(), p.get<0>());
};

auto getIndex = [&](RtreeEntry& entry) {
if (!entry.geometryIndex_) {
entry.geometryIndex_ = convertGeoPointToPoint(entry.geoPoint_.value());
}
return entry.geometryIndex_.value();
};

// use the already parsed geometries to calculate the distance
if (useMidpointForAreas_ ||
(geo1.geoPoint_.has_value() && geo2.geoPoint_.has_value())) {
return Id::makeFromDouble(ad_utility::detail::wktDistImpl(
convertPoint(geo1), convertPoint(geo2)));
} else {
// at least one area
return Id::makeFromDouble(computeDist(getIndex(geo1), getIndex(geo2)));
}
}

// ____________________________________________________________________________
Expand Down Expand Up @@ -118,10 +202,17 @@
decltype(compare)>
intermediate(compare);

auto entryLeft = getRtreeEntry(idTableLeft, rowLeft, leftJoinCol);

// Inner loop of cartesian product
for (size_t rowRight = 0; rowRight < idTableRight->size(); rowRight++) {
Id dist = computeDist(idTableLeft, idTableRight, rowLeft, rowRight,
leftJoinCol, rightJoinCol);
auto entryRight = getRtreeEntry(idTableRight, rowRight, rightJoinCol);

if (!entryLeft || !entryRight) {
continue;
}

Id dist = computeDist(entryLeft.value(), entryRight.value());

// Ensure `maxDist_` constraint
if (dist.getDatatype() != Datatype::Double ||
Expand Down Expand Up @@ -240,8 +331,8 @@
}

// ____________________________________________________________________________
std::vector<Box> SpatialJoinAlgorithms::computeBoundingBox(
const Point& startPoint) const {
std::vector<Box> SpatialJoinAlgorithms::computeQueryBox(
const Point& startPoint, double additionalDist) const {
const auto [idTableLeft, resultLeft, idTableRight, resultRight, leftJoinCol,
rightJoinCol, rightSelectedCols, numColumns, maxDist,
maxResults] = params_;
Expand All @@ -254,13 +345,14 @@
auto archaversine = [](double theta) { return std::acos(1 - 2 * theta); };

// safety buffer for numerical inaccuracies
double maxDistInMetersBuffer;
if (maxDist.value() < 10) {
double maxDistInMetersBuffer =
static_cast<double>(maxDist.value()) + additionalDist;
if (maxDistInMetersBuffer < 10) {
maxDistInMetersBuffer = 10;
} else if (static_cast<double>(maxDist.value()) <
static_cast<double>(std::numeric_limits<long long>::max()) /
1.02) {
maxDistInMetersBuffer = 1.01 * static_cast<double>(maxDist.value());
maxDistInMetersBuffer = 1.01 * maxDistInMetersBuffer;
} else {
maxDistInMetersBuffer =
static_cast<double>(std::numeric_limits<long long>::max());
Expand All @@ -270,7 +362,7 @@
// a single bounding box for the whole planet, do an optimized version
if (static_cast<double>(maxDist.value()) > circumferenceMax_ / 4.0 &&
static_cast<double>(maxDist.value()) < circumferenceMax_ / 2.01) {
return computeBoundingBoxForLargeDistances(startPoint);
return computeQueryBoxForLargeDistances(startPoint);
}

// compute latitude bound
Expand Down Expand Up @@ -323,7 +415,7 @@
}

// ____________________________________________________________________________
std::vector<Box> SpatialJoinAlgorithms::computeBoundingBoxForLargeDistances(
std::vector<Box> SpatialJoinAlgorithms::computeQueryBoxForLargeDistances(
const Point& startPoint) const {
const auto [idTableLeft, resultLeft, idTableRight, resultRight, leftJoinCol,
rightJoinCol, rightSelectedCols, numColumns, maxDist,
Expand Down Expand Up @@ -442,22 +534,74 @@
return std::array{northPoleReached, southPoleReached};
}

// ____________________________________________________________________________
Point SpatialJoinAlgorithms::calculateMidpointOfBox(const Box& box) const {
double lng = (box.min_corner().get<0>() + box.max_corner().get<0>()) / 2.0;
double lat = (box.min_corner().get<1>() + box.max_corner().get<1>()) / 2.0;
return Point(lng, lat);
}

// ____________________________________________________________________________
double SpatialJoinAlgorithms::getMaxDistFromMidpointToAnyPointInsideTheBox(
const Box& box, std::optional<Point> midpoint) const {
if (!midpoint) {
midpoint = calculateMidpointOfBox(box);
}
double distLng =
std::abs(box.min_corner().get<0>() - midpoint.value().get<0>());
double distLat =
std::abs(box.min_corner().get<1>() - midpoint.value().get<1>());
// convert to meters and return
return (distLng + distLat) * 40075000 / 360;
}

// ____________________________________________________________________________
std::optional<RtreeEntry> SpatialJoinAlgorithms::getRtreeEntry(
const IdTable* idTable, const size_t row, const ColumnIndex col) {
RtreeEntry entry{row, std::nullopt, std::nullopt, std::nullopt};
entry.geoPoint_ = getPoint(idTable, row, col);

if (!entry.geoPoint_) {
entry.geometryIndex_ = getAnyGeometry(idTable, row, col);
if (!entry.geometryIndex_) {
return std::nullopt;
}
entry.boundingBox_ = boost::apply_visitor(
BoundingBoxVisitor(), geometries_.at(entry.geometryIndex_.value()));
} else {
entry.boundingBox_ =
Box(Point(entry.geoPoint_.value().getLng(),
entry.geoPoint_.value().getLat()),
Point(entry.geoPoint_.value().getLng() + 0.00000001,
entry.geoPoint_.value().getLat() + 0.00000001));
}
return entry;
}

// ____________________________________________________________________________
std::vector<Box> SpatialJoinAlgorithms::getQueryBox(
const std::optional<RtreeEntry>& entry) const {
if (!entry.value().geoPoint_) {
auto midpoint = calculateMidpointOfBox(entry.value().boundingBox_.value());
return computeQueryBox(midpoint,
getMaxDistFromMidpointToAnyPointInsideTheBox(
entry.value().boundingBox_.value(), midpoint));
} else {
return computeQueryBox(Point(entry.value().geoPoint_.value().getLng(),
entry.value().geoPoint_.value().getLat()));
}
}

// ____________________________________________________________________________
Result SpatialJoinAlgorithms::BoundingBoxAlgorithm() {
auto printWarning = [alreadyWarned = false,
&spatialJoin = spatialJoin_]() mutable {
if (!alreadyWarned) {
std::string warning =
"The input to a spatial join contained at least one element, "
"that is not a point geometry and is thus skipped. Note that "
"QLever currently only accepts point geometries for the "
"spatial joins";
AD_LOG_WARN << warning << std::endl;
alreadyWarned = true;
if (spatialJoin.has_value()) {
AD_CORRECTNESS_CHECK(spatialJoin.value() != nullptr);
spatialJoin.value()->addWarning(warning);
}
// helper struct to avoid duplicate entries for areas
struct AddedPair {
size_t rowLeft_;
size_t rowRight_;

auto operator<=>(const AddedPair& other) const {
return (rowLeft_ == other.rowLeft_) ? (rowRight_ <=> other.rowRight_)
: (rowLeft_ <=> other.rowLeft_);
}
};

Expand All @@ -478,56 +622,65 @@
std::swap(smallerResJoinCol, otherResJoinCol);
}

// build rtree with one child
bgi::rtree<Value, bgi::quadratic<16>, bgi::indexable<Value>,
bgi::equal_to<Value>, ad_utility::AllocatorWithLimit<Value>>
rtree(bgi::quadratic<16>{}, bgi::indexable<Value>{},
bgi::equal_to<Value>{}, qec_->getAllocator());
for (size_t i = 0; i < smallerResult->numRows(); i++) {
// get point of row i
auto geopoint = getPoint(smallerResult, i, smallerResJoinCol);

if (!geopoint) {
printWarning();
// add every box together with the additional information into the rtree
std::optional<RtreeEntry> entry =
getRtreeEntry(smallerResult, i, smallerResJoinCol);
if (!entry) {
// nothing to do. When parsing a point or an area fails, a warning
// message gets printed at another place and the point/area just gets
// skipped
continue;
}

Point p(geopoint.value().getLng(), geopoint.value().getLat());

// add every point together with the row number into the rtree
rtree.insert(std::make_pair(std::move(p), i));
rtree.insert(std::pair(entry.value().boundingBox_.value(),
std::move(entry.value())));
}

// query rtree with the other child
std::vector<Value, ad_utility::AllocatorWithLimit<Value>> results{
qec_->getAllocator()};
for (size_t i = 0; i < otherResult->numRows(); i++) {
auto geopoint1 = getPoint(otherResult, i, otherResJoinCol);

if (!geopoint1) {
printWarning();
std::optional<RtreeEntry> entry =
getRtreeEntry(otherResult, i, otherResJoinCol);
if (!entry) {
// nothing to do. When parsing a point or an area fails, a warning
// message gets printed at another place and the point/area just gets
// skipped
continue;
}
std::vector<Box> queryBox = getQueryBox(entry);

Point p(geopoint1.value().getLng(), geopoint1.value().getLat());

// query the other rtree for every point using the following bounding box
std::vector<Box> bbox = computeBoundingBox(p);
results.clear();

ql::ranges::for_each(bbox, [&](const Box& bbox) {
ql::ranges::for_each(queryBox, [&](const Box& bbox) {
Jonathan24680 marked this conversation as resolved.
Show resolved Hide resolved
rtree.query(bgi::intersects(bbox), std::back_inserter(results));
});

ql::ranges::for_each(results, [&](const Value& res) {
size_t rowLeft = res.second;
std::set<AddedPair> pairs;
ql::ranges::for_each(results, [&](Value& res) {
size_t rowLeft = res.second.row_;
size_t rowRight = i;
if (!leftResSmaller) {
std::swap(rowLeft, rowRight);
}
auto distance = computeDist(idTableLeft, idTableRight, rowLeft, rowRight,
leftJoinCol, rightJoinCol);
auto distance = computeDist(res.second, entry.value());
AD_CORRECTNESS_CHECK(distance.getDatatype() == Datatype::Double);
if (distance.getDouble() * 1000 <= static_cast<double>(maxDist.value())) {
addResultTableEntry(&result, idTableLeft, idTableRight, rowLeft,
rowRight, distance);
// make sure, that no duplicate elements are inserted in the result
// table. As duplicates can only occur, when areas are not approximated
// as midpoints, the additional runtime can be saved in that case
if (useMidpointForAreas_) {
addResultTableEntry(&result, idTableLeft, idTableRight, rowLeft,
rowRight, distance);
} else if (pairs.insert(AddedPair{rowLeft, rowRight}).second) {
addResultTableEntry(&result, idTableLeft, idTableRight, rowLeft,
rowRight, distance);
}
}
});
}
Expand Down
Loading
Loading