Skip to content

Commit

Permalink
Read LKMap XTF files (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
domi-b authored Oct 31, 2024
2 parents f1d158c + e924d5d commit 17f0bc8
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 1 deletion.
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Expand Down
79 changes: 79 additions & 0 deletions src/main/java/ch/geowerkstatt/lk2dxf/LKMapXtfReader.java
Original file line number Diff line number Diff line change
@@ -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<IomObject> 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();
}
}
19 changes: 18 additions & 1 deletion src/main/java/ch/geowerkstatt/lk2dxf/Main.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
package ch.geowerkstatt.lk2dxf;

import java.io.File;
import java.util.List;

public final class Main {
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<String> 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;
}
}
}
}
58 changes: 58 additions & 0 deletions src/test/data/LKMapXtfReaderTest/Valid.xtf
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?xml version ="1.0" encoding ="UTF-8"?>
<TRANSFER xmlns="http://www.interlis.ch/INTERLIS2.3">
<HEADERSECTION VERSION="2.3" SENDER="Test">
<MODELS>
<MODEL NAME="Units" URI="http://www.interlis.ch/models" VERSION="2012-02-20" />
<MODEL NAME="Base_LV95" URI="http://www.sia.ch/405" VERSION="05.10.2018" />
<MODEL NAME="SIA405_Base_LV95" URI="http://www.sia.ch/405" VERSION="05.10.2018" />
<MODEL NAME="SIA405_LKMap_2015_LV95" URI="http://www.sia.ch/405" VERSION="27.04.2018" />
</MODELS>
<COMMENT>test data</COMMENT>
</HEADERSECTION>
<DATASECTION>
<SIA405_LKMap_2015_LV95.SIA405_LKMap BID="x1">
<SIA405_LKMap_2015_LV95.SIA405_LKMap.LKFlaeche TID="obj1">
<OBJ_ID>obj1</OBJ_ID>
<Metaattribute>
<SIA405_Base_LV95.Metaattribute>
<Datenherr>Test</Datenherr>
<Datenlieferant>Test</Datenlieferant>
<Letzte_Aenderung>20241030</Letzte_Aenderung>
</SIA405_Base_LV95.Metaattribute>
</Metaattribute>
<Eigentuemer>Test</Eigentuemer>
<Lagebestimmung>unbekannt</Lagebestimmung>
<Status>in_Betrieb</Status>
<Flaeche>
<SURFACE>
<BOUNDARY>
<POLYLINE>
<COORD>
<C1>2669992.781</C1>
<C2>1208904.666</C2>
</COORD>
<COORD>
<C1>2669971.614</C1>
<C2>1206904.416</C2>
</COORD>
<COORD>
<C1>2672924.364</C1>
<C2>1206936.166</C2>
</COORD>
<COORD>
<C1>2672818.531</C1>
<C2>1208915.250</C2>
</COORD>
<COORD>
<C1>2669992.781</C1>
<C2>1208904.666</C2>
</COORD>
</POLYLINE>
</BOUNDARY>
</SURFACE>
</Flaeche>
<Objektart>Wasser.unbekannt</Objektart>
</SIA405_LKMap_2015_LV95.SIA405_LKMap.LKFlaeche>
</SIA405_LKMap_2015_LV95.SIA405_LKMap>
</DATASECTION>
</TRANSFER>
15 changes: 15 additions & 0 deletions src/test/data/LKMapXtfReaderTest/WrongModel.xtf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version ="1.0" encoding ="UTF-8"?>
<TRANSFER xmlns="http://www.interlis.ch/INTERLIS2.3">
<HEADERSECTION VERSION="2.3" SENDER="Test">
<MODELS>
<MODEL NAME="TestData" VERSION="2024-10-30" URI="http://interlis.ch" />
</MODELS>
</HEADERSECTION>
<DATASECTION>
<TestData.Test BID="x1">
<TestData.Test.Abc TID="obj1">
<Attr>value</Attr>
</TestData.Test.Abc>
</TestData.Test>
</DATASECTION>
</TRANSFER>
32 changes: 32 additions & 0 deletions src/test/java/ch/geowerkstatt/lk2dxf/LKMapXtfReaderTest.java
Original file line number Diff line number Diff line change
@@ -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<String> 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 -> { }));
}
}
}

0 comments on commit 17f0bc8

Please sign in to comment.