Skip to content

Commit

Permalink
simplified writers
Browse files Browse the repository at this point in the history
  • Loading branch information
benediktschwab committed Sep 18, 2023
1 parent 8bee5c0 commit 1133b9f
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 155 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,16 @@

package io.rtron.main.processor

import arrow.core.None
import arrow.core.Option
import arrow.core.some
import io.rtron.io.files.CompressedFileExtension

enum class CompressionFormat {
NONE,
GZ,
ZIP,
ZST
}

fun CompressionFormat.toOptionalCompressedFileExtension(): Option<CompressedFileExtension> = when (this) {
CompressionFormat.NONE -> None
CompressionFormat.GZ -> CompressedFileExtension.GZ.some()
CompressionFormat.ZIP -> CompressedFileExtension.ZIP.some()
CompressionFormat.ZST -> CompressedFileExtension.ZST.some()
fun CompressionFormat.toFileExtension(): String = when (this) {
CompressionFormat.NONE -> ""
CompressionFormat.GZ -> ".gz"
CompressionFormat.ZIP -> ".zip"
CompressionFormat.ZST -> ".zst"
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import arrow.core.Either
import arrow.core.left
import arrow.core.right
import io.rtron.readerwriter.citygml.CitygmlVersion
import io.rtron.readerwriter.citygml.CitygmlWriterParameters
import io.rtron.readerwriter.opendrive.OpendriveWriterParameters
import io.rtron.transformer.converter.opendrive2roadspaces.Opendrive2RoadspacesParameters
import io.rtron.transformer.converter.roadspaces2citygml.Roadspaces2CitygmlParameters
import io.rtron.transformer.evaluator.opendrive.OpendriveEvaluatorParameters
Expand Down Expand Up @@ -101,10 +99,6 @@ data class OpendriveToCitygmlParameters(
cropPolygonY = cropPolygonY
)

fun deriveOpendriveWriterParameters() = OpendriveWriterParameters(
fileCompression = compressionFormat.toOptionalCompressedFileExtension()
)

fun deriveOpendrive2RoadspacesParameters() = Opendrive2RoadspacesParameters(
concurrentProcessing = false,

Expand Down Expand Up @@ -137,9 +131,4 @@ data class OpendriveToCitygmlParameters(
generateLongitudinalFillerSurfaces = Roadspaces2CitygmlParameters.DEFAULT_GENERATE_LONGITUDINAL_FILLER_SURFACES,
mappingBackwardsCompatibility = convertToCitygml2
)

fun deriveCitygmlWriterParameters() = CitygmlWriterParameters(
versions = setOf(this.getCitygmlWriteVersion()),
fileCompression = compressionFormat.toOptionalCompressedFileExtension()
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import io.rtron.io.messages.getTextSummary
import io.rtron.io.serialization.serializeToJsonFile
import io.rtron.main.project.processAllFiles
import io.rtron.readerwriter.citygml.CitygmlWriter
import io.rtron.readerwriter.opendrive.OpendriveFileReader
import io.rtron.readerwriter.opendrive.OpendriveFileWriter
import io.rtron.readerwriter.opendrive.OpendriveReader
import io.rtron.readerwriter.opendrive.OpendriveValidator
import io.rtron.readerwriter.opendrive.OpendriveWriter
import io.rtron.std.handleEmpty
import io.rtron.transformer.converter.opendrive2roadspaces.Opendrive2RoadspacesTransformer
import io.rtron.transformer.converter.roadspaces2citygml.Roadspaces2CitygmlTransformer
Expand All @@ -50,7 +50,7 @@ class OpendriveToCitygmlProcessor(

processAllFiles(
inputDirectoryPath = inputPath,
withFilenameEndings = OpendriveFileReader.supportedFilenameEndings,
withFilenameEndings = OpendriveReader.supportedFilenameEndings,
outputDirectoryPath = outputPath
) {
val outputSubDirectoryPath = outputDirectoryPath / "citygml_${parameters.getCitygmlWriteVersion()}"
Expand All @@ -71,7 +71,7 @@ class OpendriveToCitygmlProcessor(
return@processAllFiles
}
// read of OpenDRIVE model
val opendriveModel = OpendriveFileReader.readFromFile(inputFilePath)
val opendriveModel = OpendriveReader.readFromFile(inputFilePath)
.getOrElse { logger.warn(it.message); return@processAllFiles }

// evaluate OpenDRIVE model
Expand Down Expand Up @@ -103,8 +103,8 @@ class OpendriveToCitygmlProcessor(
}

// write offset OpenDRIVE model
val opendriveFileWriter = OpendriveFileWriter(parameters.deriveOpendriveWriterParameters())
opendriveFileWriter.write(opendriveCropped, outputSubDirectoryPath)
val opendriveFilePath = outputSubDirectoryPath / ("opendrive.xodr" + parameters.compressionFormat.toFileExtension())
OpendriveWriter.writeToFile(opendriveCropped, opendriveFilePath)

// transform OpenDRIVE model to Roadspaces model
val opendrive2RoadspacesTransformer = Opendrive2RoadspacesTransformer(parameters.deriveOpendrive2RoadspacesParameters())
Expand All @@ -126,7 +126,7 @@ class OpendriveToCitygmlProcessor(
citygmlModelResult.second.serializeToJsonFile(outputSubDirectoryPath / ROADSPACES_TO_CITYGML_REPORT_PATH)

// write CityGML model
CitygmlWriter.writeModel(citygmlModelResult.first, outputSubDirectoryPath, "citygml_model", parameters.deriveCitygmlWriterParameters())
CitygmlWriter.writeToFile(citygmlModelResult.first, parameters.getCitygmlWriteVersion(), outputSubDirectoryPath / "citygml_model.gml")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@

package io.rtron.main.processor

import io.rtron.readerwriter.citygml.CitygmlVersion
import io.rtron.readerwriter.citygml.CitygmlWriterParameters
import io.rtron.readerwriter.opendrive.OpendriveWriterParameters
import io.rtron.transformer.converter.opendrive2roadspaces.Opendrive2RoadspacesParameters
import io.rtron.transformer.converter.roadspaces2citygml.Roadspaces2CitygmlParameters
import io.rtron.transformer.evaluator.opendrive.OpendriveEvaluatorParameters
Expand Down Expand Up @@ -53,10 +50,6 @@ data class ValidateOpendriveParameters(
planViewGeometryAngleWarningTolerance = planViewGeometryAngleWarningTolerance
)

fun deriveOpendriveWriterParameters() = OpendriveWriterParameters(
fileCompression = compressionFormat.toOptionalCompressedFileExtension()
)

fun deriveOpendrive2RoadspacesParameters() = Opendrive2RoadspacesParameters(
concurrentProcessing = false,
numberTolerance = tolerance,
Expand Down Expand Up @@ -104,14 +97,4 @@ data class ValidateOpendriveParameters(
generateLongitudinalFillerSurfaces = false,
mappingBackwardsCompatibility = false
)

fun deriveCitygml2WriterParameters() = CitygmlWriterParameters(
versions = setOf(CitygmlVersion.V2_0),
fileCompression = compressionFormat.toOptionalCompressedFileExtension()
)

fun deriveCitygml3WriterParameters() = CitygmlWriterParameters(
versions = setOf(CitygmlVersion.V3_0),
fileCompression = compressionFormat.toOptionalCompressedFileExtension()
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ import com.charleskorn.kaml.Yaml
import io.rtron.io.messages.getTextSummary
import io.rtron.io.serialization.serializeToJsonFile
import io.rtron.main.project.processAllFiles
import io.rtron.readerwriter.citygml.CitygmlVersion
import io.rtron.readerwriter.citygml.CitygmlWriter
import io.rtron.readerwriter.opendrive.OpendriveFileReader
import io.rtron.readerwriter.opendrive.OpendriveFileWriter
import io.rtron.readerwriter.opendrive.OpendriveReader
import io.rtron.readerwriter.opendrive.OpendriveValidator
import io.rtron.readerwriter.opendrive.OpendriveWriter
import io.rtron.std.handleEmpty
import io.rtron.transformer.converter.opendrive2roadspaces.Opendrive2RoadspacesTransformer
import io.rtron.transformer.converter.roadspaces2citygml.Roadspaces2CitygmlTransformer
Expand All @@ -46,7 +47,7 @@ class ValidateOpendriveProcessor(

processAllFiles(
inputDirectoryPath = inputPath,
withFilenameEndings = OpendriveFileReader.supportedFilenameEndings,
withFilenameEndings = OpendriveReader.supportedFilenameEndings,
outputDirectoryPath = outputPath
) {
// write the parameters as yaml file
Expand All @@ -60,7 +61,7 @@ class ValidateOpendriveProcessor(
return@processAllFiles
}
// read of OpenDRIVE model
val opendriveModel = OpendriveFileReader.readFromFile(inputFilePath)
val opendriveModel = OpendriveReader.readFromFile(inputFilePath)
.getOrElse { logger.warn(it.message); return@processAllFiles }

// evaluate OpenDRIVE model
Expand All @@ -78,8 +79,8 @@ class ValidateOpendriveProcessor(

// write modified OpenDRIVE model
if (parameters.writeOpendriveFile) {
val opendriveFileWriter = OpendriveFileWriter(parameters.deriveOpendriveWriterParameters())
opendriveFileWriter.write(modifiedOpendriveModel, outputDirectoryPath)
val filePath = outputDirectoryPath / ("opendrive.xodr" + parameters.compressionFormat.toFileExtension())
OpendriveWriter.writeToFile(modifiedOpendriveModel, filePath)
}

// transform OpenDRIVE model to Roadspaces model
Expand All @@ -103,7 +104,8 @@ class ValidateOpendriveProcessor(

// write CityGML 2 model
if (parameters.writeCitygml2File) {
CitygmlWriter.writeModel(citygml2ModelResult.first, outputDirectoryPath, "citygml2_model", parameters.deriveCitygml2WriterParameters())
val filePath = outputDirectoryPath / ("citygml2_model.gml" + parameters.compressionFormat.toFileExtension())
CitygmlWriter.writeToFile(citygml2ModelResult.first, CitygmlVersion.V2_0, filePath)
}

// transform Roadspaces model to CityGML3 model
Expand All @@ -113,7 +115,8 @@ class ValidateOpendriveProcessor(

// write CityGML3 model
if (parameters.writeCitygml3File) {
CitygmlWriter.writeModel(citygml3ModelResult.first, outputDirectoryPath, "citygml3_model", parameters.deriveCitygml3WriterParameters())
val filePath = outputDirectoryPath / ("citygml3_model.gml" + parameters.compressionFormat.toFileExtension())
CitygmlWriter.writeToFile(citygml3ModelResult.first, CitygmlVersion.V3_0, filePath)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ import io.rtron.model.citygml.CitygmlModel
import mu.KotlinLogging
import org.citygml4j.xml.CityGMLContext
import org.citygml4j.xml.module.citygml.CoreModule
import java.io.OutputStream
import java.nio.charset.StandardCharsets
import java.nio.file.Path
import kotlin.io.path.Path
import kotlin.io.path.div

object CitygmlWriter {

Expand All @@ -36,18 +35,18 @@ object CitygmlWriter {

// Methods

fun writeModel(model: CitygmlModel, directoryPath: Path, fileNameWithoutExtension: String, parameters: CitygmlWriterParameters): List<Path> {
return parameters.versions.map { write(model, it, directoryPath, fileNameWithoutExtension, parameters) }
fun writeToFile(model: CitygmlModel, version: CitygmlVersion, filePath: Path) {
val outputStream = filePath.outputStreamDirectOrCompressed()
writeToStream(model, version, outputStream)
outputStream.close()

logger.info("Completed writing of file ${filePath.fileName} (around ${filePath.getFileSizeToDisplay()}).")
}

private fun write(model: CitygmlModel, version: CitygmlVersion, directoryPath: Path, fileNameWithoutExtension: String, parameters: CitygmlWriterParameters): Path {
fun writeToStream(model: CitygmlModel, version: CitygmlVersion, outputStream: OutputStream) {
val citygmlVersion = version.toGmlCitygml()
val out = citygmlContext.createCityGMLOutputFactory(citygmlVersion)!!

val fileName = fileNameWithoutExtension + ".gml" + parameters.fileCompression.fold({ "" }, { it.extensionWithDot })
val filePath = directoryPath / Path(fileName)
val outputStream = filePath.outputStreamDirectOrCompressed()

val writer = out.createCityGMLChunkWriter(outputStream, StandardCharsets.UTF_8.name())
writer.apply {
withIndent(" ")
Expand All @@ -61,10 +60,5 @@ object CitygmlWriter {
}

writer.close()
outputStream.close()
logger.info("Completed writing of file $fileName (around ${filePath.getFileSizeToDisplay()}).")
return filePath
}

val supportedFilenameEndings: Set<String> = setOf("gml")
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import java.io.InputStream
import java.nio.file.Path
import kotlin.io.path.isRegularFile

object OpendriveFileReader {
object OpendriveReader {

// Properties and Initializers
private val logger = KotlinLogging.logger {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,31 @@ package io.rtron.readerwriter.opendrive
import arrow.core.Either
import arrow.core.raise.either
import io.rtron.io.files.getFileSizeToDisplay
import io.rtron.io.files.outputStreamDirectOrCompressed
import io.rtron.model.opendrive.OpendriveModel
import io.rtron.readerwriter.opendrive.writer.OpendriveMarshaller
import io.rtron.std.BaseException
import mu.KotlinLogging
import java.io.OutputStream
import java.nio.file.Path

class OpendriveFileWriter(
val parameters: OpendriveWriterParameters
) {
object OpendriveWriter {
// Properties and Initializers
private val logger = KotlinLogging.logger {}

private val opendriveMarshaller by lazy { OpendriveMarshaller() }

// Methods
fun write(model: OpendriveModel, directoryPath: Path): Either<OpendriveWriterException, Path> = either {
val filePath = opendriveMarshaller.writeToFile(model, directoryPath, parameters.fileCompression).bind()
fun writeToFile(model: OpendriveModel, filePath: Path): Either<OpendriveWriterException, Unit> = either {
val outputStream: OutputStream = filePath.outputStreamDirectOrCompressed()
writeToStream(model, outputStream)
outputStream.close()

logger.info("Completed writing of file ${filePath.fileName} (around ${filePath.getFileSizeToDisplay()}).")
}

filePath
fun writeToStream(model: OpendriveModel, outputStream: OutputStream) {
opendriveMarshaller.writeToStream(model, outputStream)
}
}

Expand Down

This file was deleted.

Loading

0 comments on commit 1133b9f

Please sign in to comment.