Skip to content
Open
Binary file added .DS_Store
Comment thread
Knight0132 marked this conversation as resolved.
Outdated
Binary file not shown.
Empty file removed src/__init__
Empty file.
97 changes: 91 additions & 6 deletions src/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,73 @@ def to_json(self):
'edge': self.__edge.wkt
}

class Layer:
def __init__(self, layer_id, cells):
self.__id = layer_id
self.__cells = cells

@property
def id(self):
return self.__id

@property
def cells(self):
return self.__cells

@classmethod
def from_json(cls, json_data):
return cls(json_data['$id'], json_data['cells'])

def to_json(self):
return {
'$id': self.__id,
'cells': self.__cells
}

class Rlines:
def __init__(self, rlines_id, cells, ins, outs, closure):
self.__id = rlines_id
self.__cells = cells
self.__ins = ins
self.__outs = outs
self.__closure = closure

@property
def id(self):
return self.__id

@property
def cells(self):
return self.__cells

@property
def ins(self):
return self.__ins

@property
def outs(self):
return self.__outs

@property
def closure(self):
return self.__closure

@classmethod
def from_json(cls, json_data):
return cls(json_data['$id'], json_data['cells'], json_data['ins'], json_data['outs'], json_data['closure'])

def to_json(self):
return {
'$id': self.__id,
'cells': self.__cells,
'ins': self.__ins,
'outs': self.__outs,
'closure': self.__closure
}

def set_properties(self, properties: dict):
self._properties.append(properties)

def add_cell(self, cell: Cell):
if cell.id not in [c.id for c in self._cells]:
self._cells.append(cell)
Expand All @@ -158,6 +225,12 @@ def add_connection(self, connection: Connection):
else:
raise ValueError('Connection id already exists')

def set_layers(self, layers: Layer):
self._layers.append(layers)

def set_rlineses(self, rlineses: Rlines):
self._rlineses.append(rlineses)

def get_incident_matrix(self):
cells = self.cells
connections = self.connections
Expand Down Expand Up @@ -185,19 +258,31 @@ def get_hypergraph(self):

for j in range(incident_matrix_transpose.shape[1]):
hyperEdge = {}
node_id_list = []
inner_edge_id = {"ins": [], "outs": []}
for i in range(incident_matrix_transpose.shape[0]):
if incident_matrix_transpose[i, j] != 0:
inner_edge_id = connections[i].id
node_id_list.append(inner_edge_id)
if incident_matrix_transpose[i, j] == -1:
inner_edge_ins_id = connections[i].id
inner_edge_id["ins"].append(inner_edge_ins_id)
elif incident_matrix_transpose[i, j] == 1:
inner_edge_outs_id = connections[i].id
inner_edge_id["outs"].append(inner_edge_outs_id)
else:
raise ValueError('Incident matrix error')
hyperEdge['id'] = cells[j].id
hyperEdge['properties'] = cells[j].properties
hyperEdge['space'] = cells[j].space.wkt
hyperEdge['node'] = cells[j].node.wkt
hyperEdge['inner_nodelist'] = node_id_list
hyperEdge['inner_nodeset'] = inner_edge_id
hypergraph['hyperEdges'].append(hyperEdge)

self.set_hypergraph(hypergraph)

return hypergraph

def set_hypergraph(self, hypergraph):
self._hypergraph = hypergraph

def get_cell_from_id(self, cell_id):
for cell in self.cells:
if cell.id == cell_id:
Expand All @@ -217,8 +302,8 @@ def to_json(self):
'connections': [
connection.to_json() for connection in self._connections
],
'layers': self._layers,
'rlineses': self._rlineses
'layers': [layer.to_json() for layer in self._layers],
'rlineses': [rlineses.to_json() for rlineses in self._rlineses]
}

@staticmethod
Expand Down
96 changes: 96 additions & 0 deletions test/test_create.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
"""
File Name: test_create.py

Copyright (c) 2023 - 2024 IndoorJson

Author: Ziwei Xiang <knightzz1016@gmail.com>
Create Date: 2024/3/14
"""

import json
from shapely.wkt import loads
Comment thread
Knight0132 marked this conversation as resolved.
Outdated
import sys
import os

sys.path.append(os.path.abspath('../src'))

from serialization import Graph

graph = Graph()

properties = {
"name": "indoorjson3-python",
"labels": ["indoorgml", "GIS"],
"language": ["English", "中文", "한국어"],
"author": {"name": "Ziwei Xiang", "email": "knightzz1016@gmail.com"}
}

cells = []

c1_id = "c1"
c1_properties = {"roomNumber": "1101"}
c1_space = loads("POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))")
c1_node = loads("POINT (0.5 0.5)")
cell1 = graph.Cell(c1_id, c1_properties, c1_space, c1_node)
cells.append(cell1)

c2_id = "c2"
c2_properties = {"roomNumber": "1102"}
c2_space = loads("POLYGON ((1 0, 2 0, 2 1, 1 1, 1 0))")
c2_node = loads("POINT (1.5 0.5)")
cell2 = graph.Cell(c2_id, c2_properties, c2_space, c2_node)
cells.append(cell2)

