Ejemplo n.º 1
0
 public static BigFloatArray newInstance(int maxDoc) {
   BigFloatArray array = new BigFloatArray(maxDoc);
   array.ensureCapacity(maxDoc);
   return array;
 }
Ejemplo n.º 2
0
    public void load(String latFieldName, String lonFieldName, BoboIndexReader reader)
        throws IOException {
      if (reader == null) throw new NullPointerException("reader object is null");
      if (latFieldName == null) throw new NullPointerException("latitude Field Name is null");
      if (lonFieldName == null) throw new NullPointerException("longitude Field Name is null");

      String latField = latFieldName.intern();
      String lonField = lonFieldName.intern();
      int maxDoc = reader.maxDoc();

      BigFloatArray xVals = this._xValArray;
      BigFloatArray yVals = this._yValArray;
      BigFloatArray zVals = this._zValArray;

      if (xVals == null) xVals = newInstance(maxDoc);
      else xVals.ensureCapacity(maxDoc);
      if (yVals == null) yVals = newInstance(maxDoc);
      else yVals.ensureCapacity(maxDoc);
      if (zVals == null) zVals = newInstance(maxDoc);
      else zVals.ensureCapacity(maxDoc);

      this._xValArray = xVals;
      this._yValArray = yVals;
      this._zValArray = zVals;

      Term latTerm = new Term(latFieldName, "");
      TermDocs termDocs = reader.termDocs(latTerm);
      TermEnum termEnum = reader.terms(latTerm);

      float docLat, docLon;
      int termCount = 1;
      String lonValue = null;
      int length = maxDoc + 1;
      int doc;
      termDocs.next();
      try {
        do {
          Term term = termEnum.term();
          if (term == null || term.field() != latFieldName) continue;

          if (termCount > xVals.capacity())
            throw new IOException("Maximum number of values cannot exceed: " + xVals.capacity());
          if (termCount >= length)
            throw new RuntimeException(
                "There are more terms than documents in field "
                    + latFieldName
                    + " or "
                    + lonFieldName
                    + ", but its impossible to sort on tokenized fields");

          // pull the termDocs to point to the document for the current term in the termEnum
          termDocs.seek(termEnum);
          while (termDocs.next()) {
            doc = termDocs.doc();

            // read the latitude value in the current document
            docLat = Float.parseFloat(term.text().trim());
            // read the longitude value in the current document
            Document docVal = reader.document(doc, null);
            lonValue = docVal.get(lonFieldName);
            if (lonValue == null) continue;
            else docLon = Float.parseFloat(lonValue);

            // convert the lat, lon values to x,y,z coordinates
            float[] coords = GeoMatchUtil.geoMatchCoordsFromDegrees(docLat, docLon);
            _xValArray.add(doc, coords[0]);
            _yValArray.add(doc, coords[1]);
            _zValArray.add(doc, coords[2]);
          }
        } while (termEnum.next());
      } catch (Exception e) {
        // TODO: get rid of this catch phrase
        e.printStackTrace();
      } finally {
        if (termDocs != null) termDocs.close();
        if (termEnum != null) termEnum.close();
      }
    }