Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View patrickyin's full-sized avatar

Patrick Yin patrickyin

View GitHub Profile
@patrickyin
patrickyin / TilePosition.java
Created August 26, 2017 12:02
TilePosition
public TilePosition getTilePosition(float zoomLevel, double latitude, double longitude) {
int tileZoom = round(zoomLevel);
int tileX = (int) ((longitude + 180.0) / 360.0 * pow(2.0, tileZoom));
int tileY = (int)((1.0 - log(tan(latitude * PI / 180.0) + 1.0 / cos(latitude * PI / 180.0)) / PI) / 2.0 * pow(2.0, tileZoom));
tileY = (int) (pow(2.0, tileZoom) - tileY - 1.0); // flip Y, this is only for mbtiles file
return new TilePosition(tileZoom, tileX, tileY);
}
@patrickyin
patrickyin / TilePosition.java
Created August 26, 2017 12:05
TilePosition class
public class TilePosition extends Object{
private int zoom;
private int x;
private int y;
public TilePosition(int zoom, int x, int y) {
this.zoom = zoom;
this.x = x;
this.y = y;
}
public int getZoom() {
@patrickyin
patrickyin / TilePoistion.java
Created August 26, 2017 12:07
getGridBLOBData
private byte[] getGridBLOBData(TilePosition tilePosition) {
String[] columns = { "grid" };
String[] args = { Integer.toString(tilePosition.getZoom()), Integer.toString(tilePosition.getX()), Integer.toString(tilePosition.getY()) };
Cursor cursor = this.db.query("grids", columns, "zoom_level=? AND tile_column=? AND tile_row=?", args, null, null, null);
if (cursor == null || cursor.getCount() == 0)
return null;
cursor.moveToFirst();
byte[] data = cursor.getBlob(0);
cursor.close();
return data;
@patrickyin
patrickyin / Decompress.java
Created August 26, 2017 12:09
decompress
private byte[] decompress(byte[] compressedData)
{
if(compressedData == null) return null;
Inflater inflater = new Inflater();
inflater.setInput(compressedData);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(compressedData.length);
byte[] buffer = new byte[1024];
while (!inflater.finished()) {
int count = 0;
try {