-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
206 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
32
src/test/java/ch/geowerkstatt/lk2dxf/LKMapXtfReaderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 -> { })); | ||
} | ||
} | ||
} |