我正在将一个包含大量多面体的 Json 文件解析到 Realm。我已按以下方式进行设置:
RealmMultiPolygon 类:
public int dangerLevel;
public int timeOfDay;
public RealmList<RealmPolygon> realmPolygons
RealmPolygon 类:
public RealmList<RealmPolygonCoordinate> coordinates;
RealmPolygonCoordinate
public double latitude;
public double longitude;
就项目而言:
- RealmMultiPolygon 包含 8 个项目
- RealmPolygon 包含 22260 个项目
- RealmPolygonCoordinate 包含 352241 项
我试图弄清楚 RealmPolygon 是否在我正在查看的方向上并靠近我当前的位置。我正在使用这样的方位采集 31 个位置样本:
private void calculateUserDirectionLocations() {
Log.d(Constants.DEBUG, "update the locationbar");
if(compassData < 0) {
return;
}
int step = 50;
int bearingstep = 1;
double bearing = Math.round(compassData / bearingstep) * bearingstep;
if(userLocation != null) {
if(Math.abs(bearing - oldBearing) > 1) {
locationSamples.clear();
Log.d(Constants.DEBUG, "START location samples");
for(int i = 0; i <= Constants.MAX_DISTANCE; i+=step) {
if (i % step == 0) {
locationSamples.add(locationWithBearing((double) i));
}
}
Log.d(Constants.DEBUG, "END location samples");
updateColors();
oldBearing = bearing;
}
}
}
其中 compassData 是从 RotationVectorSensor 获得的(值在 0 到 360 度之间),MAX_Distance 是 1.5Km。
现在,如果我想知道 31 个 locationSample 点中的一个是否在多边形附近,我必须遍历 Realm 数据库中当前拥有的所有多边形坐标 31 次。意思是: 31 * 352241 = 10919471
这非常慢,但我似乎真的找不到更好的解决方案。任何人都知道如何更好/更快地做到这一点?
更新 1 目前我正在像这样遍历坐标:
for(RealmMultiPolygon rmp : area.avalanche.multiPolygon) {
if(rmp.timeOfDay == 2) {
for (RealmPolygon polygon : rmp.realmPolygons) {
for(LatLng sample : locationSamples) {
tempPoint = new LatLng(polygon.coordinates.first().latitude, polygon.coordinates.first().longitude);
if(SphericalUtil.computeDistanceBetween(tempPoint, sample) <= 500) {
coords.add(tempPoint);
} else {
break;
}
}
}
}
}
最佳答案
我最终通过提出一个完全不同的解决方案解决了这个问题。我现在正在根据多边形所在区域的宽度和高度创建图像作为后台任务。然后我根据拍摄的位置样本计算像素位置,并使用 getPixel 方法获取该位置上的像素。这比遍历每个多边形要快得多。
关于android - Realm Android 和具有多面计算的大数据集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38609395/