In data analysis, we often encounter GIS processing related to longitude and latitude, but Hive itself does not support it. We can implement it by implementing UDF functions.

Download the source code

Github.com/Angryshark1…

Class package download

Link: pan.baidu.com/s/19-bZz9tt…

Extract code: AP99

Implementation steps

  1. Code implementation
  2. Packaging upload
  3. The registered test

Code implementation

The core code

Implemented in gisUtil.java

public class GisUtil {
    public static Double pi = Math.PI;
    public static Double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
    public static Double a = 6378245.0;
    public static Double ee = 0.00669342162296594323;
    static double longRadius = 6378137.00;
    static double shortRadius = 6356752.3142;

    public static void main(String[] args) {
        String coords = "[[[116.394461, 39.973096 ], [116.388437, 39.973076 ], [116.386609, 39.972803 ], [116.384851, 39.972951 ], [116.381435, 39.972796 ], [116.381829, 39.972084 ], [116.381111, 39.970135 ], [116.380709, 39.967835 ], [116.370521, 39.967529 ], [116.370593, 39.965598 ], [116.372211, 39.949098 ], [116.368539, 39.948289 ], [116.367257, 39.947582 ], [116.365259, 39.946766 ], [116.357113, 39.944465 ], [116.356525, 39.944976 ], [116.356553, 39.948078 ], [116.356763, 39.948128 ], [116.356893, 39.950026 ], [116.356307, 39.951385 ], [116.355653, 39.951688 ], [116.355161, 39.95166 ], [116.351537, 39.950508 ], [116.350997, 39.947036 ], [116.351957, 39.943969 ], [116.350341, 39.942991 ], [116.346799, 39.943646 ], [116.342051, 39.943154 ], [116.340265, 39.943242 ], [116.338319, 39.942905 ], [116.335817, 39.943179 ], [116.334845, 39.943517 ], [116.334119, 39.943452 ], [116.333325, 39.943979 ], [116.332729, 39.944121 ], [116.332119, 39.944039 ], [116.331407, 39.943572 ], [116.328471, 39.942655 ], [116.329149, 39.94134 ], [116.329555, 39.940025 ], [116.330255, 39.939911 ], [116.331123, 39.938923 ], [116.333287, 39.938263 ], [116.334577, 39.925845 ], [116.335093, 39.922982 ], [116.334889, 39.922657 ], [116.334791, 39.912451 ], [116.334881, 39.911647 ], [116.335587, 39.909509 ], [116.335725, 39.908178 ], [116.335649, 39.90712 ], [116.334985, 39.90708 ], [116.334749, 39.905055 ], [116.335061, 39.9042 ], [116.335825, 39.903507 ], [116.335279, 39.903082 ], [116.335543, 39.902798 ], [116.335119, 39.902592 ], [116.335155, 39.898513 ], [116.338057, 39.898523 ], [116.338273, 39.897933 ], [116.338287, 39.897081 ], [116.331407, 39.896736 ], [116.325961, 39.896784 ], [116.326009, 39.89432 ], [116.325467, 39.894053 ], [116.325387, 39.893867 ], [116.325541, 39.893598 ], [116.326177, 39.893295 ], [116.326105, 39.890982 ], [116.326587, 39.890256 ], [116.326627, 39.889665 ], [116.324501, 39.88959 ], [116.324561, 39.887644 ], [116.323803, 39.887513 ], [116.323837, 39.884767 ], [116.323347, 39.884426 ], [116.323201, 39.883985 ], [116.321961, 39.883679 ], [116.321849, 39.881972 ], [116.322415, 39.881793 ], [116.322227, 39.881544 ], [116.321417, 39.881537 ], [116.321411, 39.875305 ], [116.323569, 39.876183 ], [116.324941, 39.876298 ], [116.329333, 39.876038 ], [116.332787, 39.875467 ], [116.341613, 39.876139 ], [116.342641, 39.87609 ], [116.343543, 39.875714 ], [116.344299, 39.875074 ], [116.344615, 39.873751 ], [116.346933, 39.873753 ], [116.347179, 39.873865 ], [116.347395, 39.873569 ], [116.349601, 39.873635 ], [116.349305, 39.870339 ], [116.349577, 39.869804 ], [116.350279, 39.869185 ], [116.351425, 39.868968 ], [116.381169, 39.871154 ], [116.399585, 39.872199 ], [116.399181, 39.880155 ], [116.398761, 39.880785 ], [116.398749, 39.883549 ], [116.397921, 39.89871 ], [116.396399, 39.899192 ], [116.395895, 39.899851 ], [116.395509, 39.907695 ], [116.392585, 39.907567 ], [116.392561, 39.908266 ], [116.392807, 39.908439 ], [116.392873, 39.908709 ], [116.392819, 39.91075 ], [116.391843, 39.910771 ], [116.391727, 39.912411 ], [116.392213, 39.912438 ], [116.392197, 39.913003 ], [116.392369, 39.913243 ], [116.391915, 39.922705 ], [116.396895, 39.922886 ], [116.396915, 39.923327 ], [116.399881, 39.923432 ], [116.399571, 39.928489 ], [116.396755, 39.928423 ], [116.395999, 39.940341 ], [116.395399, 39.94065 ], [116.394247, 39.940762 ], [116.393705, 39.957115 ], [116.386777, 39.956796 ], [116.387613, 39.96089 ], [116.387913, 39.961095 ], [116.388173, 39.963032 ], [116.389287, 39.963193 ], [116.389493, 39.963418 ], [116.388799, 39.963517 ], [116.388745, 39.964532 ], [116.388247, 39.96453 ], [116.388301, 39.965824 ], [116.390507, 39.966223 ], [116.390301, 39.968124 ], [116.391633, 39.96816 ], [116.391701, 39.968682 ], [116.392925, 39.969122 ], [116.394077, 39.969241 ], [116.394601, 39.969509 ], [116.394841, 39.970097 ], [116.394769, 39.973121 ], [116.394461, 39.973096 ] ] ]";
        System.out.println(convert2Wkt("POLYGON", coords));
        System.out.println(getDistanceWGS84(116.4570315, 34.8623005, 116.4660405, 34.8713095));
    }

