diff --git a/src/main/java/ch/geowerkstatt/lk2dxf/DxfWriter.java b/src/main/java/ch/geowerkstatt/lk2dxf/DxfWriter.java index 09f9be1..27bdc5f 100644 --- a/src/main/java/ch/geowerkstatt/lk2dxf/DxfWriter.java +++ b/src/main/java/ch/geowerkstatt/lk2dxf/DxfWriter.java @@ -100,7 +100,7 @@ private void prepareDxfForWritingEntities(Collection layerMappings () -> writeLineType("DashDotDot", 0.5, -0.25, 0.0, -0.25, 0.0, -0.25)), () -> writeTable("LAYER", layers.values().toArray(ContentWriter[]::new)), () -> writeTable("STYLE", - layerMappings.stream().map(LayerMapping::font).filter(f -> !f.isBlank()).map(f -> (ContentWriter) () -> writeStyle(f, f)).toArray(ContentWriter[]::new)), + layerMappings.stream().map(LayerMapping::font).filter(f -> !f.isBlank()).distinct().map(f -> (ContentWriter) () -> writeStyle(f, f)).toArray(ContentWriter[]::new)), () -> writeTable("VIEW"), () -> writeTable("UCS"), () -> writeTable("APPID", diff --git a/src/main/java/ch/geowerkstatt/lk2dxf/mapping/ObjectMapper.java b/src/main/java/ch/geowerkstatt/lk2dxf/mapping/ObjectMapper.java index 19ed773..1739387 100644 --- a/src/main/java/ch/geowerkstatt/lk2dxf/mapping/ObjectMapper.java +++ b/src/main/java/ch/geowerkstatt/lk2dxf/mapping/ObjectMapper.java @@ -127,6 +127,13 @@ public List getLayerMappings() { return Collections.unmodifiableList(layerMappings); } + /** + * Get the {@link TransferDescription} with all models used by the {@link LayerMapping}s. + */ + public TransferDescription getTransferDescription() { + return transferDescription; + } + private static boolean matchesEnumSubValue(List allowedValues, String attrValue) { return allowedValues.stream().anyMatch(value -> attrValue.startsWith(value + ".")); } diff --git a/src/test/java/ch/geowerkstatt/lk2dxf/DxfWriterTest.java b/src/test/java/ch/geowerkstatt/lk2dxf/DxfWriterTest.java index 8cf0467..ff70856 100644 --- a/src/test/java/ch/geowerkstatt/lk2dxf/DxfWriterTest.java +++ b/src/test/java/ch/geowerkstatt/lk2dxf/DxfWriterTest.java @@ -42,6 +42,14 @@ public void writeEmptyDxf() throws Exception { assertEquals("0\nSECTION\n2\nHEADER\n9\n$ACADVER\n1\nAC1021\n9\n$HANDSEED\n5\n3b9aca00\n9\n$INSUNITS\n70\n6\n0\nENDSEC\n0\nSECTION\n2\nCLASSES\n0\nENDSEC\n0\nSECTION\n2\nTABLES\n0\nTABLE\n2\nVPORT\n5\n1\n100\nAcDbSymbolTable\n70\n1\n0\nVPORT\n5\n2\n100\nAcDbSymbolTableRecord\n100\nAcDbViewportTableRecord\n2\n*ACTIVE\n70\n0\n0\nENDTAB\n0\nTABLE\n2\nLTYPE\n5\n3\n100\nAcDbSymbolTable\n70\n5\n0\nLTYPE\n5\n4\n100\nAcDbSymbolTableRecord\n100\nAcDbLinetypeTableRecord\n2\nByLayer\n70\n0\n72\n65\n73\n0\n40\n0\n0\nLTYPE\n5\n5\n100\nAcDbSymbolTableRecord\n100\nAcDbLinetypeTableRecord\n2\nByBlock\n70\n0\n72\n65\n73\n0\n40\n0\n0\nLTYPE\n5\n6\n100\nAcDbSymbolTableRecord\n100\nAcDbLinetypeTableRecord\n2\nContinuous\n70\n0\n72\n65\n73\n0\n40\n0\n0\nLTYPE\n5\n7\n100\nAcDbSymbolTableRecord\n100\nAcDbLinetypeTableRecord\n2\nDashed\n70\n0\n72\n65\n73\n2\n40\n0.75\n49\n0.5\n74\n0\n49\n-0.25\n74\n0\n0\nLTYPE\n5\n8\n100\nAcDbSymbolTableRecord\n100\nAcDbLinetypeTableRecord\n2\nDashDotDot\n70\n0\n72\n65\n73\n6\n40\n1.25\n49\n0.5\n74\n0\n49\n-0.25\n74\n0\n49\n0\n74\n0\n49\n-0.25\n74\n0\n49\n0\n74\n0\n49\n-0.25\n74\n0\n0\nENDTAB\n0\nTABLE\n2\nLAYER\n5\n9\n100\nAcDbSymbolTable\n70\n1\n0\nLAYER\n5\nA\n100\nAcDbSymbolTableRecord\n100\nAcDbLayerTableRecord\n2\n0\n6\nContinuous\n370\n25\n62\n0\n70\n0\n390\n0\n0\nENDTAB\n0\nTABLE\n2\nSTYLE\n5\nB\n100\nAcDbSymbolTable\n70\n0\n0\nENDTAB\n0\nTABLE\n2\nVIEW\n5\nC\n100\nAcDbSymbolTable\n70\n0\n0\nENDTAB\n0\nTABLE\n2\nUCS\n5\nD\n100\nAcDbSymbolTable\n70\n0\n0\nENDTAB\n0\nTABLE\n2\nAPPID\n5\nE\n100\nAcDbSymbolTable\n70\n1\n0\nAPPID\n5\nF\n100\nAcDbSymbolTableRecord\n100\nAcDbRegAppTableRecord\n2\nACAD\n70\n0\n0\nENDTAB\n0\nTABLE\n2\nDIMSTYLE\n5\n10\n100\nAcDbSymbolTable\n100\nAcDbDimStyleTable\n70\n0\n71\n1\n0\nENDTAB\n0\nTABLE\n2\nBLOCK_RECORD\n5\n11\n100\nAcDbSymbolTable\n70\n2\n0\nBLOCK_RECORD\n5\n12\n100\nAcDbSymbolTableRecord\n100\nAcDbBlockTableRecord\n2\n*Model_Space\n70\n0\n280\n1\n281\n0\n0\nBLOCK_RECORD\n5\n13\n100\nAcDbSymbolTableRecord\n100\nAcDbBlockTableRecord\n2\n*Paper_Space\n70\n0\n280\n1\n281\n0\n0\nENDTAB\n0\nENDSEC\n0\nSECTION\n2\nBLOCKS\n0\nBLOCK\n5\n14\n8\n0\n100\nAcDbEntity\n100\nAcDbBlockBegin\n2\n*Model_Space\n70\n0\n10\n0\n20\n0\n30\n0\n0\nENDBLK\n5\n15\n8\n0\n100\nAcDbEntity\n100\nAcDbBlockEnd\n0\nBLOCK\n5\n16\n8\n0\n100\nAcDbEntity\n100\nAcDbBlockBegin\n2\n*Paper_Space\n70\n0\n10\n0\n20\n0\n30\n0\n0\nENDBLK\n5\n17\n8\n0\n100\nAcDbEntity\n100\nAcDbBlockEnd\n0\nENDSEC\n0\nSECTION\n2\nENTITIES\n0\nENDSEC\n0\nSECTION\n2\nOBJECTS\n0\nDICTIONARY\n5\n18\n330\n0\n100\nAcDbDictionary\n281\n1\n3\nACAD_GROUP\n350\n19\n0\nDICTIONARY\n5\n19\n330\n18\n100\nAcDbDictionary\n281\n1\n0\nENDSEC\n0\nEOF\n", stringWriter.toString()); } + @Test + public void writeEmptyDxfWithLayerMappings() throws Exception { + var dxfWriter = new DxfWriter(testOutputWriter, 3, List.of(createTestLayerMapping("Layer_A"), createTestLayerMapping("Layer_B")), "APETHOWN"); + dxfWriter.close(); + + assertEquals("999\nAPETHOWN\n0\nSECTION\n2\nHEADER\n9\n$ACADVER\n1\nAC1021\n9\n$HANDSEED\n5\n3b9aca00\n9\n$INSUNITS\n70\n6\n0\nENDSEC\n0\nSECTION\n2\nCLASSES\n0\nENDSEC\n0\nSECTION\n2\nTABLES\n0\nTABLE\n2\nVPORT\n5\n1\n100\nAcDbSymbolTable\n70\n1\n0\nVPORT\n5\n2\n100\nAcDbSymbolTableRecord\n100\nAcDbViewportTableRecord\n2\n*ACTIVE\n70\n0\n0\nENDTAB\n0\nTABLE\n2\nLTYPE\n5\n3\n100\nAcDbSymbolTable\n70\n5\n0\nLTYPE\n5\n4\n100\nAcDbSymbolTableRecord\n100\nAcDbLinetypeTableRecord\n2\nByLayer\n70\n0\n72\n65\n73\n0\n40\n0\n0\nLTYPE\n5\n5\n100\nAcDbSymbolTableRecord\n100\nAcDbLinetypeTableRecord\n2\nByBlock\n70\n0\n72\n65\n73\n0\n40\n0\n0\nLTYPE\n5\n6\n100\nAcDbSymbolTableRecord\n100\nAcDbLinetypeTableRecord\n2\nContinuous\n70\n0\n72\n65\n73\n0\n40\n0\n0\nLTYPE\n5\n7\n100\nAcDbSymbolTableRecord\n100\nAcDbLinetypeTableRecord\n2\nDashed\n70\n0\n72\n65\n73\n2\n40\n0.75\n49\n0.5\n74\n0\n49\n-0.25\n74\n0\n0\nLTYPE\n5\n8\n100\nAcDbSymbolTableRecord\n100\nAcDbLinetypeTableRecord\n2\nDashDotDot\n70\n0\n72\n65\n73\n6\n40\n1.25\n49\n0.5\n74\n0\n49\n-0.25\n74\n0\n49\n0\n74\n0\n49\n-0.25\n74\n0\n49\n0\n74\n0\n49\n-0.25\n74\n0\n0\nENDTAB\n0\nTABLE\n2\nLAYER\n5\n9\n100\nAcDbSymbolTable\n70\n3\n0\nLAYER\n5\nA\n100\nAcDbSymbolTableRecord\n100\nAcDbLayerTableRecord\n2\n0\n6\nContinuous\n370\n25\n62\n0\n70\n0\n390\n0\n0\nLAYER\n5\nB\n100\nAcDbSymbolTableRecord\n100\nAcDbLayerTableRecord\n2\nLayer_A\n6\nContinuous\n370\n25\n62\n1\n70\n0\n390\n0\n0\nLAYER\n5\nC\n100\nAcDbSymbolTableRecord\n100\nAcDbLayerTableRecord\n2\nLayer_B\n6\nContinuous\n370\n25\n62\n1\n70\n0\n390\n0\n0\nENDTAB\n0\nTABLE\n2\nSTYLE\n5\nD\n100\nAcDbSymbolTable\n70\n1\n0\nSTYLE\n5\nE\n100\nAcDbSymbolTableRecord\n100\nAcDbTextStyleTableRecord\n2\narial\n3\narial\n70\n0\n0\nENDTAB\n0\nTABLE\n2\nVIEW\n5\nF\n100\nAcDbSymbolTable\n70\n0\n0\nENDTAB\n0\nTABLE\n2\nUCS\n5\n10\n100\nAcDbSymbolTable\n70\n0\n0\nENDTAB\n0\nTABLE\n2\nAPPID\n5\n11\n100\nAcDbSymbolTable\n70\n1\n0\nAPPID\n5\n12\n100\nAcDbSymbolTableRecord\n100\nAcDbRegAppTableRecord\n2\nACAD\n70\n0\n0\nENDTAB\n0\nTABLE\n2\nDIMSTYLE\n5\n13\n100\nAcDbSymbolTable\n100\nAcDbDimStyleTable\n70\n0\n71\n1\n0\nENDTAB\n0\nTABLE\n2\nBLOCK_RECORD\n5\n14\n100\nAcDbSymbolTable\n70\n3\n0\nBLOCK_RECORD\n5\n15\n100\nAcDbSymbolTableRecord\n100\nAcDbBlockTableRecord\n2\n*Model_Space\n70\n0\n280\n1\n281\n0\n0\nBLOCK_RECORD\n5\n16\n100\nAcDbSymbolTableRecord\n100\nAcDbBlockTableRecord\n2\n*Paper_Space\n70\n0\n280\n1\n281\n0\n0\nBLOCK_RECORD\n5\n17\n100\nAcDbSymbolTableRecord\n100\nAcDbBlockTableRecord\n2\nTestSymbol\n70\n0\n280\n1\n281\n0\n0\nENDTAB\n0\nENDSEC\n0\nSECTION\n2\nBLOCKS\n0\nBLOCK\n5\n18\n8\n0\n100\nAcDbEntity\n100\nAcDbBlockBegin\n2\n*Model_Space\n70\n0\n10\n0\n20\n0\n30\n0\n0\nENDBLK\n5\n19\n8\n0\n100\nAcDbEntity\n100\nAcDbBlockEnd\n0\nBLOCK\n5\n1A\n8\n0\n100\nAcDbEntity\n100\nAcDbBlockBegin\n2\n*Paper_Space\n70\n0\n10\n0\n20\n0\n30\n0\n0\nENDBLK\n5\n1B\n8\n0\n100\nAcDbEntity\n100\nAcDbBlockEnd\n0\nBLOCK\n5\n1C\n8\n0\n100\nAcDbEntity\n100\nAcDbBlockBegin\n2\nTestSymbol\n70\n0\n10\n0\n20\n0\n30\n0\n0\nCIRCLE\n5\n1D\n100\nAcDbEntity\n8\n0\n100\nAcDbCircle\n10\n0\n20\n0\n40\n0.5\n0\nENDBLK\n5\n1E\n8\n0\n100\nAcDbEntity\n100\nAcDbBlockEnd\n0\nENDSEC\n0\nSECTION\n2\nENTITIES\n0\nENDSEC\n0\nSECTION\n2\nOBJECTS\n0\nDICTIONARY\n5\n1F\n330\n0\n100\nAcDbDictionary\n281\n1\n3\nACAD_GROUP\n350\n20\n0\nDICTIONARY\n5\n20\n330\n1F\n100\nAcDbDictionary\n281\n1\n0\nENDSEC\n0\nEOF\n", stringWriter.toString()); + } + @Test public void writeCircle() throws Exception { try (var dxfWriter = new DxfWriter(testOutputWriter)) { @@ -200,6 +208,10 @@ private IomObject[] createArcTestSegments(double pointRadius, double midPointRad } private Collection createTestLayerMappings() { - return List.of(new LayerMapping("Test", List.of(), LayerMapping.OutputType.SURFACE, "", 1, "", "", "", "", "TestSymbol", "", 0.25, 1.25, "arial", null)); + return List.of(createTestLayerMapping("Test")); + } + + private LayerMapping createTestLayerMapping(String layerName) { + return new LayerMapping(layerName, List.of(), LayerMapping.OutputType.SURFACE, "", 1, "", "", "", "", "TestSymbol", "", 0.25, 1.25, "arial", null); } } diff --git a/src/test/java/ch/geowerkstatt/lk2dxf/IntegrationTest.java b/src/test/java/ch/geowerkstatt/lk2dxf/IntegrationTest.java new file mode 100644 index 0000000..5b261e9 --- /dev/null +++ b/src/test/java/ch/geowerkstatt/lk2dxf/IntegrationTest.java @@ -0,0 +1,159 @@ +package ch.geowerkstatt.lk2dxf; + +import ch.ehi.basics.logging.EhiLogger; +import ch.ehi.basics.logging.StdListener; +import ch.geowerkstatt.lk2dxf.mapping.ObjectMapper; +import ch.interlis.ili2c.Ili2cException; +import ch.interlis.iom.IomObject; +import ch.interlis.iom_j.xtf.XtfWriter; +import ch.interlis.iox.IoxException; +import ch.interlis.iox_j.EndBasketEvent; +import ch.interlis.iox_j.EndTransferEvent; +import ch.interlis.iox_j.ObjectEvent; +import ch.interlis.iox_j.StartBasketEvent; +import ch.interlis.iox_j.StartTransferEvent; +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.core.config.Configurator; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.io.File; +import java.io.IOException; +import java.net.URISyntaxException; +import java.nio.file.Files; +import java.util.ArrayList; +import java.util.Random; +import java.util.stream.Stream; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public final class IntegrationTest { + private static final String TEST_OUT_DIR = "src/test/data/Results/IntegrationTest/"; + + @BeforeAll + static void initAll() { + new File(TEST_OUT_DIR).mkdirs(); + + // Configure logging + Configurator.setRootLevel(Level.TRACE); + EhiLogger.getInstance().addListener(new EhiLogAdapter()); + EhiLogger.getInstance().removeListener(StdListener.getInstance()); + } + + @Test + public void multipleFilesWithPerimeter() throws IOException, URISyntaxException, Ili2cException, IoxException { + // Clear output file + var outputFile = new File(TEST_OUT_DIR + "multipleFilesWithPerimeter" + ".dxf"); + var logFile = new File(TEST_OUT_DIR + "multipleFilesWithPerimeter" + ".log"); + if (outputFile.exists()) { + assertTrue(outputFile.delete(), "Failed to delete existing output file"); + } + assertFalse(outputFile.exists()); + + // Generate input files + var files = new ArrayList(); + var objectCount = 2_000; + for (int i = 0; i < 3; i++) { + var file = new File(TEST_OUT_DIR + "multipleFilesWithPerimeter" + "_" + i + ".xtf"); + files.add(file); + writeTestXTF(file, i * objectCount, objectCount); + } + + // Run the main method + var perimeter = "Polygon ((2740857.55514180986210704 1256024.12385561061091721, 2607598.44303888687863946 1264279.11310092429630458, 2508533.20150150312110782 1106392.40145171224139631, 2578116.33859133720397949 1101537.89655045163817704, 2625680.80043338378891349 1178584.46284004743210971, 2706265.21925668558105826 1173867.3261284395121038, 2740857.55514180986210704 1101930.99127641879022121, 2795890.81677723582834005 1100751.7070985168684274, 2740857.55514180986210704 1256024.12385561061091721))"; + Main.main(Stream.concat(files.stream().map(File::getAbsolutePath), Stream.of(outputFile.getAbsolutePath(), "--perimeter", perimeter, "--logfile", logFile.getAbsolutePath())).toArray(String[]::new)); + + // Check output + assertTrue(outputFile.exists()); + assertTrue(outputFile.isFile()); + assertTrue(outputFile.length() < 1_150_000, "Output file is too large <" + outputFile.length() + "> did the perimeter filter work?"); + assertTrue(outputFile.length() > 10_000, "The output file is too small <" + outputFile.length() + "> it contains not much more than the default dxf content!"); + + assertTrue(logFile.exists()); + assertTrue(logFile.isFile()); + assertTrue(Files.readString(logFile.toPath()).contains("The output DXF file contains 2606 mapped objects")); + } + + private void writeTestXTF(File file, int seed, int objectCount) throws IOException, URISyntaxException, Ili2cException, IoxException { + var objectMapper = new ObjectMapper(); + var rand = new Random(seed); + + XtfWriter writer = new XtfWriter(file, objectMapper.getTransferDescription()); + try { + writer.write(new StartTransferEvent()); + writer.write(new StartBasketEvent("SIA405_LKMap_2015_LV95.SIA405_LKMap", "BASKET1")); + for (int i = seed; i < objectCount + seed; i += 4) { + var x = rand.nextDouble() * 300_000 + 2510_000; + var y = rand.nextDouble() * 200_000 + 1085_000; + var size = rand.nextDouble() * 7000 + 3000; + writer.write(new ObjectEvent(createTextObject(i, x, y, "obj_" + (i + 1)))); + writer.write(new ObjectEvent(createLineObject(i + 1, x, y, size))); + writer.write(new ObjectEvent(createFlaecheObject(i + 2, x, y))); + writer.write(new ObjectEvent(createPointObject(i + 3, x, y))); + } + writer.write(new EndBasketEvent()); + writer.write(new EndTransferEvent()); + } finally { + writer.close(); + } + } + + private final String[] precisionValues = new String[]{"genau", "unbekannt", "ungenau"}; + private final String[] lineObjectTypeValues = new String[]{"Abwasser.Fernwirkkabel", "Abwasser.Haltung_Kanal", "Abwasser.Schutzrohr", "Elektrizitaet.AnkerStrebe", "Elektrizitaet.Trasse.oberirdisch", "Elektrizitaet.Trasse.unterirdisch", "Fernwaerme.Fernwirkkabel", "Fernwaerme.Trasse", "Gas.Fernwirkkabel", "Gas.Leitung", "Gas.Schutzrohr", "Kommunikation.Trasse.oberirdisch", "Kommunikation.Trasse.unterirdisch", "Wasser.Fernwirkkabel", "Wasser.Leitung", "Wasser.Schutzrohr", "weitereMedien.Fernwirkkabel", "weitereMedien.Leitung", "weitereMedien.Schutzrohr"}; + private final String[] objectTypeValues = new String[]{"Abwasser", "Elektrizitaet", "Fernwaerme", "Gas", "Kommunikation", "Wasser", "weitereMedien"}; + private final String[] objectStatusValues = new String[]{"ausser_Betrieb", "in_Betrieb", "tot", "unbekannt", "weitere"}; + + private IomObject createLineObject(int oid, double x, double y, double size) { + var rings = 4; + var angleCount = 7; + var segments = new ArrayList(); + for (double r = size; r > 0.1; r -= size / rings) { + for (int i = 0; i < angleCount; i++) { + var angle = Math.TAU / angleCount * i; + var segment = IomObjectHelper.createCoord(Double.toString(x + r * Math.cos(angle)), Double.toString(y + r * Math.sin(angle))); + segments.add(segment); + } + } + + var polyline = IomObjectHelper.createPolyline(segments.toArray(new IomObject[0])); + var rand = new Random(oid); + return IomObjectHelper.createIomObject("SIA405_LKMap_2015_LV95.SIA405_LKMap.LKLinie", "obj_" + oid, + o -> o.addattrobj("Linie", polyline), + o -> o.addattrvalue("Objektart", lineObjectTypeValues[rand.nextInt(lineObjectTypeValues.length)]), + o -> o.addattrvalue("Lagebestimmung", precisionValues[rand.nextInt(precisionValues.length)]), + o -> o.addattrvalue("Status", objectStatusValues[rand.nextInt(objectStatusValues.length)]), + o -> o.addattrvalue("Eigentuemer", "Keine_Angabe")); + } + + private IomObject createFlaecheObject(int oid, double x, double y) { + var rand = new Random(oid); + return IomObjectHelper.createIomObject("SIA405_LKMap_2015_LV95.SIA405_LKMap.LKFlaeche", "obj_" + oid, + o -> o.addattrobj("Flaeche", IomObjectHelper.createRectangleGeometry(Double.toString(x), Double.toString(y), Double.toString(x - 1.5), Double.toString(y + 10))), + o -> o.addattrvalue("Objektart", objectTypeValues[rand.nextInt(objectTypeValues.length)]), + o -> o.addattrvalue("Lagebestimmung", precisionValues[rand.nextInt(precisionValues.length)]), + o -> o.addattrvalue("Status", objectStatusValues[rand.nextInt(objectStatusValues.length)]), + o -> o.addattrvalue("Eigentuemer", "Keine_Angabe")); + } + + private IomObject createPointObject(int oid, double x, double y) { + var rand = new Random(oid); + return IomObjectHelper.createIomObject("SIA405_LKMap_2015_LV95.SIA405_LKMap.LKPunkt", "obj_" + oid, + o -> o.addattrobj("SymbolPos", IomObjectHelper.createCoord(Double.toString(x), Double.toString(y))), + o -> o.addattrvalue("Objektart", objectTypeValues[rand.nextInt(objectTypeValues.length)]), + o -> o.addattrvalue("Lagebestimmung", precisionValues[rand.nextInt(precisionValues.length)]), + o -> o.addattrvalue("Status", objectStatusValues[rand.nextInt(objectStatusValues.length)]), + o -> o.addattrvalue("Eigentuemer", "Keine_Angabe")); + } + + private IomObject createTextObject(int oid, double x, double y, String objectRef) { + return IomObjectHelper.createIomObject("SIA405_LKMap_2015_LV95.SIA405_LKMap.LKObjekt_Text", "obj_" + oid, + o -> o.addattrobj("TextPos", IomObjectHelper.createCoord(Double.toString(x), Double.toString(y))), + o -> o.addattrvalue("TextOri", "90"), + o -> o.addattrvalue("TextHAli", "Left"), + o -> o.addattrvalue("TextVAli", "Base"), + o -> o.addattrvalue("Textinhalt", "obj_" + oid), + o -> o.addattrobj("LKObjektRef", IomObjectHelper.createIomObject("REF", null, + r -> r.setobjectrefoid(objectRef)))); + } +} diff --git a/src/test/java/ch/geowerkstatt/lk2dxf/IomObjectHelper.java b/src/test/java/ch/geowerkstatt/lk2dxf/IomObjectHelper.java index c3ecd21..1bd0424 100644 --- a/src/test/java/ch/geowerkstatt/lk2dxf/IomObjectHelper.java +++ b/src/test/java/ch/geowerkstatt/lk2dxf/IomObjectHelper.java @@ -3,6 +3,8 @@ import ch.interlis.iom.IomObject; import ch.interlis.iom_j.Iom_jObject; +import java.util.function.Consumer; + public final class IomObjectHelper { private IomObjectHelper() { } @@ -130,4 +132,13 @@ public static IomObject createArc(String a1, String a2, String c1, String c2, St arc.setattrvalue("C3", c3); return arc; } + + @SafeVarargs + public static IomObject createIomObject(String tag, String oid, Consumer... attributeSetters) { + var iomObject = new Iom_jObject(tag, oid); + for (Consumer attributeSetter : attributeSetters) { + attributeSetter.accept(iomObject); + } + return iomObject; + } } diff --git a/src/test/java/ch/geowerkstatt/lk2dxf/mapping/ObjectMapperTest.java b/src/test/java/ch/geowerkstatt/lk2dxf/mapping/ObjectMapperTest.java index bf44ad7..ce1be4f 100644 --- a/src/test/java/ch/geowerkstatt/lk2dxf/mapping/ObjectMapperTest.java +++ b/src/test/java/ch/geowerkstatt/lk2dxf/mapping/ObjectMapperTest.java @@ -1,15 +1,20 @@ package ch.geowerkstatt.lk2dxf.mapping; +import ch.ehi.basics.logging.EhiLogger; +import ch.ehi.basics.logging.StdListener; +import ch.geowerkstatt.lk2dxf.EhiLogAdapter; +import ch.geowerkstatt.lk2dxf.IomObjectHelper; import ch.geowerkstatt.lk2dxf.MappedObject; import ch.geowerkstatt.lk2dxf.XtfStreamReader; import ch.interlis.iom.IomObject; -import ch.interlis.iom_j.Iom_jObject; +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.core.config.Configurator; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import java.io.File; import java.util.List; import java.util.Map; -import java.util.function.Consumer; import java.util.stream.Stream; import static org.junit.jupiter.api.Assertions.assertArrayEquals; @@ -19,6 +24,14 @@ public final class ObjectMapperTest { private static final String TEST_FILE = "src/test/data/MapperTest/MapWithText.xtf"; + @BeforeAll + public static void setup() { + // Configure logging + Configurator.setRootLevel(Level.TRACE); + EhiLogger.getInstance().addListener(new EhiLogAdapter()); + EhiLogger.getInstance().removeListener(StdListener.getInstance()); + } + @Test public void mapObject() throws Exception { try (XtfStreamReader xtfReader = new XtfStreamReader(new File(TEST_FILE))) { @@ -82,9 +95,9 @@ public void mappingContainsEnumFilter() throws Exception { var layerMappings = createTextLayerMapping(Map.of("Plantyp", List.of("Leitungskataster"))); var objectMapper = new ObjectMapper(layerMappings); - var objA = createIomObject("SIA405_LKMap_2015_LV95.SIA405_LKMap.LKObjekt_Text", "obj_A", + var objA = IomObjectHelper.createIomObject("SIA405_LKMap_2015_LV95.SIA405_LKMap.LKObjekt_Text", "obj_A", o -> o.addattrvalue("Plantyp", "Leitungskataster")); - var objB = createIomObject("SIA405_LKMap_2015_LV95.SIA405_LKMap.LKObjekt_Text", "obj_B", + var objB = IomObjectHelper.createIomObject("SIA405_LKMap_2015_LV95.SIA405_LKMap.LKObjekt_Text", "obj_B", o -> o.addattrvalue("Plantyp", "Werkplan")); String[] actual = getMappedLayers(objectMapper, objA, objB); @@ -96,9 +109,9 @@ public void mappingContainsEnumFilterWithMultipleOptions() throws Exception { var layerMappings = createTextLayerMapping(Map.of("Plantyp", List.of("Werkplan", "Leitungskataster"))); var objectMapper = new ObjectMapper(layerMappings); - var objA = createIomObject("SIA405_LKMap_2015_LV95.SIA405_LKMap.LKObjekt_Text", "obj_A", + var objA = IomObjectHelper.createIomObject("SIA405_LKMap_2015_LV95.SIA405_LKMap.LKObjekt_Text", "obj_A", o -> o.addattrvalue("Plantyp", "Leitungskataster")); - var objB = createIomObject("SIA405_LKMap_2015_LV95.SIA405_LKMap.LKObjekt_Text", "obj_B", + var objB = IomObjectHelper.createIomObject("SIA405_LKMap_2015_LV95.SIA405_LKMap.LKObjekt_Text", "obj_B", o -> o.addattrvalue("Plantyp", "Werkplan")); String[] actual = getMappedLayers(objectMapper, objA, objB); @@ -110,9 +123,9 @@ public void mapTranslatedObject() throws Exception { var layerMappings = createTextLayerMapping(Map.of("Plantyp", List.of("Werkplan", "Uebersichtsplan.UeP10"))); var objectMapper = new ObjectMapper(layerMappings); - var objA = createIomObject("SIA405_LKMap_2015_f_LV95.SIA405_LKMap_f.LKOBJET_Texte", "obj_A", + var objA = IomObjectHelper.createIomObject("SIA405_LKMap_2015_f_LV95.SIA405_LKMap_f.LKOBJET_Texte", "obj_A", o -> o.addattrvalue("TYPE_DE_PLAN", "plan_de_reseau")); - var objB = createIomObject("SIA405_LKMap_2015_f_LV95.SIA405_LKMap_f.LKOBJET_Texte", "obj_B", + var objB = IomObjectHelper.createIomObject("SIA405_LKMap_2015_f_LV95.SIA405_LKMap_f.LKOBJET_Texte", "obj_B", o -> o.addattrvalue("TYPE_DE_PLAN", "plan_d_ensemble.pe10")); String[] actual = getMappedLayers(objectMapper, objA, objB); @@ -124,9 +137,9 @@ public void mapSubEnum() throws Exception { var layerMappings = createTextLayerMapping(Map.of("Plantyp", List.of("Uebersichtsplan"))); var objectMapper = new ObjectMapper(layerMappings); - var objA = createIomObject("SIA405_LKMap_2015_LV95.SIA405_LKMap.LKObjekt_Text", "obj_A", + var objA = IomObjectHelper.createIomObject("SIA405_LKMap_2015_LV95.SIA405_LKMap.LKObjekt_Text", "obj_A", o -> o.addattrvalue("Plantyp", "Uebersichtsplan.UeP5")); - var objB = createIomObject("SIA405_LKMap_2015_LV95.SIA405_LKMap.LKObjekt_Text", "obj_B", + var objB = IomObjectHelper.createIomObject("SIA405_LKMap_2015_LV95.SIA405_LKMap.LKObjekt_Text", "obj_B", o -> o.addattrvalue("Plantyp", "Werkplan")); String[] actual = getMappedLayers(objectMapper, objA, objB); @@ -138,11 +151,11 @@ public void mapObjectWithForwardRef() throws Exception { var layerMappings = createTextLayerMapping(Map.of("LKObjektRef->Objektart", List.of("Elektrizitaet"))); var objectMapper = new ObjectMapper(layerMappings); - var textObj = createIomObject("SIA405_LKMap_2015_LV95.SIA405_LKMap.LKObjekt_Text", "textObj", - o -> o.addattrobj("LKObjektRef", createIomObject("REF", null, + var textObj = IomObjectHelper.createIomObject("SIA405_LKMap_2015_LV95.SIA405_LKMap.LKObjekt_Text", "textObj", + o -> o.addattrobj("LKObjektRef", IomObjectHelper.createIomObject("REF", null, r -> r.setobjectrefoid("punktObj")))); - var pointObj = createIomObject("SIA405_LKMap_2015_LV95.SIA405_LKMap.LKPunkt", "punktObj", + var pointObj = IomObjectHelper.createIomObject("SIA405_LKMap_2015_LV95.SIA405_LKMap.LKPunkt", "punktObj", o -> o.addattrvalue("Objektart", "Elektrizitaet")); String[] actual = getMappedLayers(objectMapper, textObj, pointObj); @@ -154,11 +167,11 @@ public void mapObjectWithBackwardRef() throws Exception { var layerMappings = createTextLayerMapping(Map.of("LKObjektRef->Objektart", List.of("Elektrizitaet"))); var objectMapper = new ObjectMapper(layerMappings); - var textObj = createIomObject("SIA405_LKMap_2015_LV95.SIA405_LKMap.LKObjekt_Text", "textObj", - o -> o.addattrobj("LKObjektRef", createIomObject("REF", null, + var textObj = IomObjectHelper.createIomObject("SIA405_LKMap_2015_LV95.SIA405_LKMap.LKObjekt_Text", "textObj", + o -> o.addattrobj("LKObjektRef", IomObjectHelper.createIomObject("REF", null, r -> r.setobjectrefoid("punktObj")))); - var pointObj = createIomObject("SIA405_LKMap_2015_LV95.SIA405_LKMap.LKPunkt", "punktObj", + var pointObj = IomObjectHelper.createIomObject("SIA405_LKMap_2015_LV95.SIA405_LKMap.LKPunkt", "punktObj", o -> o.addattrvalue("Objektart", "Elektrizitaet")); String[] actual = getMappedLayers(objectMapper, pointObj, textObj); @@ -170,8 +183,8 @@ public void mapObjectWithMissingRef() throws Exception { var layerMappings = createTextLayerMapping(Map.of("LKObjektRef->Objektart", List.of("Elektrizitaet"))); var objectMapper = new ObjectMapper(layerMappings); - var textObj = createIomObject("SIA405_LKMap_2015_LV95.SIA405_LKMap.LKObjekt_Text", "textObj", - o -> o.addattrobj("LKObjektRef", createIomObject("REF", null, + var textObj = IomObjectHelper.createIomObject("SIA405_LKMap_2015_LV95.SIA405_LKMap.LKObjekt_Text", "textObj", + o -> o.addattrobj("LKObjektRef", IomObjectHelper.createIomObject("REF", null, r -> r.setobjectrefoid("punktObj")))); var exception = assertThrows(IllegalStateException.class, () -> objectMapper.mapObjects(Stream.of(textObj)).toList()); @@ -183,7 +196,7 @@ public void mapObjectWithEmptyRef() throws Exception { var layerMappings = createTextLayerMapping(Map.of("LKObjektRef->Objektart", List.of("Elektrizitaet"))); var objectMapper = new ObjectMapper(layerMappings); - var textObj = createIomObject("SIA405_LKMap_2015_LV95.SIA405_LKMap.LKObjekt_Text", "textObj"); + var textObj = IomObjectHelper.createIomObject("SIA405_LKMap_2015_LV95.SIA405_LKMap.LKObjekt_Text", "textObj"); String[] actual = getMappedLayers(objectMapper, textObj); assertArrayEquals(new String[] {"CatchAllText"}, actual); @@ -197,12 +210,12 @@ public void resolvePathWithStruct() throws Exception { "TestSymbol", "", 0.25, 1.25, "arial", Map.of("LKObjektRef->Objektart", List.of("Elektrizitaet")))); var objectMapper = new ObjectMapper(layerMappings); - var textObj = createIomObject("SIA405_LKMap_2015_LV95.SIA405_LKMap.LKObjekt_Text", "textObj", - o -> o.addattrobj("LKObjektRef", createIomObject("REF", null, + var textObj = IomObjectHelper.createIomObject("SIA405_LKMap_2015_LV95.SIA405_LKMap.LKObjekt_Text", "textObj", + o -> o.addattrobj("LKObjektRef", IomObjectHelper.createIomObject("REF", null, r -> r.setobjectrefoid("punktObj")))); - var pointObj = createIomObject("SIA405_LKMap_2015_LV95.SIA405_LKMap.LKPunkt", "punktObj", + var pointObj = IomObjectHelper.createIomObject("SIA405_LKMap_2015_LV95.SIA405_LKMap.LKPunkt", "punktObj", o -> o.addattrvalue("Objektart", "Elektrizitaet"), - o -> o.addattrobj("Metaattribute", createIomObject("Metaattribute", null, + o -> o.addattrobj("Metaattribute", IomObjectHelper.createIomObject("Metaattribute", null, m -> m.addattrvalue("Datenlieferant", "MINATERI")))); List output = objectMapper.mapObjects(Stream.of(textObj, pointObj)).toList(); @@ -241,13 +254,4 @@ private String[] getMappedLayers(ObjectMapper objectMapper, IomObject... objects .map(o -> o.layerMapping().layer()) .toArray(String[]::new); } - - @SafeVarargs - private static IomObject createIomObject(String tag, String oid, Consumer... attributeSetters) { - var iomObject = new Iom_jObject(tag, oid); - for (Consumer attributeSetter : attributeSetters) { - attributeSetter.accept(iomObject); - } - return iomObject; - } }