CreateHouseJsonUtil.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package com.fdkankan.common.util;
  2. import java.io.IOException;
  3. import java.util.Map;
  4. import com.alibaba.fastjson.JSON;
  5. import com.alibaba.fastjson.JSONArray;
  6. import com.alibaba.fastjson.JSONObject;
  7. public class CreateHouseJsonUtil {
  8. /**
  9. * 根据用户上传的户型图json文件生成houseType.json
  10. * @param filePath
  11. * @return
  12. */
  13. public static JSONObject createHouseTypeJsonByUser(String filePath) {
  14. JSONObject house = init();
  15. JSONArray floors = house.getJSONArray("floors");
  16. JSONArray floorJson = readFloorJson(filePath);
  17. for(int i=0;i<floorJson.size();++i) {
  18. JSONObject floor = floorJson.getJSONObject(i);
  19. JSONObject floorData = convert(floor,i);
  20. floors.add(floorData);
  21. }
  22. return house;
  23. }
  24. private static JSONObject init() {
  25. JSONObject outContent = new JSONObject();
  26. outContent.put("name", "houseType.json");
  27. outContent.put("version", "2.1");
  28. outContent.put("floors", new JSONArray());
  29. outContent.put("newVectorId", null);
  30. outContent.put("setting", null);
  31. outContent.put("boundingBox", null);
  32. return outContent;
  33. }
  34. private static JSONArray readFloorJson(String filePath) {
  35. try {
  36. JSONObject floorplan = FileUtil.readJson(filePath);
  37. JSONArray floors = floorplan.getJSONArray("floors");
  38. return floors;
  39. } catch (IOException e) {
  40. return null;
  41. }
  42. }
  43. private static JSONObject convert(JSONObject floorJson,int floor) {
  44. JSONArray rooms = floorJson.getJSONArray("rooms");
  45. JSONObject walls = floorJson.getJSONObject("walls");
  46. JSONObject points = floorJson.getJSONObject("points");
  47. JSONObject symbols = floorJson.getJSONObject("symbols");
  48. JSONArray _points = convertPoints(points);
  49. JSONArray _walls = convertPoints(walls);
  50. JSONArray _symbols = convertSymbols(symbols,floor);
  51. JSONArray _rooms = convertRooms(rooms,floor);
  52. JSONObject floorData = new JSONObject();
  53. floorData.put("points", _points);
  54. floorData.put("walls", _walls);
  55. floorData.put("symbols", _symbols);
  56. floorData.put("rooms", _rooms);
  57. return floorData;
  58. }
  59. private static JSONArray convertPoints(JSONObject points) {
  60. JSONArray _points = new JSONArray();
  61. for (Map.Entry<String, Object> entry: points.entrySet()) {
  62. JSONObject pointValue = (JSONObject)entry.getValue();
  63. JSONObject _pointValue = new JSONObject();
  64. _pointValue.put("x", pointValue.getFloat("x"));
  65. _pointValue.put("y", pointValue.getFloat("y"));
  66. _pointValue.put("parent", pointValue.getJSONObject("parent"));
  67. _pointValue.put("vectorId", pointValue.getString("vectorId"));
  68. _points.add(_pointValue);
  69. }
  70. return _points;
  71. }
  72. private static JSONArray convertWalls(JSONObject walls) {
  73. JSONArray _walls = new JSONArray();
  74. for (Map.Entry<String, Object> entry: walls.entrySet()) {
  75. JSONObject wallValue = (JSONObject)entry.getValue();
  76. JSONObject _wallValue = new JSONObject();
  77. _wallValue.put("start", wallValue.getString("start"));
  78. _wallValue.put("end", wallValue.getString("end"));
  79. _wallValue.put("children", wallValue.getJSONArray("children"));
  80. _wallValue.put("vectorId", wallValue.getString("vectorId"));
  81. _wallValue.put("width", 0.2);
  82. //leftEdgeId
  83. //rightEdgeId
  84. _walls.add(_wallValue);
  85. }
  86. return _walls;
  87. }
  88. //门/窗
  89. private static JSONArray convertSymbols(JSONObject symbols,int floor) {
  90. JSONArray _symbols = new JSONArray();
  91. for (Map.Entry<String, Object> entry: symbols.entrySet()) {
  92. JSONObject symbolValue = (JSONObject)entry.getValue();
  93. JSONObject _symbolValue = new JSONObject();
  94. _symbolValue.put("start", symbolValue.getJSONObject("startPoint"));
  95. _symbolValue.put("end", symbolValue.getJSONObject("endPoint"));
  96. _symbolValue.put("parent", symbolValue.getString("parent"));
  97. _symbolValue.put("openSide", symbolValue.getString("openSide"));
  98. _symbolValue.put("vectorId", symbolValue.getString("vectorId"));
  99. _symbolValue.put("points2d", symbolValue.getJSONArray("points2d"));
  100. _symbolValue.put("geoType", symbolValue.getString("geoType"));
  101. _symbolValue.put("floor", floor);
  102. //"groundClearance": -0.7,
  103. //"height": 1.3,
  104. _symbols.add(_symbolValue);
  105. }
  106. return _symbols;
  107. }
  108. private static JSONArray convertRooms(JSONArray rooms,int floor) {
  109. JSONArray _rooms = new JSONArray();
  110. for(int i=0;i<rooms.size();++i) {
  111. JSONObject room = rooms.getJSONObject(i);
  112. String name = room.getString("name");
  113. JSONObject center = room.getJSONObject("center");
  114. String roomId = room.getString("roomId");
  115. JSONArray wallIds = room.getJSONArray("wallIds");
  116. JSONArray wallPointIDs = room.getJSONArray("wallPointIDs");
  117. String parent = room.getString("parent");
  118. JSONObject _room = new JSONObject();
  119. _room.put("name", name);
  120. _room.put("roomId", roomId);
  121. _room.put("center", center);
  122. _room.put("wallIds", wallIds);
  123. _room.put("wallPointIDs", wallPointIDs);
  124. _room.put("parent", parent);
  125. _rooms.add(_room);
  126. }
  127. return _rooms;
  128. }
  129. /*
  130. //构件,包括:柱子,烟囱等
  131. private JSONObject convertComponents(JSONObject components) {
  132. JSONArray beams = new JSONArray();
  133. JSONArray flues = new JSONArray();
  134. JSONObject result = new JSONObject();
  135. for (Map.Entry<String, Object> entry: components.entrySet()) {
  136. JSONObject componentValue = (JSONObject)entry.getValue();
  137. JSONObject _componentValue = new JSONObject();
  138. String geoType = componentValue.getString("geoType");
  139. _componentValue.put("center", componentValue.getJSONObject("center"));
  140. _componentValue.put("angle", componentValue.getIntValue("angle"));
  141. _componentValue.put("vectorId", componentValue.getString("vectorId"));
  142. _componentValue.put("points2d", componentValue.getJSONArray("points2d"));
  143. _componentValue.put("geoType", componentValue.getString("geoType"));
  144. _componentValue.put("sideWidth", componentValue.getFloatValue("sideWidth"));
  145. _componentValue.put("sideThickness", componentValue.getFloatValue("sideThickness"));
  146. if(geoType.endsWith("Beam")) {
  147. beams.add(_componentValue);
  148. }
  149. else if(geoType.endsWith("Flue")) {
  150. flues.add(_componentValue);
  151. }
  152. }
  153. result.put("beams", beams);
  154. result.put("flues", flues);
  155. return result;
  156. }
  157. */
  158. //rooms
  159. //tags
  160. }