Skip to content

Commit

Permalink
added a transform option to the import and export commands
Browse files Browse the repository at this point in the history
  • Loading branch information
clausnagel committed Jan 2, 2025
1 parent 552e7f4 commit 146d39d
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* citydb-tool - Command-line tool for the 3D City Database
* https://www.3dcitydb.org/
*
* Copyright 2022-2025
* virtualcitysystems GmbH, Germany
* https://vc.systems/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.citydb.cli.common;

import org.citydb.model.common.Matrix3x4;
import picocli.CommandLine;

import java.util.Arrays;

public class TransformOptions implements Option {
@CommandLine.Option(names = "--transform", paramLabel = "<m0,m1,...,m11|swap-xy>",
description = "Transform coordinates using a 3x4 matrix in row-major order. Use 'swap-xy' as a shortcut.")
private String transform;

private Matrix3x4 transformationMatrix;

public Matrix3x4 getTransformationMatrix() {
return transformationMatrix;
}

@Override
public void preprocess(CommandLine commandLine) throws Exception {
if (transform != null) {
if (transform.equalsIgnoreCase("swap-xy")) {
transformationMatrix = Matrix3x4.ofRowMajor(
0, 1, 0, 0,
1, 0, 0, 0,
0, 0, 1, 0);
} else {
String[] values = transform.split(",");
if (values.length == 12) {
try {
transformationMatrix = Matrix3x4.ofRowMajor(Arrays.stream(values)
.map(Double::parseDouble)
.toList());
} catch (NumberFormatException e) {
throw new CommandLine.ParameterException(commandLine,
"Error: The elements of a 3x4 matrix must be floating point numbers but were '" +
String.join(",", values) + "'");
}
} else {
throw new CommandLine.ParameterException(commandLine,
"Error: A 3x4 matrix must be in M0,M1,...,M11 format but was '" + transform + "'");
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ public abstract class ExportController implements Command {
@CommandLine.Mixin
protected CrsOptions crsOptions;

@CommandLine.Mixin
protected TransformOptions transformOptions;

@CommandLine.ArgGroup(exclusive = false, order = Integer.MAX_VALUE,
heading = "Query and filter options:%n")
protected QueryOptions queryOptions;
Expand Down Expand Up @@ -263,6 +266,10 @@ protected ExportOptions getExportOptions() throws ExecutionException {
exportOptions.setTargetSrs(crsOptions.getTargetSrs());
}

if (transformOptions != null) {
exportOptions.setAffineTransform(transformOptions.getTransformationMatrix());
}

if (queryOptions != null) {
if (queryOptions.getLodOptions() != null) {
exportOptions.setLodOptions(queryOptions.getLodOptions().getExportLodOptions());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ enum Mode {import_all, skip, delete, terminate}
description = "Compute and overwrite extents of features.")
protected Boolean computeEnvelopes;

@CommandLine.Mixin
protected TransformOptions transformOptions;

@CommandLine.ArgGroup(exclusive = false, order = Integer.MAX_VALUE,
heading = "Database connection options:%n")
protected ConnectionOptions connectionOptions;
Expand Down Expand Up @@ -295,6 +298,10 @@ protected ImportOptions getImportOptions() throws ExecutionException {
importOptions.setNumberOfThreads(threadsOptions.getNumberOfThreads());
}

if (transformOptions != null) {
importOptions.setAffineTransform(transformOptions.getTransformationMatrix());
}

return importOptions;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import org.citydb.model.util.matrix.Matrix;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

Expand Down Expand Up @@ -58,6 +59,10 @@ public static Matrix2x2 ofRowMajor(List<Double> values) {
}
}

public static Matrix2x2 ofRowMajor(double... values) {
return ofRowMajor(values != null ? Arrays.stream(values).boxed().toList() : null);
}

public static Matrix2x2 identity() {
return new Matrix2x2(Matrix.identity(2, 2));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import org.citydb.model.util.matrix.Matrix;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

Expand Down Expand Up @@ -60,7 +61,11 @@ public static Matrix3x4 ofRowMajor(List<Double> values) {
}
}

public static Matrix3x4 ofRowMajor(double... values) {
return ofRowMajor(values != null ? Arrays.stream(values).boxed().toList() : null);
}

public static Matrix3x4 identity() {
return new Matrix3x4(Matrix.identity(4, 4).getSubMatrix(0, 2, 0, 3));
return new Matrix3x4(Matrix.identity(3, 4));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import org.citydb.model.util.matrix.Matrix;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

Expand Down Expand Up @@ -58,6 +59,10 @@ public static Matrix4x4 ofRowMajor(List<Double> values) {
}
}

public static Matrix4x4 ofRowMajor(double... values) {
return ofRowMajor(values != null ? Arrays.stream(values).boxed().toList() : null);
}

public static Matrix4x4 identity() {
return new Matrix4x4(Matrix.identity(4, 4));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ public class ExportOptions {
private OutputFile outputFile;
private int numberOfThreads;
private SrsReference targetSrs;
private LodOptions lodOptions;
private AppearanceOptions appearanceOptions;
@JSONField(serializeUsing = Matrix3x4Writer.class, deserializeUsing = Matrix3x4Reader.class)
private Matrix3x4 affineTransform;
private LodOptions lodOptions;
private AppearanceOptions appearanceOptions;

public OutputFile getOutputFile() {
if (outputFile == null) {
Expand Down Expand Up @@ -93,6 +93,15 @@ public ExportOptions setTargetSrs(SrsReference targetSrs) {
return this;
}

public Optional<Matrix3x4> getAffineTransform() {
return Optional.ofNullable(affineTransform);
}

public ExportOptions setAffineTransform(Matrix3x4 affineTransform) {
this.affineTransform = affineTransform;
return this;
}

public Optional<LodOptions> getLodOptions() {
return Optional.ofNullable(lodOptions);
}
Expand All @@ -110,13 +119,4 @@ public ExportOptions setAppearanceOptions(AppearanceOptions appearanceOptions) {
this.appearanceOptions = appearanceOptions;
return this;
}

public Optional<Matrix3x4> getAffineTransform() {
return Optional.ofNullable(affineTransform);
}

public ExportOptions setAffineTransform(Matrix3x4 affineTransform) {
this.affineTransform = affineTransform;
return this;
}
}

0 comments on commit 146d39d

Please sign in to comment.