c4f7deeb4423fa2b8968ea61d70759d5a0528fad.svn-base 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. package cn.com.goldenwater.dcproj.utils;
  2. import cn.com.goldenwater.dcproj.dto.LonLat;
  3. import net.sf.json.JSONArray;
  4. import net.sf.json.JSONObject;
  5. import org.apache.commons.io.IOUtils;
  6. import java.io.*;
  7. import java.math.BigDecimal;
  8. import java.net.*;
  9. import java.util.ArrayList;
  10. import java.util.HashMap;
  11. import java.util.List;
  12. import java.util.Map;
  13. public class MapUtil {
  14. public String getHttpResponse(String allConfigUrl) {
  15. BufferedReader in = null;
  16. StringBuffer result = null;
  17. try {
  18. // url请求中如果有中文,要在接收方用相应字符转码
  19. URI uri = new URI(allConfigUrl);
  20. URL url = uri.toURL();
  21. URLConnection connection = url.openConnection();
  22. connection.setRequestProperty("Content-type", "text/html");
  23. connection.setRequestProperty("Accept-Charset", "utf-8");
  24. connection.setRequestProperty("contentType", "utf-8");
  25. connection.connect();
  26. result = new StringBuffer();
  27. // 读取URL的响应
  28. in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
  29. String line;
  30. while ((line = in.readLine()) != null) {
  31. result.append(line);
  32. }
  33. return result.toString();
  34. } catch (Exception e) {
  35. e.printStackTrace();
  36. } finally {
  37. try {
  38. if (in != null) {
  39. in.close();
  40. }
  41. } catch (Exception e2) {
  42. e2.printStackTrace();
  43. }
  44. }
  45. return null;
  46. }
  47. public JSONObject getHttpRequest(String path, String params) {
  48. String result = "";
  49. String encoding = "UTF-8";
  50. byte[] data = new byte[0];
  51. try {
  52. data = params.getBytes(encoding);
  53. URL url = new URL(path);
  54. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  55. conn.setRequestMethod("POST");
  56. conn.setDoOutput(true);
  57. //application/x-javascript text/xml->xml数据 application/x-javascript->json对象 application/x-www-form-urlencoded->表单数据
  58. conn.setRequestProperty("Content-Type", "application/json; charset=" + encoding);
  59. conn.setRequestProperty("Content-Length", String.valueOf(data.length));
  60. conn.setConnectTimeout(5 * 1000);
  61. OutputStream outStream = conn.getOutputStream();
  62. outStream.write(data);
  63. outStream.flush();
  64. outStream.close();
  65. System.out.println(conn.getResponseCode()); //响应代码 200表示成功
  66. if (conn.getResponseCode() == 200) {
  67. InputStream inStream = conn.getInputStream();
  68. try {
  69. result = IOUtils.toString(inStream, "UTF-8");
  70. } catch (IOException e) {
  71. e.printStackTrace();
  72. } finally {
  73. try {
  74. inStream.close();
  75. } catch (IOException e) {
  76. e.printStackTrace();
  77. }
  78. }
  79. }
  80. JSONObject jsonobject = JSONObject.fromObject(result);
  81. return jsonobject;
  82. } catch (Exception e) {
  83. e.printStackTrace();
  84. }
  85. JSONObject jsonobject = JSONObject.fromObject(result);
  86. return jsonobject;
  87. }
  88. /**
  89. * 高德地图WebAPI : gps坐标转化为高德坐标 <br/>
  90. * String coordsys:高德地图坐标
  91. */
  92. public String convert(String coordsys) {
  93. try {
  94. coordsys = URLEncoder.encode(coordsys, "utf-8");
  95. } catch (UnsupportedEncodingException e) {
  96. e.printStackTrace();
  97. }
  98. String url = "http://restapi.amap.com/v3/assistant/coordinate/convert?locations=" + coordsys + "&coordsys=gps&output=json&key=fb03c6150b84f666bf69c86cba00721b";
  99. JSONObject jsonobject = JSONObject.fromObject(getHttpResponse(url));
  100. String coordinateString = jsonobject.getString("locations");
  101. return coordinateString;
  102. }
  103. public Map<String, Double> wgs84togcj02(double lng, double lat) {
  104. double dlat = transformlat(lng - 105.0, lat - 35.0);
  105. double dlng = transformlng(lng - 105.0, lat - 35.0);
  106. double radlat = lat / 180.0 * PI;
  107. double magic = Math.sin(radlat);
  108. magic = 1 - ee * magic * magic;
  109. double sqrtmagic = Math.sqrt(magic);
  110. dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * PI);
  111. dlng = (dlng * 180.0) / (a / sqrtmagic * Math.cos(radlat) * PI);
  112. double mglat = lat + dlat;
  113. double mglng = lng + dlng;
  114. Map<String, Double> d = new HashMap<>();
  115. d.put("lon", mglng);
  116. d.put("lat", mglat);
  117. return d;
  118. }
  119. public Map<String, Double> gcj02towgs84(double lng, double lat) {
  120. double dlat = transformlat(lng - 105.0, lat - 35.0);
  121. double dlng = transformlng(lng - 105.0, lat - 35.0);
  122. double radlat = lat / 180.0 * PI;
  123. double magic = Math.sin(radlat);
  124. magic = 1 - ee * magic * magic;
  125. double sqrtmagic = Math.sqrt(magic);
  126. dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * PI);
  127. dlng = (dlng * 180.0) / (a / sqrtmagic * Math.cos(radlat) * PI);
  128. double mglat = lat + dlat;
  129. double mglng = lng + dlng;
  130. Map<String, Double> d = new HashMap<>();
  131. d.put("lon", lng * 2 - mglng);
  132. d.put("lat", lat * 2 - mglat);
  133. return d;
  134. }
  135. //定义一些常量
  136. private double x_PI = 3.14159265358979324 * 3000.0 / 180.0;
  137. private double PI = 3.1415926535897932384626;
  138. private double a = 6378245.0;
  139. private double ee = 0.00669342162296594323;
  140. public double transformlng(double lng, double lat) {
  141. double ret = 300.0 + lng + 2.0 * lat + 0.1 * lng * lng + 0.1 * lng * lat + 0.1 * Math.sqrt(Math.abs(lng));
  142. ret += (20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * PI)) * 2.0 / 3.0;
  143. ret += (20.0 * Math.sin(lng * PI) + 40.0 * Math.sin(lng / 3.0 * PI)) * 2.0 / 3.0;
  144. ret += (150.0 * Math.sin(lng / 12.0 * PI) + 300.0 * Math.sin(lng / 30.0 * PI)) * 2.0 / 3.0;
  145. return ret;
  146. }
  147. public double transformlat(double lng, double lat) {
  148. 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));
  149. ret += (20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * PI)) * 2.0 / 3.0;
  150. ret += (20.0 * Math.sin(lat * PI) + 40.0 * Math.sin(lat / 3.0 * PI)) * 2.0 / 3.0;
  151. ret += (160.0 * Math.sin(lat / 12.0 * PI) + 320 * Math.sin(lat * PI / 30.0)) * 2.0 / 3.0;
  152. return ret;
  153. }
  154. public List<Map<String, Object>> getGeoJson(String middleType, String code, String scale) {
  155. List<Map<String, Object>> l = new ArrayList<>();
  156. String url = "http://hzz.mwr.gov.cn/gmr/getGeo/getGeoByCode?k=78CECB58104B369AE053BD00010A5976&area=000000000000&objCode=" + code + "&middleType=" + middleType + "&scale=" + scale;
  157. String geoString = getHttpResponse(url);
  158. JSONArray ja = JSONArray.fromObject(geoString);
  159. if (ja.size() > 0) {
  160. JSONObject jo = ja.getJSONObject(0);
  161. JSONObject geo = jo.getJSONObject("geo");
  162. JSONArray coordinates = geo.getJSONArray("coordinates");
  163. String type = geo.getString("type");
  164. if ("MultiLineString".equals(type) || "Polygon".equals(type)) {
  165. for (int i = 0; i < coordinates.size(); i++) {
  166. JSONArray coor = coordinates.getJSONArray(i);
  167. List<Map<String, Double>> a = new ArrayList();
  168. for (int j = 0; j < coor.size(); j++) {
  169. JSONArray dinates = coor.getJSONArray(j);
  170. Map<String, Double> d = wgs84togcj02(dinates.getDouble(0), dinates.getDouble(1));
  171. a.add(d);
  172. }
  173. Map<String, Object> b = new HashMap<>();
  174. b.put("pointList", a);
  175. l.add(b);
  176. }
  177. } else if ("MultiPolygon".equals(type)) {
  178. for (int i = 0; i < coordinates.size(); i++) {
  179. JSONArray coor = coordinates.getJSONArray(i);
  180. List<Map<String, Double>> a = new ArrayList();
  181. for (int j = 0; j < coor.size(); j++) {
  182. JSONArray dinates = coor.getJSONArray(j);
  183. for (int k = 0; k < dinates.size(); k++) {
  184. JSONArray points = dinates.getJSONArray(k);
  185. Map<String, Double> d = wgs84togcj02(points.getDouble(0), points.getDouble(1));
  186. a.add(d);
  187. }
  188. }
  189. Map<String, Object> b = new HashMap<>();
  190. b.put("pointList", a);
  191. l.add(b);
  192. }
  193. } else if ("LineString".equals(type)) {
  194. List<Map<String, Double>> a = new ArrayList();
  195. for (int i = 0; i < coordinates.size(); i++) {
  196. JSONArray coor = coordinates.getJSONArray(i);
  197. Map<String, Double> d = wgs84togcj02(coor.getDouble(0), coor.getDouble(1));
  198. a.add(d);
  199. }
  200. Map<String, Object> b = new HashMap<>();
  201. b.put("pointList", a);
  202. l.add(b);
  203. }
  204. }
  205. return l;
  206. }
  207. /**
  208. * 根据用户的起点和终点经纬度计算高德地图两点间距离,此距离为相对较短的距离,单位米。
  209. *
  210. * @return
  211. */
  212. public static int calculateLineDistance(double lonStart, double latStart, double lonEnd, double latEnd) {
  213. // if ((start == null) || (end == null))
  214. // {
  215. // throw new IllegalArgumentException("非法坐标值,不能为null");
  216. // }
  217. double d1 = 0.01745329251994329D;
  218. double d2 = lonStart;
  219. double d3 = latStart;
  220. double d4 = lonEnd;
  221. double d5 = latEnd;
  222. d2 *= d1;
  223. d3 *= d1;
  224. d4 *= d1;
  225. d5 *= d1;
  226. double d6 = Math.sin(d2);
  227. double d7 = Math.sin(d3);
  228. double d8 = Math.cos(d2);
  229. double d9 = Math.cos(d3);
  230. double d10 = Math.sin(d4);
  231. double d11 = Math.sin(d5);
  232. double d12 = Math.cos(d4);
  233. double d13 = Math.cos(d5);
  234. double[] arrayOfDouble1 = new double[3];
  235. double[] arrayOfDouble2 = new double[3];
  236. arrayOfDouble1[0] = (d9 * d8);
  237. arrayOfDouble1[1] = (d9 * d6);
  238. arrayOfDouble1[2] = d7;
  239. arrayOfDouble2[0] = (d13 * d12);
  240. arrayOfDouble2[1] = (d13 * d10);
  241. arrayOfDouble2[2] = d11;
  242. double d14 = Math.sqrt((arrayOfDouble1[0] - arrayOfDouble2[0]) * (arrayOfDouble1[0] - arrayOfDouble2[0])
  243. + (arrayOfDouble1[1] - arrayOfDouble2[1]) * (arrayOfDouble1[1] - arrayOfDouble2[1])
  244. + (arrayOfDouble1[2] - arrayOfDouble2[2]) * (arrayOfDouble1[2] - arrayOfDouble2[2]));
  245. double result = (Math.asin(d14 / 2.0D) * 12742001.579854401D);
  246. BigDecimal decimal = new BigDecimal(result).setScale(0, BigDecimal.ROUND_HALF_UP);
  247. return decimal.intValue();
  248. }
  249. public static int calculateArrayLineDistance(List<List> list) {
  250. int result = 0;
  251. for (int i = 0; i < list.size() - 1; i++) {
  252. List<Double> start = list.get(i);
  253. List<Double> end = list.get(i + 1);
  254. result += calculateLineDistance(start.get(0), start.get(1), end.get(0), end.get(1));
  255. }
  256. return result;
  257. }
  258. public static int calculateObjectLineDistance(List<LonLat> list) {
  259. int result = 0;
  260. for (int i = 0; i < list.size() - 1; i++) {
  261. LonLat start = list.get(i);
  262. LonLat end = list.get(i + 1);
  263. result += calculateLineDistance(start.getLon(), start.getLat(), end.getLon(), end.getLat());
  264. }
  265. return result;
  266. }
  267. }