use of org.opengis.feature.FeatureType 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.opengis.feature.FeatureType in project geotoolkit by Geomatys.
the class GridCoverageFeatureSetTest method coverageRecordRGBTest.
/**
* Test coverage rgb mapped as a feature.
*
* @throws DataStoreException
*/
@Test
public void coverageRecordRGBTest() throws DataStoreException {
// create coverage
final BufferedImage image = new BufferedImage(2, 2, BufferedImage.TYPE_INT_ARGB);
image.setRGB(0, 0, Color.BLUE.getRGB());
image.setRGB(1, 0, Color.RED.getRGB());
image.setRGB(0, 1, Color.GREEN.getRGB());
image.setRGB(1, 1, Color.PINK.getRGB());
final GridCoverageBuilder gcb = new GridCoverageBuilder();
gcb.setValues(image);
gcb.setDomain(new GridGeometry(null, PixelInCell.CELL_CENTER, new AffineTransform2D(2, 0, 0, 2, 31, 11), CommonCRS.WGS84.normalizedGeographic()));
final GridCoverage coverage = gcb.build();
// test mapped feature type
final FeatureType coverageType = GridCoverageFeatureSet.createCoverageType(coverage);
final FeatureAssociationRole role = (FeatureAssociationRole) coverageType.getProperty(TypeConventions.RANGE_ELEMENTS_PROPERTY.toString());
final FeatureType recordType = role.getValueType();
assertEquals("Coverage", coverageType.getName().toString());
assertTrue(TypeConventions.COVERAGE_TYPE.isAssignableFrom(coverageType));
assertEquals("Record", recordType.getName().tip().toString());
assertTrue(TypeConventions.COVERAGE_RECORD_TYPE.isAssignableFrom(recordType));
// convert coverage to feature
final Feature feature = coverageType.newInstance();
feature.setProperty(GridCoverageFeatureSet.coverageRecords(coverage, role));
// check records
final Collection col = (Collection) feature.getPropertyValue(TypeConventions.RANGE_ELEMENTS_PROPERTY.toString());
assertEquals(4, col.size());
final Iterator<Feature> ite = col.iterator();
final Feature r1 = ite.next();
final Feature r2 = ite.next();
final Feature r3 = ite.next();
final Feature r4 = ite.next();
assertFalse(ite.hasNext());
assertEquals(0.0, r1.getPropertyValue("Red"));
assertEquals(0.0, r1.getPropertyValue("Green"));
assertEquals(255.0, r1.getPropertyValue("Blue"));
assertEquals(255.0, r1.getPropertyValue("Transparency"));
assertEquals(Color.BLUE, r1.getPropertyValue("color"));
assertEquals(255.0, r2.getPropertyValue("Red"));
assertEquals(0.0, r2.getPropertyValue("Green"));
assertEquals(0.0, r2.getPropertyValue("Blue"));
assertEquals(255.0, r2.getPropertyValue("Transparency"));
assertEquals(Color.RED, r2.getPropertyValue("color"));
assertEquals(0.0, r3.getPropertyValue("Red"));
assertEquals(255.0, r3.getPropertyValue("Green"));
assertEquals(0.0, r3.getPropertyValue("Blue"));
assertEquals(255.0, r3.getPropertyValue("Transparency"));
assertEquals(Color.GREEN, r3.getPropertyValue("color"));
assertEquals(255.0, r4.getPropertyValue("Red"));
assertEquals(175.0, r4.getPropertyValue("Green"));
assertEquals(175.0, r4.getPropertyValue("Blue"));
assertEquals(255.0, r4.getPropertyValue("Transparency"));
assertEquals(Color.PINK, r4.getPropertyValue("color"));
}
use of org.opengis.feature.FeatureType in project geotoolkit by Geomatys.
the class GridCoverageFeatureSetTest method coverageRecord2DTest.
/**
* Test coverage 2D mapped as a feature.
*
* @throws DataStoreException
*/
@Test
public void coverageRecord2DTest() throws DataStoreException {
// create coverage
final BufferedImage image = BufferedImages.createImage(2, 2, 2, DataBuffer.TYPE_INT);
final WritableRaster raster = image.getRaster();
raster.setPixel(0, 0, new int[] { 10, 2 });
raster.setPixel(1, 0, new int[] { 30, 4 });
raster.setPixel(0, 1, new int[] { 50, 6 });
raster.setPixel(1, 1, new int[] { 70, 8 });
SampleDimension.Builder sdb = new SampleDimension.Builder();
sdb.setName("values");
sdb.addQuantitative("valuesCat", NumberRange.create(0, true, 1000, true), (MathTransform1D) MathTransforms.linear(10, -5), null);
final SampleDimension sdim1 = sdb.build();
sdb.clear();
sdb.setName("quality");
sdb.addQuantitative("qualityCat", NumberRange.create(0, true, 100, true), (MathTransform1D) MathTransforms.linear(1, 0), null);
final SampleDimension sdim2 = sdb.build();
final GridCoverageBuilder gcb = new GridCoverageBuilder();
gcb.setValues(image);
gcb.setDomain(new GridGeometry(null, PixelInCell.CELL_CENTER, new AffineTransform2D(2, 0, 0, 2, 31, 11), CommonCRS.WGS84.normalizedGeographic()));
gcb.setRanges(sdim1, sdim2);
final GridCoverage coverage = gcb.build();
// test mapped feature type
final FeatureType coverageType = GridCoverageFeatureSet.createCoverageType(coverage);
final FeatureAssociationRole role = (FeatureAssociationRole) coverageType.getProperty(TypeConventions.RANGE_ELEMENTS_PROPERTY.toString());
final FeatureType recordType = role.getValueType();
assertEquals("Coverage", coverageType.getName().toString());
assertTrue(TypeConventions.COVERAGE_TYPE.isAssignableFrom(coverageType));
assertEquals("Record", recordType.getName().tip().toString());
assertTrue(TypeConventions.COVERAGE_RECORD_TYPE.isAssignableFrom(recordType));
// convert coverage to feature
final Feature feature = coverageType.newInstance();
feature.setProperty(GridCoverageFeatureSet.coverageRecords(coverage, role));
// check records
final Collection col = (Collection) feature.getPropertyValue(TypeConventions.RANGE_ELEMENTS_PROPERTY.toString());
assertEquals(4, col.size());
final Iterator<Feature> ite = col.iterator();
final Feature r1 = ite.next();
final Feature r2 = ite.next();
final Feature r3 = ite.next();
final Feature r4 = ite.next();
assertFalse(ite.hasNext());
assertEquals(10.0 * 10 - 5, r1.getPropertyValue("values"));
assertEquals(2.0, r1.getPropertyValue("quality"));
assertEquals(GF.createPolygon(new LiteCoordinateSequence(new double[] { 30, 10, 32, 10, 32, 12, 30, 12, 30, 10 })), r1.getProperty("geometry").getValue());
assertEquals(30.0 * 10 - 5, r2.getPropertyValue("values"));
assertEquals(4.0, r2.getPropertyValue("quality"));
assertEquals(GF.createPolygon(new LiteCoordinateSequence(new double[] { 32, 10, 34, 10, 34, 12, 32, 12, 32, 10 })), r2.getProperty("geometry").getValue());
assertEquals(50.0 * 10 - 5, r3.getPropertyValue("values"));
assertEquals(6.0, r3.getPropertyValue("quality"));
assertEquals(GF.createPolygon(new LiteCoordinateSequence(new double[] { 30, 12, 32, 12, 32, 14, 30, 14, 30, 12 })), r3.getProperty("geometry").getValue());
assertEquals(70.0 * 10 - 5, r4.getPropertyValue("values"));
assertEquals(8.0, r4.getPropertyValue("quality"));
assertEquals(GF.createPolygon(new LiteCoordinateSequence(new double[] { 32, 12, 34, 12, 34, 14, 32, 14, 32, 12 })), r4.getProperty("geometry").getValue());
}
use of org.opengis.feature.FeatureType in project geotoolkit by Geomatys.
the class AbstractReadingTests method testReader.
/**
* test feature reader.
*/
@Test
public void testReader() throws Exception {
final DataStore store = getDataStore();
final List<ExpectedResult> candidates = getReaderTests();
// need at least one type to test
assertTrue(candidates.size() > 0);
for (final ExpectedResult candidate : candidates) {
final GenericName name = candidate.name;
final Resource resource = store.findResource(name.toString());
assertTrue(resource instanceof FeatureSet);
final FeatureSet featureSet = (FeatureSet) resource;
final FeatureType type = featureSet.getType();
assertNotNull(type);
assertTrue(FeatureTypeExt.equalsIgnoreConvention(candidate.type, type));
testCounts(featureSet, candidate);
testReaders(featureSet, candidate);
testBounds(featureSet, candidate);
}
}
use of org.opengis.feature.FeatureType in project geotoolkit by Geomatys.
the class AbstractReadingTests method testReaders.
/**
* test different readers.
*/
private void testReaders(final FeatureSet featureSet, final ExpectedResult candidate) throws Exception {
final FeatureType type = featureSet.getType();
final Collection<? extends PropertyType> properties = type.getProperties(true);
try (Stream<Feature> stream = featureSet.features(true)) {
stream.forEach((Feature t) -> {
// do nothing
});
throw new Exception("Asking for a reader without any query whould raise an error.");
} catch (Exception ex) {
// ok
}
// property -------------------------------------------------------------
{
// check only id query
final FeatureQuery query = new FeatureQuery();
query.setProjection(new FeatureQuery.NamedExpression(FF.property(AttributeConvention.IDENTIFIER)));
FeatureSet subset = featureSet.subset(query);
FeatureType limited = subset.getType();
assertNotNull(limited);
assertTrue(limited.getProperties(true).size() == 1);
try (Stream<Feature> stream = subset.features(false)) {
final Iterator<Feature> ite = stream.iterator();
while (ite.hasNext()) {
final Feature f = ite.next();
assertNotNull(FeatureExt.getId(f));
}
}
for (final PropertyType desc : properties) {
if (desc instanceof Operation)
continue;
final FeatureQuery sq = new FeatureQuery();
sq.setProjection(new FeatureQuery.NamedExpression(FF.property(desc.getName().tip().toString())));
subset = featureSet.subset(sq);
limited = subset.getType();
assertNotNull(limited);
assertTrue(limited.getProperties(true).size() == 1);
assertNotNull(limited.getProperty(desc.getName().toString()));
try (Stream<Feature> stream = subset.features(false)) {
final Iterator<Feature> ite = stream.iterator();
while (ite.hasNext()) {
final Feature f = ite.next();
assertNotNull(f.getProperty(desc.getName().toString()));
}
}
}
}
// sort by --------------------------------------------------------------
for (final PropertyType desc : properties) {
if (!(desc instanceof AttributeType)) {
continue;
}
final AttributeType att = (AttributeType) desc;
if (att.getMaximumOccurs() > 1) {
// do not test sort by on multi occurence properties
continue;
}
final Class clazz = att.getValueClass();
if (!Comparable.class.isAssignableFrom(clazz) || Geometry.class.isAssignableFrom(clazz)) {
// can not make a sort by on this attribut.
continue;
}
final FeatureQuery query = new FeatureQuery();
query.setSortBy(FF.sort(FF.property(desc.getName().tip().toString()), SortOrder.ASCENDING));
FeatureSet subset = featureSet.subset(query);
// count should not change with a sort by
assertEquals(candidate.size, FeatureStoreUtilities.getCount(subset, true).intValue());
try (Stream<Feature> stream = subset.features(false)) {
final Iterator<Feature> reader = stream.iterator();
Comparable last = null;
while (reader.hasNext()) {
final Feature f = reader.next();
Object obj = f.getProperty(desc.getName().toString()).getValue();
if (obj instanceof Identifier)
obj = ((Identifier) obj).getCode();
final Comparable current = (Comparable) obj;
if (current != null) {
if (last != null) {
// check we have the correct order.
assertTrue(current.compareTo(last) >= 0);
}
last = current;
} else {
// any restriction about where should be placed the feature with null values ? before ? after ?
}
}
}
query.setSortBy(FF.sort(FF.property(desc.getName().tip().toString()), SortOrder.DESCENDING));
subset = featureSet.subset(query);
// count should not change with a sort by
assertEquals(candidate.size, FeatureStoreUtilities.getCount(subset, true).intValue());
try (Stream<Feature> stream = subset.features(false)) {
final Iterator<Feature> reader = stream.iterator();
Comparable last = null;
while (reader.hasNext()) {
final Feature f = reader.next();
Object obj = f.getProperty(desc.getName().toString()).getValue();
if (obj instanceof Identifier)
obj = ((Identifier) obj).getCode();
final Comparable current = (Comparable) obj;
if (current != null) {
if (last != null) {
// check we have the correct order.
assertTrue(current.compareTo(last) <= 0);
}
last = current;
} else {
// any restriction about where should be placed the feature with null values ? before ? after ?
}
}
}
}
// start ----------------------------------------------------------------
if (candidate.size > 1) {
List<ResourceId> ids = new ArrayList<>();
try (Stream<Feature> stream = featureSet.features(false)) {
final Iterator<Feature> ite = stream.iterator();
while (ite.hasNext()) {
ids.add(FeatureExt.getId(ite.next()));
}
}
// skip the first element
final FeatureQuery query = new FeatureQuery();
query.setOffset(1);
try (Stream<Feature> stream = featureSet.subset(query).features(false)) {
final Iterator<Feature> ite = stream.iterator();
int i = 1;
while (ite.hasNext()) {
assertEquals(FeatureExt.getId(ite.next()), ids.get(i));
i++;
}
}
}
// max ------------------------------------------------------------------
if (candidate.size > 1) {
final FeatureQuery query = new FeatureQuery();
query.setLimit(1);
int i = 0;
try (Stream<Feature> stream = featureSet.subset(query).features(false)) {
final Iterator<Feature> ite = stream.iterator();
while (ite.hasNext()) {
ite.next();
i++;
}
}
assertEquals(1, i);
}
// filter ---------------------------------------------------------------
// filters are tested more deeply in the filter module
// we just make a few tests here for sanity check
// todo should we make more deep tests ?
Set<ResourceId> ids = new HashSet<>();
try (Stream<Feature> stream = featureSet.features(false)) {
final Iterator<Feature> ite = stream.iterator();
// peek only one on two ids
boolean oneOnTwo = true;
while (ite.hasNext()) {
final Feature feature = ite.next();
if (oneOnTwo) {
ids.add(FeatureExt.getId(feature));
}
oneOnTwo = !oneOnTwo;
}
}
Set<ResourceId> remaining = new HashSet<>(ids);
final FeatureQuery query = new FeatureQuery();
final Filter f;
switch(ids.size()) {
case 0:
f = Filter.exclude();
break;
case 1:
f = ids.iterator().next();
break;
default:
f = FF.or(ids);
break;
}
query.setSelection(f);
try (Stream<Feature> stream = featureSet.subset(query).features(false)) {
final Iterator<Feature> ite = stream.iterator();
while (ite.hasNext()) {
remaining.remove(FeatureExt.getId(ite.next()));
}
}
assertTrue(remaining.isEmpty());
}
Aggregations