Commit 2458ea52 authored by 黄贵华's avatar 黄贵华

udf

parent b817ecf4
package cn.com.duiba.udaf; package cn.com.duiba.udf;
import cn.com.duiba.utils.GeoHashUtil; import cn.com.duiba.utils.GeoHashUtil;
//import org.apache.hadoop.hive.ql.exec.UDAF;
import org.apache.hadoop.hive.ql.exec.UDF; import org.apache.hadoop.hive.ql.exec.UDF;
public class GeoHashUDAF extends UDF { public class GeoHashUDF extends UDF {
public String evaluate(String gps_longitude, String gps_latitude,int hashLength) { public String evaluate(String gps_longitude, String gps_latitude,int hashLength) {
try { try {
return GeoHashUtil.evaluate(gps_longitude, gps_latitude, hashLength); return GeoHashUtil.evaluate(gps_longitude, gps_latitude, hashLength);
......
package cn.com.duiba.udaf; package cn.com.duiba.udf;
import cn.com.duiba.utils.Wgs84ToBd09Util; import cn.com.duiba.utils.Wgs84ToBd09Util;
import org.apache.hadoop.hive.ql.exec.UDF; import org.apache.hadoop.hive.ql.exec.UDF;
public class Wgs84ToBd09UDAF extends UDF { public class Wgs84ToBd09UDF extends UDF {
public String evaluate(String gps_longitude, String gps_latitude) { public String evaluate(String gps_longitude, String gps_latitude) {
try { try {
return Wgs84ToBd09Util.evaluate(gps_longitude, gps_latitude); return Wgs84ToBd09Util.evaluate(gps_longitude, gps_latitude);
......
package cn.com.duiba.utils; package cn.com.duiba.utils;
import org.apache.commons.lang3.StringUtils;
import java.util.*;
public class GeoHashUtil { public class GeoHashUtil {
static double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
static double pi = 3.1415926535897932384626; // π
static double a = 6378245.0; //长半轴
static double ee = 0.00669342162296594323; //偏心率平方
private LocationBean location;
/**
* 1 2500km;2 630km;3 78km;4 30km
* 5 2.4km; 6 610m; 7 76m; 8 19m
*/
private int hashLength = 8; //经纬度转化为geohash长度
private int latLength = 20; //纬度转化为二进制长度
private int lngLength = 20; //经度转化为二进制长度
private double minLat;//每格纬度的单位大小
private double minLng;//每个经度的倒下
private static final char[] CHARS = {'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm', 'n',
'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
private static int numbits = 6 * 5;
final static HashMap<Character, Integer> lookup = new HashMap();
static {
int i = 0;
for (char c : CHARS)
lookup.put(c, i++);
}
public GeoHashUtil() {
super();
}
public GeoHashUtil(double lat, double lng) {
location = new LocationBean(lat, lng);
setMinLatLng();
}
public static String evaluate(String gps_longitude, String gps_latitude,int hashLength) {
try {
if (StringUtils.isEmpty(gps_longitude) || StringUtils.isEmpty(gps_latitude)) {
return "";
}
Double lng = Double.valueOf(gps_longitude);
Double lat = Double.valueOf(gps_latitude);
double[] wgs84_to_gcj02 = Wgs84ToBd09Util.wgs84_to_gcj02(lng, lat);
double[] gcj02_to_bd09 = Wgs84ToBd09Util.gcj02_to_bd09(wgs84_to_gcj02[0], wgs84_to_gcj02[1]);
String geohash = encode(gcj02_to_bd09[1], gcj02_to_bd09[0]);
return geohash.substring(0,hashLength);
} catch (Exception e) {
return "";
}
}
public int gethashLength() {
return hashLength;
}
/**
* @Author:lulei
* @Description: 设置经纬度的最小单位
*/
private void setMinLatLng() {
minLat = LocationBean.MAXLAT - LocationBean.MINLAT;
for (int i = 0; i < latLength; i++) {
minLat /= 2.0;
}
minLng = LocationBean.MAXLNG - LocationBean.MINLNG;
for (int i = 0; i < lngLength; i++) {
minLng /= 2.0;
}
}
/**
* @return
* @Author:lulei
* @Description: 求所在坐标点及周围点组成的九个
*/
public List<String> getGeoHashBase32For9() {
double leftLat = location.getLat() - minLat;
double rightLat = location.getLat() + minLat;
double upLng = location.getLng() - minLng;
double downLng = location.getLng() + minLng;
List<String> base32For9 = new ArrayList<String>();
//左侧从上到下 3个
String leftUp = getGeoHashBase32(leftLat, upLng);
if (!(leftUp == null || "".equals(leftUp))) {
base32For9.add(leftUp);
}
String leftMid = getGeoHashBase32(leftLat, location.getLng());
if (!(leftMid == null || "".equals(leftMid))) {
base32For9.add(leftMid);
}
String leftDown = getGeoHashBase32(leftLat, downLng);
if (!(leftDown == null || "".equals(leftDown))) {
base32For9.add(leftDown);
}
//中间从上到下 3个
String midUp = getGeoHashBase32(location.getLat(), upLng);
if (!(midUp == null || "".equals(midUp))) {
base32For9.add(midUp);
}
String midMid = getGeoHashBase32(location.getLat(), location.getLng());
if (!(midMid == null || "".equals(midMid))) {
base32For9.add(midMid);
}
String midDown = getGeoHashBase32(location.getLat(), downLng);
if (!(midDown == null || "".equals(midDown))) {
base32For9.add(midDown);
}
//右侧从上到下 3个
String rightUp = getGeoHashBase32(rightLat, upLng);
if (!(rightUp == null || "".equals(rightUp))) {
base32For9.add(rightUp);
}
String rightMid = getGeoHashBase32(rightLat, location.getLng());
if (!(rightMid == null || "".equals(rightMid))) {
base32For9.add(rightMid);
}
String rightDown = getGeoHashBase32(rightLat, downLng);
if (!(rightDown == null || "".equals(rightDown))) {
base32For9.add(rightDown);
}
return base32For9;
}
/**
* @param length
* @return
* @Author:lulei
* @Description: 设置经纬度转化为geohash长度
*/
public boolean sethashLength(int length) {
if (length < 1) {
return false;
}
hashLength = length;
latLength = (length * 5) / 2;
if (length % 2 == 0) {
lngLength = latLength;
} else {
lngLength = latLength + 1;
}
setMinLatLng();
return true;
}
/**
* @return
* @Author:lulei
* @Description: 获取经纬度的base32字符串
*/
public String getGeoHashBase32() {
return getGeoHashBase32(location.getLat(), location.getLng());
}
/**
* @param lat
* @param lng
* @return
* @Author:lulei
* @Description: 获取经纬度的base32字符串
*/
private String getGeoHashBase32(double lat, double lng) {
boolean[] bools = getGeoBinary(lat, lng);
if (bools == null) {
return null;
}
StringBuffer sb = new StringBuffer();
for (int i = 0; i < bools.length; i = i + 5) {
boolean[] base32 = new boolean[5];
for (int j = 0; j < 5; j++) {
base32[j] = bools[i + j];
}
char cha = getBase32Char(base32);
if (' ' == cha) {
return null;
}
sb.append(cha);
}
return sb.toString();
}
/**
* @param base32
* @return
* @Author:lulei
* @Description: 将五位二进制转化为base32
*/
private char getBase32Char(boolean[] base32) {
if (base32 == null || base32.length != 5) {
return ' ';
}
int num = 0;
for (boolean bool : base32) {
num <<= 1;
if (bool) {
num += 1;
}
}
return CHARS[num % CHARS.length];
}
/**
* @param lat
* @param lng
* @return
* @Author:lulei
* @Description: 获取坐标的geo二进制字符串
*/
private boolean[] getGeoBinary(double lat, double lng) {
boolean[] latArray = getHashArray(lat, LocationBean.MINLAT, LocationBean.MAXLAT, latLength);
boolean[] lngArray = getHashArray(lng, LocationBean.MINLNG, LocationBean.MAXLNG, lngLength);
return merge(latArray, lngArray);
}
/**
* @param latArray
* @param lngArray
* @return
* @Author:lulei
* @Description: 合并经纬度二进制
*/
private boolean[] merge(boolean[] latArray, boolean[] lngArray) {
if (latArray == null || lngArray == null) {
return null;
}
boolean[] result = new boolean[lngArray.length + latArray.length];
Arrays.fill(result, false);
for (int i = 0; i < lngArray.length; i++) {
result[2 * i] = lngArray[i];
}
for (int i = 0; i < latArray.length; i++) {
result[2 * i + 1] = latArray[i];
}
return result;
}
/**
* @param value
* @param min
* @param max
* @return
* @Author:lulei
* @Description: 将数字转化为geohash二进制字符串
*/
private boolean[] getHashArray(double value, double min, double max, int length) {
if (value < min || value > max) {
return null;
}
if (length < 1) {
return null;
}
boolean[] result = new boolean[length];
for (int i = 0; i < length; i++) {
double mid = (min + max) / 2.0;
if (value > mid) {
result[i] = true;
min = mid;
} else {
result[i] = false;
max = mid;
}
}
return result;
}
class LocationBean {
public static final double MINLAT = -90;
public static final double MAXLAT = 90;
public static final double MINLNG = -180;
public static final double MAXLNG = 180;
private double lat;//纬度[-90,90]
private double lng;//经度[-180,180]
public LocationBean(double lat, double lng) {
this.lat = lat;
this.lng = lng;
}
public double getLat() {
return lat;
}
public void setLat(double lat) {
this.lat = lat;
}
public double getLng() {
return lng;
}
public void setLng(double lng) {
this.lng = lng;
}
}
public double[] decode(String geoHash) {
StringBuilder buffer = new StringBuilder();
for (char c : geoHash.toCharArray()) {
int i = lookup.get(c) + 32;
buffer.append(Integer.toString(i, 2).substring(1));
}
BitSet lonset = new BitSet();
BitSet latset = new BitSet();
// even bits
int j = 0;
for (int i = 0; i < numbits * 2; i += 2) {
boolean isSet = false;
if (i < buffer.length())
isSet = buffer.charAt(i) == '1';
lonset.set(j++, isSet);
}
// odd bits
j = 0;
for (int i = 1; i < numbits * 2; i += 2) {
boolean isSet = false;
if (i < buffer.length())
isSet = buffer.charAt(i) == '1';
latset.set(j++, isSet);
}
double lon = decode(lonset, -180, 180);
double lat = decode(latset, -90, 90);
return new double[]{lat, lon};
}
private double decode(BitSet bs, double floor, double ceiling) {
double mid = 0;
for (int i = 0; i < bs.length(); i++) {
mid = (floor + ceiling) / 2;
if (bs.get(i))
floor = mid;
else
ceiling = mid;
}
return mid;
}
public static String encode(double lat, double lon) {
BitSet latbits = getBits(lat, -90, 90);
BitSet lonbits = getBits(lon, -180, 180);
StringBuilder buffer = new StringBuilder();
for (int i = 0; i < numbits; i++) {
buffer.append(lonbits.get(i) ? '1' : '0');
buffer.append(latbits.get(i) ? '1' : '0');
}
return base32(Long.parseLong(buffer.toString(), 2));
}
private static BitSet getBits(double d, double floor, double ceiling) {
BitSet buffer = new BitSet(numbits);
for (int i = 0; i < numbits; i++) {
double mid = (floor + ceiling) / 2;
if (d >= mid) {
buffer.set(i);
floor = mid;
} else {
ceiling = mid;
}
}
return buffer;
}
private static String base32(long i) {
char[] buf = new char[65];
int charPos = 64;
boolean negative = (i < 0);
if (!negative)
i = -i;
while (i <= -32) {
buf[charPos--] = CHARS[(int) (-(i % 32))];
i /= 32;
}
buf[charPos] = CHARS[(int) (-i)];
if (negative)
buf[--charPos] = '-';
return new String(buf, charPos, (65 - charPos));
}
public static void main(String[] args) {
GeoHashUtil geoHashUDAF = new GeoHashUtil();
String geohash = geoHashUDAF.evaluate("120.107209", "30.290245", 10);
System.out.println(geohash);
double[] decode = geoHashUDAF.decode(geohash);
System.out.println(decode[1]+","+decode[0]);
String evaluate = geoHashUDAF.evaluate("119.32639333333333", "41.589551666666665", 10);
System.out.println(evaluate);
String evaluate3 = geoHashUDAF.evaluate("119.32607166666666", "41.58989499999999", 10);
System.out.println(evaluate3);
String evaluate2 = geoHashUDAF.evaluate("119.38659599999998", "41.504658", 10);
System.out.println(evaluate2);
double[] decode1 = geoHashUDAF.decode(evaluate2);
System.out.println(decode1[1]+","+decode1[0]);
}
} }
package cn.com.duiba.utils; package cn.com.duiba.utils;
import com.google.common.base.Joiner;
import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
import java.util.List;
public class Wgs84ToBd09Util { public class Wgs84ToBd09Util {
static double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
static double pi = 3.1415926535897932384626; // π
static double a = 6378245.0; //长半轴
static double ee = 0.00669342162296594323; //偏心率平方
public static String evaluate(String gps_longitude, String gps_latitude) {
try {
if (StringUtils.isEmpty(gps_longitude) || StringUtils.isEmpty(gps_latitude)) {
return "";
}
Double lng = Double.valueOf(gps_longitude);
Double lat = Double.valueOf(gps_latitude);
/* if (outOfChina(lng, lat)) {
return gps_longitude + "," + gps_latitude;
}*/
double[] wgs84_to_gcj02 = wgs84_to_gcj02(lng, lat);
double[] gcj02_to_bd09 = gcj02_to_bd09(wgs84_to_gcj02[0], wgs84_to_gcj02[1]);
return gcj02_to_bd09[0] + "," + gcj02_to_bd09[1];
} catch (Exception e) {
return "";
}
}
boolean outOfChina(Double lng, Double lat) {
return !(lng > 73.66 && lng < 135.05 && lat > 3.86 && lat < 53.55);
}
/**
* WGS84转GCJ02(火星坐标系)
* :param lng:WGS84坐标系的经度
* :param lat:WGS84坐标系的纬度
* :return:
*
* @param lng
* @param lat
*/
static double[] wgs84_to_gcj02(Double lng, Double lat) {
double dlat = _transformlat(lng - 105.0, lat - 35.0);
double dlng = _transformlng(lng - 105.0, lat - 35.0);
double radlat = lat / 180.0 * pi;
double magic = Math.sin(radlat);
magic = 1 - ee * magic * magic;
double sqrtmagic = Math.sqrt(magic);
dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * pi);
dlng = (dlng * 180.0) / (a / sqrtmagic * Math.cos(radlat) * pi);
double mglat = lat + dlat;
double mglng = lng + dlng;
double[] gps = new double[2];
gps[0] = mglng;
gps[1] = mglat;
return gps;
}
static double _transformlat(double lng, double lat) {
double ret = -100.0 + 2.0 * lng + 3.0 * lat + 0.2 * lat * lat +
0.1 * lng * lat + 0.2 * Math.sqrt(Math.abs(lng));
ret += (20.0 * Math.sin(6.0 * lng * pi) + 20.0 *
Math.sin(2.0 * lng * pi)) * 2.0 / 3.0;
ret += (20.0 * Math.sin(lat * pi) + 40.0 *
Math.sin(lat / 3.0 * pi)) * 2.0 / 3.0;
ret += (160.0 * Math.sin(lat / 12.0 * pi) + 320 *
Math.sin(lat * pi / 30.0)) * 2.0 / 3.0;
return ret;
}
static double _transformlng(double lng, double lat) {
double ret = 300.0 + lng + 2.0 * lat + 0.1 * lng * lng +
0.1 * lng * lat + 0.1 * Math.sqrt(Math.abs(lng));
ret += (20.0 * Math.sin(6.0 * lng * pi) + 20.0 *
Math.sin(2.0 * lng * pi)) * 2.0 / 3.0;
ret += (20.0 * Math.sin(lng * pi) + 40.0 *
Math.sin(lng / 3.0 * pi)) * 2.0 / 3.0;
ret += (150.0 * Math.sin(lng / 12.0 * pi) + 300.0 *
Math.sin(lng / 30.0 * pi)) * 2.0 / 3.0;
return ret;
}
/**
* 火星坐标系(GCJ-02)转百度坐标系(BD-09)
* 谷歌、高德——>百度
* :param lng:火星坐标经度
* :param lat:火星坐标纬度
* :return:
*/
static double[] gcj02_to_bd09(double lng, double lat) {
double z = Math.sqrt(lng * lng + lat * lat) + 0.00002 * Math.sin(lat * x_pi);
double theta = Math.atan2(lat, lng) + 0.000003 * Math.cos(lng * x_pi);
double bd_lng = z * Math.cos(theta) + 0.0065;
double bd_lat = z * Math.sin(theta) + 0.006;
double[] gps = new double[2];
gps[0] = bd_lng;
gps[1] = bd_lat;
return gps;
}
public static void main(String[] args) {
Wgs84ToBd09Util wgs84ToBd09UDF = new Wgs84ToBd09Util();
String gps_bd = wgs84ToBd09UDF.evaluate("120.107209", "30.290245");
System.out.println(gps_bd);
String ipOri = "115.60.89.29";
String[] splits = ipOri.split("\\.");
List<String> ipParts = new ArrayList<>();
for (int i=0;i<3;i++) {
String split = splits[i];
int length = split.length();
if(length==3){
ipParts.add(split);
}else if(length==2){
ipParts.add("0"+split);
}else if(length==1){
ipParts.add("00"+ipParts);
}
}
String ipAfter = Joiner.on("").join(ipParts);
System.out.println(ipAfter);
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment