Created
April 30, 2017 11:05
-
-
Save himanshunaidu/7a091ab910bbb6b9be4fd20e399a7ef7 to your computer and use it in GitHub Desktop.
An Android Application to Find Determine prescence of water bodies using data from NASA GRACE
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.hfad.spaceappswater; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.location.Address; | |
import android.location.Geocoder; | |
import android.location.Location; | |
import android.location.LocationListener; | |
import android.location.LocationManager; | |
import android.net.wifi.p2p.WifiP2pManager; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.view.*; | |
import android.widget.*; | |
import java.io.IOException; | |
import java.util.List; | |
import java.util.Locale; | |
public class MainActivity extends AppCompatActivity{ | |
private Button location_button = null; | |
private TextView location_view = null; | |
private Button water_button = null; | |
private TextView water_view = null; | |
private Spinner water_spinner = null; | |
private String latitude = ""; | |
private String longitude = ""; | |
private int ltd; | |
private int lng; | |
private static final String TAG = "MainActivity"; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
location_button = (Button)findViewById(R.id.location_button); | |
water_button = (Button)findViewById(R.id.water_button); | |
location_view = (TextView)findViewById(R.id.location_view); | |
} | |
private class LocListener implements LocationListener { | |
//String elocation = ""; | |
@Override | |
public void onLocationChanged(Location loc) { | |
String longitude = "Longitude: " + loc.getLongitude(); | |
double lng = loc.getLongitude(); | |
Log.v(TAG, longitude); | |
double ltd = loc.getLatitude(); | |
String latitude = "Latitude: " + loc.getLatitude(); | |
Log.v(TAG, latitude); | |
/*------- To get city name from coordinates -------- */ | |
String cityName = null; | |
Geocoder gcd = new Geocoder(getBaseContext(), Locale.getDefault()); | |
List<Address> addresses; | |
try { | |
addresses = gcd.getFromLocation(loc.getLatitude(), | |
loc.getLongitude(), 1); | |
if (addresses.size() > 0) { | |
System.out.println(addresses.get(0).getLocality()); | |
cityName = addresses.get(0).getLocality(); | |
} | |
} | |
catch (IOException e) { | |
e.printStackTrace(); | |
} | |
String s = longitude + "\n" + latitude + "\n\nMy Current City is: " | |
+ cityName; | |
location_view.setText(s); | |
} | |
@Override | |
public void onProviderDisabled(String provider) {} | |
@Override | |
public void onProviderEnabled(String provider) {} | |
@Override | |
public void onStatusChanged(String provider, int status, Bundle extras) {} | |
} | |
public void locationClick(View view){ | |
String text = "Checking Location..."; | |
int duration = Toast.LENGTH_LONG; | |
Toast toast = Toast.makeText(this, text, duration); | |
toast.show(); | |
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); | |
LocationListener locationListener = new LocListener(); | |
try{ | |
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000, 10, locationListener);} | |
catch (SecurityException e){ | |
Log.d(TAG, "Access Denied! Please contact our operator"); | |
} | |
} | |
public void waterClick(View view){ | |
if((latitude.equals(""))||(longitude.equals(""))){ | |
String text = "Error!!! Location not Checked!!!"; | |
int duration = Toast.LENGTH_SHORT; | |
Toast toast = Toast.makeText(this, text, duration); | |
} | |
else{ | |
Intent intent = new Intent(MainActivity.this, MapActivity.class); | |
intent.putExtra(MapActivity.EXTRA_LATITUDE, ltd); | |
intent.putExtra(MapActivity.EXTRA_LONGITUDE, lng); | |
startActivity(intent); | |
} | |
} | |
@Override | |
protected void onSaveInstanceState(Bundle savedInstanceState){ | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.hfad.spaceappswater; | |
import android.content.Intent; | |
import android.graphics.*; | |
import android.graphics.drawable.*; | |
import android.os.Bundle; | |
import android.support.v7.app.AppCompatActivity; | |
import android.widget.*; | |
import static java.lang.Math.*; | |
/** | |
* Created by Lenovo on 29/04/2017. | |
*/ | |
public class MapActivity extends AppCompatActivity { | |
public static final String EXTRA_LATITUDE = "Latitude"; | |
public static final String EXTRA_LONGITUDE = "Longitude"; | |
private TextView latView = null; | |
private TextView lonView = null; | |
private TextView resultView = null; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_map); | |
latView = (TextView)findViewById(R.id.lat_view); | |
lonView = (TextView)findViewById(R.id.lon_view); | |
Intent intent = getIntent(); | |
double latitude = intent.getDoubleExtra(EXTRA_LATITUDE, 0); | |
double longitude = intent.getDoubleExtra(EXTRA_LONGITUDE, 0); | |
latView.setText("Latitude : " + latitude); | |
lonView.setText("Longitude : " +longitude); | |
mapCoordinate(latitude, longitude); | |
} | |
public void mapCoordinate(double lat, double lon){ | |
int r1=0, g1=0, b1=0, r2=0, g2=0, b2=0, r3=0, g3=0, b3=0; | |
int nlat = (int)round(lat), nlon = (int)round(lon); | |
int x = 0, y = 0; | |
if(lat<0){ | |
y = 90-nlat; | |
} | |
else{ | |
y = 90-nlat; | |
} | |
if(lon<0){ | |
x = 360 + nlon; | |
} | |
else{ | |
x = nlon; | |
} | |
Bitmap bitmap1 = drawableToBitmap(getDrawable(R.drawable.image1)); | |
Bitmap bitmap2 = drawableToBitmap(getDrawable(R.drawable.image2)); | |
Bitmap bitmap3 = drawableToBitmap(getDrawable(R.drawable.image3)); | |
int pixel1 = bitmap1.getPixel(x, y); | |
int pixel2 = bitmap2.getPixel(x, y); | |
int pixel3 = bitmap3.getPixel(x, y); | |
r1 = Color.red(pixel1); | |
g1 = Color.green(pixel1); | |
b1 = Color.blue(pixel1); | |
r2 = Color.red(pixel2); | |
g2 = Color.green(pixel2); | |
b2 = Color.blue(pixel2); | |
r3 = Color.red(pixel3); | |
g3 = Color.green(pixel3); | |
b3 = Color.blue(pixel3); | |
findStatus(r1, g1, b1, r2, g2, b2, r3, g3, b3); | |
} | |
public static Bitmap drawableToBitmap (Drawable drawable) { | |
if (drawable instanceof BitmapDrawable) { | |
return ((BitmapDrawable)drawable).getBitmap(); | |
} | |
int width = drawable.getIntrinsicWidth(); | |
width = width > 0 ? width : 1; | |
int height = drawable.getIntrinsicHeight(); | |
height = height > 0 ? height : 1; | |
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); | |
Canvas canvas = new Canvas(bitmap); | |
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); | |
drawable.draw(canvas); | |
return bitmap; | |
} | |
public void findStatus(int r1, int g1, int b1, int r2, int g2, int b2, int r3, int g3, int b3){ | |
TextView resultView = (TextView)findViewById(R.id.result_view); | |
String result = ""; | |
boolean available = true; | |
int r21 = r2 - r1; | |
int b21 = b2 - b1; | |
int g21 = g2 - g1; | |
int r32 = r3 - r2; | |
int b32 = b3 - b2; | |
int g32 = g3 - g2; | |
int ar21 = abs(r21); | |
int ab21 = abs(b21); | |
int ag21 = abs(g21); | |
int ar32 = abs(r32); | |
int ab32 = abs(b32); | |
int ag32 = abs(g32); | |
if((ar21+ab21+ag21)<50){ | |
if((ar32+ab32+ag32)<50){ | |
result += "No water available\n"; | |
available = false; | |
} | |
else if((ar32+ab32+ag32)<100){ | |
result += "Water moderately available\n"; | |
} | |
else{ | |
result += "Water available\n"; | |
} | |
} | |
else if((ar21+ab21+ag21)<100){ | |
result+= "Water moderately available\n"; | |
} | |
else{ | |
result += "Water available\n"; | |
} | |
if(available==true){ | |
if(r21>0 && b21<0){ | |
result += "Water level has decreased significantly"; | |
} | |
else if(r21<0 && b21>0){ | |
result += "Water level has increased significantly"; | |
} | |
else if(r21<0 && b21<0){ | |
if(ar21>b21){ | |
result+= "Water level has increased slightly"; | |
} | |
else{ | |
result+= "Water level has decreased slightly"; | |
} | |
} | |
else if(r21>0 && b21>0){ | |
if(ar21>b21){ | |
result+= "Water level has decreased slightly"; | |
} | |
else{ | |
result+= "Water level has increased slightly"; | |
} | |
} | |
else{ | |
result+= "Further analysis has to be done"; | |
} | |
} | |
resultView.setText(result); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment