| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
package org.locationtech.jts.index.quadtree; |
| 14 |
|
| 15 |
import java.io.Serializable; |
| 16 |
import java.util.ArrayList; |
| 17 |
import java.util.Iterator; |
| 18 |
import java.util.List; |
| 19 |
|
| 20 |
import org.locationtech.jts.geom.Envelope; |
| 21 |
import org.locationtech.jts.index.ItemVisitor; |
| 22 |
|
| 23 |
|
| 24 |
/** |
| 25 |
* The base class for nodes in a {@link Quadtree}. |
| 26 |
* |
| 27 |
* @version 1.7 |
| 28 |
*/ |
| 29 |
public abstract class NodeBase implements Serializable { |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
/** |
| 34 |
* Gets the index of the subquad that wholly contains the given envelope. |
| 35 |
* If none does, returns -1. |
| 36 |
* |
| 37 |
* @return the index of the subquad that wholly contains the given envelope |
| 38 |
* or -1 if no subquad wholly contains the envelope |
| 39 |
*/ |
| 40 |
public static int getSubnodeIndex(Envelope env, double centrex, double centrey) |
| 41 |
{ |
| 42 |
int subnodeIndex = -1; |
| 43 |
if (env.getMinX() >= centrex) { |
| 44 |
if (env.getMinY() >= centrey) subnodeIndex = 3; |
| 45 |
if (env.getMaxY() <= centrey) subnodeIndex = 1; |
| 46 |
} |
| 47 |
if (env.getMaxX() <= centrex) { |
| 48 |
if (env.getMinY() >= centrey) subnodeIndex = 2; |
| 49 |
if (env.getMaxY() <= centrey) subnodeIndex = 0; |
| 50 |
} |
| 51 |
return subnodeIndex; |
| 52 |
} |
| 53 |
|
| 54 |
protected List items = new ArrayList(); |
| 55 |
|
| 56 |
/** |
| 57 |
* subquads are numbered as follows: |
| 58 |
* <pre> |
| 59 |
* 2 | 3 |
| 60 |
* --+-- |
| 61 |
* 0 | 1 |
| 62 |
* </pre> |
| 63 |
*/ |
| 64 |
protected Node[] subnode = new Node[4]; |
| 65 |
|
| 66 |
public NodeBase() { |
| 67 |
} |
| 68 |
|
| 69 |
public List getItems() { return items; } |
| 70 |
|
| 71 |
public boolean hasItems() { return ! items.isEmpty(); } |
| 72 |
|
| 73 |
public void add(Object item) |
| 74 |
{ |
| 75 |
items.add(item); |
| 76 |
|
| 77 |
|
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Removes a single item from this subtree. |
| 82 |
* |
| 83 |
* @param itemEnv the envelope containing the item |
| 84 |
* @param item the item to remove |
| 85 |
* @return <code>true</code> if the item was found and removed |
| 86 |
*/ |
| 87 |
public boolean remove(Envelope itemEnv, Object item) |
| 88 |
{ |
| 89 |
|
| 90 |
if (! isSearchMatch(itemEnv)) |
| 91 |
return false; |
| 92 |
|
| 93 |
boolean found = false; |
| 94 |
for (int i = 0; i < 4; i++) { |
| 95 |
if (subnode[i] != null) { |
| 96 |
found = subnode[i].remove(itemEnv, item); |
| 97 |
if (found) { |
| 98 |
|
| 99 |
if (subnode[i].isPrunable()) |
| 100 |
subnode[i] = null; |
| 101 |
break; |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
if (found) return found; |
| 107 |
|
| 108 |
found = items.remove(item); |
| 109 |
return found; |
| 110 |
} |
| 111 |
|
| 112 |
public boolean isPrunable() |
| 113 |
{ |
| 114 |
return ! (hasChildren() || hasItems()); |
| 115 |
} |
| 116 |
|
| 117 |
public boolean hasChildren() |
| 118 |
{ |
| 119 |
for (int i = 0; i < 4; i++) { |
| 120 |
if (subnode[i] != null) |
| 121 |
return true; |
| 122 |
} |
| 123 |
return false; |
| 124 |
} |
| 125 |
|
| 126 |
public boolean isEmpty() |
| 127 |
{ |
| 128 |
boolean isEmpty = true; |
| 129 |
if (! items.isEmpty()) isEmpty = false; |
| 130 |
else { |
| 131 |
for (int i = 0; i < 4; i++) { |
| 132 |
if (subnode[i] != null) { |
| 133 |
if (!subnode[i].isEmpty()) { |
| 134 |
isEmpty = false; |
| 135 |
break; |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
return isEmpty; |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
public List addAllItems(List resultItems) |
| 146 |
{ |
| 147 |
|
| 148 |
|
| 149 |
resultItems.addAll(this.items); |
| 150 |
for (int i = 0; i < 4; i++) { |
| 151 |
if (subnode[i] != null) { |
| 152 |
subnode[i].addAllItems(resultItems); |
| 153 |
} |
| 154 |
} |
| 155 |
return resultItems; |
| 156 |
} |
| 157 |
protected abstract boolean isSearchMatch(Envelope searchEnv); |
| 158 |
|
| 159 |
public void addAllItemsFromOverlapping(Envelope searchEnv, List resultItems) |
| 160 |
{ |
| 161 |
if (! isSearchMatch(searchEnv)) |
| 162 |
return; |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
resultItems.addAll(items); |
| 167 |
|
| 168 |
for (int i = 0; i < 4; i++) { |
| 169 |
if (subnode[i] != null) { |
| 170 |
subnode[i].addAllItemsFromOverlapping(searchEnv, resultItems); |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
public void visit(Envelope searchEnv, ItemVisitor visitor) |
| 176 |
{ |
| 177 |
if (! isSearchMatch(searchEnv)) |
| 178 |
return; |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
visitItems(searchEnv, visitor); |
| 183 |
|
| 184 |
for (int i = 0; i < 4; i++) { |
| 185 |
if (subnode[i] != null) { |
| 186 |
subnode[i].visit(searchEnv, visitor); |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
private void visitItems(Envelope searchEnv, ItemVisitor visitor) |
| 192 |
{ |
| 193 |
|
| 194 |
for (Iterator i = items.iterator(); i.hasNext(); ) { |
| 195 |
visitor.visitItem(i.next()); |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
int depth() |
| 202 |
{ |
| 203 |
int maxSubDepth = 0; |
| 204 |
for (int i = 0; i < 4; i++) { |
| 205 |
if (subnode[i] != null) { |
| 206 |
int sqd = subnode[i].depth(); |
| 207 |
if (sqd > maxSubDepth) |
| 208 |
maxSubDepth = sqd; |
| 209 |
} |
| 210 |
} |
| 211 |
return maxSubDepth + 1; |
| 212 |
} |
| 213 |
|
| 214 |
int size() |
| 215 |
{ |
| 216 |
int subSize = 0; |
| 217 |
for (int i = 0; i < 4; i++) { |
| 218 |
if (subnode[i] != null) { |
| 219 |
subSize += subnode[i].size(); |
| 220 |
} |
| 221 |
} |
| 222 |
return subSize + items.size(); |
| 223 |
} |
| 224 |
|
| 225 |
int getNodeCount() |
| 226 |
{ |
| 227 |
int subSize = 0; |
| 228 |
for (int i = 0; i < 4; i++) { |
| 229 |
if (subnode[i] != null) { |
| 230 |
subSize += subnode[i].size(); |
| 231 |
} |
| 232 |
} |
| 233 |
return subSize + 1; |
| 234 |
} |
| 235 |
|
| 236 |
} |
| 237 |
|