Skip to content

Commit

Permalink
multipoint support in geometrycollections, update util
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickbr committed Sep 3, 2024
1 parent 6684c32 commit 9116309
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 32 deletions.
6 changes: 3 additions & 3 deletions src/spatialjoin/Sweeper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ void Sweeper::add(const I32MultiLine& a, const std::string& gid, bool side,
}

// _____________________________________________________________________________
void Sweeper::addMp(const I32MultiPoint& a, const std::string& gid, bool side,
void Sweeper::add(const I32MultiPoint& a, const std::string& gid, bool side,
WriteBatch& batch) const {
uint16_t subid = 0; // a subid of 0 means "single point"
if (a.size() > 1) subid = 1;

addMp(a, gid, subid, side, batch);
add(a, gid, subid, side, batch);
}

// _____________________________________________________________________________
Expand All @@ -97,7 +97,7 @@ void Sweeper::add(const I32MultiLine& a, const std::string& gid, size_t subId,
}

// _____________________________________________________________________________
void Sweeper::addMp(const I32MultiPoint& a, const std::string& gid,
void Sweeper::add(const I32MultiPoint& a, const std::string& gid,
size_t subid, bool side, WriteBatch& batch) const {
size_t newId = subid;
for (const auto& point : a) {
Expand Down
4 changes: 2 additions & 2 deletions src/spatialjoin/Sweeper.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,9 @@ class Sweeper {
WriteBatch& batch) const;
void add(const util::geo::I32Point& a, const std::string& gid, size_t subid,
bool side, WriteBatch& batch) const;
void addMp(const util::geo::I32MultiPoint& a, const std::string& gid, size_t,
void add(const util::geo::I32MultiPoint& a, const std::string& gid, size_t,
bool side, WriteBatch& batch) const;
void addMp(const util::geo::I32MultiPoint& a, const std::string& gid,
void add(const util::geo::I32MultiPoint& a, const std::string& gid,
bool side, WriteBatch& batch) const;

void add(const std::string& a, const util::geo::I32Box& box,
Expand Down
72 changes: 51 additions & 21 deletions src/spatialjoin/WKTParse.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ inline util::geo::I32Polygon parsePolygon(const char* c, const char** endr) {
util::geo::I32Polygon poly;
while ((c = strchr(c, '('))) {
c++;
const char* end = 0;
const char* end = 0;
const auto& line = parseLineString(c, &end);

if (!end) return {}; // parse error
Expand All @@ -78,7 +78,6 @@ inline util::geo::I32Polygon parsePolygon(const char* c, const char** endr) {
poly.getInners().push_back(std::move(line));
i++;


auto q = strchr(c + 1, ')');
auto cc = strchr(c + 1, '(');

Expand All @@ -95,7 +94,8 @@ inline util::geo::I32Polygon parsePolygon(const char* c, const char** endr) {
}

// _____________________________________________________________________________
inline util::geo::I32MultiLine parseMultiLineString(const char* c, const char** endr) {
inline util::geo::I32MultiLine parseMultiLineString(const char* c,
const char** endr) {
util::geo::I32MultiLine ml;
while ((c = strchr(c, '('))) {
c++;
Expand All @@ -106,7 +106,8 @@ inline util::geo::I32MultiLine parseMultiLineString(const char* c, const char**
auto nextComma = strchr(end + 1, ',');
auto nextCloseBracket = strchr(end + 1, ')');

if (!nextComma || (nextComma && nextCloseBracket && nextComma > nextCloseBracket)) {
if (!nextComma ||
(nextComma && nextCloseBracket && nextComma > nextCloseBracket)) {
if (endr) (*endr) = nextCloseBracket;
return ml;
}
Expand All @@ -118,7 +119,8 @@ inline util::geo::I32MultiLine parseMultiLineString(const char* c, const char**
}

// _____________________________________________________________________________
inline util::geo::I32MultiPolygon parseMultiPolygon(const char* c, const char** endr) {
inline util::geo::I32MultiPolygon parseMultiPolygon(const char* c,
const char** endr) {
util::geo::I32MultiPolygon mp;
do {
c = strchr(c, '(');
Expand All @@ -133,7 +135,8 @@ inline util::geo::I32MultiPolygon parseMultiPolygon(const char* c, const char**
auto nextComma = strchr(end + 1, ',');
auto nextCloseBracket = strchr(end + 1, ')');

if (!nextComma || (nextComma && nextCloseBracket && nextComma > nextCloseBracket)) {
if (!nextComma ||
(nextComma && nextCloseBracket && nextComma > nextCloseBracket)) {
if (endr) (*endr) = nextCloseBracket;
return mp;
}
Expand Down Expand Up @@ -201,12 +204,10 @@ inline void parseLine(char* c, size_t len, size_t gid, sj::Sweeper* sweeper,
c += 6;
const auto& point = parsePoint(c);
sweeper->add(point, id, side, batch);
return;
} else if (len > 11 && memcmp(c, "MULTIPOINT(", 11) == 0) {
c += 11;
const auto& mp = parseLineString(c, 0);
if (mp.size() != 0) sweeper->addMp(mp, id, side, batch);
return;
const auto& mp = util::geo::I32MultiPoint(parseLineString(c, 0));
if (mp.size() != 0) sweeper->add(mp, id, side, batch);
} else if (len > 11 && memcmp(c, "LINESTRING(", 11) == 0) {
c += 11;
const auto& line = parseLineString(c, 0);
Expand All @@ -225,7 +226,9 @@ inline void parseLine(char* c, size_t len, size_t gid, sj::Sweeper* sweeper,
if (mp.size()) sweeper->add(mp, id, side, batch);
} else if (len > 19 && memcmp(c, "GEOMETRYCOLLECTION(", 19) == 0) {
c += 18;
size_t subId = 1;

util::geo::I32Collection col;
size_t numGeoms = 0;

do {
c++;
Expand All @@ -238,8 +241,8 @@ inline void parseLine(char* c, size_t len, size_t gid, sj::Sweeper* sweeper,

if (!end) break;

sweeper->add(point, id, subId, side, batch);
subId++;
col.push_back(point);
numGeoms++;
c = const_cast<char*>(strchr(end, ','));
} else if (memcmp(c, "POLYGON(", 8) == 0) {
c += 7;
Expand All @@ -248,8 +251,8 @@ inline void parseLine(char* c, size_t len, size_t gid, sj::Sweeper* sweeper,

if (!end) break;
if (poly.getOuter().size() > 1) {
sweeper->add(poly, id, subId, side, batch);
subId++;
col.push_back(poly);
numGeoms++;
}
c = const_cast<char*>(strchr(end, ','));
} else if (memcmp(c, "LINESTRING(", 11) == 0) {
Expand All @@ -259,8 +262,19 @@ inline void parseLine(char* c, size_t len, size_t gid, sj::Sweeper* sweeper,

if (!end) break;
if (line.size() > 1) {
sweeper->add(line, id, subId, side, batch);
subId++;
col.push_back(line);
numGeoms++;
}
c = const_cast<char*>(strchr(end, ','));
} else if (memcmp(c, "MULTIPOINT(", 11) == 0) {
c += 11;
const char* end = 0;
const auto& line = parseLineString(c, &end);

if (!end) break;
if (line.size() > 1) {
col.push_back(util::geo::I32MultiPoint(std::move(line)));
numGeoms += line.size();
}
c = const_cast<char*>(strchr(end, ','));
} else if (memcmp(c, "MULTIPOLYGON(", 13) == 0) {
Expand All @@ -270,8 +284,8 @@ inline void parseLine(char* c, size_t len, size_t gid, sj::Sweeper* sweeper,

if (!end) break;
if (mp.size()) {
sweeper->add(mp, id, subId, side, batch);
subId++;
col.push_back(mp);
numGeoms += mp.size();
}
c = const_cast<char*>(strchr(end, ','));
} else if (memcmp(c, "MULTILINESTRING(", 16) == 0) {
Expand All @@ -281,12 +295,28 @@ inline void parseLine(char* c, size_t len, size_t gid, sj::Sweeper* sweeper,

if (!end) break;
if (ml.size()) {
sweeper->add(ml, id, subId, side, batch);
subId++;
col.push_back(ml);
numGeoms += ml.size();
}
c = const_cast<char*>(strchr(end, ','));
}
} while (c && *c);

size_t subId = numGeoms > 1 ? 1 : 0;

for (const auto& a : col) {
if (a.getType() == 0) sweeper->add(a.getPoint(), id, subId, side, batch);
if (a.getType() == 1) sweeper->add(a.getLine(), id, subId, side, batch);
if (a.getType() == 2)
sweeper->add(a.getPolygon(), id, subId, side, batch);
if (a.getType() == 3)
sweeper->add(a.getMultiLine(), id, subId, side, batch);
if (a.getType() == 4)
sweeper->add(a.getMultiPolygon(), id, subId, side, batch);
if (a.getType() == 6)
sweeper->add(a.getMultiPoint(), id, subId, side, batch);
subId++;
}
}
}

Expand Down
12 changes: 7 additions & 5 deletions src/spatialjoin/tests/datasets/collectiontests
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
GEOMETRYCOLLECTION(POINT(0.0010 0.0010))
GEOMETRYCOLLECTION(POINT(0.0010 0.0010))
MULTIPOINT(0.0010 0.0010, 0.0011 0.0011)
POINT(0.0010 0.0010)
GEOMETRYCOLLECTION( POINT( 0.0010 0.0010 ) )
MULTIPOINT(0.0010 0.0010, 0.0011 0.0011)
GEOMETRYCOLLECTION(MULTIPOINT(0.0010 0.0010, 0.0011 0.0011))
MULTIPOINT(0.0011 0.0011, 0.0010 0.0010)
LINESTRING(0.0010 0.0010, 0.0009 0.0009)
LINESTRING(0.0010 0.0010, 0.0009 0.0009, 0.0011 0.0011)
GEOMETRYCOLLECTION(POLYGON((0.00 0.00, 0.000 0.001, 0.001 0.001, 0.001 0.000, 0 0)))
GEOMETRYCOLLECTION(POLYGON((0.00 0.00, 0.000 0.001, -0.001 0.001, -0.001 0.000, 0 0)))
GEOMETRYCOLLECTION(POLYGON((0.00 0.00, 0.000 0.001, -0.001 0.001, -0.001 0.000, 0 0)), POLYGON((0.0005 0.00, 0.0005 0.001, 0.001 0.001, 0.001 0.0005, 00.0005 0)))
MULTIPOLYGON(((0.00 0.00, 0.000 0.001, 0.001 0.001, 0.001 0.000, 0 0)))
MULTIPOINT(0.00 0.00, 0.0005 0.00)
GEOMETRYCOLLECTION(MULTIPOINT(0.00 0.00, 0.0005 0.00))
GEOMETRYCOLLECTION(MULTIPOLYGON(((0.0005 0.00, 0.0005 0.001, 0.001 0.001, 0.001 0.0005, 00.0005 0)), ((0.00 0.00, 0.000 0.001, -0.001 0.001, -0.001 0.000, 0 0))))
LINESTRING(0.0010 0.0010, 0.0009 0.0009)
MULTILINESTRING((0.005 0.005, 0.006 0.006))
Expand Down Expand Up @@ -53,4 +53,6 @@ GEOMETRYCOLLECTION(,)
GEOMETRYCOLLECTION(MULTIPOLYGON(((0.00 0.00, 0.000 0.001, -0.001 0.001, -0.001 0.000, 0 0))), MULTIPOLYGON(((0.0005 0.00, 0.0005 0.001, 0.001 0.001, 0.001 0.0005, 00.0005 0))))
GEOMETRYCOLLECTION(MULTILINESTRING((0.0011 0.0011, 0.0007 0.0007)), MULTILINESTRING((0.003 0.003, 0.005 0.003)))


GEOMETRYCOLLECTION(
GEOMETRYCOLLECTION(POLYGON(
GEOMETRYCOLLECTION( POLYGON(33)
2 changes: 1 addition & 1 deletion src/util
Submodule util updated 1 files
+4 −0 geo/Collection.h

0 comments on commit 9116309

Please sign in to comment.