Skip to content

Commit

Permalink
Fix pregeneralized
Browse files Browse the repository at this point in the history
  • Loading branch information
jodygarnett committed Oct 31, 2012
1 parent 43f4005 commit c8cfb08
Show file tree
Hide file tree
Showing 6 changed files with 274 additions and 420 deletions.
Expand Up @@ -62,9 +62,7 @@ public class PreGeneralizedFeatureCollection implements
protected String geomPropertyName, backendGeomPropertyName;

protected int[] indexMapping;

List<CollectionListener> listeners = new ArrayList<CollectionListener>();


public PreGeneralizedFeatureCollection(
SimpleFeatureCollection backendCollection,
SimpleFeatureType featureType, int[] indexMapping, String geomPropertyName,
Expand All @@ -77,26 +75,22 @@ public PreGeneralizedFeatureCollection(
this.indexMapping = indexMapping;
}

protected UnsupportedOperationException unsupported() {
return new UnsupportedOperationException(
"Cannot modify a pregeneralized feature collection");
}

/*
* (non-Javadoc)
*
* @see org.geotools.feature.FeatureCollection#accepts(org.opengis.feature.FeatureVisitor,
* org.opengis.util.ProgressListener) Logic copied from DefaultFeatureCollection class
*/
public void accepts(FeatureVisitor visitor, ProgressListener progress) throws IOException {
Iterator iterator = null;
SimpleFeatureIterator iterator = null;
if (progress == null)
progress = new NullProgressListener();
try {
float size = size();
float position = 0;
progress.started();
for (iterator = iterator(); !progress.isCanceled() && iterator.hasNext(); progress

for (iterator = features(); !progress.isCanceled() && iterator.hasNext(); progress
.progress(position++ / size)) {
try {
SimpleFeature feature = (SimpleFeature) iterator.next();
Expand All @@ -107,60 +101,10 @@ public void accepts(FeatureVisitor visitor, ProgressListener progress) throws IO
}
} finally {
progress.complete();
close(iterator);
iterator.close();
}
}

public boolean add(SimpleFeature arg0) {
throw unsupported();

}

public boolean addAll(Collection arg0) {
throw unsupported();

}

public boolean addAll(FeatureCollection arg0) {
throw unsupported();

}

public void addListener(CollectionListener listener) throws NullPointerException {
listeners.add(listener);

}

public void clear() {
throw unsupported();
}

public void close(FeatureIterator<SimpleFeature> it) {
it.close();

}


public void close(Iterator it) {
if (it==null) return;

try { // check for a possible close method
Method closeMethod = it.getClass().getMethod("close", new Class[]{});

if (closeMethod!=null) {
int mod = closeMethod.getModifiers();
if (Modifier.isPublic(mod) && (Modifier.isStatic(mod)==false))
closeMethod.invoke(it, new Object[]{});
}
} catch (SecurityException e) {
return;
} catch (NoSuchMethodException e) {
return;
} catch (Exception e) {
throw new RuntimeException("Error calling close method for "+it.getClass().getName());
}
}

public boolean contains(Object feature) {
if (feature instanceof PreGeneralizedSimpleFeature)
return backendCollection.contains(((PreGeneralizedSimpleFeature) feature).feature);
Expand Down Expand Up @@ -205,55 +149,6 @@ public boolean isEmpty() {

}

public Iterator<SimpleFeature> iterator() {
final Iterator<SimpleFeature> iterator = backendCollection.iterator();

return new Iterator<SimpleFeature>() {

public boolean hasNext() {
return iterator.hasNext();
}

public SimpleFeature next() {
SimpleFeature feature = (SimpleFeature) iterator.next();
return new PreGeneralizedSimpleFeature(featureType, indexMapping, feature,
geomPropertyName, backendGeomPropertyName);
}

public void remove() {
throw unsupported();
}
public void close() {
backendCollection.close(iterator);
}
};
}

public void purge() {
throw unsupported();

}

public boolean remove(Object arg0) {
throw unsupported();

}

public boolean removeAll(Collection arg0) {
throw unsupported();

}

public void removeListener(CollectionListener listener) throws NullPointerException {
listeners.remove(listener);

}

public boolean retainAll(Collection arg0) {
throw unsupported();

}

public int size() {
return backendCollection.size();
}
Expand Down
Expand Up @@ -162,33 +162,37 @@ protected void generalizeShapeFile(File shapeFile, DataStore shapeDS, File targe

SimpleFeatureCollection fcoll = fs.getFeatures();
SimpleFeatureIterator it = fcoll.features();
int countTotal = fcoll.size();

List<FeatureWriter<SimpleFeatureType, SimpleFeature>> writers = new ArrayList<FeatureWriter<SimpleFeatureType, SimpleFeature>>();
for (int i = 0; i < dataStores.length; i++) {
writers.add(dataStores[i].getFeatureWriter(typeName, Transaction.AUTO_COMMIT));
}

int counter = 0;
while (it.hasNext()) {
SimpleFeature feature = it.next();
for (int i = 0; i < distanceArray.length; i++) {
FeatureWriter<SimpleFeatureType, SimpleFeature> w = writers.get(i);
SimpleFeature genFeature = w.next();
genFeature.setAttributes(feature.getAttributes());
Geometry newGeom = TopologyPreservingSimplifier.simplify((Geometry) feature
.getDefaultGeometry(), distanceArray[i]);
genFeature.setDefaultGeometry(newGeom);
w.write();
try {
int countTotal = fcoll.size();

List<FeatureWriter<SimpleFeatureType, SimpleFeature>> writers = new ArrayList<FeatureWriter<SimpleFeatureType, SimpleFeature>>();
for (int i = 0; i < dataStores.length; i++) {
writers.add(dataStores[i].getFeatureWriter(typeName, Transaction.AUTO_COMMIT));
}

int counter = 0;
while (it.hasNext()) {
SimpleFeature feature = it.next();
for (int i = 0; i < distanceArray.length; i++) {
FeatureWriter<SimpleFeatureType, SimpleFeature> w = writers.get(i);
SimpleFeature genFeature = w.next();
genFeature.setAttributes(feature.getAttributes());
Geometry newGeom = TopologyPreservingSimplifier.simplify((Geometry) feature
.getDefaultGeometry(), distanceArray[i]);
genFeature.setDefaultGeometry(newGeom);
w.write();
}
counter++;
showProgress(countTotal, counter);

}
for (FeatureWriter<SimpleFeatureType, SimpleFeature> w : writers){
w.close();
}
counter++;
showProgress(countTotal, counter);

}
fcoll.close(it);

for (FeatureWriter<SimpleFeatureType, SimpleFeature> w : writers)
w.close();
finally {
it.close();
}

for (DataStore ds : dataStores) {
ds.dispose();
Expand Down
Expand Up @@ -321,11 +321,15 @@ protected void testGetFeatures(String configName) {
assertTrue("the_geom".equals(fCollection.getSchema().getGeometryDescriptor()
.getLocalName()));
SimpleFeatureIterator iterator = fCollection.features();
while (iterator.hasNext()) {
SimpleFeature f = iterator.next();
checkPoints(f, 0.0);
try {
while (iterator.hasNext()) {
SimpleFeature f = iterator.next();
checkPoints(f, 0.0);
}
}
finally {
iterator.close();
}
fCollection.close(iterator);

q.getHints().put(Hints.GEOMETRY_DISTANCE, 5.0);
fCollection = fs.getFeatures(q);
Expand All @@ -334,11 +338,15 @@ protected void testGetFeatures(String configName) {
assertTrue("the_geom".equals(fCollection.getSchema().getGeometryDescriptor()
.getLocalName()));
iterator = fCollection.features();
while (iterator.hasNext()) {
SimpleFeature f = iterator.next();
checkPoints(f, 5.0);
try {
while (iterator.hasNext()) {
SimpleFeature f = iterator.next();
checkPoints(f, 5.0);
}
}
finally {
iterator.close();
}
fCollection.close(iterator);

q.getHints().put(Hints.GEOMETRY_DISTANCE, 10.0);
fCollection = fs.getFeatures(q);
Expand All @@ -347,11 +355,15 @@ protected void testGetFeatures(String configName) {
assertTrue("the_geom".equals(fCollection.getSchema().getGeometryDescriptor()
.getLocalName()));
iterator = fCollection.features();
while (iterator.hasNext()) {
SimpleFeature f = iterator.next();
checkPoints(f, 10.0);
try {
while (iterator.hasNext()) {
SimpleFeature f = iterator.next();
checkPoints(f, 10.0);
}
}
finally {
iterator.close();
}
fCollection.close(iterator);

q.getHints().put(Hints.GEOMETRY_DISTANCE, 20.0);
fCollection = fs.getFeatures(q);
Expand All @@ -360,25 +372,31 @@ protected void testGetFeatures(String configName) {
assertTrue("the_geom".equals(fCollection.getSchema().getGeometryDescriptor()
.getLocalName()));
iterator = fCollection.features();
while (iterator.hasNext()) {
SimpleFeature f = iterator.next();
checkPoints(f, 20.0);
try {
while (iterator.hasNext()) {
SimpleFeature f = iterator.next();
checkPoints(f, 20.0);
}
}
finally {
iterator.close();
}
fCollection.close(iterator);

q.getHints().put(Hints.GEOMETRY_DISTANCE, 50.0);
fCollection = fs.getFeatures(q);
typeName = fCollection.getSchema().getTypeName();
assertTrue("GenStreams".equals(typeName));
assertTrue("the_geom".equals(fCollection.getSchema().getGeometryDescriptor()
.getLocalName()));
iterator = fCollection.features();
while (iterator.hasNext()) {
SimpleFeature f = iterator.next();
checkPoints(f, 0.0);
try {
while (iterator.hasNext()) {
SimpleFeature f = iterator.next();
checkPoints(f, 0.0);
}
}
finally {
iterator.close();
}
fCollection.close(iterator);

}
} catch (Exception ex) {
Assert.fail(ex.getMessage());
Expand Down Expand Up @@ -411,32 +429,33 @@ protected void testGetFeatures2(String configName) {
assertTrue("GenStreams".equals(typeName));
assertTrue(fCollection.size() > 0);
assertFalse(fCollection.isEmpty());
Iterator<SimpleFeature> iterator = fCollection.iterator();
while (iterator.hasNext()) {
SimpleFeature f = iterator.next();
checkPoints(f, 0.0);
SimpleFeatureIterator iterator = fCollection.features();
try {
while (iterator.hasNext()) {
SimpleFeature f = iterator.next();
checkPoints(f, 0.0);
}
}
finally {
iterator.close();
}
fCollection.close(iterator);

fCollection = fs.getFeatures(Filter.INCLUDE);
typeName = fCollection.getSchema().getTypeName();
assertTrue("GenStreams".equals(typeName));
assertTrue(fCollection.size() > 0);
assertFalse(fCollection.isEmpty());
iterator = fCollection.iterator();
while (iterator.hasNext()) {
SimpleFeature f = iterator.next();
checkPoints(f, 0.0);
}
boolean error = true;
iterator = fCollection.features();
try {
iterator.remove();
} catch (UnsupportedOperationException e) {
error = false;
while (iterator.hasNext()) {

SimpleFeature f = iterator.next();
checkPoints(f, 0.0);
}
// iterator.remove() no longer provided
}
finally {
iterator.close();
}
assertFalse(error);
fCollection.close(iterator);

ds.dispose();

} catch (Exception ex) {
Expand Down

0 comments on commit c8cfb08

Please sign in to comment.