-
Notifications
You must be signed in to change notification settings - Fork 1
Initial work #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Knight0132
wants to merge
10
commits into
IndoorSpatial:main
Choose a base branch
from
Knight0132:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Initial work #1
Changes from 3 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9be8b41
Add new serialization modules and tests
Knight0132 a85e20a
Update serialization and test modifications, remove unused __init__
Knight0132 045b595
add test for serialization
Knight0132 b09fa0f
spilt all classes into different files
Knight0132 36e9684
modify the test for seiralization and deserialization, and then simpl…
Knight0132 3c5526d
modify the test and delete some wrong files
Knight0132 87a7ccf
add the function of visualization for graph and hypergraph
Knight0132 f6924da
add the visualization of connectionPoint and rlines
Knight0132 bc2be73
modify the visualization
Knight0132 3f98886
simplify visualization
Knight0132 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,322 @@ | ||
| """ | ||
| File Name: serialization.py | ||
|
|
||
| Copyright (c) 2023 - 2024 IndoorJson | ||
|
|
||
| Author: Ziwei Xiang <[email protected]> | ||
| Create Date: 2024/3/13 | ||
| """ | ||
|
|
||
| import numpy as np | ||
| from shapely.wkt import loads | ||
|
|
||
|
|
||
| class Graph: | ||
Knight0132 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| def __init__(self): | ||
| self._properties = [] | ||
| self._cells = [] | ||
| self._connections = [] | ||
| self._layers = [] | ||
| self._rlineses = [] | ||
| self._hypergraph = {} | ||
|
|
||
| @property | ||
| def properties(self): | ||
| return self._properties | ||
|
|
||
| @property | ||
| def cells(self): | ||
| return self._cells | ||
|
|
||
| @property | ||
| def connections(self): | ||
| return self._connections | ||
|
|
||
| @property | ||
| def layers(self): | ||
| return self._layers | ||
|
|
||
| @property | ||
| def rlineses(self): | ||
| return self._rlineses | ||
|
|
||
| @property | ||
| def hypergraph(self): | ||
| return self._hypergraph | ||
|
|
||
| class Cell: | ||
Knight0132 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| def __init__(self, cell_id, properties, space, node): | ||
| self.__id = cell_id | ||
| self._properties = properties | ||
| self.__space = space | ||
| self.__node = node | ||
|
|
||
| @property | ||
| def id(self): | ||
| return self.__id | ||
|
|
||
| @property | ||
| def properties(self): | ||
| return self._properties | ||
|
|
||
| @property | ||
| def space(self): | ||
| return self.__space | ||
|
|
||
| @property | ||
| def node(self): | ||
| return self.__node | ||
|
|
||
| @classmethod | ||
| def from_json(cls, json_data): | ||
| return cls(json_data['$id'], json_data['properties'], | ||
| loads(json_data['space']), loads(json_data['node'])) | ||
|
|
||
| def to_json(self): | ||
| return { | ||
| '$id': self.__id, | ||
| 'properties': self._properties, | ||
| 'space': self.__space.wkt, | ||
| 'node': self.__node.wkt | ||
| } | ||
|
|
||
| class Connection: | ||
|
|
||
| def __init__(self, connections_id, properties, source, target, bound, | ||
| edge): | ||
| self.__id = connections_id | ||
| self._properties = properties | ||
| self.__source = source | ||
| self.__target = target | ||
| self.__bound = bound | ||
| self.__edge = edge | ||
|
|
||
| @property | ||
| def id(self): | ||
| return self.__id | ||
|
|
||
| @property | ||
| def properties(self): | ||
| return self._properties | ||
|
|
||
| @property | ||
| def source(self): | ||
| return self.__source | ||
|
|
||
| @property | ||
| def target(self): | ||
| return self.__target | ||
|
|
||
| @property | ||
| def bound(self): | ||
| return self.__bound | ||
|
|
||
| @property | ||
| def edge(self): | ||
| return self.__edge | ||
|
|
||
| @classmethod | ||
| def from_json(cls, json_data): | ||
| return cls(json_data['$id'], json_data['properties'], | ||
| json_data['fr'], json_data['to'], | ||
| loads(json_data['bound']), loads(json_data['edge'])) | ||
|
|
||
| def to_json(self): | ||
| return { | ||
| '$id': self.__id, | ||
| 'properties': self._properties, | ||
| 'source': self.__source, | ||
| 'target': self.__target, | ||
| 'bound': self.__bound.wkt, | ||
| '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) | ||
| else: | ||
| raise ValueError('Cell id already exists') | ||
|
|
||
| def add_connection(self, connection: Connection): | ||
| if connection.id not in [c.id for c in self._connections]: | ||
| if connection.source in [ | ||
| c.id for c in self._cells | ||
| ] and connection.target in [c.id for c in self._cells]: | ||
| self._connections.append(connection) | ||
| elif connection.source not in [ | ||
| c.id for c in self._cells | ||
| ] and connection.target in [c.id for c in self._cells]: | ||
| raise ValueError('Source cell does not exist') | ||
| elif connection.source in [ | ||
| c.id for c in self._cells | ||
| ] and connection.target not in [c.id for c in self._cells]: | ||
| raise ValueError('Target cell does not exist') | ||
| else: | ||
| raise ValueError('Source and target cell do not exist') | ||
| 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 | ||
| incident_matrix = np.zeros((len(cells), len(connections)), dtype=int) | ||
| for j, connections in enumerate(connections): | ||
| source = self.get_cell_from_id(connections.source) | ||
| target = self.get_cell_from_id(connections.target) | ||
| source_index = cells.index(source) | ||
| target_index = cells.index(target) | ||
| incident_matrix[source_index, j] = 1 | ||
| incident_matrix[target_index, j] = -1 | ||
| return incident_matrix | ||
|
|
||
| def get_hypergraph(self): | ||
| cells = self.cells | ||
| connections = self.connections | ||
| hypergraph = self._hypergraph | ||
| hypergraph['hyperNodes'] = [] | ||
| hypergraph['hyperEdges'] = [] | ||
| incident_matrix = self.get_incident_matrix() | ||
| incident_matrix_transpose = incident_matrix.T | ||
|
|
||
| for hyperNode in connections: | ||
| hypergraph['hyperNodes'].append(hyperNode.to_json()) | ||
|
|
||
| for j in range(incident_matrix_transpose.shape[1]): | ||
| hyperEdge = {} | ||
| inner_edge_id = {"ins": [], "outs": []} | ||
| for i in range(incident_matrix_transpose.shape[0]): | ||
| if incident_matrix_transpose[i, j] != 0: | ||
| 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_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: | ||
| return cell | ||
| return None | ||
|
|
||
| def get_connection_from_id(self, connection_id): | ||
| for connection in self.connections: | ||
| if connection.id == connection_id: | ||
| return connection | ||
| return None | ||
|
|
||
| def to_json(self): | ||
Knight0132 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return { | ||
| 'properties': self._properties, | ||
| 'cells': [cell.to_json() for cell in self._cells], | ||
| 'connections': [ | ||
| connection.to_json() for connection in self._connections | ||
| ], | ||
| 'layers': [layer.to_json() for layer in self._layers], | ||
| 'rlineses': [rlineses.to_json() for rlineses in self._rlineses] | ||
| } | ||
|
|
||
| @staticmethod | ||
| def from_json(json_data): | ||
Knight0132 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| graph = Graph() | ||
| graph._properties = json_data['properties'] | ||
| graph._cells = [ | ||
| Graph.Cell.from_json(cell) for cell in json_data['cells'] | ||
| ] | ||
| graph._connections = [ | ||
| Graph.Connection.from_json(connection) | ||
| for connection in json_data['connections'] | ||
| ] | ||
| graph._layers = json_data['layers'] | ||
| graph._rlineses = json_data['rlineses'] | ||
| return graph | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| { | ||
| "properties": { | ||
| "name": "indoorjson3-cpp", | ||
| "labels": ["indoorgml", "GIS"], | ||
| "language": ["English", "中文", "한국어"], | ||
| "author": { | ||
| "name": "Kunlin Yu", | ||
| "email": "[email protected]" | ||
| } | ||
| }, | ||
| "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", | ||
| "开放时间": "全天", | ||
| "오픈 시간": "하루 종일" | ||
| }, | ||
| "fr": "c1", | ||
| "to": "c2", | ||
| "bound": "LINESTRING (1 0, 1 1)", | ||
| "edge": "LINESTRING (0.5 0.5, 1.5 0.5)" | ||
| }, | ||
| { | ||
| "$id": "conn3-1", | ||
| "properties": {"type": "window"}, | ||
| "fr": "c3", | ||
| "to": "c1", | ||
| "bound": "LINESTRING (1 0, 1 1)", | ||
| "edge": "LINESTRING (0.5 0.5, 1.5 0.5)" | ||
| } | ||
| ], | ||
| "layers": [ | ||
| { | ||
| "$id": "layer", | ||
| "cells": ["c1", "c2"] | ||
| } | ||
| ], | ||
| "rlineses": [ | ||
| { | ||
| "$id": "rlines1", | ||
| "cell": "c1", | ||
| "ins": ["conn3-1"], | ||
| "outs": ["conn1-2"], | ||
| "closure": [] | ||
| } | ||
| ] | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.