    /**
     * 判断是否为合规经度
     *
     * @param lon 经度
     * @return
     */
    public static Boolean isLegalLon(String lon) {
        try {
            return Double.valueOf(lon) >= -180 && Double.valueOf(lon) <= 180;
        } catch (Exception exception) {
            return false;
        }
    }

    /**
     * 判断是否为合规纬度
     *
     * @param lat 纬度
     */
    public static Boolean isLegalLat(String lat) {
        try {
            return Double.valueOf(lat) >= -90 && Double.valueOf(lat) <= 90;
        } catch (Exception exception) {
            return false;
        }
    }

    /**
     * 根据WGS84坐标系经纬度计算距离
     *
     * @param lon1 经度
     * @param lat1 纬度
     * @param lon2 经度
     * @param lat2 纬度
     * @return
     */
    public static double getDistanceWGS84(double lon1, double lat1, double lon2, double lat2) {

        GlobalCoordinates from = new GlobalCoordinates(lat1, lon1);
        GlobalCoordinates to = new GlobalCoordinates(lat2, lon2);

        GeodeticCurve geoCurve = new GeodeticCalculator().calculateGeodeticCurve(Ellipsoid.WGS84, from, to);

        return geoCurve.getEllipsoidalDistance();
    }

    /**
     * 经纬度转墨卡托
     *
     * @param lon 经度
     * @param lat 纬度
     * @return
     */
    public static double[] lonlat2XY(double lon, double lat) {
        double[] xy = new double[2];
        double x = lon * pi * longRadius / 180;
        double y = Math.log(Math.tan((90 + lat) * pi / 360)) / (pi / 180);

        y = y * pi * shortRadius / 180;

        xy[0] = x;
        xy[1] = y;

        return xy;
    }

