GISHelper.java 4.69 KB
package com.pashanhoo.landsurvey.utils;

/**
 * Created by jiangbotao on 2018/3/28.
 */

import java.text.DecimalFormat;

import android.util.Log;

import com.esri.android.map.MapView;
import com.esri.core.geometry.GeometryEngine;
import com.esri.core.geometry.Point;
import com.esri.core.geometry.Polygon;
import com.esri.core.geometry.SpatialReference;

/**
 * GIS相关持有工具类
 *
 * @author JiangXusheng
 * @date 2013-7-6 下午1:16:07
 */
public class GISHelper {
    public static final int VALIDATE_X_Y = 999;

    public static final double AX = 0.83;
    public static final double BX = 1 - AX;

    static final String TAG = GISHelper.class.getName();

    public enum MarkerSymbolType {
        TEXT, IMAGE, SIMPLE, IMAGE_TEXT, SIMPLE_TEXT, SIMPLE_LINE;
    }

    private static final double EARTH_RADIUS = 6378137.0;

    /**
     * 计算两点间的距离
     */
    public static double gps2m(double lat_a, double lng_a, double lat_b, double lng_b) {
        double radLat1 = (lat_a * Math.PI / 180.0);
        double radLat2 = (lat_b * Math.PI / 180.0);
        double a = radLat1 - radLat2;
        double b = (lng_a - lng_b) * Math.PI / 180.0;
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
        s = s * EARTH_RADIUS;
        s = Math.round(s * 10000) / 10000;
        return s;
    }

    // 计算方位角pab。

    /**
     * 计算方位角
     */
    public static double gps2d(double lat_a, double lng_a, double lat_b, double lng_b) {
        double d = 0;
        lat_a = lat_a * Math.PI / 180;
        lng_a = lng_a * Math.PI / 180;
        lat_b = lat_b * Math.PI / 180;
        lng_b = lng_b * Math.PI / 180;

        d = Math.sin(lat_a) * Math.sin(lat_b) + Math.cos(lat_a) * Math.cos(lat_b) * Math.cos(lng_b - lng_a);
        d = Math.sqrt(1 - d * d);
        d = Math.cos(lat_b) * Math.sin(lng_b - lng_a) / d;
        d = Math.asin(d) * 180 / Math.PI;

        // d = Math.round(d*10000);
        return d;
    }

    public static double calulateXYAnagle(double startx, double starty, double endx, double endy) {
        double tan = Math.atan(Math.abs((endy - starty) / (endx - startx))) * 180 / Math.PI;
        if (endx > startx && endy > starty)// 第一象限
        {
            return -tan;
        } else if (endx > startx && endy < starty)// 第二象限
        {
            return tan;
        } else if (endx < startx && endy > starty)// 第三象限
        {
            return tan - 180;
        } else {
            return 180 - tan;
        }

    }


    private static double getPieValue(double dN, double dPieN_1) {
        return AX * dN + BX * dPieN_1;
    }


    public static boolean isValidateData(Point point) {
        boolean rst = false;
        if (null != point && !point.isEmpty() && point.getX() != VALIDATE_X_Y && point.getY() != VALIDATE_X_Y && point.getX() != 0.0 && point.getY() != 0.0) {
            rst = true;
        }
        return rst;
    }


    public static long double2Long(double x) {
        return (long) (x * 1000000 + 0.5);
    }

    public static double long2Double(long x) {
        return (x / 1000000.0);
    }


    private static DecimalFormat format = new DecimalFormat("0.0");

    // 经纬度转墨卡托

    /**
     * SpatialReference.WKID_WGS84(4326) to
     * SpatialReference.WKID_WGS84_WEB_MERCATOR_AUXILIARY_SPHERE(102100)
     *
     * @param lonLat
     * @return
     */
    public static Point lonLat2Mercator(Point lonLat) {

        // double x = lonLat.getX() * 20037508.34 / 180;
        // double y = Math.log(Math.tan((90 + lonLat.getY()) * Math.PI / 360)) /
        // (Math.PI / 180);
        // y = y * 20037508.34 / 180;

        Point point = (Point) GeometryEngine.project(lonLat, SpatialReference.create(SpatialReference.WKID_WGS84), SpatialReference.create(SpatialReference.WKID_WGS84_WEB_MERCATOR_AUXILIARY_SPHERE));

        // PtaDebug.e(TAG, "x:"+x+".."+point.getX()+",y"+y+".."+point.getY());

        return point;
    }

    // 墨卡托转经纬度

    /**
     * SpatialReference.WKID_WGS84_WEB_MERCATOR_AUXILIARY_SPHERE(102100) to
     * SpatialReference.WKID_WGS84(4326)
     *
     * @param mercator
     * @return
     */
    public static Point mercator2lonLat(Point mercator) {
        // double x = mercator.getX() / 20037508.34 * 180;
        // double y = mercator.getY() / 20037508.34 * 180;
        // y = 180 / Math.PI * (2 * Math.atan(Math.exp(y * Math.PI / 180)) -
        // Math.PI / 2);

        Point point = (Point) GeometryEngine
                .project(mercator, SpatialReference.create(SpatialReference.WKID_WGS84_WEB_MERCATOR_AUXILIARY_SPHERE), SpatialReference.create(SpatialReference.WKID_WGS84));
        return point;
    }

}