2010-04-26 18:24:27 +02:00
|
|
|
|
package com.osmand.data;
|
2010-04-26 17:20:50 +02:00
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
2010-04-27 23:42:19 +02:00
|
|
|
|
import com.osmand.osm.MapUtils;
|
2010-04-26 17:20:50 +02:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param <T> - object to store in that manager
|
|
|
|
|
*/
|
|
|
|
|
public class DataTileManager<T> {
|
|
|
|
|
|
|
|
|
|
private int zoom = 15;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* map for objects stores as 'xTile_yTile' -> List<T>
|
|
|
|
|
*/
|
|
|
|
|
private Map<String, List<T>> objects = new HashMap<String, List<T>>();
|
|
|
|
|
|
|
|
|
|
public int getZoom() {
|
|
|
|
|
return zoom;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setZoom(int zoom) {
|
|
|
|
|
// TODO !!! it is required to reindex all stored objects
|
|
|
|
|
if(!objects.isEmpty()){
|
|
|
|
|
throw new UnsupportedOperationException();
|
|
|
|
|
}
|
|
|
|
|
this.zoom = zoom;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void putObjects(int tx, int ty, List<T> r){
|
|
|
|
|
if(objects.containsKey(evTile(tx, ty))){
|
|
|
|
|
r.addAll(objects.get(evTile(tx, ty)));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-06 11:08:58 +02:00
|
|
|
|
public List<T> getAllObjects(){
|
|
|
|
|
List<T> l = new ArrayList<T>();
|
|
|
|
|
for(String s : objects.keySet()){
|
|
|
|
|
l.addAll(objects.get(s));
|
|
|
|
|
}
|
|
|
|
|
return l;
|
|
|
|
|
}
|
|
|
|
|
|
2010-04-27 14:37:49 +02:00
|
|
|
|
public List<T> getObjects(double latitudeUp, double longitudeUp, double latitudeDown, double longitudeDown) {
|
2010-04-27 23:42:19 +02:00
|
|
|
|
int tileXUp = (int) MapUtils.getTileNumberX(zoom, longitudeUp);
|
|
|
|
|
int tileYUp = (int) MapUtils.getTileNumberY(zoom, latitudeUp);
|
|
|
|
|
int tileXDown = (int) MapUtils.getTileNumberX(zoom, longitudeDown);
|
|
|
|
|
int tileYDown = (int) MapUtils.getTileNumberY(zoom, latitudeDown);
|
2010-04-27 14:37:49 +02:00
|
|
|
|
List<T> result = new ArrayList<T>();
|
|
|
|
|
for (int i = tileXUp; i <= tileXDown; i++) {
|
|
|
|
|
for (int j = tileYUp; j <= tileYDown; j++) {
|
|
|
|
|
putObjects(i, j, result);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2010-04-26 17:20:50 +02:00
|
|
|
|
/**
|
|
|
|
|
* @depth of the neighbor tile to visit
|
|
|
|
|
* returns not exactly sorted list,
|
|
|
|
|
* however the first objects are from closer tile than last
|
|
|
|
|
*/
|
|
|
|
|
public List<T> getClosestObjects(double latitude, double longitude, int depth){
|
2010-04-27 23:42:19 +02:00
|
|
|
|
int tileX = (int) MapUtils.getTileNumberX(zoom, longitude);
|
|
|
|
|
int tileY = (int) MapUtils.getTileNumberY(zoom, latitude);
|
2010-04-26 17:20:50 +02:00
|
|
|
|
List<T> result = new ArrayList<T>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
putObjects(tileX, tileY, result);
|
|
|
|
|
|
|
|
|
|
// that's very difficult way visiting node :
|
2010-04-27 14:37:49 +02:00
|
|
|
|
// similar to visit by spiral
|
|
|
|
|
// however the simplest way could be to visit row by row & after sort tiles by distance (that's less efficient)
|
2010-04-26 17:20:50 +02:00
|
|
|
|
|
|
|
|
|
// go through circle
|
|
|
|
|
for (int i = 1; i <= depth; i++) {
|
|
|
|
|
|
2010-05-06 11:08:58 +02:00
|
|
|
|
// goes <20>
|
2010-04-26 17:20:50 +02:00
|
|
|
|
for (int j = 0; j <= i; j++) {
|
|
|
|
|
// left & right
|
|
|
|
|
int dx = j == 0 ? 0 : -1;
|
|
|
|
|
for (; dx < 1 || (j < i && dx == 1); dx += 2) {
|
|
|
|
|
// north
|
|
|
|
|
putObjects(tileX + dx * j, tileY + i, result);
|
|
|
|
|
// east
|
|
|
|
|
putObjects(tileX + i, tileY - dx * j, result);
|
|
|
|
|
// south
|
|
|
|
|
putObjects(tileX - dx * j, tileY - i, result);
|
|
|
|
|
// west
|
|
|
|
|
putObjects(tileX - i, tileY + dx * j, result);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String evTile(int tileX, int tileY){
|
|
|
|
|
return tileX +"_"+tileY;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public String evaluateTile(double latitude, double longitude){
|
2010-04-27 23:42:19 +02:00
|
|
|
|
int tileX = (int) MapUtils.getTileNumberX(zoom, longitude);
|
|
|
|
|
int tileY = (int) MapUtils.getTileNumberY(zoom, latitude);
|
2010-04-26 17:20:50 +02:00
|
|
|
|
return evTile(tileX, tileY);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String registerObject(double latitude, double longitude, T object){
|
|
|
|
|
String tile = evaluateTile(latitude, longitude);
|
|
|
|
|
if(!objects.containsKey(tile)){
|
|
|
|
|
objects.put(tile, new ArrayList<T>());
|
|
|
|
|
}
|
|
|
|
|
objects.get(tile).add(object);
|
|
|
|
|
return tile;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|