CityGML 3.0 (Python version) parser for reading, writing, and converting CityGML files into JSON using Python. Since there is no suitable, easy-to-use, Python-based CityGML 3.0 parser available, I developed this. In the future, this parser is planned to be used to physically implement ISO/TS 19166 BIM-GIS conceptual mapping. The parser function will be further updated.
Version 0.1
- Parse CityGML 3.0 files into Python objects.
- Modify CityGML objects and write back to XML format.
- Convert CityGML to JSON for easier data processing and integration.
- Convert CityGML to MESH (under development).
Download CityGML 3.0_parser class with members HTML document
house
building
building
road
city
difference between input and output citygml
- Support CityGML 2.0. In addition, there is citygml2 to citygml3 convertor. It's working.
- Fix name space problem
To install the required dependencies, run:
pip install numpy xsdata lxml pdoc trimesh shapely
Converter usage
python citygml_json --input ./sample/CityGML_3.gml --output ./CityGML_3.json
python citygml_converter --input ./sample/CityGML_3.gml --output ./output.gml
from citygml_parser3 import *
from xsdata.formats.dataclass.parsers import XmlParser
# Initialize parser
parser = XmlParser()
# Parse CityGML file
model = parser.parse("./sample/1_SimpleBuilding/CityGML_3.gml")
# Print parsed model
print(model)
city_objects = model.city_object_member
for city_object in city_objects:
building = city_object.building
print(f'building id: {building.id}')
print(f'building name: {building.name}')
try:
for bound in building.boundary:
wall = bound.wall_surface
if wall:
print(f'wall id: {wall.id}')
roof = bound.roof_surface
if roof:
print(f'roof id: {roof.id}')
floor = bound.floor_surface
if floor:
print(f'floor id: {floor.id}')
ground = bound.ground_surface
if ground:
print(f'ground id: {ground.id}')
if building.lod1_solid:
print(f'building lod1_solid')
for sf in building.lod2_solid.solid.exterior.shell.surface_member:
print(f'surface: {sf.href}')
if building.lod2_solid:
print(f'building lod2_solid')
for sf in building.lod2_solid.solid.exterior.shell.surface_member:
print(f'surface: {sf.href}')
if building.lod3_solid:
print(f'building lod3_solid')
for sf in building.lod2_solid.solid.exterior.shell.surface_member:
print(f'surface: {sf.href}')
if building.lod4_solid:
print(f'building lod4_solid')
for sf in building.lod2_solid.solid.exterior.shell.surface_member:
print(f'surface: {sf.href}')
except Exception as e:
pass
from xsdata.formats.dataclass.context import XmlContext
from xsdata.formats.dataclass.serializers import XmlSerializer
from xsdata.formats.dataclass.serializers.config import SerializerConfig
from pathlib import Path
config = SerializerConfig(indent=" ")
context = XmlContext()
serializer = XmlSerializer(context=context, config=config)
# Output file path
path = Path("CityGML_3_output.gml")
# Write back to CityGML format
with path.open("w") as fp:
serializer.write(fp, model)
You can convert CityGML files to JSON format using the included citygml_json.py
script.
python citygml_json.py --input_file ./sample/CityGML_3.gml --output_file ./CityGML_3.json
from citygml_json import convert_citygml_to_json
input_gml = "./sample/CityGML_3.gml"
output_json = "./CityGML_3.json"
convert_citygml_to_json(input_gml, output_json)
print("CityGML successfully converted to JSON.")
You can convert CityGML to MESH format. It supports building's boundary, lod solid which consists of polygon surface.
python citygml_mesh.py --input_file ./sample/ManhattanSmall.gml --output_file ./ManhattanSmall.glb
citygml_parser/
│── citygml_parser3/ # CityGML parsing module
│── sample/ # Sample CityGML files
│── docs/ # manual
│── citygml_parser_example.py # Example
│── citygml_converter.py # CityGML conversion
│── citygml_json.py # CityGML to JSON conversion
│── citygml_to_mesh.py # CityGML to Mesh conversion
│── README.md # Project documentation
- Name: Taewook Kang
- Email: [email protected]
This project is licensed under the MIT License.
This project is inspired by CityGML 3.0, an OGC standard for 3D city modeling.