c3_id = "c3"
c3_properties = {"roomNumber": "1103"}
c3_space = loads("POLYGON ((0 1, 1 1, 1 2, 0 2, 0 1))")
c3_node = loads("POINT (0.5 1.5)")
cell3 = graph.Cell(c3_id, c3_properties, c3_space, c3_node)
cells.append(cell3)

for cell in cells:
graph.add_cell(cell)

connections = []

con1_id = "conn1-2"
con1_properties = {"type": "door", "开放时间": "全天", "오픈 시간": "하루 종일"}
con1_fr = c1_id
con1_to = c2_id
con1_bound = loads("LINESTRING (1 0, 1 1)")
con1_edge = loads("LINESTRING (0.5 0.5, 1.5 0.5)")
connection1 = graph.Connection(con1_id, con1_properties, con1_fr, con1_to, con1_bound, con1_edge)
connections.append(connection1)

con2_id = "conn3-1"
con2_properties = {"type": "window"}
con2_fr = c3_id
con2_to = c1_id
con2_bound = loads("LINESTRING (1 0, 1 1)")
con2_edge = loads("LINESTRING (0.5 0.5, 1.5 0.5)")
connection2 = graph.Connection(con2_id, con2_properties, con2_fr, con2_to, con2_bound, con2_edge)
connections.append(connection2)

for connection in connections:
graph.add_connection(connection)

layer_id = "layer"
layer_cells = [cell.id for cell in cells]
layer = graph.Layer(layer_id, layer_cells)
graph.set_layers(layer)

hypergraph = graph.get_hypergraph()
for hyperEdge in hypergraph['hyperEdges']:
i = 0
if hyperEdge["inner_nodeset"]["ins"] and hyperEdge["inner_nodeset"]["outs"]:
i += 1
rlines_id = str("rlines") + str(i)
rlines_cell = hyperEdge["id"]
rlines_ins = hyperEdge["inner_nodeset"]["ins"]
rlines_outs = hyperEdge["inner_nodeset"]["outs"]
rlines_closure = []
rlines = graph.Rlines(rlines_id, rlines_cell, rlines_ins, rlines_outs, rlines_closure)
graph.set_rlineses(rlines)

with open('test_created.json', 'w', encoding='utf-8') as f:
json.dump(graph.to_json(), f, indent=4)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why there are no "assert" or "expect" in your test code?

76 changes: 76 additions & 0 deletions test/test_created.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
{
"properties": [],
"cells": [
{
"$id": "c1",
"properties": {
"roomNumber": "1101"
},
"space": "POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))",
"node": "POINT (0.5 0.5)"
},
{
"$id": "c2",
"properties": {
"roomNumber": "1102"
},
"space": "POLYGON ((1 0, 2 0, 2 1, 1 1, 1 0))",
"node": "POINT (1.5 0.5)"
},
{
"$id": "c3",
"properties": {
"roomNumber": "1103"
},
"space": "POLYGON ((0 1, 1 1, 1 2, 0 2, 0 1))",
"node": "POINT (0.5 1.5)"
}
],
"connections": [
{
"$id": "conn1-2",
"properties": {
"type": "door",
"\u5f00\u653e\u65f6\u95f4": "\u5168\u5929",
"\uc624\ud508 \uc2dc\uac04": "\ud558\ub8e8 \uc885\uc77c"
},
"source": "c1",
"target": "c2",
"bound": "LINESTRING (1 0, 1 1)",
"edge": "LINESTRING (0.5 0.5, 1.5 0.5)"
},
{
"$id": "conn3-1",
"properties": {
"type": "window"
},
"source": "c3",
"target": "c1",
"bound": "LINESTRING (1 0, 1 1)",
"edge": "LINESTRING (0.5 0.5, 1.5 0.5)"
}
],
"layers": [
{
"$id": "layer",
"cells": [
"c1",
"c2",
"c3"
]
}
],
"rlineses": [
{
"$id": "rlines1",
"cells": "c1",
"ins": [
"conn3-1"
],
"outs": [
"conn1-2"
],
"closure": []
}
]
}
30 changes: 20 additions & 10 deletions test/test_hypergraph.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,14 @@
},
"space": "POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))",
"node": "POINT (0.5 0.5)",
"inner_nodelist": [
"conn1-2",
"conn3-1"
]
"inner_nodeset": {
"ins": [
"conn3-1"
],
"outs": [
"conn1-2"
]
}
},
{
"id": "c2",
Expand All @@ -43,9 +47,12 @@
},
"space": "POLYGON ((1 0, 2 0, 2 1, 1 1, 1 0))",
"node": "POINT (1.5 0.5)",
"inner_nodelist": [
"conn1-2"
]
"inner_nodeset": {
"ins": [
"conn1-2"
],
"outs": []
}
},
{
"id": "c3",
Expand All @@ -54,9 +61,12 @@
},
"space": "POLYGON ((0 1, 1 1, 1 2, 0 2, 0 1))",
"node": "POINT (0.5 1.5)",
"inner_nodelist": [
"conn3-1"
]
"inner_nodeset": {
"ins": [],
"outs": [
"conn3-1"
]
}
}
]
}