2016-07-27 62 views
2

我正在解析一個Json文件,其中包含很多多面體到Realm。我已經設定,方式如下:Realm Android和具有多面計算的大型數據集

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個locationsamples與軸承這樣的:

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.5公里。

現在,如果我想知道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; 
          } 
         } 
        } 
       } 
      } 
+1

你可以使它平行 –

+0

*我必須遍歷我目前在我的Realm數據庫中所有的多邊形座標31次*你如何做到這一點? –

+0

你可以顯示在領域和循環中執行查詢的代碼塊嗎? –

回答

1

我終於通過想出一個完全不同的解決方案解決了這個問題。我現在基於多邊形所在區域的寬度和高度創建一個圖像作爲後臺任務。然後,我根據所拍攝的位置數據計算像素位置,並使用getPixel方法獲取該位置上的像素。這比循環遍歷每個多邊形要快。