    /**
     * 墨卡托转经纬度
     *
     * @param mercatorX X
     * @param mercatorY Y
     * @return
     */
    public static double[] xy2Lonlat(double mercatorX, double mercatorY) {
        double[] xy = new double[2];
        double x = mercatorX / (pi * longRadius) * 180;
        double y = mercatorY / (pi * shortRadius) * 180;

        y = 180 / pi * (2 * Math.atan(Math.exp(y * pi / 180)) - pi / 2);

        xy[0] = x;
        xy[1] = y;

        return xy;
    }

    /**
     * 判断指定坐标是否在中国境内
     *
     * @param lat 纬度
     * @param lon 经度
     * @return
     */
    public static boolean outOfChina(Double lat, Double lon) {
        if (lon < 72.004 || lon > 137.8347) {
            return true;
        }
        return lat < 0.8293 || lat > 55.8271;
    }

    /**
     * WGS84转火星
     *
     * @param lat 纬度
     * @param lon 经度
     * @return
     */
    public static Double[] wgs84ToGcj02(Double lat, Double lon) {
        if (outOfChina(lat, lon)) {
            return new Double[]{lat, lon};
        }
        Double dLat = transformLat(lon - 105.0, lat - 35.0);
        Double dLon = transformLon(lon - 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);
        dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
        Double mgLat = lat + dLat;
        Double mgLon = lon + dLon;
        return new Double[]{mgLat, mgLon};
    }

    /**
     * 火星转WGS84
     *
     * @param lat 纬度
     * @param lon 经度
     * @return
     */
    public static Double[] gcj02ToWgs84(Double lat, Double lon) {
        Double[] gps = transform(lat, lon);
        Double lontitude = lon * 2 - gps[1];
        Double latitude = lat * 2 - gps[0];
        return new Double[]{latitude, lontitude};
    }

    /**
     * 火星转百度
     *
     * @param lat 纬度
     * @param lon 经度
     * @return
     */
    public static Double[] gcj02ToBd09(Double lat, Double lon) {
        Double x = lon, y = lat;
        Double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
        Double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
        Double tempLon = z * Math.cos(theta) + 0.0065;
        Double tempLat = z * Math.sin(theta) + 0.006;
        Double[] gps = {tempLat, tempLon};
        return gps;
    }

    /**
     * 百度转火星
     *
     * @param lat 纬度
     * @param lon 经度
     * @return
     */
    public static Double[] bd09ToGcj02(Double lat, Double lon) {
        Double x = lon - 0.0065, y = lat - 0.006;
        Double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
        Double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
        Double tempLon = z * Math.cos(theta);
        Double tempLat = z * Math.sin(theta);
        Double[] gps = {tempLat, tempLon};
        return gps;
    }

    /**
     * WGS84转百度
     *
     * @param lat 纬度
     * @param lon 经度
     * @return
     */
    public static Double[] wgs84ToBd09(Double lat, Double lon) {
        Double[] gcj02 = wgs84ToGcj02(lat, lon);
        Double[] bd09 = gcj02ToBd09(gcj02[0], gcj02[1]);
        return bd09;
    }

    /**
     * 百度转WGS84
     *
     * @param lat 纬度
     * @param lon 经度
     * @return
     */
    public static Double[] bd09ToWgs84(Double lat, Double lon) {
        Double[] gcj02 = bd09ToGcj02(lat, lon);
        Double[] gps84 = gcj02ToWgs84(gcj02[0], gcj02[1]);
        // 保留小数点后六位
        gps84[0] = retain6(gps84[0]);
        gps84[1] = retain6(gps84[1]);
        return gps84;
    }

    /**
     * 保留小数点后指定位数
     *
     * @param num    数值
     * @param digits 小数点位数
     * @return
     */
    public static Double retain(Double num, Integer digits) {
        String result = String.format("%." + digits + "f", num);
        return Double.valueOf(result);
    }

