mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-09-19 22:50:21 +00:00
cityjson: Implement conversion of all LODs instead of only the most detailed
This commit is contained in:
@@ -35,4 +35,4 @@ The example file that could be used is example/3D_BAG_example.json
|
|||||||
- [x] Do not use template IFC for new IFC file, but make IFC file from scratch
|
- [x] Do not use template IFC for new IFC file, but make IFC file from scratch
|
||||||
- [x] Create mapping to IFC for all CityJSON object types & semantic surfaces
|
- [x] Create mapping to IFC for all CityJSON object types & semantic surfaces
|
||||||
- [ ] Implement conversion of all CitYJSON geometries
|
- [ ] Implement conversion of all CitYJSON geometries
|
||||||
- [ ] Implement conversion of all LODs instead of only the most detailed
|
- [x] Implement conversion of all LODs instead of only the most detailed
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
# ifccityjson - Python CityJSON to IFC converter
|
# ifccityjson - Python CityJSON to IFC converter
|
||||||
# Copyright (C) 2021 Laurens J.N. Oostwegel <l.oostwegel@gmail.com>
|
# Copyright (C) 2021 Laurens J.N. Oostwegel <l.oostwegel@gmail.com>
|
||||||
#
|
#
|
||||||
@@ -26,9 +25,9 @@ JSON_TO_IFC = {
|
|||||||
"Building": ["IfcBuilding"],
|
"Building": ["IfcBuilding"],
|
||||||
"BuildingPart": ["IfcBuilding", {"CompositionType": "PARTIAL"}],
|
"BuildingPart": ["IfcBuilding", {"CompositionType": "PARTIAL"}],
|
||||||
"BuildingInstallation": ["IfcBuildingElementProxy"],
|
"BuildingInstallation": ["IfcBuildingElementProxy"],
|
||||||
"Road": ["IfcCivilElement"], # Update for IFC4.3
|
"Road": ["IfcCivilElement"], # Update for IFC4.3
|
||||||
"Railway": ["IfcCivilElement"], # Update for IFC4.3
|
"Railway": ["IfcCivilElement"], # Update for IFC4.3
|
||||||
"TransportSquare": ["IfcCivilElement"], # Update for IFC4.3
|
"TransportSquare": ["IfcCivilElement"], # Update for IFC4.3
|
||||||
"TINRelief": ["IfcGeographicElement", {"PredefinedType": "TERRAIN"}],
|
"TINRelief": ["IfcGeographicElement", {"PredefinedType": "TERRAIN"}],
|
||||||
"WaterBody": ["IfcGeographicElement", {"PredefinedType": "USERDEFINED",
|
"WaterBody": ["IfcGeographicElement", {"PredefinedType": "USERDEFINED",
|
||||||
"ObjectType": "WaterBody"}], # Update for IFC4.3
|
"ObjectType": "WaterBody"}], # Update for IFC4.3
|
||||||
@@ -66,6 +65,7 @@ JSON_TO_IFC = {
|
|||||||
"AuxiliaryTrafficArea": ["IfcCivilElement"] # Update for IFC4.3
|
"AuxiliaryTrafficArea": ["IfcCivilElement"] # Update for IFC4.3
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class Cityjson2ifc:
|
class Cityjson2ifc:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.city_model = None
|
self.city_model = None
|
||||||
@@ -74,11 +74,10 @@ class Cityjson2ifc:
|
|||||||
self.geometry = GeometryIO()
|
self.geometry = GeometryIO()
|
||||||
self.configuration()
|
self.configuration()
|
||||||
|
|
||||||
|
def configuration(self, file_destination="output.ifc", name_attribute=None, split=True):
|
||||||
def configuration(self, file_destination="output.ifc", name_attribute=None):
|
|
||||||
self.properties["file_destination"] = file_destination
|
self.properties["file_destination"] = file_destination
|
||||||
self.properties["name_attribute"] = name_attribute
|
self.properties["name_attribute"] = name_attribute
|
||||||
|
self.properties["split"] = split
|
||||||
|
|
||||||
def convert(self, city_model):
|
def convert(self, city_model):
|
||||||
self.city_model = city_model
|
self.city_model = city_model
|
||||||
@@ -109,7 +108,8 @@ class Cityjson2ifc:
|
|||||||
# Meter is assumed as unit for now
|
# Meter is assumed as unit for now
|
||||||
unit = self.IFC_model.createIfcSIUnit(None, "LENGTHUNIT", None, "METRE")
|
unit = self.IFC_model.createIfcSIUnit(None, "LENGTHUNIT", None, "METRE")
|
||||||
crs = self.IFC_model.create_entity("IfcProjectedCrs", Name=f"epsg:{epsg}")
|
crs = self.IFC_model.create_entity("IfcProjectedCrs", Name=f"epsg:{epsg}")
|
||||||
self.IFC_model.create_entity("IfcMapConversion", self.IFC_representation_context, **self.properties["local_translation"])
|
self.IFC_model.create_entity("IfcMapConversion", self.IFC_representation_context,
|
||||||
|
**self.properties["local_translation"])
|
||||||
|
|
||||||
def create_new_file(self):
|
def create_new_file(self):
|
||||||
self.IFC_model = ifcopenshell.api.run("project.create_file")
|
self.IFC_model = ifcopenshell.api.run("project.create_file")
|
||||||
@@ -118,13 +118,27 @@ class Cityjson2ifc:
|
|||||||
self.properties["owner_history"] = self.create_owner_history()
|
self.properties["owner_history"] = self.create_owner_history()
|
||||||
self.IFC_representation_context = ifcopenshell.api.run("context.add_context", self.IFC_model,
|
self.IFC_representation_context = ifcopenshell.api.run("context.add_context", self.IFC_model,
|
||||||
**{"context": "Model"})
|
**{"context": "Model"})
|
||||||
self.IFC_representation_sub_context = ifcopenshell.api.run("context.add_context", self.IFC_model,
|
|
||||||
**{"context": "Model",
|
if not self.city_model.has_metadata() or "presentLoDs" not in self.city_model.j["metadata"]:
|
||||||
"subcontext": "Body", # LODs as subcontext
|
self.city_model.update_metadata()
|
||||||
"target_view": "MODEL_VIEW"})
|
self.IFC_representation_sub_contexts = {}
|
||||||
|
for lod in self.city_model.j["metadata"]["presentLoDs"]:
|
||||||
|
# TODO in ifcopenshell.api.context.add_context add support for UserDefinedTargetView
|
||||||
|
# self.IFC_representation_sub_contexts[str(lod)] = ifcopenshell.api.run("context.add_context", self.IFC_model,
|
||||||
|
# **{"context": "Model",
|
||||||
|
# "subcontext": "Body",
|
||||||
|
# "target_view": "USERDEFINED",
|
||||||
|
# "UserDefinedTargetView":str(lod)})
|
||||||
|
self.IFC_representation_sub_contexts[str(lod)] = self.IFC_model.create_entity("IfcGeometricRepresentationSubContext",
|
||||||
|
**{"ContextType": "Model",
|
||||||
|
"ContextIdentifier": "Body",
|
||||||
|
"TargetView": "USERDEFINED",
|
||||||
|
"ParentContext": self.IFC_representation_context,
|
||||||
|
"UserDefinedTargetView": "LOD" + str(lod)})
|
||||||
|
|
||||||
self.IFC_site = ifcopenshell.api.run("root.create_entity", self.IFC_model,
|
self.IFC_site = ifcopenshell.api.run("root.create_entity", self.IFC_model,
|
||||||
**{"ifc_class": "IfcSite",
|
**{"ifc_class": "IfcSite",
|
||||||
"name": "My Site"})
|
"name": "My Site"})
|
||||||
|
|
||||||
def create_owner_history(self):
|
def create_owner_history(self):
|
||||||
actor = self.IFC_model.createIfcActorRole("ENGINEER", None, None)
|
actor = self.IFC_model.createIfcActorRole("ENGINEER", None, None)
|
||||||
@@ -137,7 +151,8 @@ class Cityjson2ifc:
|
|||||||
p_o = self.IFC_model.createIfcPersonAndOrganization(person, organization)
|
p_o = self.IFC_model.createIfcPersonAndOrganization(person, organization)
|
||||||
application = self.IFC_model.createIfcApplication(organization, "v0.0.x", "ifccityjson", "ifccityjson")
|
application = self.IFC_model.createIfcApplication(organization, "v0.0.x", "ifccityjson", "ifccityjson")
|
||||||
timestamp = int(datetime.now().timestamp())
|
timestamp = int(datetime.now().timestamp())
|
||||||
ownerHistory = self.IFC_model.createIfcOwnerHistory(p_o, application, "READWRITE", None, None, None, None, timestamp)
|
ownerHistory = self.IFC_model.createIfcOwnerHistory(p_o, application, "READWRITE", None, None, None, None,
|
||||||
|
timestamp)
|
||||||
|
|
||||||
return ownerHistory
|
return ownerHistory
|
||||||
|
|
||||||
@@ -168,29 +183,26 @@ class Cityjson2ifc:
|
|||||||
if len(obj.geometry) == 0:
|
if len(obj.geometry) == 0:
|
||||||
print(f"Warning: Object {obj_id} has no geometry.")
|
print(f"Warning: Object {obj_id} has no geometry.")
|
||||||
|
|
||||||
for geom in obj.geometry:
|
|
||||||
if geom.lod > lod:
|
|
||||||
geometry = geom
|
|
||||||
lod = geom.lod
|
|
||||||
|
|
||||||
IFC_semantic_surface_children = []
|
IFC_semantic_surface_children = []
|
||||||
if geometry and geometry.surfaces:
|
IFC_shape_representations = []
|
||||||
for surface_id in geometry.surfaces:
|
for geometry in obj.geometry:
|
||||||
IFC_child_class = JSON_TO_IFC[geometry.surfaces[surface_id]["type"]][0]
|
lod = geometry.lod
|
||||||
child_data = {"GlobalId": ifcopenshell.guid.new(),
|
IFC_geometry, shape_representation_type = None, None
|
||||||
"Name": IFC_child_class
|
# representation = self.create_IFC_LOD_representation(obj, geometry)
|
||||||
}
|
if geometry and geometry.surfaces:
|
||||||
|
IFC_semantic_surface_children = self.create_IFC_semantic_surface_children(geometry, lod)
|
||||||
# CREATE ENTITY
|
if geometry:
|
||||||
surface_geometry = self.geometry.create_IFC_surface(self.IFC_model, geometry, surface_id)
|
IFC_geometry, shape_representation_type = self.geometry.create_IFC_geometry(self.IFC_model,
|
||||||
if surface_geometry:
|
geometry)
|
||||||
child_data["Representation"] = self.create_IFC_representation(surface_geometry, 'brep')
|
|
||||||
IFC_semantic_surface_children.append(self.IFC_model.create_entity(IFC_child_class, **child_data))
|
|
||||||
|
|
||||||
elif geometry:
|
|
||||||
IFC_geometry, shape_representation_type = self.geometry.create_IFC_geometry(self.IFC_model, geometry)
|
|
||||||
if IFC_geometry:
|
if IFC_geometry:
|
||||||
data["Representation"] = self.create_IFC_representation(IFC_geometry, shape_representation_type)
|
IFC_shape_representation = self.create_IFC_shape_representation(IFC_geometry,
|
||||||
|
shape_representation_type,
|
||||||
|
lod)
|
||||||
|
IFC_shape_representations.append(IFC_shape_representation)
|
||||||
|
|
||||||
|
if len(IFC_shape_representations) > 0:
|
||||||
|
data["Representation"] = self.IFC_model.create_entity("IfcProductDefinitionShape",
|
||||||
|
Representations=IFC_shape_representations)
|
||||||
data["GlobalId"] = ifcopenshell.guid.new()
|
data["GlobalId"] = ifcopenshell.guid.new()
|
||||||
data["Name"] = IFC_name
|
data["Name"] = IFC_name
|
||||||
|
|
||||||
@@ -212,9 +224,9 @@ class Cityjson2ifc:
|
|||||||
|
|
||||||
if IFC_semantic_surface_children:
|
if IFC_semantic_surface_children:
|
||||||
self.IFC_model.create_entity("IfcRelAggregates",
|
self.IFC_model.create_entity("IfcRelAggregates",
|
||||||
**{"GlobalId": ifcopenshell.guid.new(),
|
**{"GlobalId": ifcopenshell.guid.new(),
|
||||||
"RelatedObjects": IFC_semantic_surface_children,
|
"RelatedObjects": IFC_semantic_surface_children,
|
||||||
"RelatingObject": IFC_object})
|
"RelatingObject": IFC_object})
|
||||||
|
|
||||||
self.create_property_set(obj.attributes, IFC_object)
|
self.create_property_set(obj.attributes, IFC_object)
|
||||||
|
|
||||||
@@ -225,16 +237,30 @@ class Cityjson2ifc:
|
|||||||
"RelatingStructure": parent_children['Parent']}
|
"RelatingStructure": parent_children['Parent']}
|
||||||
)
|
)
|
||||||
|
|
||||||
def create_IFC_representation(self, IFC_geometry, shape_representation_type):
|
def create_IFC_semantic_surface_children(self, geometry, lod):
|
||||||
|
IFC_semantic_surface_children = []
|
||||||
|
for surface_id in geometry.surfaces:
|
||||||
|
IFC_child_class = JSON_TO_IFC[geometry.surfaces[surface_id]["type"]][0]
|
||||||
|
child_data = {"GlobalId": ifcopenshell.guid.new(),
|
||||||
|
"Name": IFC_child_class
|
||||||
|
}
|
||||||
|
|
||||||
|
# CREATE ENTITY
|
||||||
|
surface_geometry = self.geometry.create_IFC_surface(self.IFC_model, geometry, surface_id)
|
||||||
|
if surface_geometry:
|
||||||
|
child_data["Representation"] = self.create_IFC_shape_representation(surface_geometry, 'brep', lod)
|
||||||
|
IFC_semantic_surface_children.append(self.IFC_model.create_entity(IFC_child_class, **child_data))
|
||||||
|
return IFC_semantic_surface_children
|
||||||
|
|
||||||
|
def create_IFC_shape_representation(self, IFC_geometry, shape_representation_type, lod):
|
||||||
if not isinstance(IFC_geometry, list):
|
if not isinstance(IFC_geometry, list):
|
||||||
IFC_geometry = [IFC_geometry]
|
IFC_geometry = [IFC_geometry]
|
||||||
|
|
||||||
shape_representation = self.IFC_model.create_entity("IfcShapeRepresentation",
|
shape_representation = self.IFC_model.create_entity("IfcShapeRepresentation",
|
||||||
self.IFC_representation_sub_context, 'Body', shape_representation_type,
|
self.IFC_representation_sub_contexts[str(lod)], 'Body',
|
||||||
|
shape_representation_type,
|
||||||
IFC_geometry)
|
IFC_geometry)
|
||||||
product_representation = self.IFC_model.create_entity("IfcProductDefinitionShape",
|
return shape_representation
|
||||||
Representations=[shape_representation])
|
|
||||||
return product_representation
|
|
||||||
|
|
||||||
def create_property_set(self, CJ_attributes, IFC_entity):
|
def create_property_set(self, CJ_attributes, IFC_entity):
|
||||||
IFC_object_properties = []
|
IFC_object_properties = []
|
||||||
|
|||||||
@@ -22,7 +22,7 @@
|
|||||||
"geometry": [
|
"geometry": [
|
||||||
{
|
{
|
||||||
"type": "MultiLineString",
|
"type": "MultiLineString",
|
||||||
"lod": 1,
|
"lod": 2.3,
|
||||||
"boundaries": [
|
"boundaries": [
|
||||||
[
|
[
|
||||||
2,
|
2,
|
||||||
@@ -84,7 +84,7 @@
|
|||||||
"geometry": [
|
"geometry": [
|
||||||
{
|
{
|
||||||
"type": "CompositeSurface",
|
"type": "CompositeSurface",
|
||||||
"lod": 1,
|
"lod": 1.2,
|
||||||
"boundaries": [
|
"boundaries": [
|
||||||
[
|
[
|
||||||
[
|
[
|
||||||
|
|||||||
@@ -16,7 +16,6 @@
|
|||||||
#
|
#
|
||||||
# You should have received a copy of the GNU Lesser General Public License
|
# You should have received a copy of the GNU Lesser General Public License
|
||||||
# along with ifccityjson. If not, see <http://www.gnu.org/licenses/>.
|
# along with ifccityjson. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
from cjio import cityjson
|
from cjio import cityjson
|
||||||
from cityjson2ifc import Cityjson2ifc
|
from cityjson2ifc import Cityjson2ifc
|
||||||
@@ -30,6 +29,11 @@ if __name__ == '__main__':
|
|||||||
parser.add_argument("-i", "--input", type=str, help="input CityJSON file", required=True)
|
parser.add_argument("-i", "--input", type=str, help="input CityJSON file", required=True)
|
||||||
parser.add_argument("-o", "--output", type=str, help="output IFC file. Standard is output.ifc")
|
parser.add_argument("-o", "--output", type=str, help="output IFC file. Standard is output.ifc")
|
||||||
parser.add_argument("-n", "--name", type=str, help="Attribute containing the name")
|
parser.add_argument("-n", "--name", type=str, help="Attribute containing the name")
|
||||||
|
parser.add_argument('--split-lod', dest='split', action='store_true',
|
||||||
|
help="Split the file in multiple LoDs")
|
||||||
|
parser.add_argument('--no-split-lod', dest='split', action='store_false',
|
||||||
|
help="Do not split the file in multiple LoDs")
|
||||||
|
parser.set_defaults(split=True)
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
city_model = cityjson.load(args.input)
|
city_model = cityjson.load(args.input)
|
||||||
@@ -38,6 +42,7 @@ if __name__ == '__main__':
|
|||||||
data["name_attribute"] = args.name
|
data["name_attribute"] = args.name
|
||||||
if args.output:
|
if args.output:
|
||||||
data["file_destination"] = args.output
|
data["file_destination"] = args.output
|
||||||
|
data["split"] = args.split
|
||||||
|
|
||||||
converter = Cityjson2ifc()
|
converter = Cityjson2ifc()
|
||||||
converter.configuration(**data)
|
converter.configuration(**data)
|
||||||
|
|||||||
Reference in New Issue
Block a user