Ejemplo n.º 1
0
  /*
   * (non-Javadoc)
   * @see org.oscim.map.reader.IMapDatabase#executeQuery(org.oscim.core.Tile,
   * org.oscim.map.reader.MapDatabaseCallback)
   */
  @Override
  public QueryResult executeQuery(JobTile tile, IMapDatabaseCallback mapDatabaseCallback) {
    if (sMapFileHeader == null) return QueryResult.FAILED;

    if (mIntBuffer == null) mIntBuffer = new int[MAXIMUM_WAY_NODES_SEQUENCE_LENGTH * 2];

    mWayNodePosition = 0;

    try {
      // prepareExecution();
      QueryParameters queryParameters = new QueryParameters();
      queryParameters.queryZoomLevel = sMapFileHeader.getQueryZoomLevel(tile.zoomLevel);
      // get and check the sub-file for the query zoom level
      SubFileParameter subFileParameter =
          sMapFileHeader.getSubFileParameter(queryParameters.queryZoomLevel);
      if (subFileParameter == null) {
        LOG.warning("no sub-file for zoom level: " + queryParameters.queryZoomLevel);
        return QueryResult.FAILED;
      }

      QueryCalculations.calculateBaseTiles(queryParameters, tile, subFileParameter);
      QueryCalculations.calculateBlocks(queryParameters, subFileParameter);
      processBlocks(mapDatabaseCallback, queryParameters, subFileParameter);
    } catch (IOException e) {
      LOG.log(Level.SEVERE, null, e);
      return QueryResult.FAILED;
    }
    return QueryResult.SUCCESS;
  }
Ejemplo n.º 2
0
  private int[][] readZoomTable(SubFileParameter subFileParameter) {
    int rows = subFileParameter.zoomLevelMax - subFileParameter.zoomLevelMin + 1;
    int[][] zoomTable = new int[rows][2];

    int cumulatedNumberOfPois = 0;
    int cumulatedNumberOfWays = 0;

    for (int row = 0; row < rows; ++row) {
      cumulatedNumberOfPois += mReadBuffer.readUnsignedInt();
      cumulatedNumberOfWays += mReadBuffer.readUnsignedInt();

      if (cumulatedNumberOfPois < 0 || cumulatedNumberOfPois > MAXIMUM_ZOOM_TABLE_OBJECTS) {
        LOG.warning("invalid cumulated number of POIs in row " + row + ' ' + cumulatedNumberOfPois);
        if (mDebugFile) {
          LOG.warning(DEBUG_SIGNATURE_BLOCK + mSignatureBlock);
        }
        return null;
      } else if (cumulatedNumberOfWays < 0 || cumulatedNumberOfWays > MAXIMUM_ZOOM_TABLE_OBJECTS) {
        LOG.warning("invalid cumulated number of ways in row " + row + ' ' + cumulatedNumberOfWays);
        if (sMapFileHeader.getMapFileInfo().debugFile) {
          LOG.warning(DEBUG_SIGNATURE_BLOCK + mSignatureBlock);
        }
        return null;
      }

      zoomTable[row][0] = cumulatedNumberOfPois;
      zoomTable[row][1] = cumulatedNumberOfWays;
    }

    return zoomTable;
  }
Ejemplo n.º 3
0
 /*
  * (non-Javadoc)
  * @see org.oscim.map.reader.IMapDatabase#getMapFileInfo()
  */
 @Override
 public MapFileInfo getMapInfo() {
   if (sMapFileHeader == null) {
     throw new IllegalStateException("no map file is currently opened");
   }
   return sMapFileHeader.getMapFileInfo();
 }
