From 6d2ef705c639016784abbd30401a7e7d7a72b6ec Mon Sep 17 00:00:00 2001 From: Junjie <xjj@123> Date: 星期五, 22 十一月 2024 13:21:58 +0800 Subject: [PATCH] #读取地图文件优化 --- src/main/java/com/zy/common/utils/NavigateMapData.java | 250 +++++++++++++++++++++++++++++-------------------- 1 files changed, 148 insertions(+), 102 deletions(-) diff --git a/src/main/java/com/zy/common/utils/NavigateMapData.java b/src/main/java/com/zy/common/utils/NavigateMapData.java index b843053..554de1b 100644 --- a/src/main/java/com/zy/common/utils/NavigateMapData.java +++ b/src/main/java/com/zy/common/utils/NavigateMapData.java @@ -11,6 +11,7 @@ import com.zy.common.model.enums.NavigationMapType; import com.zy.core.enums.RedisKeyType; import com.zy.core.enums.ShuttleTaskModeType; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.ClassPathResource; import org.springframework.stereotype.Component; @@ -24,6 +25,9 @@ */ @Component public class NavigateMapData { + + @Autowired + private RedisUtil redisUtil; private Integer lev;//鍦板浘妤煎眰 @@ -41,51 +45,72 @@ public int[][] getData(Integer mapType, List<int[]> whitePoints, List<int[]> shuttlePoints) { try { - String mapFilename = "map_" + lev + ".json"; - ClassPathResource classPathResource = new ClassPathResource(mapFilename); - InputStream inputStream = classPathResource.getInputStream(); - byte[] buffer = new byte[inputStream.available()]; - inputStream.read(buffer); - File file = File.createTempFile("prefix","suffix"); - try (OutputStream outStream = new FileOutputStream(file)) { - outStream.write(buffer); + Object object = redisUtil.get(RedisKeyType.BASIC_MAP.key + lev); + if (object == null) { + return null; } - StringBuffer stringBuffer = new StringBuffer(); - if (file.isFile() && file.exists()) { - InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "GBK"); - BufferedReader br = new BufferedReader(isr); - String lineTxt = null; - while ((lineTxt = br.readLine()) != null) { - stringBuffer.append(lineTxt); + //瑙f瀽json鍦板浘鏁版嵁 + ArrayList arrayList = JSON.parseObject(object.toString(), ArrayList.class); + List<List<MapNode>> lists = filterMap(mapType, arrayList, lev, whitePoints, shuttlePoints);//杩囨护鍦板浘鏁版嵁 + int[][] map = new int[lists.size()][]; + int j = 0; + for (List<MapNode> list : lists) { + int[] tmp = new int[list.size()]; + int i = 0; + for (MapNode mapNode : list) { + //灏嗘暟鎹坊鍔犺繘浜岀淮鏁扮粍 + tmp[i++] = mapNode.getValue(); } - br.close(); - - //瑙f瀽json鍦板浘鏁版嵁 - ArrayList arrayList = JSON.parseObject(stringBuffer.toString(), ArrayList.class); - List<List<MapNode>> lists = filterMap(mapType, arrayList, lev, whitePoints, shuttlePoints);//杩囨护鍦板浘鏁版嵁 - int[][] map = new int[lists.size()][]; - int j = 0; - for (List<MapNode> list : lists) { - int[] tmp = new int[list.size()]; - int i = 0; - for (MapNode mapNode : list) { - //灏嗘暟鎹坊鍔犺繘浜岀淮鏁扮粍 - tmp[i++] = mapNode.getValue(); - } - //鏁版嵁娣诲姞杩涗竴缁存暟缁� - map[j++] = tmp; - } - - if (!file.delete()) { - System.out.println("涓存椂鏂囦欢鍒犻櫎澶辫触"); - } - return map; - } else { - System.out.println("鏂囦欢涓嶅瓨鍦�!"); + //鏁版嵁娣诲姞杩涗竴缁存暟缁� + map[j++] = tmp; } - } catch (IOException ioException) { - ioException.printStackTrace(); + +// String mapFilename = "map_" + lev + ".json"; +// ClassPathResource classPathResource = new ClassPathResource(mapFilename); +// InputStream inputStream = classPathResource.getInputStream(); +// byte[] buffer = new byte[inputStream.available()]; +// inputStream.read(buffer); +// File file = File.createTempFile("prefix","suffix"); +// try (OutputStream outStream = new FileOutputStream(file)) { +// outStream.write(buffer); +// } +// +// StringBuffer stringBuffer = new StringBuffer(); +// if (file.isFile() && file.exists()) { +// InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "GBK"); +// BufferedReader br = new BufferedReader(isr); +// String lineTxt = null; +// while ((lineTxt = br.readLine()) != null) { +// stringBuffer.append(lineTxt); +// } +// br.close(); +// +// //瑙f瀽json鍦板浘鏁版嵁 +// ArrayList arrayList = JSON.parseObject(stringBuffer.toString(), ArrayList.class); +// List<List<MapNode>> lists = filterMap(mapType, arrayList, lev, whitePoints, shuttlePoints);//杩囨护鍦板浘鏁版嵁 +// int[][] map = new int[lists.size()][]; +// int j = 0; +// for (List<MapNode> list : lists) { +// int[] tmp = new int[list.size()]; +// int i = 0; +// for (MapNode mapNode : list) { +// //灏嗘暟鎹坊鍔犺繘浜岀淮鏁扮粍 +// tmp[i++] = mapNode.getValue(); +// } +// //鏁版嵁娣诲姞杩涗竴缁存暟缁� +// map[j++] = tmp; +// } +// +// if (!file.delete()) { +// System.out.println("涓存椂鏂囦欢鍒犻櫎澶辫触"); +// } +// return map; +// } else { +// System.out.println("鏂囦欢涓嶅瓨鍦�!"); +// } + } catch (Exception e) { + e.printStackTrace(); } return null; } @@ -129,40 +154,51 @@ //鑾峰彇JSON鏍煎紡鏁版嵁 public List<List<MapNode>> getJsonData(Integer mapType, List<int[]> whitePoints, List<int[]> shuttlePoints) { try { - String mapFilename = "map_" + lev + ".json"; - ClassPathResource classPathResource = new ClassPathResource(mapFilename); - InputStream inputStream = classPathResource.getInputStream(); - byte[] buffer = new byte[inputStream.available()]; - inputStream.read(buffer); - File file = File.createTempFile("prefix","suffix"); - try (OutputStream outStream = new FileOutputStream(file)) { - outStream.write(buffer); + Object object = redisUtil.get(RedisKeyType.BASIC_MAP.key + lev); + if (object == null) { + return null; } - StringBuffer stringBuffer = new StringBuffer(); - if (file.isFile() && file.exists()) { - InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "GBK"); - BufferedReader br = new BufferedReader(isr); - String lineTxt = null; - while ((lineTxt = br.readLine()) != null) { - stringBuffer.append(lineTxt); - } - br.close(); + //瑙f瀽json鍦板浘鏁版嵁 + ArrayList arrayList = JSON.parseObject(object.toString(), ArrayList.class); + List<List<MapNode>> lists = filterMap(mapType, arrayList, lev, whitePoints, shuttlePoints);//杩囨护鍦板浘鏁版嵁 + return lists; - //瑙f瀽json鍦板浘鏁版嵁 - ArrayList arrayList = JSON.parseObject(stringBuffer.toString(), ArrayList.class); - List<List<MapNode>> lists = filterMap(mapType, arrayList, lev, whitePoints, shuttlePoints);//杩囨护鍦板浘鏁版嵁 - if (!file.delete()) { - System.out.println("涓存椂鏂囦欢鍒犻櫎澶辫触"); - } - - return lists; - } else { - System.out.println("鏂囦欢涓嶅瓨鍦�!"); - } - } catch (IOException ioException) { - ioException.printStackTrace(); +// String mapFilename = "map_" + lev + ".json"; +// ClassPathResource classPathResource = new ClassPathResource(mapFilename); +// InputStream inputStream = classPathResource.getInputStream(); +// byte[] buffer = new byte[inputStream.available()]; +// inputStream.read(buffer); +// File file = File.createTempFile("prefix","suffix"); +// try (OutputStream outStream = new FileOutputStream(file)) { +// outStream.write(buffer); +// } +// +// StringBuffer stringBuffer = new StringBuffer(); +// if (file.isFile() && file.exists()) { +// InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "GBK"); +// BufferedReader br = new BufferedReader(isr); +// String lineTxt = null; +// while ((lineTxt = br.readLine()) != null) { +// stringBuffer.append(lineTxt); +// } +// br.close(); +// +// //瑙f瀽json鍦板浘鏁版嵁 +// ArrayList arrayList = JSON.parseObject(stringBuffer.toString(), ArrayList.class); +// List<List<MapNode>> lists = filterMap(mapType, arrayList, lev, whitePoints, shuttlePoints);//杩囨护鍦板浘鏁版嵁 +// +// if (!file.delete()) { +// System.out.println("涓存椂鏂囦欢鍒犻櫎澶辫触"); +// } +// +// return lists; +// } else { +// System.out.println("鏂囦欢涓嶅瓨鍦�!"); +// } + } catch (Exception e) { + e.printStackTrace(); } return null; } @@ -170,40 +206,50 @@ //鑾峰彇JSON鏍煎紡鏁版嵁 public List<List<MapNode>> getJsonData(Integer lev, Integer mapType, List<int[]> whitePoints, List<int[]> shuttlePoints) { try { - String mapFilename = "map_" + lev + ".json"; - ClassPathResource classPathResource = new ClassPathResource(mapFilename); - InputStream inputStream = classPathResource.getInputStream(); - byte[] buffer = new byte[inputStream.available()]; - inputStream.read(buffer); - File file = File.createTempFile("prefix","suffix"); - try (OutputStream outStream = new FileOutputStream(file)) { - outStream.write(buffer); + Object object = redisUtil.get(RedisKeyType.BASIC_MAP.key + lev); + if (object == null) { + return null; } - StringBuffer stringBuffer = new StringBuffer(); - if (file.isFile() && file.exists()) { - InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "GBK"); - BufferedReader br = new BufferedReader(isr); - String lineTxt = null; - while ((lineTxt = br.readLine()) != null) { - stringBuffer.append(lineTxt); - } - br.close(); + //瑙f瀽json鍦板浘鏁版嵁 + ArrayList arrayList = JSON.parseObject(object.toString(), ArrayList.class); + List<List<MapNode>> lists = filterMap(mapType, arrayList, lev, whitePoints, shuttlePoints);//杩囨护鍦板浘鏁版嵁 + return lists; - //瑙f瀽json鍦板浘鏁版嵁 - ArrayList arrayList = JSON.parseObject(stringBuffer.toString(), ArrayList.class); - List<List<MapNode>> lists = filterMap(mapType, arrayList, lev, whitePoints, shuttlePoints);//杩囨护鍦板浘鏁版嵁 - - if (!file.delete()) { - System.out.println("涓存椂鏂囦欢鍒犻櫎澶辫触"); - } - - return lists; - } else { - System.out.println("鏂囦欢涓嶅瓨鍦�!"); - } - } catch (IOException ioException) { - ioException.printStackTrace(); +// String mapFilename = "map_" + lev + ".json"; +// ClassPathResource classPathResource = new ClassPathResource(mapFilename); +// InputStream inputStream = classPathResource.getInputStream(); +// byte[] buffer = new byte[inputStream.available()]; +// inputStream.read(buffer); +// File file = File.createTempFile("prefix","suffix"); +// try (OutputStream outStream = new FileOutputStream(file)) { +// outStream.write(buffer); +// } +// +// StringBuffer stringBuffer = new StringBuffer(); +// if (file.isFile() && file.exists()) { +// InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "GBK"); +// BufferedReader br = new BufferedReader(isr); +// String lineTxt = null; +// while ((lineTxt = br.readLine()) != null) { +// stringBuffer.append(lineTxt); +// } +// br.close(); +// +// //瑙f瀽json鍦板浘鏁版嵁 +// ArrayList arrayList = JSON.parseObject(stringBuffer.toString(), ArrayList.class); +// List<List<MapNode>> lists = filterMap(mapType, arrayList, lev, whitePoints, shuttlePoints);//杩囨护鍦板浘鏁版嵁 +// +// if (!file.delete()) { +// System.out.println("涓存椂鏂囦欢鍒犻櫎澶辫触"); +// } +// +// return lists; +// } else { +// System.out.println("鏂囦欢涓嶅瓨鍦�!"); +// } + } catch (Exception e) { + e.printStackTrace(); } return null; } -- Gitblit v1.9.1