Skip to content

Commit

Permalink
feat(shp): support detecting type based on file name with result_ prefix
Browse files Browse the repository at this point in the history
...as found in hale-connect transformation results when exporting to
Shapefiles.

ING-4416
  • Loading branch information
stempler committed Sep 5, 2024
1 parent 42adc10 commit 299de8c
Show file tree
Hide file tree
Showing 16 changed files with 76 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UTF-8
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PROJCS["ETRS_1989_UTM_Zone_32N",GEOGCS["GCS_ETRS_1989",DATUM["D_ETRS_1989",SPHEROID["GRS_1980",6378137.0,298.257222101]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Transverse_Mercator"],PARAMETER["False_Easting",500000.0],PARAMETER["False_Northing",0.0],PARAMETER["Central_Meridian",9.0],PARAMETER["Scale_Factor",0.9996],PARAMETER["Latitude_Of_Origin",0.0],UNIT["Meter",1.0]]
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UTF-8
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PROJCS["ETRS_1989_UTM_Zone_32N",GEOGCS["GCS_ETRS_1989",DATUM["D_ETRS_1989",SPHEROID["GRS_1980",6378137.0,298.257222101]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Transverse_Mercator"],PARAMETER["False_Easting",500000.0],PARAMETER["False_Northing",0.0],PARAMETER["Central_Meridian",9.0],PARAMETER["Scale_Factor",0.9996],PARAMETER["Latitude_Of_Origin",0.0],UNIT["Meter",1.0]]
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

import javax.xml.namespace.QName;

Expand Down Expand Up @@ -242,15 +243,20 @@ public static Pair<TypeDefinition, Integer> getMostCompatibleShapeType(TypeIndex
TypeDefinition preferredType = types
.getType(new QName(ShapefileConstants.SHAPEFILE_NS, preferredName));
if (allowNonShapefileTypes && preferredType == null) {
preferredType = types.getMappingRelevantTypes().stream()
.filter(t -> t.getName().getLocalPart().equals(preferredName)).findFirst()
.orElse(null);
preferredType = findPreferred(preferredName, findName -> {
return types.getMappingRelevantTypes().stream()
.filter(t -> t.getName().getLocalPart().equals(findName)).findFirst()
.orElse(null);
});
}
if (allowNonShapefileTypes && preferredType == null) {
preferredType = types.getMappingRelevantTypes().stream()
// check displayname as well (e.g. in case of XSD where this
// represents the XML element)
.filter(t -> t.getDisplayName().equals(preferredName)).findFirst().orElse(null);
preferredType = findPreferred(preferredName, findName -> {
return types.getMappingRelevantTypes().stream()
// check displayname as well (e.g. in case of XSD where this
// represents the XML element)
.filter(t -> t.getDisplayName().equals(preferredName)).findFirst()
.orElse(null);
});
}
if (preferredType != null) {
int comp = checkCompatibility(preferredType, dataType, allowNonShapefileTypes);
Expand Down Expand Up @@ -292,6 +298,23 @@ else if (maxCompatibility > 0 && comp == maxCompatibility) {
return null;
}

private static TypeDefinition findPreferred(String preferredName,
Function<String, TypeDefinition> find) {
TypeDefinition result = find.apply(preferredName);

if (result == null) {
// also allow for type w/o result_ prefix as the case for transformation results
// on hale-connect by default
String checkName = preferredName;
while (result == null && checkName.startsWith("result_")) {
checkName = checkName.substring("result_".length());
result = find.apply(checkName);
}
}

return result;
}

/**
* Determines if the compatibility rating between the two Shapefile type
* definitions.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,48 @@ class ShapeInstanceReaderTest extends AbstractPlatformTest {
validateArokFnpIkg(list, 'geometrie')
}

/**
* Test reading Shapefile instances using an XML schema, type detection based on file name, but name has result_ prefix.
*/
@Test
void testReadXsdInstancesResultPrefix1() {
Schema xmlSchema = TestUtil.loadSchema(getClass().getClassLoader().getResource("testdata/arokfnp/arok-fnp.xsd").toURI())

InstanceCollection instances = loadInstances(xmlSchema, getClass().getClassLoader().getResource("testdata/arokfnp/alt_name/result_ikg.shp").toURI())

assertNotNull(instances)
List<Instance> list = instances.toList()

// test count
assertThat(list).hasSize(14)

// instance validation
validateArokFnpIkg(list, 'geometrie')
}

/**
* Test reading Shapefile instances using an XML schema, type detection based on file name, but name has result_ prefix.
*/
@CompileStatic(TypeCheckingMode.SKIP) // due to strange Groovy compile error on assertThat
@Test
void testReadXsdInstancesResultPrefix2() {
Schema xmlSchema = TestUtil.loadSchema(getClass().getClassLoader().getResource("testdata/arokfnp/arok-fnp.xsd").toURI())

InstanceCollection instances = loadInstances(xmlSchema, getClass().getClassLoader().getResource("testdata/arokfnp/alt_name/result_fnpgelt.shp").toURI())

assertNotNull(instances)
List<Instance> list = instances.toList()

// test count
assertThat(list).hasSize(14)

// type validation
Set<String> types = list.collect { it.definition.displayName }.toSet()
assertThat(types)
.hasSize(1)
.containsExactly('fnpgelt')
}

@CompileStatic(TypeCheckingMode.SKIP)
private void validateArokFnpIkg(List<Instance> instances, String geometryPropertyName) {
Map<String, List<Instance>> instancesByType = [:]
Expand Down

0 comments on commit 299de8c

Please sign in to comment.