Ejemplo n.º 4
0
  /**
   * Processes the given number of ways.
   *
   * @param queryParameters the parameters of the current query.
   * @param mapDatabaseCallback the callback which handles the extracted ways.
   * @param numberOfWays how many ways should be processed.
   * @return true if the ways could be processed successfully, false otherwise.
   */
  private boolean processWays(
      QueryParameters queryParameters, IMapDatabaseCallback mapDatabaseCallback, int numberOfWays) {

    Tag[] tags = null;
    Tag[] wayTags = sMapFileHeader.getMapFileInfo().wayTags;
    int[] textPos = new int[3];
    // float[] labelPosition;
    boolean skippedWays = false;
    int wayDataBlocks;

    // skip string block
    int stringsSize = mReadBuffer.readUnsignedInt();
    stringOffset = mReadBuffer.getBufferPosition();
    mReadBuffer.skipBytes(stringsSize);

    for (int elementCounter = numberOfWays; elementCounter != 0; --elementCounter) {
      if (mDebugFile) {
        // get and check the way signature
        mSignatureWay = mReadBuffer.readUTF8EncodedString(SIGNATURE_LENGTH_WAY);
        if (!mSignatureWay.startsWith("---WayStart")) {
          LOG.warning("invalid way signature: " + mSignatureWay);
          LOG.warning(DEBUG_SIGNATURE_BLOCK + mSignatureBlock);
          return false;
        }
      }

      if (queryParameters.useTileBitmask) {
        elementCounter = mReadBuffer.skipWays(queryParameters.queryTileBitmask, elementCounter);

        if (elementCounter == 0) return true;

        if (elementCounter < 0) return false;

        if (mReadBuffer.lastTagPosition > 0) {
          int pos = mReadBuffer.getBufferPosition();
          mReadBuffer.setBufferPosition(mReadBuffer.lastTagPosition);

          byte numberOfTags = (byte) (mReadBuffer.readByte() & WAY_NUMBER_OF_TAGS_BITMASK);

          tags = mReadBuffer.readTags(wayTags, numberOfTags);
          if (tags == null) return false;

          skippedWays = true;

          mReadBuffer.setBufferPosition(pos);
        }
      } else {
        int wayDataSize = mReadBuffer.readUnsignedInt();
        if (wayDataSize < 0) {
          LOG.warning("invalid way data size: " + wayDataSize);
          if (mDebugFile) {
            LOG.warning(DEBUG_SIGNATURE_BLOCK + mSignatureBlock);
          }
          LOG.warning("EEEEEK way... 2");
          return false;
        }

        // ignore the way tile bitmask (2 bytes)
        mReadBuffer.skipBytes(2);
      }

      // get the special byte which encodes multiple flags
      byte specialByte = mReadBuffer.readByte();

      // bit 1-4 represent the layer
      byte layer = (byte) ((specialByte & WAY_LAYER_BITMASK) >>> WAY_LAYER_SHIFT);
      // bit 5-8 represent the number of tag IDs
      byte numberOfTags = (byte) (specialByte & WAY_NUMBER_OF_TAGS_BITMASK);

      boolean changed = skippedWays;
      skippedWays = false;

      if (numberOfTags != 0) {
        tags = mReadBuffer.readTags(wayTags, numberOfTags);
        changed = true;
      }
      if (tags == null) return false;

      // get the feature bitmask (1 byte)
      byte featureByte = mReadBuffer.readByte();

      // bit 1-6 enable optional features
      boolean featureWayDoubleDeltaEncoding =
          (featureByte & WAY_FEATURE_DOUBLE_DELTA_ENCODING) != 0;

      // check if the way has a name
      if ((featureByte & WAY_FEATURE_NAME) != 0) {
        textPos[0] = mReadBuffer.readUnsignedInt();
        String str = mReadBuffer.readUTF8EncodedStringAt(stringOffset + textPos[0]);
        // if (changed) {
        // Tag[] tmp = tags;
        // tags = new Tag[tmp.length + 1];
        // System.arraycopy(tmp, 0, tags, 0, tmp.length);
        // }
        // tags[tags.length - 1] = new Tag(Tag.TAG_KEY_NAME, str,
        // false);
      } else textPos[0] = -1;

      // check if the way has a house number
      if ((featureByte & WAY_FEATURE_HOUSE_NUMBER) != 0) {
        textPos[1] = mReadBuffer.readUnsignedInt();

      } else textPos[1] = -1;

      // check if the way has a reference
      if ((featureByte & WAY_FEATURE_REF) != 0) textPos[2] = mReadBuffer.readUnsignedInt();
      else textPos[2] = -1;

      if ((featureByte & WAY_FEATURE_LABEL_POSITION) != 0)
        // labelPosition =
        readOptionalLabelPosition();
      // else
      // labelPosition = null;

      if ((featureByte & WAY_FEATURE_DATA_BLOCKS_BYTE) != 0) {
        wayDataBlocks = mReadBuffer.readUnsignedInt();

        if (wayDataBlocks < 1) {
          LOG.warning("invalid number of way data blocks: " + wayDataBlocks);
          logDebugSignatures();
          return false;
        }
      } else {
        wayDataBlocks = 1;
      }

      for (int wayDataBlock = 0; wayDataBlock < wayDataBlocks; ++wayDataBlock) {
        short[] wayLengths = processWayDataBlock(featureWayDoubleDeltaEncoding);
        if (wayLengths == null) return false;

        // wayDataContainer.textPos = textPos;
        int l = wayLengths[0];

        boolean closed = mWayNodes[0] == mWayNodes[l - 2] && mWayNodes[1] == mWayNodes[l - 1];

        mapDatabaseCallback.renderWay(layer, tags, mWayNodes, wayLengths, closed);
      }
    }

    return true;
  }
