Skip to content

Commit

Permalink
fix for relational queries (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
clarisma committed Sep 9, 2024
1 parent 9ad4c3a commit 0cfed8d
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Include this dependency in your project's `pom.xml`:
<dependency>
<groupId>com.geodesk</groupId>
<artifactId>geodesk</artifactId>
<version>0.2.0</version>
<version>0.2.1-SNAPSHOT</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.geodesk</groupId>
<artifactId>geodesk</artifactId>
<version>0.2.0</version>
<version>0.2.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>GeoDesk</name>
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/geodesk/feature/Features.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.locationtech.jts.geom.prep.PreparedGeometry;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

Expand Down Expand Up @@ -496,4 +497,16 @@ default Features within(PreparedGeometry prepared)
* @return
*/
Features select(Features other);

/**
* Adds all features in this collection to the given collection.
*
* @param collection a general-purpose collection (such as `List`, `Set`,
* o another type derived from `java.util.Collection`)
*/
default void addTo(Collection<Feature> collection)
{
for(Feature f: this) collection.add(f);
}

}
17 changes: 16 additions & 1 deletion src/main/java/com/geodesk/feature/query/NodeParentView.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,26 @@ public NodeParentView(FeatureStore store, ByteBuffer buf,
StoredNode node, int pRelations, int types, Matcher matcher, Filter filter)
{
super(store, buf, pRelations, types, matcher, filter);
assert((types & TypeBits.WAYS) != 0 && (types & TypeBits.RELATIONS) != 0);
this.node = node;
}

@Override protected Features newWith(int types, Matcher matcher, Filter filter)
{
return node.parents(types, matcher, filter);
if((types & TypeBits.RELATIONS) == 0)
{
// view has been restricted to ways only
assert((types & TypeBits.WAYS) != 0);
return node.parentWays(types, matcher, filter);
}
else if ((types & TypeBits.WAYS) == 0)
{
// view has been restricted to relations only
assert((types & TypeBits.RELATIONS) != 0);
return new ParentRelationView(store, buf, ptr, types, matcher, filter);
}
assert((types & TypeBits.WAYS) != 0 && (types & TypeBits.RELATIONS) != 0);
return new NodeParentView(store, buf, node, ptr, types, matcher, filter);
}

@Override public Iterator<Feature> iterator()
Expand All @@ -52,6 +66,7 @@ private class Iter extends ParentRelationView.Iter

public Iter()
{
assert((types & (TypeBits.RELATIONS | TypeBits.WAYS)) != 0);
wayQuery = new Query(node.parentWays(types, matcher, filter));
// TODO: To improve performance, we could start the query so it
// can fetch the parent ways in the background, while the caller
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ private void fetchNext()
pRel = (pCurrent & 0xffff_fffe) + ((rel >> 2) << 1);
// TODO: simplify alignment rules!
// TODO: Doesn't need rebasing; pointer is always 2-byte aligned
// But we still need to mask off the last-flag
}
if (matcher.accept(relBuf, pRel))
{
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/geodesk/feature/query/Query.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ public void start(Filter filter)
currentPos = -1;

// Submit initial tasks
int maxPendingTiles = 8; // TODO
int maxPendingTiles = 8;
// TODO: Uncap this limit, set at runtime based on core count
while(pendingTiles < maxPendingTiles)
{
if(!tileWalker.next())
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/com/geodesk/feature/query/View.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,18 @@ protected Features select(int newTypes, String query)
{
if (child instanceof AnonymousWayNode wayNode)
{
// An anonymous nodes *always* has at least one parent way,
// and *never* has parent relations
return wayNode.parents(types, matcher, filter);
}
return ((StoredNode)child).parents(types, matcher, filter);
}
else
{
// Ways and relations can only have relations as parents
if ((types & TypeBits.RELATIONS) == 0) return EmptyView.ANY;
StoredFeature f = (StoredFeature) child;
if(!f.belongsToRelation()) return EmptyView.ANY;
return new ParentRelationView(store, f.buffer(),
f.getRelationTablePtr(), types, matcher, filter);
}
Expand All @@ -142,10 +146,18 @@ protected Features select(int newTypes, String query)

@Override public Features nodesOf(Feature parent)
{
if ((types & TypeBits.NODES) == 0) return EmptyView.ANY;
if(parent.isWay())
{
StoredWay way = (StoredWay) parent;
if ((types & TypeBits.NODES) == 0) return EmptyView.ANY;
if(matcher != Matcher.ALL &&
(way.flags() & FeatureFlags.WAYNODE_FLAG) == 0)
{
// GOQL queries only return feature nodes; if the Way's
// waynode_flag is cleared, it only contains anonymous
// nodes, so we return an empty set
return EmptyView.ANY;
}
return new WayNodeView(store, way.buffer(), way.pointer(),
types, matcher, filter);
}
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/com/geodesk/feature/store/StoredNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ public WorldView parentWays(int types, Matcher matcher, Filter filter)

public Features parents(int types, Matcher matcher, Filter filter)
{
// types &= TypeBits.WAYS | TypeBits.RELATIONS; // TODO: should not be needed? (added 9/9/24)

int acceptedFlags = ((types & TypeBits.RELATIONS) != 0) ?
FeatureFlags.RELATION_MEMBER_FLAG : 0;
acceptedFlags |= ((types & TypeBits.WAYS) != 0) ?
Expand All @@ -129,10 +131,14 @@ public Features parents(int types, Matcher matcher, Filter filter)
return EmptyView.ANY;
}

@Override public Features parents()
{
return parents(TypeBits.ALL, Matcher.ALL, null);
}
@Override public Features parents()
{
if(id() == 3465728159L)
{
System.out.print("!!!");
}
return parents(TypeBits.RELATIONS | TypeBits.WAYS, Matcher.ALL, null);
}

@Override public Features parents(String query)
{
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/geodesk/geom/Tile.java
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ public static TileBox childrenOfTileAtZoom(int tile, int zoom)
return box;
}

// TODO: only works for positive deltas, and does not wrap!
public static int relative(int tile, int deltaCol, int deltaRow)
{
return tile + (deltaRow << 12) + deltaCol;
Expand Down

0 comments on commit 0cfed8d

Please sign in to comment.