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

Add PolylinesOverlap function #36

Merged
merged 9 commits into from
Dec 20, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@
import com.vividsolutions.jts.index.strtree.STRtree;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;

public final class PolylinesOverlapIoxPlugin extends BaseInterlisFunction {
private static final Map<HasEqualLinePartKey, Boolean> HAS_EQUAL_LINE_PART_CACHE = new HashMap<>();

@Override
public String getQualifiedIliName() {
return "GeoW_FunctionsExt.PolylinesOverlap";
Expand Down Expand Up @@ -49,8 +53,20 @@ protected Value evaluateInternal(String validationKind, String usageScope, IomOb
polylineObjects = EvaluationHelper.evaluateAttributes(validator, argObjects, attributePath);
}

List<CompoundCurve> lines = convertToJTSLines(polylineObjects);
boolean hasOverlap = hasEqualLinePart(lines);
Collection<IomObject> inputObjects = argObjects.getComplexObjects();
boolean hasObjectIds = inputObjects.stream().anyMatch(o -> o.getobjectoid() != null);
domi-b marked this conversation as resolved.
Show resolved Hide resolved
if (!hasObjectIds) {
List<CompoundCurve> lines = convertToJTSLines(polylineObjects);
return new Value(hasEqualLinePart(lines));
}

List<String> objectIds = inputObjects.stream().map(IomObject::getobjectoid).collect(Collectors.toList());
HasEqualLinePartKey key = new HasEqualLinePartKey(objectIds, argPath.isUndefined() ? null : argPath.getValue());

boolean hasOverlap = HAS_EQUAL_LINE_PART_CACHE.computeIfAbsent(key, k -> {
domi-b marked this conversation as resolved.
Show resolved Hide resolved
List<CompoundCurve> lines = convertToJTSLines(polylineObjects);
return hasEqualLinePart(lines);
});
return new Value(hasOverlap);
}

Expand Down Expand Up @@ -99,4 +115,31 @@ private static boolean linesHaveEqualPart(CompoundCurve a, CompoundCurve b) {
int interiorIntersection = relation.get(0, 0);
return interiorIntersection == 1;
}

private static final class HasEqualLinePartKey {
private final List<String> objectIds;
private final String attributeName;

HasEqualLinePartKey(List<String> objectIds, String attributeName) {
this.objectIds = objectIds;
this.attributeName = attributeName;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
HasEqualLinePartKey that = (HasEqualLinePartKey) o;
return Objects.equals(objectIds, that.objectIds) && Objects.equals(attributeName, that.attributeName);
}

@Override
public int hashCode() {
return Objects.hash(objectIds, attributeName);
}
}
}
Loading