Skip to content

Commit

Permalink
Updates for GeoTools FeatureCollection api changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jdeolive committed Nov 3, 2012
1 parent 06bc9c7 commit bd8840d
Show file tree
Hide file tree
Showing 28 changed files with 166 additions and 461 deletions.
Expand Up @@ -167,7 +167,7 @@ protected void write(FeatureCollectionResponse featureCollection, OutputStream o
}
}
} finally {
fc.close(i);
i.close();
}
}

Expand Down
Expand Up @@ -195,9 +195,10 @@ public void writeFeatures(SimpleFeatureCollection fColl, FeatureTypeStyle[] ftsL
} catch (NoSuchElementException ex) {
throw new DataSourceException(ex.getMessage(), ex);
} finally {
if(iter!=null)
//make sure we always close
fColl.close(iter);
if(iter!=null) {
//make sure we always close
iter.close();
}

This comment has been minimized.

Copy link
@jodygarnett

jodygarnett Nov 7, 2012

Member

There is a DataUtilities.close( iterator ) that aaime requested, it does allow you to avoid null checks

}
}

Expand Down
Expand Up @@ -13,6 +13,7 @@
import org.geotools.data.DataUtilities;
import org.geotools.data.store.DataFeatureCollection;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureIterator;
import org.geotools.geometry.jts.ReferencedEnvelope;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.filter.identity.FeatureId;
Expand Down Expand Up @@ -64,7 +65,7 @@ public int getCount() throws IOException {

class CompositeIterator implements Iterator {
int index;
Iterator iterator;
FeatureIterator iterator;

public CompositeIterator() {
index = 0;
Expand All @@ -83,11 +84,11 @@ public boolean hasNext() {
while (index < collections.size()) {
//close current before we move to next
if (iterator != null) {
((FeatureCollection) collections.get(index - 1)).close(iterator);
iterator.close();
}

//grap next
iterator = ((FeatureCollection) collections.get(index++)).iterator();
iterator = ((FeatureCollection) collections.get(index++)).features();

if (iterator.hasNext()) {
return true;
Expand All @@ -97,7 +98,7 @@ public boolean hasNext() {
//no more
if (iterator != null) {
//close the last iterator
((FeatureCollection) collections.get(collections.size() - 1)).close(iterator);
iterator.close();
}

return false;
Expand All @@ -108,43 +109,17 @@ public Object next() {
}
}

public boolean addAll(Collection arg0) {
throw new RuntimeException("Can't add to a composite featurecollection; you need to add to one of the constituent collections direclty.");
}

public boolean removeAll(Collection arg0) {
Iterator it = collections.iterator();
boolean result = false;
while (it.hasNext()){
FeatureCollection col = (FeatureCollection)it.next();
result |= col.removeAll(arg0);
}
return result;
}

public boolean retainAll(Collection arg0) {
boolean result = false;

Iterator it = collections.iterator();
while (it.hasNext()){
FeatureCollection col = (FeatureCollection)it.next();
result |= col.removeAll(arg0);
}

return result;
}

public Object[] toArray(Object[] arg0) {
List list = new ArrayList();

Iterator it = collections.iterator();
while(it.hasNext()){
FeatureCollection col = (FeatureCollection)it.next();
Iterator it2 = col.iterator();
FeatureIterator it2 = col.features();
while (it2.hasNext()){

This comment has been minimized.

Copy link
@jodygarnett

jodygarnett Nov 7, 2012

Member

I have been doing the try / finally thing to ensure iterator is closed

list.add(it.next());
}
col.close(it2);
it2.close();
}

return list.toArray(arg0);
Expand Down
Expand Up @@ -120,7 +120,7 @@ public void accepts(FeatureVisitor visitor, ProgressListener progress) {
visitor.visit(it.next());
}
} finally {
close(it);
it.close();
}
}

Expand All @@ -132,24 +132,6 @@ public SimpleFeatureIterator features() {
return new ReprojectingFeatureIterator(delegate.features());
}

public Iterator iterator() {
return new ReprojectingIterator(delegate.iterator());
}

public void close(SimpleFeatureIterator iterator) {
if (iterator instanceof ReprojectingFeatureIterator) {
delegate.close(((ReprojectingFeatureIterator) iterator).getDelegate());
}

iterator.close();
}

public void close(Iterator iterator) {
if (iterator instanceof ReprojectingIterator) {
delegate.close(((ReprojectingIterator) iterator).getDelegate());
}
}

public SimpleFeatureType getFeatureType() {
return schema;
}
Expand Down Expand Up @@ -218,7 +200,7 @@ public Object[] toArray(Object[] a) {

public ReferencedEnvelope getBounds() {
ReferencedEnvelope bounds = null;
Iterator i = iterator();
SimpleFeatureIterator i = features();

try {
if (!i.hasNext()) {
Expand All @@ -237,7 +219,7 @@ public ReferencedEnvelope getBounds() {

return bounds;
} finally {
close(i);
i.close();
}
}

Expand Down Expand Up @@ -339,6 +321,7 @@ public SimpleFeature next() throws NoSuchElementException {
}

public void close() {
if (delegate != null) delegate.close();
delegate = null;
}
}
Expand Down
Expand Up @@ -12,6 +12,7 @@
import org.geotools.data.FeatureWriter;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.feature.FeatureIterator;
import org.geotools.feature.IllegalAttributeException;
import org.geotools.feature.collection.DelegateSimpleFeatureIterator;
import org.geotools.feature.simple.SimpleFeatureBuilder;
Expand Down Expand Up @@ -40,22 +41,8 @@ public SimpleFeatureType getSchema() {
return target;
}

public Iterator<SimpleFeature> iterator() {
return new RetypingIterator(delegate.iterator(), target);
}

public void close(Iterator<SimpleFeature> iterator) {
RetypingIterator retyping = (RetypingIterator) iterator;
delegate.close(retyping.delegate);
}

public SimpleFeatureIterator features() {
return new DelegateSimpleFeatureIterator(this, iterator());
}

public void close(SimpleFeatureIterator iterator) {
DelegateSimpleFeatureIterator delegate = (DelegateSimpleFeatureIterator) iterator;
delegate.close();
return new RetypingIterator(delegate.features(), target);
}

static SimpleFeature retype(SimpleFeature source, SimpleFeatureBuilder builder)
Expand Down Expand Up @@ -102,11 +89,11 @@ public static FeatureId reTypeId(FeatureId sourceId, SimpleFeatureType original,
return sourceId;
}

public static class RetypingIterator implements Iterator<SimpleFeature> {
public static class RetypingIterator implements SimpleFeatureIterator {
SimpleFeatureBuilder builder;
Iterator<SimpleFeature> delegate;
SimpleFeatureIterator delegate;

public RetypingIterator(Iterator<SimpleFeature> delegate, SimpleFeatureType target) {
public RetypingIterator(SimpleFeatureIterator delegate, SimpleFeatureType target) {
this.delegate = delegate;
this.builder = new SimpleFeatureBuilder(target);
}
Expand All @@ -123,8 +110,9 @@ public SimpleFeature next() {
}
}

public void remove() {
delegate.remove();
@Override
public void close() {
delegate.close();
}
}

Expand Down
Expand Up @@ -43,11 +43,6 @@ public SimpleFeatureIterator features() {
return new CheckAttributesFeatureIterator(delegate.features(), writableAttributes);
}

@Override
public Iterator iterator() {
return new FeatureIteratorIterator<SimpleFeature>(features());
}

public class CheckAttributesFeatureIterator implements SimpleFeatureIterator {

SimpleFeatureIterator delegate;
Expand Down
Expand Up @@ -49,7 +49,6 @@ public boolean canSecure(Class clazz) {
|| FeatureStore.class.isAssignableFrom(clazz)
|| FeatureLocking.class.isAssignableFrom(clazz)
|| FeatureCollection.class.isAssignableFrom(clazz)
|| Iterator.class.isAssignableFrom(clazz)
|| FeatureIterator.class.isAssignableFrom(clazz)
|| AbstractGridCoverage2DReader.class.isAssignableFrom(clazz)
|| AbstractGridFormat.class.isAssignableFrom(clazz)
Expand Down Expand Up @@ -109,8 +108,6 @@ public Object secure(Object object, WrapperPolicy policy) {
return new SecuredSimpleFeatureCollection((SimpleFeatureCollection) object, policy);
} else if (FeatureCollection.class.isAssignableFrom(clazz)) {
return new SecuredFeatureCollection((FeatureCollection) object, policy);
} else if (Iterator.class.isAssignableFrom(clazz)) {
return new SecuredIterator((Iterator) object, policy);
} else if (SimpleFeatureIterator.class.isAssignableFrom(clazz)) {
return new SecuredSimpleFeatureIterator((SimpleFeatureIterator) object);
} else if (FeatureIterator.class.isAssignableFrom(clazz)) {
Expand Down

0 comments on commit bd8840d

Please sign in to comment.