    /**
     * 指定图形类型和坐标串,拼接成WKT格式
     *
     * @param type        图形类型,
     * @param coordinates 坐标串
     * @return
     */
    public static String convert2Wkt(String type, String coordinates) {

        //"Point", "MultiPoint", "LineString", "MultiLineString", "Polygon", "MultiPolygon"
        StringBuffer wkt = new StringBuffer();

        JSONArray jsonArray = JSONObject.parseArray(coordinates);

        type = type.toUpperCase();
        switch (type) {
            case "POINT":
                //[100.0, 0.0]
                wkt.append("POINT(").append(jsonArray.getString(0)).append(" ")
                        .append(jsonArray.getString(1)).append(")");
                break;
            case "MULTIPOINT":
                //[
                //        [100.0, 0.0],
                //        [101.0, 1.0]
                //    ]
                wkt.append("MULTIPOINT(");
                for (int i = 0; i < jsonArray.size(); i++) {
                    JSONArray point = jsonArray.getJSONArray(i);
                    wkt.append(point.getString(0)).append(" ").append(point.get(1)).append(",");
                }
                wkt.deleteCharAt(wkt.length() - 1);
                wkt.append(")");
                break;
            case "LINESTRING":
                //和multipoint结构一样
                //[
                //                [101.0, 0.0],
                //                [102.0, 1.0]
                //            ]
                wkt.append("LINESTRING").append(wktLineString(jsonArray));
                break;
            case "MULTILINESTRING":
                //[
                //        [
                //            [100.0, 0.0],
                //            [101.0, 1.0]
                //        ],
                //        [
                //            [102.0, 2.0],
                //            [103.0, 3.0]
                //        ]
                //    ]
                wkt.append("MULTILINESTRING(");
                for (int i = 0; i < jsonArray.size(); i++) {
                    wkt.append(wktLineString(jsonArray.getJSONArray(i))).append(",");
                }
                wkt.deleteCharAt(wkt.length() - 1);
                wkt.append(")");
                break;
            case "POLYGON":
                //POLYGON((1 1,5 1,5 5,1 5,1 1),(2 2,2 3,3 3,3 2,2 2))
                //"coordinates": [
                //            [
                //                [-180.0, 10.0],
                //                [20.0, 90.0],
                //                [180.0, -5.0],
                //                [-30.0, -90.0]
                //            ]
                //        ]
                wkt.append("POLYGON").append(wktPolygon(jsonArray));
                break;
            case "MULTIPOLYGON":
                //解析为多个polygon
                wkt.append("MULTIPOLYGON(");
                for (int i = 0; i < jsonArray.size(); i++) {
                    wkt.append(wktPolygon(jsonArray.getJSONArray(i))).append(",");
                }
                wkt.deleteCharAt(wkt.length() - 1);
                wkt.append(")");
                break;
        }

        return wkt.toString();
    }

    private static Double retain6(Double num) {
        return retain(num, 6);
    }

    private static String wktLineString(JSONArray jsonArray) {

        StringBuffer wkt = new StringBuffer();

        wkt.append("(");
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONArray point = jsonArray.getJSONArray(i);
            wkt.append(point.getString(0)).append(" ").append(point.get(1)).append(",");
        }
        wkt.deleteCharAt(wkt.length() - 1);
        wkt.append(")");

