use of org.geotoolkit.storage.feature.FeatureCollection in project geotoolkit by Geomatys.
the class KrigingProcess method execute.
@Override
protected void execute() throws ProcessException {
final CoordinateReferenceSystem crs = inputParameters.getValue(IN_CRS);
double step = inputParameters.getValue(IN_STEP);
final DirectPosition[] coords = inputParameters.getValue(IN_POINTS);
final Dimension maxDim = inputParameters.getValue(IN_DIMENSION);
// calculate the envelope
double minx = Double.POSITIVE_INFINITY;
double miny = Double.POSITIVE_INFINITY;
double minz = Double.POSITIVE_INFINITY;
double maxx = Double.NEGATIVE_INFINITY;
double maxy = Double.NEGATIVE_INFINITY;
double maxz = Double.NEGATIVE_INFINITY;
// organise values in a table
final int s = coords.length;
final double[] x = new double[s];
final double[] y = new double[s];
final double[] z = new double[s];
for (int i = 0; i < s; i++) {
final double cx = coords[i].getOrdinate(0);
final double cy = coords[i].getOrdinate(1);
final double cz = coords[i].getOrdinate(2);
x[i] = cx;
y[i] = cy;
z[i] = cz;
if (cx < minx)
minx = cx;
if (cx > maxx)
maxx = cx;
if (cy < miny)
miny = cy;
if (cy > maxy)
maxy = cy;
if (cz < minz)
minz = cz;
if (cz > maxz)
maxz = cz;
}
final Rectangle2D rect = new Rectangle2D.Double(minx, miny, maxx - minx, maxy - miny);
final Dimension dim = new Dimension(s * s, s * s);
// limit size to 200
if (maxDim != null) {
if (dim.height > maxDim.height)
dim.height = maxDim.height;
if (dim.width > maxDim.width)
dim.width = maxDim.width;
}
// final ObjectiveAnalysis ob = new ObjectiveAnalysis(rect, dim);
final BDHObjectiveAnalysis ob = new BDHObjectiveAnalysis(rect, dim);
if (crs instanceof ProjectedCRS) {
// The default ObjectiveAnalysis algorithm is designed for GeographicCRS.
// In case of ProjectedCRS, we need to apply a scale factor that convert
// metres to some approximation of angles of longitude/latitude.
// Use standard length of nautical mile.
ob.setScaleFactor(1. / (60 * 1852));
}
// double[] computed;
// try {
// // computed = ob.interpole(x, y, z);
// // computed = ob.interpolate(null);
// computed = ob.interpolate((double[])null);
// } catch (Exception ex) {
// throw new ProcessException(null, this, ex);
// }
// final double[] cx = ob.getXs();
// final double[] cy = ob.getYs();
ob.setInputs(x, y, z);
RenderedImage renderedImage = ob.createImage();
final int outLength = ob.getOutputLength();
final int rIWidth = renderedImage.getWidth();
final int rIHeight = renderedImage.getHeight();
final double[] cx = new double[rIWidth];
final double[] cy = new double[rIHeight];
final double[] cz = new double[outLength];
final PixelIterator it = new PixelIterator.Builder().setIteratorOrder(SequenceType.LINEAR).create(renderedImage);
int comp = 0;
while (it.next()) {
cz[comp++] = it.getSampleDouble(0);
}
final double x0 = Math.min(cx[0], cx[cx.length - 1]);
final double y0 = Math.min(cy[0], cy[cy.length - 1]);
final double spanX = Math.abs((x[x.length - 1] - x[0]) / rIWidth);
final double spanY = Math.abs((y[y.length - 1] - y[0]) / rIHeight);
for (int i = 0; i < rIWidth; i++) {
cx[i] = x0 + spanX * i;
}
for (int i = 0; i < rIHeight; i++) {
cy[i] = y0 + spanY * i;
}
// create the coverage //////////////////////////////////////////////////
// final GeneralEnvelope env = new GeneralEnvelope(
// new Rectangle2D.Double(x[0], y[0], x[x.length-1]-x[0], y[y.length-1]-y[0]));
// env.setCoordinateReferenceSystem(crs);
// final GeneralEnvelope env = new GeneralEnvelope(
// new Rectangle2D.Double(renderedImage.getMinX(), renderedImage.getMinY(), renderedImage.getWidth(), renderedImage.getHeight()));
// env.setCoordinateReferenceSystem(crs);
final GeneralEnvelope env = new GeneralEnvelope(new double[] { x0, y0 }, new double[] { x0 + spanX, y0 + spanY });
env.setCoordinateReferenceSystem(crs);
final GridCoverage coverage = toCoverage(cz, cx, cy, env);
outputParameters.getOrCreate(OUT_COVERAGE).setValue(coverage);
// test
renderedImage = coverage.render(null);
// create the isolines //////////////////////////////////////////////////
if (step <= 0) {
// do not generate isolines
return;
}
step = 2.0;
final double[] palier = new double[(int) ((maxz - minz) / step)];
for (int i = 0; i < palier.length; i++) {
palier[i] = minz + i * step;
}
final GeometryFactory GF = org.geotoolkit.geometry.jts.JTS.getFactory();
final FeatureTypeBuilder ftb = new FeatureTypeBuilder();
ftb.setName("isoline");
ftb.addAttribute(LineString.class).setName("geometry").setCRS(crs).addRole(AttributeRole.DEFAULT_GEOMETRY);
ftb.addAttribute(Double.class).setName("value");
final FeatureType type = ftb.build();
final FeatureCollection col = FeatureStoreUtilities.collection("id", type);
int inc = 0;
final PixelIterator rit = PixelIterator.create(renderedImage);
for (int i = 0; i < palier.length; i++) {
final MultiLineString geometry = MarchingSquares.build(rit, palier[i], 0, true);
// if (cshps.get(0).x > cshps.get(cshps.size()-1).x) {
// // the coordinates are going left, reverse order
// Collections.reverse(cshps);
// }
final double value = palier[i];
final Feature f = type.newInstance();
f.setPropertyValue(AttributeConvention.IDENTIFIER, String.valueOf(inc++));
f.setPropertyValue("geometry", geometry);
f.setPropertyValue("value", value);
col.add(f);
}
// /////////////// debug///////////////////////
// FeatureIterator featIter = col.iterator();
//
// final List<Shape> shapes = new ArrayList<>();
//
//
// while (featIter.hasNext()) {
// Feature feaTemp = featIter.next();
// LineString lineS = (LineString)feaTemp.getProperty("geometry").getValue();
// Coordinate[] coordst = lineS.getCoordinates();
// GeneralPath isoline = null;
// for(final Coordinate coord : coordst) {
// if(isoline == null){
// isoline = new GeneralPath(GeneralPath.WIND_EVEN_ODD);
// isoline.moveTo(coord.x, coord.y);
// }else{
// isoline.lineTo(coord.x, coord.y);
// }
// }
// shapes.add(isoline);
// }
//
// final JFrame frm = new JFrame();
// JPanel jp = new JPanel(){
// @Override
// protected void paintComponent(Graphics g) {
// super.paintComponent(g);
//
// Graphics2D g2 = (Graphics2D) g;
// g2.setTransform(new AffineTransform2D(1, 0, 0, 1, this.getWidth()/2.0, this.getHeight()/2.0));
// // g2.drawRenderedImage(renderImage, new AffineTransform2D(1, 0, 0, 1, 0,0));
// g2.setColor(Color.BLACK);
// for(Shape shape : shapes){
// g2.draw(shape);
// }
// }
// };
// ////////////////////////////////////FIN ISOLINE////////////////////////////////
//
// frm.setTitle("isoline");
// frm.setSize(1200, 1200);
// frm.setLocationRelativeTo(null);
// frm.add(jp);
// frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// frm.setVisible(true);
// /////////////////////////////////////////////
outputParameters.getOrCreate(OUT_LINES).setValue(col);
}
use of org.geotoolkit.storage.feature.FeatureCollection in project geotoolkit by Geomatys.
the class FeatureStreamsTest method testStartIndexIterator.
@Test
public void testStartIndexIterator() {
FeatureCollection collection = buildSimpleFeatureCollection();
FeatureIterator ite = FeatureStreams.skip(collection.iterator(), 0);
assertEquals(3, FeatureStoreUtilities.calculateCount(ite));
ite = FeatureStreams.skip(collection.iterator(), 1);
assertEquals(2, FeatureStoreUtilities.calculateCount(ite));
ite = FeatureStreams.skip(collection.iterator(), 2);
assertTrue(ite.next() != null);
try {
ite.next();
fail("Should have raise a no such element exception.");
} catch (NoSuchElementException ex) {
// ok
}
// check has next do not iterate
ite = FeatureStreams.skip(collection.iterator(), 1);
testIterationOnNext(ite, 2);
// check sub iterator is properly closed
CheckCloseFeatureIterator checkIte = new CheckCloseFeatureIterator(collection.iterator());
assertFalse(checkIte.isClosed());
ite = FeatureStreams.skip(checkIte, 1);
while (ite.hasNext()) ite.next();
ite.close();
assertTrue(checkIte.isClosed());
}
use of org.geotoolkit.storage.feature.FeatureCollection in project geotoolkit by Geomatys.
the class FeatureStreamsTest method testFilterIterator.
@Test
public void testFilterIterator() {
FeatureCollection collection = buildSimpleFeatureCollection();
FeatureIterator ite = FeatureStreams.filter(collection.iterator(), Filter.include());
assertEquals(3, FeatureStoreUtilities.calculateCount(ite));
ite = FeatureStreams.filter(collection.iterator(), Filter.exclude());
assertEquals(0, FeatureStoreUtilities.calculateCount(ite));
ite = FeatureStreams.filter(collection.iterator(), FF.equal(FF.literal("aaa"), FF.property("att_string")));
assertEquals(1, FeatureStoreUtilities.calculateCount(ite));
ite = FeatureStreams.filter(collection.iterator(), FF.equal(FF.literal("aaa"), FF.property("att_string")));
assertEquals(id3, ite.next().getPropertyValue(AttributeConvention.IDENTIFIER));
try {
ite.next();
fail("Should have raise a no such element exception.");
} catch (NoSuchElementException ex) {
// ok
}
// check has next do not iterate
ite = FeatureStreams.filter(collection.iterator(), Filter.include());
testIterationOnNext(ite, 3);
// check sub iterator is properly closed
CheckCloseFeatureIterator checkIte = new CheckCloseFeatureIterator(collection.iterator());
assertFalse(checkIte.isClosed());
ite = FeatureStreams.filter(checkIte, Filter.include());
while (ite.hasNext()) ite.next();
ite.close();
assertTrue(checkIte.isClosed());
}
use of org.geotoolkit.storage.feature.FeatureCollection in project geotoolkit by Geomatys.
the class FeatureStreamsTest method testEmptyReader.
@Test
public void testEmptyReader() {
FeatureCollection collection = buildSimpleFeatureCollection();
final FeatureReader iterator = FeatureStreams.emptyReader(collection.getType());
assertEquals(iterator.getFeatureType(), collection.getType());
assertFalse(iterator.hasNext());
try {
iterator.next();
fail("Next on empty iterator should have raised a no such element exception.");
} catch (NoSuchElementException ex) {
// ok
}
try {
iterator.remove();
fail("Remove should have raise an error.");
} catch (Exception ex) {
// ok
}
iterator.close();
}
use of org.geotoolkit.storage.feature.FeatureCollection in project geotoolkit by Geomatys.
the class FeatureStreamsTest method testCacheIterator.
@Test
@Ignore("See #GEOTK-489")
public void testCacheIterator() {
FeatureCollection collection = buildSimpleFeatureCollection();
FeatureIterator ite = FeatureStreams.cached(collection.iterator(), 1);
assertEquals(3, FeatureStoreUtilities.calculateCount(ite));
ite = FeatureStreams.cached(collection.iterator(), 1);
int mask = 0;
Feature f;
while (ite.hasNext()) {
f = ite.next();
final Object id = f.getPropertyValue(AttributeConvention.IDENTIFIER);
if (id1.equals(id)) {
mask |= 1 << 0;
} else if (id2.equals(id)) {
mask |= 1 << 1;
} else if (id3.equals(id)) {
mask |= 1 << 2;
}
}
ite.close();
if (mask != 7) {
fail("missing features in iterations");
}
}
Aggregations