Permalink
Browse files

Streaming feature copy.

  • Loading branch information...
1 parent 41ebc35 commit 3406dd3b9b071c45124d2d4a4b096e8d939ede4c @tschaub committed Feb 1, 2012
Showing with 24 additions and 16 deletions.
  1. +24 −16 lib/geoscript/workspace/workspace.js
@@ -7,8 +7,8 @@ var Projection = require("../proj").Projection;
var geotools = Packages.org.geotools;
var DefaultQuery = geotools.data.DefaultQuery;
-var ListFeatureCollection = geotools.data.collection.ListFeatureCollection;
var Transaction = geotools.data.Transaction;
+var DefaultTransaction = geotools.data.DefaultTransaction;
/** api: (define)
* module = workspace
@@ -157,23 +157,31 @@ var Workspace = UTIL.extend(GeoObject, {
query.setCoordinateSystemReproject(projection._projection);
}
- // loop through batches of features and add to the new layer
- var iterator = layer._source.dataStore.getFeatureReader(query, Transaction.AUTO_COMMIT);
- var i, _features;
- var _schema = layer._source.getSchema();
- while (true) {
- i = 0;
- _features = new ListFeatureCollection(_schema);
- while (iterator.hasNext() && i < 1000) {
- _features.add(iterator.next());
- ++i;
- }
- if (_features.isEmpty()) {
- break;
+ var reader = layer._source.getDataStore().getFeatureReader(query, Transaction.AUTO_COMMIT);
+ var transaction = new DefaultTransaction();
+ var writer = _source.getDataStore().getFeatureWriterAppend(schema._schema.getName(), transaction);
+ var inFeature, outFeature, geom;
+
+ try {
+ while (reader.hasNext()) {
+ inFeature = reader.next();
+ outFeature = writer.next();
+ outFeature.setAttributes(inFeature.getAttributes());
+
+ // mask empty geometry or PostGIS will complain
+ geom = outFeature.getDefaultGeometry();
+ if (geom != null && geom.isEmpty()) {
+ outFeature.setDefaultGeometry(null);
+ }
+
+ writer.write();
}
- _source.addFeatures(_features);
+ transaction.commit();
+ } finally {
+ // TODO deal with failures closing and before
+ transaction.close();
}
- iterator.close();
+
return Layer.from_(_source, this);
},

0 comments on commit 3406dd3

Please sign in to comment.