        return wkt.toString();
    }

    private static String wktPolygon(JSONArray jsonArray) {

        StringBuffer wkt = new StringBuffer();

        wkt.append("(");
        for (int i = 0; i < jsonArray.size(); i++) {
            //是一组组的LineString
            wkt.append("(");
            JSONArray lineString = jsonArray.getJSONArray(i);
            for (int j = 0; j < lineString.size(); j++) {
                JSONArray pointArray = lineString.getJSONArray(j);
                wkt.append(pointArray.getString(0)).append(" ").append(pointArray.getString(1))
                        .append(",");
            }
            if (wkt.length() > 0) {
                //删除最后一个逗号
                wkt.deleteCharAt(wkt.length() - 1);
            }
            wkt.append("),");
        }
        if (wkt.length() > 0) {
            wkt.deleteCharAt(wkt.length() - 1);
        }
        wkt.append(")");

        return wkt.toString();
    }

    private static Double transformLat(Double x, Double y) {
        Double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y
                + 0.2 * Math.sqrt(Math.abs(x));
        ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(y * pi) + 40.0 * Math.sin(y / 3.0 * pi)) * 2.0 / 3.0;
        ret += (160.0 * Math.sin(y / 12.0 * pi) + 320 * Math.sin(y * pi / 30.0)) * 2.0 / 3.0;
        return ret;
    }

    private static Double transformLon(Double x, Double y) {
        Double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1
                * Math.sqrt(Math.abs(x));
        ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(x * pi) + 40.0 * Math.sin(x / 3.0 * pi)) * 2.0 / 3.0;
        ret += (150.0 * Math.sin(x / 12.0 * pi) + 300.0 * Math.sin(x / 30.0
                * pi)) * 2.0 / 3.0;
        return ret;
    }

    private static Double[] transform(Double lat, Double lon) {
        if (outOfChina(lat, lon)) {
            return new Double[]{lat, lon};
        }
        Double dLat = transformLat(lon - 105.0, lat - 35.0);
        Double dLon = transformLon(lon - 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);
        dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
        Double mgLat = lat + dLat;
        Double mgLon = lon + dLon;
        return new Double[]{mgLat, mgLon};
    }
}
Copy the code

Function code

Create a new class to inherit UDF, and call GisUtil class to implement the corresponding function

/**
 * 使用方法:百度坐标系转火星坐标系
 * > add jar ..../udfforhive-1.0-SNAPSHOT.jar;
 * > create temporary function bd09togcj02 as "com.oidd.udf.hive.Bd09ToGcj02";
 * > select bd09togcj02(118.74231624081231, 32.068722091339836);
 */
public class Bd09ToGcj02 extends UDF {
    public static void main(String[] args) {
        System.out.println(new Bd09ToGcj02().evaluate(118.74231624081231, 32.068722091339836));
    }

    public String evaluate(double lon, double lat) {
        Double[] coord = GisUtil.bd09ToGcj02(lat, lon);
        return String.format("%f,%f", coord[1], coord[0]);
    }
}
Copy the code

Packaging upload

The pom. XML configuration is as follows

<? The XML version = "1.0" encoding = "utf-8"? > < project XMLNS = "http://maven.apache.org/POM/4.0.0" XMLNS: xsi = "http://www.w3.org/2001/XMLSchema-instance" Xsi: schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion > 4.0.0 < / modelVersion > < groupId >. Com gis. Hive < / groupId > < artifactId > gisutilforhive < / artifactId > <version> 1.0-snapshot </version> <name> gisutilforHive </name> <description> Such as coordinate transformation and distance calculation < / description > < properties > < project. Build. SourceEncoding > utf-8 < / project. Build. SourceEncoding > Piler < maven.com. Source > 1.7 < / maven.com piler. Source > < maven.com piler. Target > 1.7 < / maven.com piler. Target > < scala version > 2.11 < / scala version > < spark. Version > 2.2.0 < / spark version > < geotools. Version > 14.1 < / geotools version > </properties> <repositories> <repository> <id>osgeo</id> <name>OSGeo Release Repository</name> <url>https://repo.osgeo.org/repository/release/</url> <snapshots> <enabled>false</enabled> </snapshots> <releases> <enabled>true</enabled> </releases> </repository> <repository> <id>osgeo-snapshot</id> <name>OSGeo Snapshot Repository</name> <url>https://repo.osgeo.org/repository/snapshot/</url> <snapshots> <enabled>true</enabled> </snapshots> <releases> <enabled>false</enabled> </releases> </repository> <repository> <id>public</id> <name>aliyun nexus</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> </repository> </repositories> <dependencies> <! <groupId>org.apache.hive</groupId> <artifactId> Hive -exec</artifactId> </artifactId> <version>${spark.version}</version> </dependency> <! -- dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.4< version> </dependency> <! <groupId>org.geotools</groupId> <artifactId>gt-geojson</artifactId> <version>${geotools.version}</version> </dependency> <! </artifactId> geodesy</artifactId> <version>1.1.3</version> </groupId> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <appendAssemblyId>false</appendAssemblyId> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>assembly</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>8</source> <target>8</target> </configuration> </plugin> </plugins> </build> </project>Copy the code

