List of usage examples for org.apache.commons.collections BinaryHeap pop
public Object pop() throws NoSuchElementException
From source file:com.opendoorlogistics.core.scripts.formulae.FmLookupNearest.java
@Override public Object execute(FunctionParameters parameters) { TableParameters tp = (TableParameters) parameters; ODLTableReadOnly table = (ODLTableReadOnly) tp.getTableById(refs.datastoreIndx, refs.tableId); if (type == LCType.LL) { return executeLL(parameters, table); }/* w ww.j a va 2 s.c o m*/ CachedProcessedGeom searchObject = getSearchGeom(parameters); if (searchObject == null || searchObject.geometry == null) { return Functions.EXECUTION_ERROR; } class RowElement implements Comparable<RowElement> { int row; CachedProcessedGeom geom; double minDistance; @Override public int compareTo(RowElement o) { int ret = Double.compare(minDistance, o.minDistance); if (ret == 0) { ret = Integer.compare(row, o.row); } return ret; } } // Get all geometries, transformed into the coord system and with bounding circles. // Place them in a binary heap, sorted by their minimum possible distance according to bounding circle int nr = table.getRowCount(); BinaryHeap sortedHeap = new BinaryHeap(); for (int row = 0; row < nr; row++) { CachedProcessedGeom otherGeom = null; switch (type) { case GL: Pair<LatLong, Boolean> result = getLatLongFromRow(table, row); if (result.getSecond() == false) { // critical error return Functions.EXECUTION_ERROR; } else if (result.getFirst() != null) { LatLong ll = result.getFirst(); // put into our comparison object and convert otherGeom = new CachedProcessedGeom(ll, transform); } break; case GG: case LG: { Object val = table.getValueAt(row, refs.columnIndices[0]); if (val != null) { ODLGeomImpl odlGeom = (ODLGeomImpl) ColumnValueProcessor.convertToMe(ODLColumnType.GEOM, val); if (odlGeom == null) { // critical error return Functions.EXECUTION_ERROR; } otherGeom = toCoordSystem(odlGeom); if (otherGeom == null || otherGeom.geometry == null) { // critical error return Functions.EXECUTION_ERROR; } } } default: break; } if (otherGeom != null) { RowElement rowElement = new RowElement(); rowElement.row = row; rowElement.minDistance = searchObject.boundingCircle.minimumSeparation(otherGeom.boundingCircle); rowElement.geom = otherGeom; sortedHeap.add(rowElement); } } // loop over the table RowElement closest = null; double closestDistance = Double.MAX_VALUE; while (sortedHeap.size() > 0) { RowElement row = (RowElement) sortedHeap.pop(); if (row.minDistance > closestDistance) { // We can stop searching now as the minimum possible distance is greater than our closest break; } // Explicitly get the distance double distance = searchObject.geometry.distance(row.geom.geometry); if (distance < closestDistance) { closestDistance = distance; closest = row; } } if (closest != null) { return getReturnObject(table, closest.row); } return null; }