Skip to content

Commit

Permalink
upgrade version
Browse files Browse the repository at this point in the history
  • Loading branch information
LJWLgl committed May 22, 2020
1 parent 61b5a23 commit 97b053b
Show file tree
Hide file tree
Showing 7 changed files with 351 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<dependency>
<groupId>io.github.ljwlgl</groupId>
<artifactId>common-util</artifactId>
<version>2.0.1</version>
<version>2.0.7</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.github.ljwlgl</groupId>
<artifactId>common-util</artifactId>
<version>2.0.5</version>
<version>2.0.7</version>
<name>common-util</name>
<url>https://github.com/LJWLgl/CommonUtil</url>
<description>Commonly used tools for business development</description>
Expand Down
222 changes: 222 additions & 0 deletions src/main/java/io/github/ljwlgl/util/CoordinateTransformUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
package io.github.ljwlgl.util;

/**
* @author zq_gan
* @since 2019/10/23
*
* 百度坐标(BD09)、国测局坐标(火星坐标,GCJ02)、和WGS84坐标系之间的转换的工具
* 参考 https://github.com/wandergis/coordtransform 实现的Java版本
**/

public class CoordinateTransformUtil {
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;


/**
* 将原本坐标系的经纬度转换成新的坐标系的经纬度
* @param newCoordinateType 新坐标系,如baidu,wgs84
* @param originalCoordinateType 原坐标系,如baidu,wgs84
* @param lat 原纬度
* @param lon 原经度
* @return 新坐标系的经纬度
*/
public static double[] convertLatLonByCoordinate(String newCoordinateType, String originalCoordinateType, double lat, double lon) {
if (originalCoordinateType == null) {
return null;
}

boolean bd09ToWgs84 = (originalCoordinateType.equalsIgnoreCase("bd09") || originalCoordinateType.equalsIgnoreCase("baidu")
&& (newCoordinateType.equalsIgnoreCase("google")) || newCoordinateType.equalsIgnoreCase("wgs84"));
boolean gcj02toWgs84 = (originalCoordinateType.equalsIgnoreCase("gaode") || originalCoordinateType.equalsIgnoreCase("gcj02")
&& (newCoordinateType.equalsIgnoreCase("google")) || newCoordinateType.equalsIgnoreCase("wgs84"));

boolean wgs84ToBd09 = (originalCoordinateType.equalsIgnoreCase("google") || originalCoordinateType.equalsIgnoreCase("wgs84"))
&& (newCoordinateType.equalsIgnoreCase("bd09") || newCoordinateType.equalsIgnoreCase("baidu"));
boolean gcj02ToBd09 = (originalCoordinateType.equalsIgnoreCase("gaode") || originalCoordinateType.equalsIgnoreCase("gcj02"))
&& (newCoordinateType.equalsIgnoreCase("bd09") || newCoordinateType.equalsIgnoreCase("baidu"));

boolean wgs84ToGcj02 = (originalCoordinateType.equalsIgnoreCase("google") || originalCoordinateType.equalsIgnoreCase("wgs84"))
&& (newCoordinateType.equalsIgnoreCase("gaode") || newCoordinateType.equalsIgnoreCase("gcj02"));
boolean bd09ToGcj02 = (originalCoordinateType.equalsIgnoreCase("bd09") || originalCoordinateType.equalsIgnoreCase("baidu"))
&& (newCoordinateType.equalsIgnoreCase("gaode") || newCoordinateType.equalsIgnoreCase("gcj02"));

if (originalCoordinateType.equals(newCoordinateType)) {
return new double[]{lat, lon};
} else if (bd09ToWgs84) {
return bd09towgs84(lon, lat);
} else if (gcj02toWgs84) {
return gcj02towgs84(lon, lat);
} else if (wgs84ToBd09) {
return wgs84tobd09(lon, lat);
} else if (gcj02ToBd09) {
return gcj02tobd09(lon, lat);
} else if (wgs84ToGcj02) {
return wgs84togcj02(lon, lat);
} else if (bd09ToGcj02) {
return bd09togcj02(lon, lat);
} else {
return null;
}
}

/**
* 百度坐标系(BD-09)转WGS坐标
*
* @param lng 百度坐标纬度
* @param lat 百度坐标经度
* @return WGS84坐标数组
*/
public static double[] bd09towgs84(double lng, double lat) {
double[] gcj = bd09togcj02(lng, lat);
double[] wgs84 = gcj02towgs84(gcj[0], gcj[1]);
return wgs84;
}

/**
* WGS坐标转百度坐标系(BD-09)
*
* @param lng WGS84坐标系的经度
* @param lat WGS84坐标系的纬度
* @return 百度坐标数组
*/
public static double[] wgs84tobd09(double lng, double lat) {
double[] gcj = wgs84togcj02(lng, lat);
double[] bd09 = gcj02tobd09(gcj[0], gcj[1]);
return bd09;
}

/**
* 火星坐标系(GCJ-02)转百度坐标系(BD-09)
*
* 谷歌、高德——>百度
* @param lng 火星坐标经度
* @param lat 火星坐标纬度
* @return 百度坐标数组
*/
public static double[] gcj02tobd09(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;
return new double[] { bd_lng, bd_lat };
}

/**
* 百度坐标系(BD-09)转火星坐标系(GCJ-02)
*
* 百度——>谷歌、高德
* @param bd_lon 百度坐标纬度
* @param bd_lat 百度坐标经度
* @return 火星坐标数组
*/
public static double[] bd09togcj02(double bd_lon, double bd_lat) {
double x = bd_lon - 0.0065;
double y = bd_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 gg_lng = z * Math.cos(theta);
double gg_lat = z * Math.sin(theta);
return new double[] { gg_lng, gg_lat };
}

/**
* WGS84转GCJ02(火星坐标系)
*
* @param lng WGS84坐标系的经度
* @param lat WGS84坐标系的纬度
* @return 火星坐标数组
*/
public static double[] wgs84togcj02(double lng, double lat) {
if (out_of_china(lng, lat)) {
return new double[] { lng, 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;
return new double[] { mglng, mglat };
}

/**
* GCJ02(火星坐标系)转GPS84
*
* @param lng 火星坐标系的经度
* @param lat 火星坐标系纬度
* @return WGS84坐标数组
*/
public static double[] gcj02towgs84(double lng, double lat) {
if (out_of_china(lng, lat)) {
return new double[] { lng, 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;
return new double[] { lng * 2 - mglng, lat * 2 - mglat };
}

/**
* 纬度转换
*
* @param lng
* @param lat
* @return
*/
public 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;
}

/**
* 经度转换
*
* @param lng
* @param lat
* @return
*/
public 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;
}

/**
* 判断是否在国内,不在国内不做偏移
*
* @param lng
* @param lat
* @return
*/
public static boolean out_of_china(double lng, double lat) {
if (lng < 72.004 || lng > 137.8347) {
return true;
} else if (lat < 0.8293 || lat > 55.8271) {
return true;
}
return false;
}

}
12 changes: 7 additions & 5 deletions src/main/java/io/github/ljwlgl/util/GZIPUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
import java.util.zip.GZIPOutputStream;

/**
* Created by zczhang on 2017/4/18.
*/
* @author zqgan
* @since 2019/4/8
**/

public class GZIPUtil {

private static Logger logger = LoggerFactory.getLogger(GZIPUtil.class);
Expand Down Expand Up @@ -66,7 +68,7 @@ public static byte[] compress(byte[] bytes) {
try {
gzip = new GZIPOutputStream(out);
gzip.write(bytes);

} catch (IOException e) {
logger.error(e.getMessage(), e);
}finally {
Expand Down Expand Up @@ -102,7 +104,7 @@ public static byte[] uncompress(byte[] bytes) {
out.write(buffer, 0, n);
}
in.close();

} catch (IOException e) {
logger.error(e.getMessage(), e);
}finally {
Expand Down Expand Up @@ -153,4 +155,4 @@ public static String uncompress2Str(byte[] bytes, String encoding) {
}
return str;
}
}
}
62 changes: 62 additions & 0 deletions src/main/java/io/github/ljwlgl/util/LanguageUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package io.github.ljwlgl.util;

import java.util.regex.Pattern;

/**
* @author zq_gan
* @since 2020/5/21
**/

public class LanguageUtil {


/**
* 是否只有字母
*/
public static boolean isOnlyLetter(String str) {
return str != null && str.length() > 0 && Pattern.matches("^[A-Za-z]+$", str);
}

/**
* 是否只有数字和空格
*/
public static boolean isLetter(String str) {
return str != null && str.length() > 0 && Pattern.matches("^[A-Za-z\\u0020]+$", str);
}

/**
* 是否只有汉字和空格
*/
public static boolean isChinese(String str) {
return str != null && str.length() > 0 && Pattern.matches("^[\\u4e00-\\u9fa5\\u0020]+$", str);
}

/**
* 是否只有英文和数字
*/
public static boolean isLetterAndNumber(String str) {
return str != null && str.length() > 0 && Pattern.matches("^[0-9A-Za-z\\u0020]+$", str);
}

/**
* 是否只有中文和数字
*/
public static boolean isChineseAndNumber(String str) {
return str != null && str.length() > 0 && Pattern.matches("^[0-9\\u4e00-\\u9fa5\\u0020]+$", str);
}

/**
* 是否只有韩文和数字
*/
public static boolean isKoreanAndNumber(String str) {
return str != null && str.length() > 0 && Pattern.matches("^[0-9\\uac00-\\ud7a3\\u0020]+$", str);
}

/**
* 是否只有日文和数字
*/
public static boolean isJapanAndNumber(String str) {
return str != null && str.length() > 0 && Pattern.matches("^[0-9\\u0800-\\u4e00\\u0020]+$", str);
}

}
Loading

0 comments on commit 97b053b

Please sign in to comment.