citygml_parser

CityGML 3.0 (Python version) parser for reading, writing, and converting CityGML files into JSON using Python.

19166 · city · citygml · digital · iso
入门
GitHub在线演示
Stars:9
License:未知
更新:2025/11/1

README

CityGML Parser

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. In addition, if you need landxml parser (python version), refer to landxml parser. The parser function will be further updated.

🚀 Features

Version 0.1



house

building

building

road

city

difference between input and output citygml

Next Plan


📂 Installation and usage

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

📄 Usage

📍 1. Read a CityGML File

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)

📍 2. List CityGML buliding and surface information

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


result


📍 3. Modify and Write CityGML File

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)

📍 4. Convert CityGML to JSON

You can convert CityGML files to JSON format using the included citygml_json.py script.

📌 Convert via Command Line

python citygml_json.py --input_file ./sample/CityGML_3.gml --output_file ./CityGML_3.json

📌 Convert using Python

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.")

**📍 5. Convert CityGML to MESH (under development) **

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


📂 Project Structure

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

👤 Author


📜 License

This project is licensed under the MIT License.


🙌 Acknowledgments

This project is inspired by CityGML 3.0, an OGC standard for 3D city modeling.