Skip to content

Commit

Permalink
fixup! fix: download linked objects in a WFS using "resolvedepth"
Browse files Browse the repository at this point in the history
  • Loading branch information
emanuelaepure10 committed Aug 15, 2024
1 parent 3bf9e14 commit bc581c7
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public interface GMLConstants {
public static final String NS_GML_32 = "http://www.opengis.net/gml/3.2";

/**
* The GML 3.2 namespace
* Namespace URL for the WFS standard, defined by the OGC
*/
public static final String NS_WFS = "http://www.opengis.net/wfs";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ class StreamGmlReaderTest {
assertEquals(expected, count)
}

// this test might not work anymore in the future. At that moment please ignore it
// @Ignore
@Test
public void testWfsPagination() {
/*
Expand All @@ -146,7 +148,7 @@ class StreamGmlReaderTest {
def schemaUrl = 'https://geodienste.komm.one/ows/services/org.107.7e499bca-5e63-4595-b3c4-eaece8b68608_wfs?SERVICE=WFS&VERSION=2.0.0&REQUEST=DescribeFeatureType'
def dataUrl = 'https://geodienste.komm.one/ows/services/org.107.7e499bca-5e63-4595-b3c4-eaece8b68608_wfs?SERVICE=WFS&VERSION=2.0.0&REQUEST=GetFeature&typenames=xplan:BP_Plan&resolvedepth=*'
def paging = 100
def expected = 754
def expected = 2262

def schema = loadSchema(URI.create(schemaUrl))

Expand All @@ -162,13 +164,9 @@ class StreamGmlReaderTest {
while (it.hasNext()) {
((InstanceIterator) it).skip()
count++
if (count % 100 == 0) {
println("$count instances skipped")
}
}
}

println("$count instances skipped")
assertEquals(expected, count)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ public class GmlInstanceIterator implements InstanceIterator {
private Map<QName, TypeDefinition> allElements;

private TypeDefinition nextType;
private Map<TypeDefinition, String> nextTypeDetails = new HashMap<>();
// private Map<TypeDefinition, String> nextTypeDetails = new HashMap<>();
private boolean isAnAdditionalObject = false;

/**
* The index in the stream for the element returned next with
Expand Down Expand Up @@ -124,7 +125,7 @@ public GmlInstanceIterator() {
super();

nextType = null;
nextTypeDetails = new HashMap<>();
// isAnAdditionalObject = false;

try {
in = new BufferedInputStream(source.getInput());
Expand Down Expand Up @@ -215,7 +216,6 @@ private void proceedToNext() throws XMLStreamException {
typeStack.push(def);
elementStack.push(elementName);

boolean isAnAdditionalObject = false;
// Now you want to find the parent element
for (int i = elementStack.size() - 1; i >= 0; i--) {
if (elementStack.get(i).getNamespaceURI().startsWith(GMLConstants.NS_WFS)
Expand All @@ -239,9 +239,6 @@ private void proceedToNext() throws XMLStreamException {

if (def != null && isAllowedType(def)) {
nextType = def;
if (isAnAdditionalObject) {
nextTypeDetails.put(def, ADDITIONAL_OBJECTS);
}
}
}
else if (event == XMLStreamConstants.END_ELEMENT) {
Expand Down Expand Up @@ -595,19 +592,15 @@ public synchronized Instance next() {
strict, null, crsProvider, nextType, null, false, ignoreNamespaces,
ioProvider);

if (nextTypeDetails != null && !nextTypeDetails.isEmpty()) {
for (Map.Entry<TypeDefinition, String> entry : nextTypeDetails.entrySet()) {
TypeDefinition key = entry.getKey();
String val = entry.getValue();
((MutableInstance) instance).setMetaData(ADDITIONAL_OBJECTS, key);
}
if (isAnAdditionalObject) {
((MutableInstance) instance).setMetaData(ADDITIONAL_OBJECTS, true);
}
return instance;
} catch (XMLStreamException e) {
throw new IllegalStateException(e);
} finally {
nextType = null;
nextTypeDetails = new HashMap<>();
// isAnAdditionalObject = false;
typeStack.pop(); // parseInstance consumes END_ELEMENT
elementStack.pop();
}
Expand Down Expand Up @@ -686,7 +679,7 @@ public synchronized void close() {
// ignore
}
nextType = null;
nextTypeDetails = null;
// isAnAdditionalObject = false;
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,24 @@
import eu.esdihumboldt.hale.io.gml.geometry.GMLConstants;

/**
* Filter the instances by gml:id, storing only those with unique IDs.
* Filter the instances by storing only those with unique IDs.
*/
public class DuplicateIDsFilterIterator implements InstanceIterator {

// HashSet to store unique instance IDs, ensuring no duplicates are
// processed.
private final HashSet<String> uniqueIDInstances = new HashSet<String>();

// Iterator for traversing through instances.
private final InstanceIterator iterator;

// Holds the next instance to be processed, which should be filled if the
// next() or skip() have already been called for the current instance
private Instance nextInstance;
private boolean instranceAlreadyReturned = false;

// Flag to indicate if the current instance has already been returned by the
// iterator.
private boolean instanceAlreadyReturned = false;

/**
* @param instanceIterator InstanceIterator
Expand All @@ -50,7 +60,7 @@ public DuplicateIDsFilterIterator(InstanceIterator instanceIterator) {
public void close() {
iterator.close();
nextInstance = null;
instranceAlreadyReturned = true;
instanceAlreadyReturned = true;
}

/**
Expand Down Expand Up @@ -85,12 +95,24 @@ public void skip() {
}

/**
* @return boolean
* Determines if there is a next instance available for iteration.
*
* @return boolean - true if there is another instance to process, false if
* the iteration is complete.
*
* This method checks if the current instance has already been
* returned. If it has, or if the next instance has not been
* initialized, it attempts to fetch the next unique instance.
*
* The iteration continues until a non-duplicate instance is found,
* which is then stored in `nextInstance`. If no more instances are
* available, the method returns false.
*
* @see java.util.Iterator#hasNext()
*/
@Override
public boolean hasNext() {
if (nextInstance == null || instranceAlreadyReturned) {
if (nextInstance == null || instanceAlreadyReturned) {

while (true) {
if (iterator != null && !iterator.hasNext()) {
Expand All @@ -100,7 +122,7 @@ public boolean hasNext() {
Instance instance = iterator.next();
if (!isTheInstancePresent(instance)) {
nextInstance = instance;
instranceAlreadyReturned = false;
instanceAlreadyReturned = false;
return true;
}
}
Expand All @@ -124,10 +146,16 @@ public Instance next() {
}
throw new NoSuchElementException();
}
instranceAlreadyReturned = true;
instanceAlreadyReturned = true;
return nextInstance;
}

/**
* @param instance Instance to be checked if it has been already given or
* should be returned for further investigation
* @return true if the instance to be checked has been returned already,
* false in contrary case
*/
private boolean isTheInstancePresent(Instance instance) {
for (QName propertyName : instance.getPropertyNames()) {
if (isGmlIdProperty(propertyName)) {
Expand All @@ -152,8 +180,7 @@ private boolean isTheInstancePresent(Instance instance) {
private boolean isGmlIdProperty(QName propertyName) {
return (propertyName.getNamespaceURI().startsWith(GMLConstants.NS_WFS)
|| propertyName.getNamespaceURI().startsWith(GMLConstants.GML_NAMESPACE_CORE))
&& "id".equals(propertyName.getLocalPart())
&& "gml".equals(propertyName.getPrefix());
&& "id".equals(propertyName.getLocalPart());
}

}

0 comments on commit bc581c7

Please sign in to comment.