| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- package cn.com.goldenwater.dcproj.utils;
- import cn.com.goldenwater.dcproj.dto.LonLat;
- import net.sf.json.JSONArray;
- import net.sf.json.JSONObject;
- import org.apache.commons.io.IOUtils;
- import java.io.*;
- import java.math.BigDecimal;
- import java.net.*;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- public class MapUtil {
- public String getHttpResponse(String allConfigUrl) {
- BufferedReader in = null;
- StringBuffer result = null;
- try {
- // url请求中如果有中文,要在接收方用相应字符转码
- URI uri = new URI(allConfigUrl);
- URL url = uri.toURL();
- URLConnection connection = url.openConnection();
- connection.setRequestProperty("Content-type", "text/html");
- connection.setRequestProperty("Accept-Charset", "utf-8");
- connection.setRequestProperty("contentType", "utf-8");
- connection.connect();
- result = new StringBuffer();
- // 读取URL的响应
- in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
- String line;
- while ((line = in.readLine()) != null) {
- result.append(line);
- }
- return result.toString();
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- if (in != null) {
- in.close();
- }
- } catch (Exception e2) {
- e2.printStackTrace();
- }
- }
- return null;
- }
- public JSONObject getHttpRequest(String path, String params) {
- String result = "";
- String encoding = "UTF-8";
- byte[] data = new byte[0];
- try {
- data = params.getBytes(encoding);
- URL url = new URL(path);
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- conn.setRequestMethod("POST");
- conn.setDoOutput(true);
- //application/x-javascript text/xml->xml数据 application/x-javascript->json对象 application/x-www-form-urlencoded->表单数据
- conn.setRequestProperty("Content-Type", "application/json; charset=" + encoding);
- conn.setRequestProperty("Content-Length", String.valueOf(data.length));
- conn.setConnectTimeout(5 * 1000);
- OutputStream outStream = conn.getOutputStream();
- outStream.write(data);
- outStream.flush();
- outStream.close();
- System.out.println(conn.getResponseCode()); //响应代码 200表示成功
- if (conn.getResponseCode() == 200) {
- InputStream inStream = conn.getInputStream();
- try {
- result = IOUtils.toString(inStream, "UTF-8");
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- inStream.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- JSONObject jsonobject = JSONObject.fromObject(result);
- return jsonobject;
- } catch (Exception e) {
- e.printStackTrace();
- }
- JSONObject jsonobject = JSONObject.fromObject(result);
- return jsonobject;
- }
- /**
- * 高德地图WebAPI : gps坐标转化为高德坐标 <br/>
- * String coordsys:高德地图坐标
- */
- public String convert(String coordsys) {
- try {
- coordsys = URLEncoder.encode(coordsys, "utf-8");
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- String url = "http://restapi.amap.com/v3/assistant/coordinate/convert?locations=" + coordsys + "&coordsys=gps&output=json&key=fb03c6150b84f666bf69c86cba00721b";
- JSONObject jsonobject = JSONObject.fromObject(getHttpResponse(url));
- String coordinateString = jsonobject.getString("locations");
- return coordinateString;
- }
- public Map<String, Double> wgs84togcj02(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;
- Map<String, Double> d = new HashMap<>();
- d.put("lon", mglng);
- d.put("lat", mglat);
- return d;
- }
- public Map<String, Double> gcj02towgs84(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;
- Map<String, Double> d = new HashMap<>();
- d.put("lon", lng * 2 - mglng);
- d.put("lat", lat * 2 - mglat);
- return d;
- }
- //定义一些常量
- private double x_PI = 3.14159265358979324 * 3000.0 / 180.0;
- private double PI = 3.1415926535897932384626;
- private double a = 6378245.0;
- private double ee = 0.00669342162296594323;
- public 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;
- }
- public 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;
- }
- public List<Map<String, Object>> getGeoJson(String middleType, String code, String scale) {
- List<Map<String, Object>> l = new ArrayList<>();
- String url = "http://hzz.mwr.gov.cn/gmr/getGeo/getGeoByCode?k=78CECB58104B369AE053BD00010A5976&area=000000000000&objCode=" + code + "&middleType=" + middleType + "&scale=" + scale;
- String geoString = getHttpResponse(url);
- JSONArray ja = JSONArray.fromObject(geoString);
- if (ja.size() > 0) {
- JSONObject jo = ja.getJSONObject(0);
- JSONObject geo = jo.getJSONObject("geo");
- JSONArray coordinates = geo.getJSONArray("coordinates");
- String type = geo.getString("type");
- if ("MultiLineString".equals(type) || "Polygon".equals(type)) {
- for (int i = 0; i < coordinates.size(); i++) {
- JSONArray coor = coordinates.getJSONArray(i);
- List<Map<String, Double>> a = new ArrayList();
- for (int j = 0; j < coor.size(); j++) {
- JSONArray dinates = coor.getJSONArray(j);
- Map<String, Double> d = wgs84togcj02(dinates.getDouble(0), dinates.getDouble(1));
- a.add(d);
- }
- Map<String, Object> b = new HashMap<>();
- b.put("pointList", a);
- l.add(b);
- }
- } else if ("MultiPolygon".equals(type)) {
- for (int i = 0; i < coordinates.size(); i++) {
- JSONArray coor = coordinates.getJSONArray(i);
- List<Map<String, Double>> a = new ArrayList();
- for (int j = 0; j < coor.size(); j++) {
- JSONArray dinates = coor.getJSONArray(j);
- for (int k = 0; k < dinates.size(); k++) {
- JSONArray points = dinates.getJSONArray(k);
- Map<String, Double> d = wgs84togcj02(points.getDouble(0), points.getDouble(1));
- a.add(d);
- }
- }
- Map<String, Object> b = new HashMap<>();
- b.put("pointList", a);
- l.add(b);
- }
- } else if ("LineString".equals(type)) {
- List<Map<String, Double>> a = new ArrayList();
- for (int i = 0; i < coordinates.size(); i++) {
- JSONArray coor = coordinates.getJSONArray(i);
- Map<String, Double> d = wgs84togcj02(coor.getDouble(0), coor.getDouble(1));
- a.add(d);
- }
- Map<String, Object> b = new HashMap<>();
- b.put("pointList", a);
- l.add(b);
- }
- }
- return l;
- }
- /**
- * 根据用户的起点和终点经纬度计算高德地图两点间距离,此距离为相对较短的距离,单位米。
- *
- * @return
- */
- public static int calculateLineDistance(double lonStart, double latStart, double lonEnd, double latEnd) {
- // if ((start == null) || (end == null))
- // {
- // throw new IllegalArgumentException("非法坐标值,不能为null");
- // }
- double d1 = 0.01745329251994329D;
- double d2 = lonStart;
- double d3 = latStart;
- double d4 = lonEnd;
- double d5 = latEnd;
- d2 *= d1;
- d3 *= d1;
- d4 *= d1;
- d5 *= d1;
- double d6 = Math.sin(d2);
- double d7 = Math.sin(d3);
- double d8 = Math.cos(d2);
- double d9 = Math.cos(d3);
- double d10 = Math.sin(d4);
- double d11 = Math.sin(d5);
- double d12 = Math.cos(d4);
- double d13 = Math.cos(d5);
- double[] arrayOfDouble1 = new double[3];
- double[] arrayOfDouble2 = new double[3];
- arrayOfDouble1[0] = (d9 * d8);
- arrayOfDouble1[1] = (d9 * d6);
- arrayOfDouble1[2] = d7;
- arrayOfDouble2[0] = (d13 * d12);
- arrayOfDouble2[1] = (d13 * d10);
- arrayOfDouble2[2] = d11;
- double d14 = Math.sqrt((arrayOfDouble1[0] - arrayOfDouble2[0]) * (arrayOfDouble1[0] - arrayOfDouble2[0])
- + (arrayOfDouble1[1] - arrayOfDouble2[1]) * (arrayOfDouble1[1] - arrayOfDouble2[1])
- + (arrayOfDouble1[2] - arrayOfDouble2[2]) * (arrayOfDouble1[2] - arrayOfDouble2[2]));
- double result = (Math.asin(d14 / 2.0D) * 12742001.579854401D);
- BigDecimal decimal = new BigDecimal(result).setScale(0, BigDecimal.ROUND_HALF_UP);
- return decimal.intValue();
- }
- public static int calculateArrayLineDistance(List<List> list) {
- int result = 0;
- for (int i = 0; i < list.size() - 1; i++) {
- List<Double> start = list.get(i);
- List<Double> end = list.get(i + 1);
- result += calculateLineDistance(start.get(0), start.get(1), end.get(0), end.get(1));
- }
- return result;
- }
- public static int calculateObjectLineDistance(List<LonLat> list) {
- int result = 0;
- for (int i = 0; i < list.size() - 1; i++) {
- LonLat start = list.get(i);
- LonLat end = list.get(i + 1);
- result += calculateLineDistance(start.getLon(), start.getLat(), end.getLon(), end.getLat());
- }
- return result;
- }
- }
|