|
@@ -0,0 +1,362 @@
|
|
|
+package com.fdkankan.scene.common.house;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import java.io.File;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+public class JsonToHouseUtil {
|
|
|
+ public static void main(String[] args) {
|
|
|
+ floorplanCadToHouseJson("D:\\fddowm\\KK-t-3s7KlBajCc\\scene\\data\\dataKK-t-3s7KlBajCc\\floorplan_cad.json");
|
|
|
+ houstFloorToHouseJson("D:\\fddowm\\houst_floor.json");
|
|
|
+ }
|
|
|
+
|
|
|
+ public static int floorplanCadToHouseJson(String srcPath){
|
|
|
+ HouseJson houseJson = new HouseJson();
|
|
|
+ List<Floor> floorList = new ArrayList<>();
|
|
|
+ houseJson.setFloors(floorList);
|
|
|
+ try {
|
|
|
+ String msg = FileWriterUtil.readFile(srcPath);
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(msg);
|
|
|
+ JSONArray floors = jsonObject.getJSONArray("floors");
|
|
|
+ for (Object o : floors) {
|
|
|
+ JSONObject floor = (JSONObject) o;
|
|
|
+ HashMap<String,List<String>> map = new HashMap<>();
|
|
|
+ JSONArray segment = floor.getJSONArray("segment");
|
|
|
+ List<Wall> wallList = new ArrayList<>();
|
|
|
+ for (Object seo : segment) {
|
|
|
+ JSONObject segmentObj = (JSONObject) seo;
|
|
|
+ Wall wall = new Wall();
|
|
|
+ wall.setVectorId("Wall"+segmentObj.getInteger("id"));
|
|
|
+ wall.setStart("Point"+ segmentObj.getInteger("a"));
|
|
|
+ wall.setEnd("Point"+segmentObj.getInteger("b"));
|
|
|
+ wallList.add(wall);
|
|
|
+
|
|
|
+ setPointParent(map,wall,"start");
|
|
|
+ setPointParent(map,wall,"end");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ JSONArray vertex = floor.getJSONArray("vertex-xy");
|
|
|
+ List<Point> pointList = new ArrayList<>();
|
|
|
+ for (Object vo : vertex) {
|
|
|
+ JSONObject vertexObj = (JSONObject) vo;
|
|
|
+ Integer vertexId = vertexObj.getInteger("id");
|
|
|
+ Point point = new Point();
|
|
|
+ point.setVectorId("Point"+vertexId);
|
|
|
+ point.setX(vertexObj.getDouble("x"));
|
|
|
+ point.setY( vertexObj.getDouble("y"));
|
|
|
+
|
|
|
+ JSONObject parent = new JSONObject();
|
|
|
+ List<String> strings = map.get(point.getVectorId());
|
|
|
+ if(strings!=null){
|
|
|
+ for (String res : strings) {
|
|
|
+ parent.put(res.split(",")[0],res.split(",")[1]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ point.setParent(parent);
|
|
|
+
|
|
|
+ pointList.add(point);
|
|
|
+ }
|
|
|
+ Floor floor1 = new Floor();
|
|
|
+ floor1.setPoints(pointList);
|
|
|
+ floor1.setWalls(wallList);
|
|
|
+ floorList.add(floor1);
|
|
|
+ }
|
|
|
+ int i = checkPointAndWall(floorList);
|
|
|
+ if(i !=0){
|
|
|
+ return i;
|
|
|
+ }
|
|
|
+ String tag = srcPath.replace("floorplan_cad.json","");
|
|
|
+ FileWriterUtil.writerJson(tag,"house.json",JSONObject.toJSONString(houseJson));
|
|
|
+ }catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ return -3;
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Integer houstFloorToHouseJson(String srcPath){
|
|
|
+ HouseJson houseJson = new HouseJson();
|
|
|
+ List<Floor> floorList = new ArrayList<>();
|
|
|
+ houseJson.setFloors(floorList);
|
|
|
+ try {
|
|
|
+ String msg = FileWriterUtil.readFile(srcPath);
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(msg);
|
|
|
+ JSONArray floors = jsonObject.getJSONArray("floors");
|
|
|
+ for (Object o : floors) {
|
|
|
+ JSONObject floor = (JSONObject) o;
|
|
|
+ JSONArray segment = floor.getJSONArray("wall");
|
|
|
+ List<Wall> wallList = new ArrayList<>();
|
|
|
+ HashMap<String,List<String>> map = new HashMap<>();
|
|
|
+ for (Object seo : segment) {
|
|
|
+ JSONObject segmentObj = (JSONObject) seo;
|
|
|
+ Wall wall = new Wall();
|
|
|
+ wall.setVectorId("Wall"+segmentObj.getInteger("id"));
|
|
|
+ wall.setStart("Point"+ segmentObj.getInteger("p1"));
|
|
|
+ wall.setEnd("Point"+segmentObj.getInteger("p2"));
|
|
|
+ wallList.add(wall);
|
|
|
+ setPointParent(map,wall,"start");
|
|
|
+ setPointParent(map,wall,"end");
|
|
|
+ }
|
|
|
+ JSONArray vertex = floor.getJSONArray("vertex");
|
|
|
+ List<Point> pointList = new ArrayList<>();
|
|
|
+ for (Object vo : vertex) {
|
|
|
+ JSONObject vertexObj = (JSONObject) vo;
|
|
|
+ Integer vertexId = vertexObj.getInteger("id");
|
|
|
+ Point point = new Point();
|
|
|
+ point.setVectorId("Point"+vertexId);
|
|
|
+ point.setX(vertexObj.getDouble("x"));
|
|
|
+ point.setY( vertexObj.getDouble("y"));
|
|
|
+
|
|
|
+ JSONObject parent = new JSONObject();
|
|
|
+ List<String> strings = map.get(point.getVectorId());
|
|
|
+ if(strings!=null){
|
|
|
+ for (String res : strings) {
|
|
|
+ parent.put(res.split(",")[0],res.split(",")[1]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ point.setParent(parent);
|
|
|
+ pointList.add(point);
|
|
|
+ }
|
|
|
+ List<Symbol> symbolList = new ArrayList<>();
|
|
|
+ setSymbol(floor,symbolList,"window","SingleWindow");
|
|
|
+ setSymbol(floor,symbolList,"groundCase","FrenchWindow");
|
|
|
+ setSymbol(floor,symbolList,"bayCase","BayWindow");
|
|
|
+ setSymbol(floor,symbolList,"door","SingleDoor");
|
|
|
+ setSymbol(floor,symbolList,"slideDoor","SlideDoor");
|
|
|
+ setSymbol(floor,symbolList,"doubleDoor","DoubleDoor");
|
|
|
+ List<Component> componentList = new ArrayList<>();
|
|
|
+ setComponents(floor,componentList,"column","Beam"); //柱子
|
|
|
+ setComponents(floor,componentList,"furnFlue","Flue"); //烟道
|
|
|
+ setComponents(floor,componentList,"furnColumn","FurnColumn"); //框架柱
|
|
|
+ setComponents(floor,componentList,"stair","Corridor"); //楼道
|
|
|
+
|
|
|
+
|
|
|
+ Floor floor1 = new Floor();
|
|
|
+ floor1.setPoints(pointList);
|
|
|
+ floor1.setWalls(wallList);
|
|
|
+ floor1.setSymbols(symbolList);
|
|
|
+ floor1.setComponents(componentList);
|
|
|
+ floorList.add(floor1);
|
|
|
+ }
|
|
|
+
|
|
|
+ int i = checkPointAndWall(floorList);
|
|
|
+ if(i !=0){
|
|
|
+ return i;
|
|
|
+ }
|
|
|
+ String tag = srcPath.replace("houst_floor.json","");
|
|
|
+ FileWriterUtil.writerJson(tag,"house.json",JSONObject.toJSONString(houseJson));
|
|
|
+ }catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ return -3;
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private static void setComponents(JSONObject floor, List<Component> componentList,String key, String geoType) {
|
|
|
+ JSONArray array = floor.getJSONArray(key);
|
|
|
+ for (Object o : array) {
|
|
|
+ JSONObject obj = (JSONObject) o;
|
|
|
+ JSONArray pos = obj.getJSONArray("pos");
|
|
|
+ String line = obj.getString("line");
|
|
|
+ Component component = new Component();
|
|
|
+ component.setVectorId(geoType+componentList.size());
|
|
|
+ if(line!=null){
|
|
|
+ component.setParent("Wall"+line);
|
|
|
+ }
|
|
|
+ component.setGeoType(geoType);
|
|
|
+ Point point1 = new Point(pos.get(0),pos.get(1));
|
|
|
+ Point point2 = new Point(pos.get(2),pos.get(3));
|
|
|
+ Point point3 = new Point(pos.get(4),pos.get(5));
|
|
|
+ Point point4 = new Point(pos.get(6),pos.get(7));
|
|
|
+ List<Point> pointList = Arrays.asList(point1, point2, point3, point4);
|
|
|
+ component.setVertexes(pointList);
|
|
|
+ componentList.add(component);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private static void setSymbol( JSONObject floor, List<Symbol> symbolList,String key,String geoType){
|
|
|
+ JSONArray windows = floor.getJSONArray(key);
|
|
|
+ for (Object windowO : windows) {
|
|
|
+ JSONObject window = (JSONObject) windowO;
|
|
|
+ JSONArray pos = window.getJSONArray("pos");
|
|
|
+ JSONArray ctl = window.getJSONArray("ctl");
|
|
|
+ Integer within = window.getInteger("within");
|
|
|
+ String id = window.getString("id")==null ?symbolList.size()+"" :window.getString("id");
|
|
|
+
|
|
|
+ Symbol symbolInfo = new Symbol();
|
|
|
+ symbolInfo.setParent("Wall"+window.getString("line"));
|
|
|
+ symbolInfo.setGeoType(geoType);
|
|
|
+ symbolInfo.setVectorId(geoType+id);
|
|
|
+ symbolInfo.setStart(new Point(pos.get(0),pos.get(1)));
|
|
|
+ symbolInfo.setEnd(new Point(pos.get(2),pos.get(3)));
|
|
|
+ symbolInfo.setOpenSide("LEFT");
|
|
|
+ if(ctl!=null && ctl.size() >0){
|
|
|
+ Double p = Double.valueOf(pos.get(1).toString());
|
|
|
+ Double d = Double.valueOf(ctl.get(1).toString());
|
|
|
+ if(p >d){
|
|
|
+ symbolInfo.setOpenSide("RIGHT");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ setDoorOpenSide(symbolInfo,within);
|
|
|
+ symbolList.add(symbolInfo);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ private static void setDoorOpenSide(Symbol symbolInfo, Integer within) {
|
|
|
+ if(within == null){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Point start = symbolInfo.getStart();
|
|
|
+ Point end = symbolInfo.getEnd();
|
|
|
+ if(end.getX() >= start.getX() && end.getY() >= start.getY()){
|
|
|
+ if(within == 0 || within ==3){
|
|
|
+ if(start.getX() >=0){
|
|
|
+ symbolInfo.setOpenSide("LEFT");
|
|
|
+ }else {
|
|
|
+ symbolInfo.setOpenSide("RIGHT");
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ if(start.getX() >=0){
|
|
|
+ symbolInfo.setOpenSide("RIGHT");
|
|
|
+ }else {
|
|
|
+ symbolInfo.setOpenSide("LEFT");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(end.getX() >= start.getX() && end.getY() < start.getY()){
|
|
|
+ if(within == 0 || within ==3){
|
|
|
+ if(start.getY() >=0){
|
|
|
+ symbolInfo.setOpenSide("RIGHT");
|
|
|
+ }else {
|
|
|
+ symbolInfo.setOpenSide("LEFT");
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ if(start.getY() >=0){
|
|
|
+ symbolInfo.setOpenSide("LEFT");
|
|
|
+ }else {
|
|
|
+ symbolInfo.setOpenSide("RIGHT");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ if(end.getX() < start.getX() && end.getY() >= start.getY()){
|
|
|
+ if(within == 0 || within ==3){
|
|
|
+ if(start.getY() >=0){
|
|
|
+ symbolInfo.setOpenSide("LEFT");
|
|
|
+ }else {
|
|
|
+ symbolInfo.setOpenSide("RIGHT");
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ if(start.getY() >=0){
|
|
|
+ symbolInfo.setOpenSide("RIGHT");
|
|
|
+ }else {
|
|
|
+ symbolInfo.setOpenSide("LEFT");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(end.getX() < start.getX() && end.getY() < start.getY()){
|
|
|
+ if(within == 0 || within ==3){
|
|
|
+ if(start.getY() >=0){
|
|
|
+ symbolInfo.setOpenSide("LEFT");
|
|
|
+ }else {
|
|
|
+ symbolInfo.setOpenSide("RIGHT");
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ if(start.getY() >=0){
|
|
|
+ symbolInfo.setOpenSide("RIGHT");
|
|
|
+ }else {
|
|
|
+ symbolInfo.setOpenSide("LEFT");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(within == 3 || within == 1){
|
|
|
+ if(symbolInfo.getOpenSide().equals("LEFT")){
|
|
|
+ symbolInfo.setOpenSide("RIGHT");
|
|
|
+ }else {
|
|
|
+ symbolInfo.setOpenSide("LEFT");
|
|
|
+ }
|
|
|
+ symbolInfo.setStart(end);
|
|
|
+ symbolInfo.setEnd(start);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private static void setPointParent(HashMap<String, List<String>> map, Wall wall, String type){
|
|
|
+ String key = null;
|
|
|
+ if(type.equals("start")){
|
|
|
+ key = wall.getStart();
|
|
|
+ }else {
|
|
|
+ key = wall.getEnd();
|
|
|
+ }
|
|
|
+ List<String> list = map.get(key);
|
|
|
+ if(list == null){
|
|
|
+ list = new ArrayList<>();
|
|
|
+ }
|
|
|
+ list.add(wall.getVectorId()+","+type);
|
|
|
+ map.put(key,list);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Integer ToHouseJson(String src) {
|
|
|
+ File file = new File(src);
|
|
|
+ Integer bo = -4;
|
|
|
+ if(file.exists() && file.isDirectory()){
|
|
|
+ File[] files = file.listFiles();
|
|
|
+ List<String> fileNameList = new ArrayList<>();
|
|
|
+ for (File file1 : files) {
|
|
|
+ fileNameList.add(file1.getName());
|
|
|
+ }
|
|
|
+ if(fileNameList.contains("houst_floor.json")){
|
|
|
+ log.info("JsonToHouseUtil:houstFloorToHouseJson--->{}",src +File.separator + "houst_floor.json");
|
|
|
+ bo = JsonToHouseUtil.houstFloorToHouseJson(src +File.separator + "houst_floor.json");
|
|
|
+ }else {
|
|
|
+ log.info("JsonToHouseUtil:floorplanCadToHouseJson--->{}",src +File.separator + "houst_floor.json");
|
|
|
+ bo = JsonToHouseUtil.floorplanCadToHouseJson(src +File.separator + "floorplan_cad.json");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return bo;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static int checkPointAndWall(List<Floor> floors){
|
|
|
+ if(floors.size() <=0){
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ Integer wallSize =0;
|
|
|
+ Integer pointSize =0;
|
|
|
+ for (Floor floor : floors) {
|
|
|
+ List<Point> points = floor.getPoints();
|
|
|
+ if(!checkPoint(points)){
|
|
|
+ return -2;
|
|
|
+ }
|
|
|
+ List<Wall> walls = floor.getWalls();
|
|
|
+ wallSize += points.size();
|
|
|
+ pointSize += walls.size();
|
|
|
+ }
|
|
|
+
|
|
|
+ if(wallSize == 0 || pointSize == 0){
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static boolean checkPoint(List<Point> pointList) {
|
|
|
+ HashMap<String,Integer> map = new HashMap<>();
|
|
|
+ for (Point point : pointList) {
|
|
|
+ String key = "x:"+point.getX()+"-y:"+point.getY();
|
|
|
+ if(map.get(key) == null){
|
|
|
+ map.put(key,1);
|
|
|
+ }else {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+}
|