【发布时间】:2016-12-01 06:05:22
【问题描述】:
我正在解析一个 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;
}
}
}
}
}
【问题讨论】:
-
你可以让它并行
-
我必须循环遍历我目前在 Realm 数据库中的所有多边形坐标 31 次你是怎么做到的?
-
你能显示在领域和循环中执行查询的代码块吗?
-
什么是
area.avalanche.multiPolygon? -
@TimCastelijns 是包含 MultiPolygons 的 RealmList。
标签: android realm coordinates polygon large-data