Skip to content

Repository files navigation

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

  • 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 (triangulated geometry for all city objects, interior holes and xlink:href surface references supported).
    Download CityGML 3.0_parser class with members HTML document


house

building

building

road

city

difference between input and output citygml

Next Plan


Installation

Install from the source tree (Python 3.9 or later). All dependencies are resolved by pip:

git clone https://github.com/mac999/citygml_parser.git
cd citygml_parser
pip install .

For development (editable install, so source edits take effect immediately):

pip install -e .

To install only the runtime dependencies without the package:

pip install -r requirements.txt

To build the HTML manual, install the optional docs extra (pdoc):

pip install .[docs]

Usage from the command line

After installation the following commands are available on the PATH:

citygml-json --input ./sample/CityGML_3.gml --output ./CityGML_3.json
citygml-convert --input ./sample/CityGML_3.gml --output ./output.gml
citygml-to-mesh --input ./sample/CityGML_3.gml --output ./mesh/CityGML_3.glb

The same scripts can be run directly from the source tree:

python citygml_json.py --input ./sample/CityGML_3.gml --output ./CityGML_3.json
python citygml_converter.py --input ./sample/CityGML_3.gml --output ./output.gml
python citygml_to_mesh.py --input ./sample/CityGML_3.gml --output ./mesh/CityGML_3.glb

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 building 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 ./sample/CityGML_3.gml --output ./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

You can convert CityGML to MESH format. Every gml:Polygon in the model is triangulated, so any CityObject (buildings, roads, vegetation, city furniture, terrain, etc.) is meshed - not only buildings. Concave surfaces and interior rings (holes) are triangulated with earcut, and surfaces referenced through xlink:href (e.g. a lodXSolid shell pointing to shared boundary polygons) are de-duplicated by gml:id so each polygon is meshed once.

python citygml_to_mesh.py --input ./sample/CityGML_3.gml --output ./mesh/CityGML_3.glb


Project Structure

citygml_parser/
  citygml_parser3.py         # CityGML 3.0 parsing module (schema dataclasses)
  sample/                    # Sample CityGML files
  docs/                      # manual
  citygml_parser_example.py  # Example
  citygml_converter.py       # CityGML read/write conversion
  citygml_json.py            # CityGML to JSON conversion
  citygml_to_mesh.py         # CityGML to Mesh conversion
  pyproject.toml             # Package metadata, dependencies, CLI entry points
  requirements.txt           # Runtime dependencies only
  MANIFEST.in                # Files included in the source distribution
  LICENSE                    # MIT license
  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.

About

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

Topics

Resources

Stars

14 stars

Watchers

2 watching

Forks

Releases

Packages

Contributors

Languages