Ejemplo n.º 5
0
  /**
   * Processes the given number of POIs.
   *
   * @param mapDatabaseCallback the callback which handles the extracted POIs.
   * @param numberOfPois how many POIs should be processed.
   * @return true if the POIs could be processed successfully, false otherwise.
   */
  private boolean processPOIs(IMapDatabaseCallback mapDatabaseCallback, int numberOfPois) {
    Tag[] poiTags = sMapFileHeader.getMapFileInfo().poiTags;
    Tag[] tags = null;

    for (int elementCounter = numberOfPois; elementCounter != 0; --elementCounter) {
      if (mDebugFile) {
        // get and check the POI signature
        mSignaturePoi = mReadBuffer.readUTF8EncodedString(SIGNATURE_LENGTH_POI);
        if (!mSignaturePoi.startsWith("***POIStart")) {
          LOG.warning("invalid POI signature: " + mSignaturePoi);
          LOG.warning(DEBUG_SIGNATURE_BLOCK + mSignatureBlock);
          return false;
        }
      }

      // Log.d("MapDatabase", "read POI");

      // get the POI latitude offset (VBE-S)
      int latitude = mTileLatitude + mReadBuffer.readSignedInt();

      // get the POI longitude offset (VBE-S)
      int longitude = mTileLongitude + mReadBuffer.readSignedInt();

      // get the special byte which encodes multiple flags
      byte specialByte = mReadBuffer.readByte();

      // bit 1-4 represent the layer
      byte layer = (byte) ((specialByte & POI_LAYER_BITMASK) >>> POI_LAYER_SHIFT);
      // bit 5-8 represent the number of tag IDs
      byte numberOfTags = (byte) (specialByte & POI_NUMBER_OF_TAGS_BITMASK);

      // boolean changed = false;

      if (numberOfTags != 0) {
        tags = mReadBuffer.readTags(poiTags, numberOfTags);
        // changed = true;
      }
      if (tags == null) return false;

      // get the feature bitmask (1 byte)
      byte featureByte = mReadBuffer.readByte();

      // bit 1-3 enable optional features

      // check if the POI has a name
      if ((featureByte & POI_FEATURE_NAME) != 0) {
        // int pos = mReadBuffer.getPositionAndSkip();
        String str = mReadBuffer.readUTF8EncodedString();

        Tag[] tmp = tags;
        tags = new Tag[tmp.length + 1];
        System.arraycopy(tmp, 0, tags, 0, tmp.length);
        tags[tags.length - 1] = new Tag("name", str, false);
      }

      // check if the POI has a house number
      if ((featureByte & POI_FEATURE_HOUSE_NUMBER) != 0) {
        // mReadBuffer.getPositionAndSkip();
        // String str =
        mReadBuffer.readUTF8EncodedString();
      }

      // check if the POI has an elevation
      if ((featureByte & POI_FEATURE_ELEVATION) != 0) {
        mReadBuffer.readSignedInt();
        // mReadBuffer.getPositionAndSkip();// tags.add(new
        // Tag(Tag.TAG_KEY_ELE,
        // Integer.toString(mReadBuffer.readSignedInt())));
      }

      mapDatabaseCallback.renderPointOfInterest(layer, tags, latitude, longitude);
    }

    return true;
  }
Ejemplo n.º 6
0
  /*
   * (non-Javadoc)
   * @see org.oscim.map.reader.IMapDatabase#openFile(java.io.File)
   */
  @Override
  public OpenResult open(Map<String, String> options) {
    // if (options == null) {
    // options = new HashMap<String, String>(1);
    // options.put("mapfile", "/sdcard/bremen.map");
    // }
    try {
      // if (options == null || options.get("mapfile") == null) {
      // // throw new
      // // IllegalArgumentException("mapFile must not be null");
      // return new OpenResult("no file!");
      // }

      // make sure to close any previously opened file first
      close();

      File file = new File(Environment.getExternalStorageDirectory().getPath() + "/bremen.map");

      System.out.println(
          "load "
              + file
              + " "
              + (Environment.getExternalStorageDirectory().getPath() + "/bremen.map"));

      // File file = new File(options.get("mapfile"));

      // check if the file exists and is readable
      if (!file.exists()) {
        return new OpenResult("file does not exist: " + file);
      } else if (!file.isFile()) {
        return new OpenResult("not a file: " + file);
      } else if (!file.canRead()) {
        return new OpenResult("cannot read file: " + file);
      }

      // open the file in read only mode
      mInputFile = new RandomAccessFile(file, READ_ONLY_MODE);
      mFileSize = mInputFile.length();
      mReadBuffer = new ReadBuffer(mInputFile);

      if (instances > 0) {
        instances++;
        return OpenResult.SUCCESS;
      }

      sMapFileHeader = new MapFileHeader();
      OpenResult openResult = sMapFileHeader.readHeader(mReadBuffer, mFileSize);
      if (!openResult.isSuccess()) {
        close();
        return openResult;
      }

      prepareExecution();

      instances++;

      return OpenResult.SUCCESS;
    } catch (IOException e) {
      LOG.log(Level.SEVERE, null, e);
      // make sure that the file is closed
      close();
      return new OpenResult(e.getMessage());
    }
  }