根據經緯度判斷車輛是否進度電子圍欄
阿新 • • 發佈:2019-01-26
public class GPS {
private String strLat;//標準的緯度
private String strLon;//標準的經度
private double dDFLat;//緯度。單位:度分
private double dDFLon;//經度。單位:度分
private double dFLat;//緯度。單位:分
private double dFLon;//經度。單位:分
private double dDLat;//緯度。單位:度
private double dDLon;//經度。單位:度
private int iNetLat;//緯度的網格座標
private int iNetLon;//經度的網格座標
private boolean isValid;//GPS是否有效
private boolean isNetValid;//網格座標是否有效
public boolean isValid() {
return isValid;
}
public boolean isNetValid() {
return isNetValid;
}
public String getGpsCode()
{
return strLat + "," + strLon;
}
public GPS() {
this.strLat = "";
this.strLon = "";
this.dDFLat = 0.0;
this.dDFLon = 0.0;
this.dFLat = 0.0;
this.dFLon = 0.0;
this.dDLat = 0.0;
this.dDLon = 0.0;
this.iNetLat = 0;
this.iNetLon = 0;
this.isValid = false;
this.isNetValid = false;
}
public boolean setByGpsCode(String gpsCode) {
if(null == gpsCode)
{
return false;
}
String[] temp = gpsCode.split(",");
if(temp.length < 2)
{
return false;
}
if(this.setByStandardGps(temp[0], temp[1]))
{
isValid = true;
return true;
}
return false;
}
public boolean setByStandardGps(String strLat, String strLon) {
this.strLat = strLat;
this.strLon = strLon;
if(this.setStandardToDFCoordinate()
&& this.setFCoordinateWithDFCoordinate()
&& this.calcDCoordinate())
{
this.isValid = true;
return true;
}
return false;
}
public String getStrLat() {
return strLat;
}
public String getStrLon() {
return strLon;
}
public double getDFLat() {
return dDFLat;
}
public double getDFLon() {
return dDFLon;
}
public double getFLat() {
return dFLat;
}
public double getFLon() {
return dFLon;
}
/**
*
* @return 緯度。單位:度
*/
public double getDLat() {
return dDLat;
}
/**
*
* @return 經度。單位:度
*/
public double getDLon() {
return dDLon;
}
public int getNetLat() {
return iNetLat;
}
public int getNetLon() {
return iNetLon;
}
/**
* 標準 轉 度分
*
*/
private boolean setStandardToDFCoordinate()
{
if(strLat.length() != 10 || strLon.length() != 11)
{
return false;
}
try
{
String temp = strLat.substring(1);
if (strLat.startsWith("N"))
{
this.dDFLat = Double.parseDouble(temp);
}
else if (strLat.startsWith("S"))
{
this.dDFLat = -Double.parseDouble(temp);
}
else
{
return false;
}
temp = strLon.substring(1);
if (strLon.startsWith("E"))
{
this.dDFLon = Double.parseDouble(temp);
}
else if (strLon.startsWith("W"))
{
this.dDFLon = -Double.parseDouble(temp);
}
else
{
return false;
}
}
catch(NumberFormatException e)
{
return false;
}
return true;
}
/**
* 度分 轉 分
*
*/
private boolean setFCoordinateWithDFCoordinate()
{
int iDLat = (int)(dDFLat/100);
int iDLon = (int)(dDFLon/100);
this.dFLat = iDLat*60+(dDFLat-iDLat*100);
this.dFLon = iDLon*60+(dDFLon-iDLon*100);
if(this.dFLat > 5400.0 || this.dFLat < -5400.0
|| this.dFLon > 10800.0 || this.dFLon < -10800.0)
{
return false;
}
return true;
}
/**
* 計算以度為單位的GPS格式
*/
public boolean calcDCoordinate()
{
int iDLat = (int)(dDFLat/100);
int iDLon = (int)(dDFLon/100);
this.dDLat = (dDFLat-iDLat*100)/60 + iDLat;
this.dDLon = (dDFLon-iDLon*100)/60 + iDLon;
return true;
}
/**
* 計算網格座標
* @param referGPS 參照站點的GPS
*/
public boolean calcNetCoordinate(GPS referGPS)
{
if(!this.isValid)
{
return false;
}
double minLat = referGPS.getFLat()-30;
double minLon = referGPS.getFLon()-30;
this.iNetLat = (int)((dFLat-minLat)/0.0058);
this.iNetLon = (int)((dFLon-minLon)/0.0053);
isNetValid = true;
return true;
}
/**
* 判斷車輛是否在定點範圍內
* @param busLat 車輛緯度(例:N2312.1230)
* @param busLon 車輛經度(例:E11223.1230)
* @param siteLat 定點緯度(例:N2312.1230)
* @param siteLon 定點經度(例:E11223.1230)
* @param radius 定點半徑
* @return true-是,false-否
*例子isInSiteRange("N2312.1230", "E11223.1230", "N2312.1230", "E11223.1230", 10);
*/
public static boolean isInSiteRange(String busLat, String busLon, String siteLat, String siteLon, int radius)
{
GPS busGps = new GPS();
busGps.setByStandardGps(busLat, busLon);
GPS siteGps = new GPS();
siteGps.setByStandardGps(siteLat, siteLon);
busGps.calcNetCoordinate(siteGps);
siteGps.calcNetCoordinate(siteGps);
//網格座標計算失敗
if(!busGps.isNetValid() || !busGps.isNetValid())
{
return false;
}
int x1 = busGps.getNetLon();
int y1 = busGps.getNetLat();
int x2 = siteGps.getNetLon();
int y2 = siteGps.getNetLat();
int distance = (int)(Math.sqrt(Math.abs((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)))*10);
return distance <= radius ? true : false;
}
}
private String strLat;//標準的緯度
private String strLon;//標準的經度
private double dDFLat;//緯度。單位:度分
private double dDFLon;//經度。單位:度分
private double dFLat;//緯度。單位:分
private double dFLon;//經度。單位:分
private double dDLat;//緯度。單位:度
private double dDLon;//經度。單位:度
private int iNetLat;//緯度的網格座標
private int iNetLon;//經度的網格座標
private boolean isValid;//GPS是否有效
private boolean isNetValid;//網格座標是否有效
public boolean isValid() {
return isValid;
}
public boolean isNetValid() {
return isNetValid;
}
public String getGpsCode()
{
return strLat + "," + strLon;
}
public GPS() {
this.strLat = "";
this.strLon = "";
this.dDFLat = 0.0;
this.dDFLon = 0.0;
this.dFLat = 0.0;
this.dFLon = 0.0;
this.dDLat = 0.0;
this.dDLon = 0.0;
this.iNetLat = 0;
this.iNetLon = 0;
this.isValid = false;
this.isNetValid = false;
}
public boolean setByGpsCode(String gpsCode) {
if(null == gpsCode)
{
return false;
}
String[] temp = gpsCode.split(",");
if(temp.length < 2)
{
return false;
}
if(this.setByStandardGps(temp[0], temp[1]))
{
isValid = true;
return true;
}
return false;
}
public boolean setByStandardGps(String strLat, String strLon) {
this.strLat = strLat;
this.strLon = strLon;
if(this.setStandardToDFCoordinate()
&& this.setFCoordinateWithDFCoordinate()
&& this.calcDCoordinate())
{
this.isValid = true;
return true;
}
return false;
}
public String getStrLat() {
return strLat;
}
public String getStrLon() {
return strLon;
}
public double getDFLat() {
return dDFLat;
}
public double getDFLon() {
return dDFLon;
}
public double getFLat() {
return dFLat;
}
public double getFLon() {
return dFLon;
}
/**
*
* @return 緯度。單位:度
*/
public double getDLat() {
return dDLat;
}
/**
*
* @return 經度。單位:度
*/
public double getDLon() {
return dDLon;
}
public int getNetLat() {
return iNetLat;
}
public int getNetLon() {
return iNetLon;
}
/**
* 標準 轉 度分
*
*/
private boolean setStandardToDFCoordinate()
{
if(strLat.length() != 10 || strLon.length() != 11)
{
return false;
}
try
{
String temp = strLat.substring(1);
if (strLat.startsWith("N"))
{
this.dDFLat = Double.parseDouble(temp);
}
else if (strLat.startsWith("S"))
{
this.dDFLat = -Double.parseDouble(temp);
}
else
{
return false;
}
temp = strLon.substring(1);
if (strLon.startsWith("E"))
{
this.dDFLon = Double.parseDouble(temp);
}
else if (strLon.startsWith("W"))
{
this.dDFLon = -Double.parseDouble(temp);
}
else
{
return false;
}
}
catch(NumberFormatException e)
{
return false;
}
return true;
}
/**
* 度分 轉 分
*
*/
private boolean setFCoordinateWithDFCoordinate()
{
int iDLat = (int)(dDFLat/100);
int iDLon = (int)(dDFLon/100);
this.dFLat = iDLat*60+(dDFLat-iDLat*100);
this.dFLon = iDLon*60+(dDFLon-iDLon*100);
if(this.dFLat > 5400.0 || this.dFLat < -5400.0
|| this.dFLon > 10800.0 || this.dFLon < -10800.0)
{
return false;
}
return true;
}
/**
* 計算以度為單位的GPS格式
*/
public boolean calcDCoordinate()
{
int iDLat = (int)(dDFLat/100);
int iDLon = (int)(dDFLon/100);
this.dDLat = (dDFLat-iDLat*100)/60 + iDLat;
this.dDLon = (dDFLon-iDLon*100)/60 + iDLon;
return true;
}
/**
* 計算網格座標
* @param referGPS 參照站點的GPS
*/
public boolean calcNetCoordinate(GPS referGPS)
{
if(!this.isValid)
{
return false;
}
double minLat = referGPS.getFLat()-30;
double minLon = referGPS.getFLon()-30;
this.iNetLat = (int)((dFLat-minLat)/0.0058);
this.iNetLon = (int)((dFLon-minLon)/0.0053);
isNetValid = true;
return true;
}
/**
* 判斷車輛是否在定點範圍內
* @param busLat 車輛緯度(例:N2312.1230)
* @param busLon 車輛經度(例:E11223.1230)
* @param siteLat 定點緯度(例:N2312.1230)
* @param siteLon 定點經度(例:E11223.1230)
* @param radius 定點半徑
* @return true-是,false-否
*例子isInSiteRange("N2312.1230", "E11223.1230", "N2312.1230", "E11223.1230", 10);
*/
public static boolean isInSiteRange(String busLat, String busLon, String siteLat, String siteLon, int radius)
{
GPS busGps = new GPS();
busGps.setByStandardGps(busLat, busLon);
GPS siteGps = new GPS();
siteGps.setByStandardGps(siteLat, siteLon);
busGps.calcNetCoordinate(siteGps);
siteGps.calcNetCoordinate(siteGps);
//網格座標計算失敗
if(!busGps.isNetValid() || !busGps.isNetValid())
{
return false;
}
int x1 = busGps.getNetLon();
int y1 = busGps.getNetLat();
int x2 = siteGps.getNetLon();
int y2 = siteGps.getNetLat();
int distance = (int)(Math.sqrt(Math.abs((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)))*10);
return distance <= radius ? true : false;
}
}