Skip to content

Instantly share code, notes, and snippets.

@kuwapa
kuwapa / dialog_nps.xml
Created April 3, 2020 04:05
How to create an transparent alert dialog at bottom of screen with icon sticking out. Like this - https://imgur.com/a/PcEOEbJ
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:gravity="center"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
@kuwapa
kuwapa / bright_style.json
Last active August 18, 2021 16:24
How to use local MBTiles in Maplibre
{
"version": 8,
"name": "Bright",
"metadata": {
"mapbox:autocomposite": false,
"mapbox:groups": {
"1444849242106.713": {
"collapsed": false,
"name": "Places"
},
@kuwapa
kuwapa / style.json
Last active February 13, 2023 08:17
"sources": {
"openmaptiles": {
"type": "vector",
"url": "mbtiles:///data/user/0/com.example.maplibretest/cache/india_coimbatore.mbtiles"
}
}
//Fetching the local styles JSON file from the assets
val styleJsonInputStream = assets.open("bright.json")
//Creating a new file to which to copy the json content to
val dir = File(filesDir.absolutePath)
val styleFile = File(dir, "bright.json")
//Copying the original JSON content to new file
copyStreamToFile(styleJsonInputStream, styleFile)
//Getting reference to mbtiles file in assets
country region bbox
Afghanistan Badakhshan 35.44241059007663,69.99854943181077,38.47367340200013,74.89230676300008
Afghanistan Badghis 34.5093667668628,62.651762729566315,36.0317323437726,65.04732710206406
Afghanistan Baghlan 35.006700750838945,68.005234408641,36.571204739407165,69.9610323420689
Afghanistan Balkh 35.66725474700894,66.2478739774021,37.385212708,68.18909915610112
Afghanistan Bamyan 33.93792877938961,66.33060794540035,35.469618232295545,68.28268517398158
Afghanistan Farah 31.388741287780192,60.56150191200004,33.51668834119397,64.73840538909741
Afghanistan Faryab 35.18699982398266,63.88739790189163,37.241997468508316,65.8166858250907
Afghanistan Ghazni 32.07184113206256,66.8291821628265,34.20933340081575,68.80017296680893
Afghanistan Ghor 33.12257802995208,63.07075524305998,35.26973379198091,66.7740950866189
@kuwapa
kuwapa / create_bounds.kt
Last active October 17, 2021 10:17
Mapbox, Maplibre limit map to bounds code
fun MapboxMap.limitViewToBounds(bounds: LatLngBounds) {
val newBoundsHeight = bounds.latitudeSpan - projection.visibleRegion.latLngBounds.latitudeSpan
val newBoundsWidth = bounds.longitudeSpan - projection.visibleRegion.latLngBounds.longitudeSpan
val leftTopLatLng = LatLng(
bounds.latNorth - (bounds.latitudeSpan - newBoundsHeight) / 2,
bounds.lonEast - (bounds.longitudeSpan - newBoundsWidth) / 2 - newBoundsWidth,
)
@kuwapa
kuwapa / map.kt
Created October 16, 2021 20:29
restricting map to bounds. Gist #2
map.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 0),
object : MapboxMap.CancelableCallback {
override fun onCancel() {}
override fun onFinish() {
map.setMinZoomPreference(map.cameraPosition.zoom)
map.limitViewToBounds(bounds)
map.addOnScaleListener(object : MapboxMap.OnScaleListener {
@kuwapa
kuwapa / big_query_scheduled_export
Last active December 28, 2021 14:20
Scheduled script for BigQuery to automatically export data to Google Cloud Storage
-- This statement gets the date of 2 days back. Did this becasue if the script is running on 25th,
-- I'm not sure if the data for 24th is ready or not. It depends on what timezone the analytics server
-- is running off of and when do they consider 24th to be over. So 2 days period is safe
DECLARE backup_date DATE DEFAULT DATE_SUB(CURRENT_DATE(), INTERVAL 2 day);
DECLARE date_str STRING;
DECLARE table_name STRING;
DECLARE sql STRING;
DECLARE date_str_folder STRING;
DECLARE date_str_table STRING;
import math
def deg2num(lat_deg, lon_deg, zoom):
lat_rad = math.radians(lat_deg)
n = 2.0 ** zoom
xtile = int((lon_deg + 180.0) / 360.0 * n)
ytile = int((1.0 - math.asinh(math.tan(lat_rad)) / math.pi) / 2.0 * n)
return (xtile, ytile)
def getTotalTileCount(leftBottom, rightTop, fromZoom, toZoom):
totalTileCount = 0
for zoom in range(fromZoom, toZoom + 1):
leftBottomTiles = deg2num(leftBottom, zoom)
rightTopTiles = deg2num(rightTop, zoom)
currentTileCount = (rightTopTiles[0] - leftBottomTiles[0] + 1) * (leftBottomTiles[1] - rightTopTiles[1] + 1)
print("zoom = " + str(zoom) + ", leftBottomTiles = " + str(leftBottomTiles) +
", rightTopTiles = " + str(rightTopTiles) + ", tileCount = " + str(currentTileCount))