From e924d5d12104b84320068d70866047e806078d3a Mon Sep 17 00:00:00 2001 From: Dominic Burger Date: Wed, 30 Oct 2024 16:17:09 +0100 Subject: [PATCH] Read LKMap XTF files --- build.gradle | 4 + .../geowerkstatt/lk2dxf/LKMapXtfReader.java | 79 +++++++++++++++++++ .../java/ch/geowerkstatt/lk2dxf/Main.java | 19 ++++- src/test/data/LKMapXtfReaderTest/Valid.xtf | 58 ++++++++++++++ .../data/LKMapXtfReaderTest/WrongModel.xtf | 15 ++++ .../lk2dxf/LKMapXtfReaderTest.java | 32 ++++++++ 6 files changed, 206 insertions(+), 1 deletion(-) create mode 100644 src/main/java/ch/geowerkstatt/lk2dxf/LKMapXtfReader.java create mode 100644 src/test/data/LKMapXtfReaderTest/Valid.xtf create mode 100644 src/test/data/LKMapXtfReaderTest/WrongModel.xtf create mode 100644 src/test/java/ch/geowerkstatt/lk2dxf/LKMapXtfReaderTest.java diff --git a/build.gradle b/build.gradle index a6ed4a9..ec73cc1 100644 --- a/build.gradle +++ b/build.gradle @@ -9,9 +9,13 @@ version = '1.0-SNAPSHOT' repositories { mavenCentral() + maven { url "https://jars.interlis.ch" } } dependencies { + implementation 'ch.interlis:iox-ili:1.23.1' + implementation 'ch.interlis:ili2c-core:5.5.4' + testImplementation platform('org.junit:junit-bom:5.10.0') testImplementation 'org.junit.jupiter:junit-jupiter' } diff --git a/src/main/java/ch/geowerkstatt/lk2dxf/LKMapXtfReader.java b/src/main/java/ch/geowerkstatt/lk2dxf/LKMapXtfReader.java new file mode 100644 index 0000000..c0aecda --- /dev/null +++ b/src/main/java/ch/geowerkstatt/lk2dxf/LKMapXtfReader.java @@ -0,0 +1,79 @@ +package ch.geowerkstatt.lk2dxf; + +import ch.ehi.basics.settings.Settings; +import ch.interlis.iom.IomObject; +import ch.interlis.iox.EndBasketEvent; +import ch.interlis.iox.EndTransferEvent; +import ch.interlis.iox.IoxEvent; +import ch.interlis.iox.IoxException; +import ch.interlis.iox.IoxReader; +import ch.interlis.iox.ObjectEvent; +import ch.interlis.iox.StartBasketEvent; +import ch.interlis.iox.StartTransferEvent; +import ch.interlis.iox_j.logging.LogEventFactory; +import ch.interlis.iox_j.utility.ReaderFactory; + +import java.io.File; +import java.util.function.Consumer; + +/** + * A reader for LKMap INTERLIS transfer files. + */ +public final class LKMapXtfReader implements AutoCloseable { + private static final String BASKET_NAME = "SIA405_LKMap_2015_LV95.SIA405_LKMap"; + private static final ReaderFactory READER_FACTORY = new ReaderFactory(); + + private final IoxReader reader; + + /** + * Creates a new reader for LKMap INTERLIS transfer files. + * @param xtfFile The file to read from. + * @throws IoxException If an error occurs while creating the transfer file reader. + */ + public LKMapXtfReader(File xtfFile) throws IoxException { + LogEventFactory logEventFactory = new LogEventFactory(); + Settings settings = new Settings(); + this.reader = READER_FACTORY.createReader(xtfFile, logEventFactory, settings); + } + + /** + * Reads the objects streamed by the reader and passes them to the consumer. + * @param consumer A consumer to process the objects. + * @throws IoxException If an error occurs while reading the objects. + * @throws IllegalStateException If the transfer file is not in the expected format. + */ + public void readObjects(Consumer consumer) throws IoxException { + IoxEvent event = reader.read(); + if (!(event instanceof StartTransferEvent)) { + throw new IllegalStateException("Expected start transfer event, got: " + event); + } + + event = reader.read(); + while (!(event instanceof EndTransferEvent)) { + if (event instanceof StartBasketEvent startBasketEvent) { + if (!BASKET_NAME.equals(startBasketEvent.getType())) { + throw new IllegalStateException("Invalid basket type: " + startBasketEvent.getType()); + } + } else { + throw new IllegalStateException("Expected start basket event, got: " + event); + } + + event = reader.read(); + while (event instanceof ObjectEvent objectEvent) { + consumer.accept(objectEvent.getIomObject()); + event = reader.read(); + } + + if (!(event instanceof EndBasketEvent)) { + throw new IllegalStateException("Expected end basket event, got: " + event); + } + + event = reader.read(); + } + } + + @Override + public void close() throws Exception { + reader.close(); + } +} diff --git a/src/main/java/ch/geowerkstatt/lk2dxf/Main.java b/src/main/java/ch/geowerkstatt/lk2dxf/Main.java index c9d1923..5ed1364 100644 --- a/src/main/java/ch/geowerkstatt/lk2dxf/Main.java +++ b/src/main/java/ch/geowerkstatt/lk2dxf/Main.java @@ -1,5 +1,8 @@ package ch.geowerkstatt.lk2dxf; +import java.io.File; +import java.util.List; + public final class Main { private Main() { } @@ -7,6 +10,20 @@ private Main() { } * Application entry point. */ public static void main(String[] args) { - System.out.println("Hello world!"); + processFiles(List.of(args)); + } + + private static void processFiles(List xtfFiles) { + for (String xtfFile : xtfFiles) { + try (LKMapXtfReader reader = new LKMapXtfReader(new File(xtfFile))) { + reader.readObjects(iomObject -> { + System.out.println(iomObject.getobjectoid()); + }); + } catch (Exception e) { + System.err.println("Failed to process file: " + xtfFile); + e.printStackTrace(); + return; + } + } } } diff --git a/src/test/data/LKMapXtfReaderTest/Valid.xtf b/src/test/data/LKMapXtfReaderTest/Valid.xtf new file mode 100644 index 0000000..c84e682 --- /dev/null +++ b/src/test/data/LKMapXtfReaderTest/Valid.xtf @@ -0,0 +1,58 @@ + + + + + + + + + + test data + + + + + obj1 + + + Test + Test + 20241030 + + + Test + unbekannt + in_Betrieb + + + + + + 2669992.781 + 1208904.666 + + + 2669971.614 + 1206904.416 + + + 2672924.364 + 1206936.166 + + + 2672818.531 + 1208915.250 + + + 2669992.781 + 1208904.666 + + + + + + Wasser.unbekannt + + + + \ No newline at end of file diff --git a/src/test/data/LKMapXtfReaderTest/WrongModel.xtf b/src/test/data/LKMapXtfReaderTest/WrongModel.xtf new file mode 100644 index 0000000..d4e5eee --- /dev/null +++ b/src/test/data/LKMapXtfReaderTest/WrongModel.xtf @@ -0,0 +1,15 @@ + + + + + + + + + + + value + + + + \ No newline at end of file diff --git a/src/test/java/ch/geowerkstatt/lk2dxf/LKMapXtfReaderTest.java b/src/test/java/ch/geowerkstatt/lk2dxf/LKMapXtfReaderTest.java new file mode 100644 index 0000000..cf69adf --- /dev/null +++ b/src/test/java/ch/geowerkstatt/lk2dxf/LKMapXtfReaderTest.java @@ -0,0 +1,32 @@ +package ch.geowerkstatt.lk2dxf; + +import org.junit.jupiter.api.Test; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +public final class LKMapXtfReaderTest { + private static final String TEST_DIR = "src/test/data/LKMapXtfReaderTest/"; + + @Test + public void readValidXtf() throws Exception { + try (LKMapXtfReader reader = new LKMapXtfReader(new File(TEST_DIR + "Valid.xtf"))) { + List objectIds = new ArrayList<>(); + + reader.readObjects(iomObject -> objectIds.add(iomObject.getobjectoid())); + + assertArrayEquals(new String[] {"obj1"}, objectIds.toArray()); + } + } + + @Test + public void readXtfForWrongModel() throws Exception { + try (LKMapXtfReader reader = new LKMapXtfReader(new File(TEST_DIR + "WrongModel.xtf"))) { + assertThrows(IllegalStateException.class, () -> reader.readObjects(iomObject -> { })); + } + } +}