Hi Justin!
Here is what I have so far:
I am no expert, but I am trying to do the following:
1. Write features in batches. The batch size is configurable but defaults to 1000.
2. Use a transaction but by default use the best kind of transaction. For shapefiles this means a null transaction, for Property and Memory layers an auto_commit transaction, and for everything else a DefaultTransaction.
API wise, here is a basic example:
def writer = new Writer(layer, batch: 500)
try {
pts.eachWithIndex{Point pt, int i ->
Feature f = writer.newFeature
f.geom = pt
f['id'] = i
writer.add(f)
}
} finally {
writer.close()
}
Basically, you create a geoscript.layer.Writer with the Layer you want to add Features to. Then you start addings Features. If you add more Features than the batch size, the Features are commited in a single Transaction. You always must close the Writer at the end which will commit any remaining Features.
I was also thinkging about adding a getWriter() and withWriter(Closure c) methods to the Layer class. The withWriter method is nice because you don't have wrap your code in a try catch block, the withWriter method does it for you. Here is an example:
layer.withWriter { Writer w ->
pts.eachWithIndex{Point pt, int i ->
Feature f = writer.newFeature
f.geom = pt
f['id'] = i
writer.add(f)
}
}
and here is the implementation:
void withWriter(Map options = [:], Closure c) {
Writer w = new Writer(options, this)
try {
c.call(w)
} finally {
w.close()
}
}
Closures are one of my favorite Groovy features. I hope this helps.
Thanks,
Jared