public STRtree buildTraceSRTtree(Map<Long, Geometry> traceMap) {
if (CollectionUtils.isEmpty(traceMap)) {
return null;
}
STRtree stRtree = new STRtree();
for (Map.Entry<Long, Geometry> entry : traceMap.entrySet()) {
Long key = entry.getKey();
Geometry value = entry.getValue();
Envelope envelope = value.getEnvelopeInternal();
stRtree.insert(envelope, key);
}
stRtree.build();
return stRtree;
}
private Long getTraceIndex(Geometry geom, Map<Long, Geometry> traceMap, STRtree stRtree, Double expandBy) {
if (geom != null && stRtree != null) {
Coordinate coordinate = geom.getCoordinates()[0];
Point point = geometryFactory.createPoint(coordinate);
Envelope envelopeInternal = point.getEnvelopeInternal();
if (expandBy != null) {
envelopeInternal.expandBy(expandBy);
}
List<Long> indexs = stRtree.query(envelopeInternal);
Long traceIndex = indexs.get(0);
double distanceMin = traceMap.get(traceIndex).distance(point);
for (int i = 1; i < indexs.size(); i++) {
Long tmpIndex = indexs.get(i);
double tmp = traceMap.get(tmpIndex).distance(point);
if (tmp < distanceMin) {
distanceMin = tmp;
traceIndex = tmpIndex;
}
}
return traceIndex;
}
return null;
}
网友评论