Android Open Source - Flight-Computer-Android-Flightradar24 Utf Grid Helper






From Project

Back to project page Flight-Computer-Android-Flightradar24.

License

The source code is released under:

GNU General Public License

If you think the Android project Flight-Computer-Android-Flightradar24 listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.nutiteq.utils;
//w  ww  .  jav  a2  s  . c  o m
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.InflaterInputStream;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


public class UtfGridHelper {

    public static final String TEMPLATED_FULL_KEY = "templated_full";
    public static final String TEMPLATED_TEASER_KEY = "templated_teaser";
    public static final String TEMPLATED_LOCATION_KEY = "templated_location";

    
    static byte[] ZLIB_HEADER = {(byte) 0x78, (byte) 0x9C};

    public static class MBTileUTFGrid {
        public String[] grid = null;
        public String[] keys = null;
        public JSONObject data = null;

      }
    
    public static MBTileUTFGrid decodeUtfGrid(byte[] gridBytes) throws IOException, JSONException {

        String gridJSON;
        
        if(gridBytes[0] == ZLIB_HEADER[0] && gridBytes[1] == ZLIB_HEADER[1]){
            // seems to be compressed with ZLIB
            InflaterInputStream in = new InflaterInputStream(
                    new ByteArrayInputStream(gridBytes));
            ByteArrayOutputStream inflatedOut = new ByteArrayOutputStream();
            int readLength;
            byte[] block = new byte[1024];
            while ((readLength = in.read(block)) != -1)
                inflatedOut.write(block, 0, readLength);
            inflatedOut.flush();
            gridJSON = new String(inflatedOut.toByteArray());
        }else{
            // uncompressed
            gridJSON = new String(gridBytes);
            
            // remove callback
            if(!gridJSON.startsWith("{")){
                gridJSON = gridJSON.substring(gridJSON.indexOf("(")+1,gridJSON.lastIndexOf(")"));
            }
        }
        
        MBTileUTFGrid grid = new MBTileUTFGrid();
        JSONObject root = new JSONObject(gridJSON);
        JSONArray gridA = root.getJSONArray("grid");
        JSONArray keysA = root.getJSONArray("keys");
        grid.grid = new String[gridA.length()];

        for (int i = 0; i < gridA.length(); i++) {
            grid.grid[i] = gridA.getString(i);
        }
        grid.keys = new String[keysA.length()];
        for (int i = 0; i < keysA.length(); i++) {
            grid.keys[i] = keysA.getString(i);
        }
        
        grid.data = root.optJSONObject("data");

        return grid;

    }
    
    /**
     * get clicked UTFgrid code within the tile. 
     * from https://github.com/mapbox/mbtiles-spec/blob/master/1.1/utfgrid.md "Mapping an ID to a key"
     * @param tileSize usually 256
     * @param clickedX 
     * @param clickedY
     * @param grid
     * @return
     */
    public static int utfGridCode(int tileSize, int clickedX, int clickedY,
            MBTileUTFGrid grid) {
        
        double factor = tileSize / grid.grid.length;
        int row = (int) (clickedY / factor);
        int col = (int) (clickedX / factor);
        int id = grid.grid[row].codePointAt(col);
        
        // decode id
        if(id >= 93) --id;
        if(id >= 35) --id;
        id -= 32;
        return id;
    }
}




Java Source Code List

com.flightcomputer.Circle.java
com.flightcomputer.DownloadActivity.java
com.flightcomputer.EditPreferences.java
com.flightcomputer.FCActivity.java
com.flightcomputer.GpsSkyActivity.java
com.flightcomputer.IgcLoad.java
com.flightcomputer.LeonardoLiveWriter.java
com.flightcomputer.MapEventListener.java
com.flightcomputer.PositionWriter.java
com.flightcomputer.PressureAltimeterActivity.java
com.flightcomputer.RouteLine.java
com.flightcomputer.Task.java
com.flightcomputer.TrckLine.java
com.flightcomputer.filefilter.FilterByFileExtension.java
com.flightcomputer.filefilter.ValidFileFilter.java
com.flightcomputer.filefilter.ValidMapFile.java
com.flightcomputer.filefilter.ValidRenderTheme.java
com.flightcomputer.filepicker.FilePickerIconAdapter.java
com.flightcomputer.filepicker.FilePicker.java
com.flightcomputer.utilities.BaroService.java
com.flightcomputer.utilities.BeepThread.java
com.flightcomputer.utilities.GpsSkyView.java
com.flightcomputer.utilities.KalmanFilter.java
com.flightcomputer.utilities.PiecewiseLinearFunction.java
com.flightcomputer.utilities.Point2d.java
com.flightcomputer.utilities.VerticalProgressBar_Reverse.java
com.flightcomputer.utilities.VerticalProgressBar.java
com.flightcomputer.openGlUtils.java
com.nutiteq.advancedmap.mapquest.SearchQueryResults.java
com.nutiteq.advancedmap.mapquest.SearchSuggestionProvider.java
com.nutiteq.services.geocode.MapQuestGeocoder.java
com.nutiteq.utils.MapsforgeMapLayer.java
com.nutiteq.utils.MbTilesDatabaseHelper.java
com.nutiteq.utils.MyLocationCircle.java
com.nutiteq.utils.NetUtils.java
com.nutiteq.utils.UiUtils.java
com.nutiteq.utils.UtfGridHelper.java
com.nutiteq.utils.UtfGridLayerInterface.java