After the package is packaged, upload the package to the Hive client. The package can be a local directory or an HDFS directory. You are advised to use the HDFS directory for global use.

Upload to a local directory

/ gis/lib/gisutilforhive – 1.0 – the SNAPSHOT. The jar

Uploaded to the HDFS

HDFS: / / IP: port/libs/gisutilforhive – 1.0 – the SNAPSHOT. The jar

Methods registration

Method 1: Temporary use

Hive > add jar /gis/lib/ gisutilforHive-1.0-snapshot. jar; # remote HDFS hive > add jar HDFS: / / IP: port/libs/gisutilforhive - 1.0 - the SNAPSHOT. Jar; # create temporary function hive > create temporary function Bd09ToGcj02 as' common. Gis. Hive. Bd09ToGcj02 '; hive> create temporary function Bd09ToWgs84 as 'common.gis.hive.Bd09ToWgs84'; hive> create temporary function Convert2Wkt as 'common.gis.hive.Convert2Wkt'; hive> create temporary function Gcj02ToBd09 as 'common.gis.hive.Gcj02ToBd09'; hive> create temporary function Gcj02ToWgs84 as 'common.gis.hive.Gcj02ToWgs84'; hive> create temporary function OutOfChina as 'common.gis.hive.OutOfChina'; hive> create temporary function Retain as 'common.gis.hive.Retain'; hive> create temporary function Wgs84ToBd09 as 'common.gis.hive.Wgs84ToBd09'; hive> create temporary function Wgs84ToGcj02 as 'common.gis.hive.Wgs84ToGcj02'; hive> create temporary function LL2XY as 'common.gis.hive.LL2XY'; hive> create temporary function XY2LL as 'common.gis.hive.XY2LL'; hive> create temporary function GetDistance as 'common.gis.hive.GetDistance'; hive> create temporary function IsLegalLon as 'common.gis.hive.IsLegalLon'; hive> create temporary function IsLegalLat as 'common.gis.hive.IsLegalLat'; Select OutOfChina(0,0); OK true Time Fetched: 0.52 seconds, 1 row(s)Copy the code

Method 2: Global use

