From acb7df7c2b2d8c0d9b7412328c30b93f2865d5d4 Mon Sep 17 00:00:00 2001 From: Claus Nagel Date: Sat, 27 Jan 2024 18:04:34 +0100 Subject: [PATCH] improved support of template appearances for the to-cityjson command --- .../tools/command/ToCityJSONCommand.java | 1 + .../citygml4j/tools/util/GlobalObjects.java | 33 +++++++++++-------- .../tools/util/GlobalObjectsReader.java | 12 +++++-- 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/citygml4j/tools/command/ToCityJSONCommand.java b/src/main/java/org/citygml4j/tools/command/ToCityJSONCommand.java index 87d7bcf..0db8522 100644 --- a/src/main/java/org/citygml4j/tools/command/ToCityJSONCommand.java +++ b/src/main/java/org/citygml4j/tools/command/ToCityJSONCommand.java @@ -126,6 +126,7 @@ public Integer call() throws ExecutionException { log.debug("Reading global appearances, groups and implicit geometries from input file."); GlobalObjects globalObjects = GlobalObjectsReader.defaults() + .withTemplateAppearances(true) .read(inputFile, getCityGMLContext()); try (CityGMLReader reader = createSkippingCityGMLReader(in, inputFile, inputOptions, diff --git a/src/main/java/org/citygml4j/tools/util/GlobalObjects.java b/src/main/java/org/citygml4j/tools/util/GlobalObjects.java index 2e67fa1..c36d34f 100644 --- a/src/main/java/org/citygml4j/tools/util/GlobalObjects.java +++ b/src/main/java/org/citygml4j/tools/util/GlobalObjects.java @@ -23,10 +23,9 @@ import org.citygml4j.core.model.appearance.Appearance; import org.citygml4j.core.model.cityobjectgroup.CityObjectGroup; +import org.citygml4j.core.model.core.AbstractAppearanceProperty; import org.citygml4j.core.model.core.ImplicitGeometry; -import org.citygml4j.core.util.reference.DefaultReferenceResolver; import org.xmlobjects.gml.model.geometry.AbstractGeometry; -import org.xmlobjects.gml.util.reference.ReferenceResolver; import javax.xml.namespace.QName; import java.util.ArrayList; @@ -47,8 +46,6 @@ public enum Type { private final List appearances = new ArrayList<>(); private final List cityObjectGroups = new ArrayList<>(); private final Map templateGeometries = new HashMap<>(); - private final ReferenceResolver referenceResolver = DefaultReferenceResolver.newInstance() - .storeRefereesWithReferencedObject(true); GlobalObjects() { } @@ -75,18 +72,26 @@ void add(CityObjectGroup cityObjectGroup, QName name) { cityObjectGroups.add(cityObjectGroup); } - void add(ImplicitGeometry implicitGeometry) { - add(implicitGeometry, 0); + void add(ImplicitGeometry implicitGeometry, boolean withTemplateAppearances) { + add(implicitGeometry, 0, withTemplateAppearances); } - void add(ImplicitGeometry implicitGeometry, int lod) { - if (implicitGeometry.getRelativeGeometry() != null - && implicitGeometry.getRelativeGeometry().isSetInlineObject() - && implicitGeometry.getRelativeGeometry().getObject().getId() != null) { - referenceResolver.resolveReferences(implicitGeometry); - AbstractGeometry template = implicitGeometry.getRelativeGeometry().getObject(); - template.getLocalProperties().set(TEMPLATE_LOD, lod); - templateGeometries.put(template.getId(), template); + void add(ImplicitGeometry implicitGeometry, int lod, boolean withTemplateAppearances) { + if (implicitGeometry.getRelativeGeometry() != null) { + if (implicitGeometry.getRelativeGeometry().isSetInlineObject() + && implicitGeometry.getRelativeGeometry().getObject().getId() != null) { + AbstractGeometry template = implicitGeometry.getRelativeGeometry().getObject(); + template.getLocalProperties().set(TEMPLATE_LOD, lod); + templateGeometries.put(template.getId(), template); + } + + if (withTemplateAppearances && implicitGeometry.isSetAppearances()) { + implicitGeometry.getAppearances().stream() + .map(AbstractAppearanceProperty::getObject) + .filter(Appearance.class::isInstance) + .map(Appearance.class::cast) + .forEach(appearances::add); + } } } } diff --git a/src/main/java/org/citygml4j/tools/util/GlobalObjectsReader.java b/src/main/java/org/citygml4j/tools/util/GlobalObjectsReader.java index ed3f66a..88a6bfc 100644 --- a/src/main/java/org/citygml4j/tools/util/GlobalObjectsReader.java +++ b/src/main/java/org/citygml4j/tools/util/GlobalObjectsReader.java @@ -42,6 +42,7 @@ public class GlobalObjectsReader { private final EnumSet types; + private boolean withTemplateAppearances; private GlobalObjectsReader(EnumSet types) { this.types = types; @@ -71,6 +72,11 @@ public static GlobalObjectsReader onlyImplicitGeometries() { return new GlobalObjectsReader(EnumSet.of(GlobalObjects.Type.IMPLICIT_GEOMETRY)); } + public GlobalObjectsReader withTemplateAppearances(boolean withTemplateAppearances) { + this.withTemplateAppearances = withTemplateAppearances; + return this; + } + public GlobalObjects read(Path file, CityGMLContext context) throws ExecutionException { try { GlobalObjects globalObjects = new GlobalObjects(); @@ -91,13 +97,15 @@ public GlobalObjects read(Path file, CityGMLContext context) throws ExecutionExc geometryInfo.getImplicitGeometries(lod).stream() .filter(ImplicitGeometryProperty::isSetInlineObject) .map(ImplicitGeometryProperty::getObject) - .forEach(implicitGeometry -> globalObjects.add(implicitGeometry, lod)); + .forEach(implicitGeometry -> + globalObjects.add(implicitGeometry, lod, withTemplateAppearances)); } geometryInfo.getNonLodImplicitGeometries().stream() .filter(ImplicitGeometryProperty::isSetInlineObject) .map(ImplicitGeometryProperty::getObject) - .forEach(globalObjects::add); + .forEach(implicitGeometry -> + globalObjects.add(implicitGeometry, withTemplateAppearances)); } } }