All Downloads are FREE. Search and download functionalities are using the official Maven repository.

prerna.reactor.task.lambda.map.function.GoogleLatLongLambda Maven / Gradle / Ivy

The newest version!
package prerna.reactor.task.lambda.map.function;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.fasterxml.jackson.databind.ObjectMapper;

import prerna.engine.api.IHeadersDataRow;
import prerna.io.connector.google.GoogleLatLongGetter;
import prerna.om.GeoLocation;
import prerna.reactor.task.lambda.map.AbstractMapLambda;
import prerna.util.Constants;
import prerna.util.DIHelper;

public class GoogleLatLongLambda extends AbstractMapLambda {
	
	private static final Logger classLogger = LogManager.getLogger(GoogleLatLongLambda.class);

	// cahing of some results
	private static final String DIR_SEPARATOR = java.nio.file.FileSystems.getDefault().getSeparator();
	
	private static String cache_file_loc = null;
	private static Map> localcache = new HashMap>();

	static {
		String baseFolder = DIHelper.getInstance().getProperty(Constants.BASE_FOLDER);
		cache_file_loc = baseFolder + DIR_SEPARATOR + "geo" + DIR_SEPARATOR + "latlong.json";
		File f = new File(cache_file_loc);
		if(f.exists()) {
			Map> mapData = null;
			try {
				mapData = new ObjectMapper().readValue(f, Map.class);
			} catch (IOException e) {
				classLogger.error(Constants.STACKTRACE, e);
				// do noting
			}
			
			if(mapData != null) {
				localcache.putAll(mapData);
			}
		}
	}
	
	// col index we care about to get lat/long from
	private int colIndex;
	// total number of columns
	private int totalCols;
	
	@Override
	public IHeadersDataRow process(IHeadersDataRow row) {
		// construct new values to append onto the row
		// add new headers
		String[] newHeaders = new String[]{"lat", "long"};
		Object[] newValues = new Object[2];
					
		// grab the column index we want to use as the address
		String address = row.getValues()[colIndex].toString().toLowerCase().replace("_", " ");
		if(localcache.containsKey(address)) {
			List cacheValues = localcache.get(address);
			newValues[0] = cacheValues.get(0); 
			newValues[1] = cacheValues.get(1);
		} else {
			Hashtable params = new Hashtable();
			params.put("address", address);
			
			// add new values
			try {
				GoogleLatLongGetter goog = new GoogleLatLongGetter();
				// geo location object flushes the JSON return into something for getters and setters
				GeoLocation location = (GeoLocation) goog.execute(this.user, params);
				newValues[0] = location.getLatitude();
				newValues[1] = location.getLongitude();
				
				// cache it
				List cacheV = new Vector();
				cacheV.add((double) location.getLatitude());
				cacheV.add((double) location.getLongitude());
				localcache.put(address, cacheV);
				
//				File f = new File(cache_file_loc);
//				if(!f.getParentFile().exists()) {
//					f.getParentFile().mkdirs();
//				}
//				if(f.exists()) {
//					f.delete();
//				}
//				try {
//					Gson gson = new GsonBuilder().setPrettyPrinting().create();
//					// write json to file
//					FileUtils.writeStringToFile(f, gson.toJson(localcache));
//				} catch (IOException e1) {
//					classLogger.error(Constants.STACKTRACE, e1);
//				}
				
			} catch(Exception e) {
				classLogger.error(Constants.STACKTRACE, e);
			}
		}
		
		row.addFields(newHeaders, newValues);
		return row;
	}
	
	@Override
	public void init(List> headerInfo, List columns) {
		this.headerInfo = headerInfo;
		this.totalCols = headerInfo.size();
		
		String headerToConvert = columns.get(0);
		for(int j = 0; j < totalCols; j++) {
			Map headerMap = headerInfo.get(j);
			String alias = headerMap.get("alias").toString();
			if(alias.equals(headerToConvert)) {
				// we found the index
				this.colIndex = j;
			}
		}
		
		// this modifies the header info map by reference
		Map latHeader = getBaseHeader("lat", "NUMBER");
		this.headerInfo.add(latHeader);
		Map longHeader = getBaseHeader("long", "NUMBER");
		this.headerInfo.add(longHeader);
	}
	
	/**
	 * Grab a base header object
	 * @param name
	 * @param type
	 * @return
	 */
	private Map getBaseHeader(String name, String type) {
		Map header = new HashMap();
		header.put("alias", name);
		header.put("header", name);
		header.put("derived", true);
		header.put("type", type.toUpperCase());
		return header;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy