package com.fdkankan.model.utils; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.fdkankan.common.util.FileUtils; import com.fdkankan.model.bean.PointBean; import com.fdkankan.model.bean.SegmentBean; import com.fdkankan.model.bean.VertexBean; import com.fdkankan.model.bean.WallBean; import java.io.IOException; import java.util.Collection; import java.util.HashMap; import java.util.Map; public class CreateHouseJsonUtil { /** * 根据用户上传的户型图json文件生成houseType.json * @param filePath * @return */ public static JSONObject createHouseTypeJsonByUser(String filePath) { JSONObject house = init(); JSONArray floors = house.getJSONArray("floors"); JSONArray floorJson = readFloorJson(filePath); for(int i=0;i vertexMap = new HashMap<>(); Map pointMap = new HashMap<>(); Map vpMap = new HashMap<>(); JSONArray vertexArr = floor.getJSONArray("vertex-xy"); for(int i = 0; i < vertexArr.size(); i++){ Object o = vertexArr.get(i); VertexBean vertexBean = JSON.parseObject(JSON.toJSONString(o), VertexBean.class); Integer vertexId = vertexBean.getId(); vertexMap.put(vertexId, vertexBean); String pointId = "Point" + i; pointMap.put(pointId, PointBean.builder().vectorId(pointId).x(vertexBean.getX()).y(vertexBean.getY()).build()); vpMap.put(vertexId, pointId); } //处理墙 Map segmentMap = new HashMap<>(); Map wallMap = new HashMap<>(); Map swMap = new HashMap<>(); JSONArray segmentArr = floor.getJSONArray("segment"); Map startMap = new HashMap<>(); Map endMap = new HashMap<>(); for(int i = 0; i < segmentArr.size(); i++){ Object o = segmentArr.get(i); SegmentBean segmentBean = JSON.parseObject(JSON.toJSONString(o), SegmentBean.class); String startPointId = vpMap.get(segmentBean.getA()); String endPointId = vpMap.get(segmentBean.getB()); segmentBean.setStartPointId(startPointId); segmentBean.setEndPointId(endPointId); Integer segmentId = segmentBean.getId(); segmentMap.put(segmentId, segmentBean); String wallId = "Wall" + i; WallBean wallBean = WallBean.builder() .vectorId(wallId) .start(segmentBean.getStartPointId()) .end(segmentBean.getEndPointId()) .children(new String[]{}) .width(0.2d) .build(); wallMap.put(wallId, wallBean); startMap.put(wallBean.getStart(), wallBean.getVectorId()); endMap.put(wallBean.getEnd(), wallBean.getVectorId()); swMap.put(segmentId, wallId); } Collection pointBeans = pointMap.values(); for (PointBean pointBean : pointBeans) { Map parent = new HashMap<>(); String startParent = startMap.get(pointBean.getVectorId()); String endParent = endMap.get(pointBean.getVectorId()); parent.put(startParent, "start"); parent.put(endParent, "end"); pointBean.setParent(parent); } JSONArray pointArr = JSON.parseArray(JSON.toJSONString(pointBeans)); result[0] = pointArr; Collection wallBeans = wallMap.values(); JSONArray wallArr = JSON.parseArray(JSON.toJSONString(wallBeans)); result[1] = wallArr; return result; } private static JSONObject init() { JSONObject outContent = new JSONObject(); outContent.put("name", "houseType.json"); outContent.put("version", "2.1"); outContent.put("floors", new JSONArray()); outContent.put("newVectorId", null); outContent.put("setting", null); outContent.put("boundingBox", null); return outContent; } private static JSONArray readFloorJson(String filePath) { try { JSONObject floorplan = FileUtils.readJson(filePath); JSONArray floors = floorplan.getJSONArray("floors"); return floors; } catch (IOException e) { return null; } } private static JSONObject convert(JSONObject floorJson,int floor) { JSONArray rooms = floorJson.getJSONArray("rooms"); JSONObject walls = floorJson.getJSONObject("walls"); JSONObject points = floorJson.getJSONObject("points"); JSONObject symbols = floorJson.getJSONObject("symbols"); JSONArray _points = convertPoints(points); JSONArray _walls = convertWalls(walls); JSONArray _symbols = convertSymbols(symbols,floor); JSONArray _rooms = convertRooms(rooms,floor); JSONObject floorData = new JSONObject(); floorData.put("points", _points); floorData.put("walls", _walls); floorData.put("symbols", _symbols); floorData.put("rooms", _rooms); return floorData; } private static JSONArray convertPoints(JSONObject points) { JSONArray _points = new JSONArray(); for (Map.Entry entry: points.entrySet()) { JSONObject pointValue = (JSONObject)entry.getValue(); JSONObject _pointValue = new JSONObject(); _pointValue.put("x", pointValue.getFloat("x")); _pointValue.put("y", pointValue.getFloat("y")); _pointValue.put("parent", pointValue.getJSONObject("parent")); _pointValue.put("vectorId", pointValue.getString("vectorId")); _points.add(_pointValue); } return _points; } private static JSONArray convertWalls(JSONObject walls) { JSONArray _walls = new JSONArray(); for (Map.Entry entry: walls.entrySet()) { JSONObject wallValue = (JSONObject)entry.getValue(); JSONObject _wallValue = new JSONObject(); _wallValue.put("start", wallValue.getString("start")); _wallValue.put("end", wallValue.getString("end")); _wallValue.put("children", wallValue.getJSONArray("children")); _wallValue.put("vectorId", wallValue.getString("vectorId")); _wallValue.put("width", 0.2); //leftEdgeId //rightEdgeId _walls.add(_wallValue); } return _walls; } //门/窗 private static JSONArray convertSymbols(JSONObject symbols,int floor) { JSONArray _symbols = new JSONArray(); for (Map.Entry entry: symbols.entrySet()) { JSONObject symbolValue = (JSONObject)entry.getValue(); JSONObject _symbolValue = new JSONObject(); _symbolValue.put("start", symbolValue.getJSONObject("startPoint")); _symbolValue.put("end", symbolValue.getJSONObject("endPoint")); _symbolValue.put("parent", symbolValue.getString("parent")); _symbolValue.put("openSide", symbolValue.getString("openSide")); _symbolValue.put("vectorId", symbolValue.getString("vectorId")); _symbolValue.put("points2d", symbolValue.getJSONArray("points2d")); _symbolValue.put("geoType", symbolValue.getString("geoType")); _symbolValue.put("floor", floor); //"groundClearance": -0.7, //"height": 1.3, _symbols.add(_symbolValue); } return _symbols; } private static JSONArray convertRooms(JSONArray rooms,int floor) { JSONArray _rooms = new JSONArray(); for(int i=0;i