use of com.graphhopper.util.shapes.GHPoint in project graphhopper by graphhopper.
the class CompressedArray method get.
public GHPoint get(long index) {
int segmentNo = (int) (index / entriesPerSegment);
int entry = (int) (index % entriesPerSegment);
try {
if (segmentNo >= segments.size()) {
return null;
}
byte[] bytes = segments.get(segmentNo);
VLongStorage store = new VLongStorage(decompress(bytes));
long len = store.getLength();
for (int i = 0; store.getPosition() < len; i++) {
long latlon = store.readVLong();
if (i == entry) {
GHPoint point = new GHPoint();
algo.decode(latlon, point);
return point;
}
}
return null;
} catch (ArrayIndexOutOfBoundsException ex) {
throw new RuntimeException("index " + index + "=> segNo:" + segmentNo + ", entry=" + entry + ", segments:" + segments.size(), ex);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
use of com.graphhopper.util.shapes.GHPoint in project graphhopper by graphhopper.
the class ViaRoutingTemplate method lookup.
@Override
public List<QueryResult> lookup(List<GHPoint> points, FlagEncoder encoder) {
if (points.size() < 2)
throw new IllegalArgumentException("At least 2 points have to be specified, but was:" + points.size());
EdgeFilter edgeFilter = new DefaultEdgeFilter(encoder);
queryResults = new ArrayList<>(points.size());
for (int placeIndex = 0; placeIndex < points.size(); placeIndex++) {
GHPoint point = points.get(placeIndex);
QueryResult res;
if (ghRequest.hasPointHints()) {
res = locationIndex.findClosest(point.lat, point.lon, new NameSimilarityEdgeFilter(edgeFilter, ghRequest.getPointHints().get(placeIndex)));
if (!res.isValid()) {
res = locationIndex.findClosest(point.lat, point.lon, edgeFilter);
}
} else {
res = locationIndex.findClosest(point.lat, point.lon, edgeFilter);
}
if (!res.isValid())
ghResponse.addError(new PointNotFoundException("Cannot find point " + placeIndex + ": " + point, placeIndex));
queryResults.add(res);
}
return queryResults;
}
use of com.graphhopper.util.shapes.GHPoint in project graphhopper by graphhopper.
the class TestAlgoCollector method queryIndex.
void queryIndex(Graph g, LocationIndex idx, double lat, double lon, double expectedDist) {
QueryResult res = idx.findClosest(lat, lon, EdgeFilter.ALL_EDGES);
if (!res.isValid()) {
errors.add("node not found for " + lat + "," + lon);
return;
}
GHPoint found = res.getSnappedPoint();
double dist = distCalc.calcDist(lat, lon, found.lat, found.lon);
if (Math.abs(dist - expectedDist) > .1) {
errors.add("queried lat,lon=" + (float) lat + "," + (float) lon + " (found: " + (float) found.lat + "," + (float) found.lon + ")" + "\n expected distance:" + expectedDist + ", but was:" + dist);
}
}
use of com.graphhopper.util.shapes.GHPoint in project graphhopper by graphhopper.
the class SpatialRuleLookupArray method addRule.
@Override
public void addRule(SpatialRule rule) {
if (rule == null)
throw new IllegalArgumentException("rule cannot be null");
if (rule.equals(SpatialRule.EMPTY))
throw new IllegalArgumentException("rule cannot be EMPTY");
addSingleRule(rule);
int ruleContainerIndex = addRuleContainer(new SpatialRuleContainer().addRule(rule));
for (Polygon polygon : rule.getBorders()) {
for (int i = getXIndexForLon(polygon.getMinLon()); i < getXIndexForLon(polygon.getMaxLon()) + 1; i++) {
for (int j = getYIndexForLat(polygon.getMinLat()); j < getYIndexForLat(polygon.getMaxLat()) + 1; j++) {
if (i >= lookupArray.length || j >= lookupArray[0].length) {
continue;
}
GHPoint center = getCoordinatesForIndex(i, j);
// TODO: Consider creating a new method in Polygon that does the 5 checks - p.partOfTile?
if (polygon.contains(center) || polygon.contains(center.getLat() - checkDiff, center.getLon() - checkDiff) || polygon.contains(center.getLat() - checkDiff, center.getLon() + checkDiff) || polygon.contains(center.getLat() + checkDiff, center.getLon() - checkDiff) || polygon.contains(center.getLat() + checkDiff, center.getLon() + checkDiff)) {
if (lookupArray[i][j] == EMPTY_RULE_INDEX) {
lookupArray[i][j] = (byte) ruleContainerIndex;
} else {
// Merge Rules
SpatialRuleContainer curContainer = getContainerFor2DIndex(i, j);
SpatialRuleContainer newContainer = curContainer.copy().addRule(rule);
int newRuleContainerIndex = addRuleContainer(newContainer);
lookupArray[i][j] = (byte) newRuleContainerIndex;
}
}
}
}
}
}
use of com.graphhopper.util.shapes.GHPoint in project graphhopper by graphhopper.
the class Location2IDQuadtree method fillQuadtree.
private GHBitSet fillQuadtree(int size) {
int locs = graph.getNodes();
if (locs <= 0) {
throw new IllegalStateException("check your graph - it is empty!");
}
GHBitSet filledIndices = new GHBitSetImpl(size);
GHPoint coord = new GHPoint();
for (int nodeId = 0; nodeId < locs; nodeId++) {
double lat = nodeAccess.getLatitude(nodeId);
double lon = nodeAccess.getLongitude(nodeId);
int key = (int) keyAlgo.encode(lat, lon);
long bytePos = (long) key * 4;
if (filledIndices.contains(key)) {
int oldNodeId = index.getInt(bytePos);
keyAlgo.decode(key, coord);
// decide which one is closer to 'key'
double distNew = distCalc.calcNormalizedDist(coord.lat, coord.lon, lat, lon);
double oldLat = nodeAccess.getLatitude(oldNodeId);
double oldLon = nodeAccess.getLongitude(oldNodeId);
double distOld = distCalc.calcNormalizedDist(coord.lat, coord.lon, oldLat, oldLon);
// new point is closer to quad tree point (key) so overwrite old
if (distNew < distOld) {
index.setInt(bytePos, nodeId);
}
} else {
index.setInt(bytePos, nodeId);
filledIndices.add(key);
}
}
return filledIndices;
}
Aggregations