|
| 1 | +/* |
| 2 | + * Copyright (c) 2020 Martin Davis. |
| 3 | + * |
| 4 | + * All rights reserved. This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 6 | + * and Eclipse Distribution License v. 1.0 which accompanies this distribution. |
| 7 | + * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html |
| 8 | + * and the Eclipse Distribution License is available at |
| 9 | + * |
| 10 | + * http://www.eclipse.org/org/documents/edl-v10.php. |
| 11 | + */ |
| 12 | +package org.locationtech.jts.geom; |
| 13 | + |
| 14 | +import org.locationtech.jts.operation.relate.RelateOp; |
| 15 | +import org.locationtech.jts.operation.relateng.RelateNG; |
| 16 | +import org.locationtech.jts.operation.relateng.RelatePredicate; |
| 17 | + |
| 18 | +/** |
| 19 | + * Internal class which encapsulates the runtime switch to use RelateNG. |
| 20 | + * <p> |
| 21 | + * This class allows the {@link Geometry} predicate methods to be |
| 22 | + * switched between the original {@link RelateOp} algorithm |
| 23 | + * and the modern {@link RelateNG} codebase |
| 24 | + * via a system property <code>jts.relate</code>. |
| 25 | + * <ul> |
| 26 | + * <li><code>jts.relate=old</code> - (default) use original RelateOp algorithm |
| 27 | + * <li><code>jts.relate=ng</code> - use RelateNG |
| 28 | + * </ul> |
| 29 | + * |
| 30 | + * @author mdavis |
| 31 | + * |
| 32 | + */ |
| 33 | +class GeometryRelate |
| 34 | +{ |
| 35 | + public static String RELATE_PROPERTY_NAME = "jts.relate"; |
| 36 | + |
| 37 | + public static String RELATE_PROPERTY_VALUE_NG = "ng"; |
| 38 | + public static String RELATE_PROPERTY_VALUE_OLD = "old"; |
| 39 | + |
| 40 | + /** |
| 41 | + * Currently the old relate implementation is the default |
| 42 | + */ |
| 43 | + public static boolean RELATE_NG_DEFAULT = false; |
| 44 | + |
| 45 | + private static boolean isRelateNG = RELATE_NG_DEFAULT; |
| 46 | + |
| 47 | + static { |
| 48 | + setRelateImpl(System.getProperty(RELATE_PROPERTY_NAME)); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * This function is provided primarily for unit testing. |
| 53 | + * It is not recommended to use it dynamically, since |
| 54 | + * that may result in inconsistent overlay behaviour. |
| 55 | + * |
| 56 | + * @param relateImplCode the code for the overlay method (may be null) |
| 57 | + */ |
| 58 | + static void setRelateImpl(String relateImplCode) { |
| 59 | + if (relateImplCode == null) |
| 60 | + return; |
| 61 | + // set flag explicitly since current value may not be default |
| 62 | + isRelateNG = RELATE_NG_DEFAULT; |
| 63 | + |
| 64 | + if (RELATE_PROPERTY_VALUE_NG.equalsIgnoreCase(relateImplCode) ) |
| 65 | + isRelateNG = true; |
| 66 | + } |
| 67 | + |
| 68 | + static boolean intersects(Geometry a, Geometry b) |
| 69 | + { |
| 70 | + if (isRelateNG) { |
| 71 | + return RelateNG.relate(a, b, RelatePredicate.intersects()); |
| 72 | + } |
| 73 | + if (a.isGeometryCollection() || b.isGeometryCollection()) { |
| 74 | + for (int i = 0 ; i < a.getNumGeometries() ; i++) { |
| 75 | + for (int j = 0 ; j < b.getNumGeometries() ; j++) { |
| 76 | + if (a.getGeometryN(i).intersects(b.getGeometryN(j))) { |
| 77 | + return true; |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + return false; |
| 82 | + } |
| 83 | + return RelateOp.relate(a, b).isIntersects(); |
| 84 | + } |
| 85 | + |
| 86 | + static boolean contains(Geometry a, Geometry b) |
| 87 | + { |
| 88 | + if (isRelateNG) { |
| 89 | + return RelateNG.relate(a, b, RelatePredicate.contains()); |
| 90 | + } |
| 91 | + // optimization - lower dimension cannot contain areas |
| 92 | + if (b.getDimension() == 2 && a.getDimension() < 2) { |
| 93 | + return false; |
| 94 | + } |
| 95 | + // optimization - P cannot contain a non-zero-length L |
| 96 | + // Note that a point can contain a zero-length lineal geometry, |
| 97 | + // since the line has no boundary due to Mod-2 Boundary Rule |
| 98 | + if (b.getDimension() == 1 && a.getDimension() < 1 && b.getLength() > 0.0) { |
| 99 | + return false; |
| 100 | + } |
| 101 | + // optimization - envelope test |
| 102 | + if (! a.getEnvelopeInternal().contains(b.getEnvelopeInternal())) |
| 103 | + return false; |
| 104 | + return RelateOp.relate(a, b).isContains(); |
| 105 | + } |
| 106 | + |
| 107 | + static boolean covers(Geometry a, Geometry b) |
| 108 | + { |
| 109 | + if (isRelateNG) { |
| 110 | + return RelateNG.relate(a, b, RelatePredicate.covers()); |
| 111 | + } |
| 112 | + // optimization - lower dimension cannot cover areas |
| 113 | + if (b.getDimension() == 2 && a.getDimension() < 2) { |
| 114 | + return false; |
| 115 | + } |
| 116 | + // optimization - P cannot cover a non-zero-length L |
| 117 | + // Note that a point can cover a zero-length lineal geometry |
| 118 | + if (b.getDimension() == 1 && a.getDimension() < 1 && b.getLength() > 0.0) { |
| 119 | + return false; |
| 120 | + } |
| 121 | + // optimization - envelope test |
| 122 | + if (! a.getEnvelopeInternal().covers(b.getEnvelopeInternal())) |
| 123 | + return false; |
| 124 | + // optimization for rectangle arguments |
| 125 | + if (a.isRectangle()) { |
| 126 | + // since we have already tested that the test envelope is covered |
| 127 | + return true; |
| 128 | + } |
| 129 | + return RelateOp.relate(a, b).isCovers(); |
| 130 | + } |
| 131 | + |
| 132 | + static boolean coveredBy(Geometry a, Geometry b) |
| 133 | + { |
| 134 | + if (isRelateNG) { |
| 135 | + return RelateNG.relate(a, b, RelatePredicate.coveredBy()); |
| 136 | + } |
| 137 | + return covers(b, a); |
| 138 | + } |
| 139 | + |
| 140 | + static boolean crosses(Geometry a, Geometry b) |
| 141 | + { |
| 142 | + if (isRelateNG) { |
| 143 | + return RelateNG.relate(a, b, RelatePredicate.crosses()); |
| 144 | + } |
| 145 | + // short-circuit test |
| 146 | + if (! a.getEnvelopeInternal().intersects(b.getEnvelopeInternal())) |
| 147 | + return false; |
| 148 | + return RelateOp.relate(a, b).isCrosses(a.getDimension(), b.getDimension()); |
| 149 | + } |
| 150 | + |
| 151 | + static boolean disjoint(Geometry a, Geometry b) |
| 152 | + { |
| 153 | + if (isRelateNG) { |
| 154 | + return RelateNG.relate(a, b, RelatePredicate.disjoint()); |
| 155 | + } |
| 156 | + return ! intersects(a, b); |
| 157 | + } |
| 158 | + |
| 159 | + static boolean equalsTopo(Geometry a, Geometry b) |
| 160 | + { |
| 161 | + if (isRelateNG) { |
| 162 | + return RelateNG.relate(a, b, RelatePredicate.equalsTopo()); |
| 163 | + } |
| 164 | + if (! a.getEnvelopeInternal().equals(b.getEnvelopeInternal())) |
| 165 | + return false; |
| 166 | + return RelateOp.relate(a, b).isEquals(a.getDimension(), b.getDimension()); |
| 167 | + } |
| 168 | + |
| 169 | + static boolean overlaps(Geometry a, Geometry b) |
| 170 | + { |
| 171 | + if (isRelateNG) { |
| 172 | + return RelateNG.relate(a, b, RelatePredicate.overlaps()); |
| 173 | + } |
| 174 | + if (! a.getEnvelopeInternal().intersects(b.getEnvelopeInternal())) |
| 175 | + return false; |
| 176 | + return RelateOp.relate(a, b).isOverlaps(a.getDimension(), b.getDimension()); |
| 177 | + } |
| 178 | + |
| 179 | + static boolean touches(Geometry a, Geometry b) |
| 180 | + { |
| 181 | + if (isRelateNG) { |
| 182 | + return RelateNG.relate(a, b, RelatePredicate.touches()); |
| 183 | + } |
| 184 | + if (! a.getEnvelopeInternal().intersects(b.getEnvelopeInternal())) |
| 185 | + return false; |
| 186 | + return RelateOp.relate(a, b).isTouches(a.getDimension(), b.getDimension()); |
| 187 | + } |
| 188 | + |
| 189 | + static boolean within(Geometry a, Geometry b) |
| 190 | + { |
| 191 | + if (isRelateNG) { |
| 192 | + return RelateNG.relate(a, b, RelatePredicate.within()); |
| 193 | + } |
| 194 | + return contains(b, a); |
| 195 | + } |
| 196 | + |
| 197 | + static IntersectionMatrix relate(Geometry a, Geometry b) |
| 198 | + { |
| 199 | + if (isRelateNG) { |
| 200 | + return RelateNG.relate(a, b); |
| 201 | + } |
| 202 | + Geometry.checkNotGeometryCollection(a); |
| 203 | + Geometry.checkNotGeometryCollection(b); |
| 204 | + return RelateOp.relate(a, b); |
| 205 | + } |
| 206 | + |
| 207 | + static boolean relate(Geometry a, Geometry b, String intersectionPattern) |
| 208 | + { |
| 209 | + if (isRelateNG) { |
| 210 | + return RelateNG.relate(a, b, intersectionPattern); |
| 211 | + } |
| 212 | + Geometry.checkNotGeometryCollection(a); |
| 213 | + Geometry.checkNotGeometryCollection(b); |
| 214 | + return RelateOp.relate(a, b).matches(intersectionPattern); |
| 215 | + } |
| 216 | + |
| 217 | +} |
0 commit comments