import pandas as pd
import geohash
import time
import itertools
import numpy as np
import random 

'''
This module is meant to be used in conjunction with pipekml however I could see how it could have other uses.

Module: pipegeohash.py

Purpose: A simple tool for geohashing an entire table at once,and allowing you to update your both corresponding tables simultaneously

Functions to be used:
1) map_table(csvfile,presicion)-given a csv file name and a presicion from (1 to 8) return the coresponding geohash
2) df2list(df)-takes a dataframe to a list
3) list2df(list)-takes a list and turn it into DataFrame

Created by: Bennett Murphy
email: murphy214@marshall.edu
'''

#takes a dataframe and turns it into a list
def df2list(df):
    df = [df.columns.values.tolist()]+df.values.tolist()
    return df


#takes a list and turns it into a datafrae
def list2df(df):
    df = pd.DataFrame(df[1:], columns=df[0])
    return df

# encoding the geohash string containing arguments into a function that will be mapped
def my_encode(argstring):
	argstring = str.split(argstring,'_')
	lat,long,precision = float(argstring[0]),float(argstring[1]),int(argstring[2])
	try:
		hash = geohash.encode(lat,long,precision)
	except:
		hash = ''
	return hash

# getting lat and long headers
def get_latlong_headers(headers):
	for row in headers:
		if 'lat' in str(row).lower():
			latheader = row
		elif 'lon' in str(row).lower():
			longheader = row
	return [latheader,longheader]


# given a dataframe and a list of columnsn 
# drops columns from df
def drop_columns(table,columns):
	list = []
	count = 0
	for row in columns:
		columnrow = row
		for row in table.columns.values.tolist():
			if columnrow == row:
				list.append(count)
			count += 1

	table.drop(table.columns[list], axis=1, inplace=True)	
	return table
# function making the geohash string and returning the table with an 
# appropriate geohash column 
def geohash_table(data,latlongheaders,precision):