Hive > drop function if exists Bd09ToGcj02; create function Bd09ToGcj02 as 'common.gis.hive.Bd09ToGcj02' using jar 'HDFS: / / IP: port/libs/gisutilforhive - 1.0 - the SNAPSHOT. Jar'; hive> drop function if exists Bd09ToWgs84; create function Bd09ToWgs84 as 'common.gis.hive.Bd09ToWgs84' using jar 'HDFS: / / IP: port/libs/gisutilforhive - 1.0 - the SNAPSHOT. Jar'; hive> drop function if exists Convert2Wkt; create function Convert2Wkt as 'common.gis.hive.Convert2Wkt' using jar 'HDFS: / / IP: port/libs/gisutilforhive - 1.0 - the SNAPSHOT. Jar'; hive> drop function if exists Gcj02ToBd09; create function Gcj02ToBd09 as 'common.gis.hive.Gcj02ToBd09' using jar 'HDFS: / / IP: port/libs/gisutilforhive - 1.0 - the SNAPSHOT. Jar'; hive> drop function if exists Gcj02ToWgs84; create function Gcj02ToWgs84 as 'common.gis.hive.Gcj02ToWgs84' using jar 'HDFS: / / IP: port/libs/gisutilforhive - 1.0 - the SNAPSHOT. Jar'; hive> drop function if exists OutOfChina; create function OutOfChina as 'common.gis.hive.OutOfChina' using jar 'HDFS: / / IP: port/libs/gisutilforhive - 1.0 - the SNAPSHOT. Jar'; hive> drop function if exists Retain; The create function Retain as' common. Gis. Hive. Retain 'using jar' HDFS: / / IP: port/libs/gisutilforhive - 1.0 - the SNAPSHOT. Jar '; hive> drop function if exists Wgs84ToBd09; create function Wgs84ToBd09 as 'common.gis.hive.Wgs84ToBd09' using jar 'HDFS: / / IP: port/libs/gisutilforhive - 1.0 - the SNAPSHOT. Jar'; hive> drop function if exists Wgs84ToGcj02; create function Wgs84ToGcj02 as 'common.gis.hive.Wgs84ToGcj02' using jar 'HDFS: / / IP: port/libs/gisutilforhive - 1.0 - the SNAPSHOT. Jar'; hive> drop function if exists LL2XY; The create function LL2XY as' common. Gis. Hive. LL2XY 'using jar' HDFS: / / IP: port/libs/gisutilforhive - 1.0 - the SNAPSHOT. Jar '; hive> drop function if exists XY2LL; The create function XY2LL as' common. Gis. Hive. XY2LL 'using jar' HDFS: / / IP: port/libs/gisutilforhive - 1.0 - the SNAPSHOT. Jar '; hive> drop function if exists GetDistance; create function GetDistance as 'common.gis.hive.GetDistance' using jar 'HDFS: / / IP: port/libs/gisutilforhive - 1.0 - the SNAPSHOT. Jar'; hive> drop function if exists IsLegalLon; create function IsLegalLon as 'common.gis.hive.IsLegalLon' using jar 'HDFS: / / IP: port/libs/gisutilforhive - 1.0 - the SNAPSHOT. Jar'; hive> drop function if exists IsLegalLat; create function IsLegalLat as 'common.gis.hive.IsLegalLat' using jar 'HDFS: / / IP: port/libs/gisutilforhive - 1.0 - the SNAPSHOT. Jar'; hive> quit; Hive > select OutOfChina(0,0); Converting to local HDFS: / / IP: port/libs/gisutilforhive - 1.0 - the SNAPSHOT. The jar is Added [/ TMP / 5beee0f5-63f9-4f18-a41.1d - 1cd136EE0645_resources/gisutilforHive-1.0-snapshot. jar] To class path Added resources: [HDFS: / / IP: port/libs/gisutilforhive - 1.0 - the SNAPSHOT. Jar] OK true Time seems: 2.155 seconds, Fetched: 1 row (s)Copy the code

Methods the sample

Baidu coordinate system to Mars coordinate system

The select bd09togcj02 (118.74231624081231, 32.068722091339836);

Baidu coordinates to WGS84 coordinates

The select bd09towgs84 (118.74231624081231, 32.068722091339836);

Convert coordinates to WKT format

The select convert2wkt (” POINT “, “[118.74231624081231, 32.068722091339836]”));

Mars coordinate system to baidu coordinate system

The select gcj02tobd09 (118.74231624081231, 32.068722091339836);

Mars to WGS84

The select gcj02towgs84 (118.74231624081231, 32.068722091339836);

WGS84 coordinate system latitude and longitude to calculate the distance

The select xy2ll (13218334.179541, 3759686.929327);

Latitude and longitude to metric coordinates

The select ll2xy (118.74231624081231, 32.068722091339836);

Determine whether the specified location is in China, WGS84 coordinate system

The select outofchina (118.74231624081231, 32.068722091339836);

Reserved to the specified number of decimal places

The select retain (118.74231624081231, 4);

WGS84 coordinate system to Baidu coordinate system

The select wgs84tobd09 (118.74231624081231, 32.068722091339836);

WGS84 to Mars

The select wgs84togcj02 (118.74231624081231, 32.068722091339836);

Metric coordinates of longitude and latitude

The select xy2ll (13218334.179541, 3759686.929327);

Whether it is a compliant longitude

The select islegallon (118.74231624081231);

Whether the latitude is compliant

The select islegallat (32.068722091339836);