use of org.geotoolkit.storage.memory.InMemoryFeatureSet in project geotoolkit by Geomatys.
the class FeatureStoreUtilitiesTest method testGetEnvelope.
@Test
public void testGetEnvelope() throws DataStoreException {
final GeometryFactory gf = org.geotoolkit.geometry.jts.JTS.getFactory();
final FeatureTypeBuilder ftb = new FeatureTypeBuilder();
ftb.setName("Candidate");
ftb.addAttribute(Geometry.class).setName("geom").setCRS(CommonCRS.WGS84.normalizedGeographic()).addRole(AttributeRole.DEFAULT_GEOMETRY);
final FeatureType type = ftb.build();
final Feature f1 = type.newInstance();
f1.setPropertyValue("geom", gf.createPoint(new Coordinate(10, 20)));
final Feature f2 = type.newInstance();
f2.setPropertyValue("geom", gf.createPoint(new Coordinate(-30, -40)));
final FeatureSet fs = new InMemoryFeatureSet(type, Arrays.asList(f1, f2));
final Envelope envelope = FeatureStoreUtilities.getEnvelope(fs);
final GeneralEnvelope expected = new GeneralEnvelope(CommonCRS.WGS84.normalizedGeographic());
expected.setRange(0, -30, 10);
expected.setRange(1, -40, 20);
Assert.assertEquals(expected, envelope);
}
use of org.geotoolkit.storage.memory.InMemoryFeatureSet in project geotoolkit by Geomatys.
the class CellSymbolizerRenderer method presentations.
private Stream<Presentation> presentations(MapLayer layer, final GridCoverageResource resource) {
// adjust envelope, we need cells to start at crs 0,0 to avoid artifacts
// when building tiles
final int cellSize = symbol.getSource().getCellSize();
final AffineTransform2D displayToObjective = renderingContext.getDisplayToObjective();
double objCellSize = AffineTransforms2D.getScale(displayToObjective) * cellSize;
final GeneralEnvelope env = new GeneralEnvelope(renderingContext.getCanvasObjectiveBounds());
final int hidx = CRSUtilities.firstHorizontalAxis(env.getCoordinateReferenceSystem());
// round under and above to match cell size
env.setRange(hidx, objCellSize * Math.floor(env.getMinimum(hidx) / objCellSize), objCellSize * Math.ceil(env.getMaximum(hidx) / objCellSize));
env.setRange(hidx + 1, objCellSize * Math.floor(env.getMinimum(hidx + 1) / objCellSize), objCellSize * Math.ceil(env.getMaximum(hidx + 1) / objCellSize));
GridCoverage coverage;
try {
coverage = resource.read(renderingContext.getGridGeometry());
} catch (NoSuchDataException ex) {
// no data on requested area
return Stream.empty();
} catch (Exception ex) {
ExceptionPresentation ep = new ExceptionPresentation(ex);
ep.setLayer(layer);
ep.setResource(resource);
return Stream.of(ep);
}
if (coverage != null) {
coverage = coverage.forConvertedValues(true);
}
if (coverage == null) {
LOGGER.log(Level.WARNING, "Reprojected coverage is null.");
return Stream.empty();
}
// create all cell features
final GeneralEnvelope area = new GeneralEnvelope(coverage.getGridGeometry().getEnvelope());
// round under and above to match cell size
area.setRange(hidx, objCellSize * Math.floor(area.getMinimum(hidx) / objCellSize), objCellSize * Math.ceil(area.getMaximum(hidx) / objCellSize));
area.setRange(hidx + 1, objCellSize * Math.floor(area.getMinimum(hidx + 1) / objCellSize), objCellSize * Math.ceil(area.getMaximum(hidx + 1) / objCellSize));
final int nbx = (int) Math.ceil(area.getSpan(0) / objCellSize);
final int nby = (int) Math.ceil(area.getSpan(1) / objCellSize);
final RenderedImage image = coverage.render(null);
final int nbBand = image.getSampleModel().getNumBands();
final Statistics[][][] stats = new Statistics[nbBand][nby][nbx];
MathTransform gridToCRS = coverage.getGridGeometry().getGridToCRS(PixelInCell.CELL_CENTER);
final PixelIterator ite = PixelIterator.create(image);
int i, x, y;
final double[] gridCoord = new double[gridToCRS.getSourceDimensions()];
final double[] crsCoord = new double[gridToCRS.getTargetDimensions()];
final double[] pixel = new double[nbBand];
try {
while (ite.next()) {
Point position = ite.getPosition();
gridCoord[0] = position.getX();
gridCoord[1] = position.getY();
gridToCRS.transform(gridCoord, 0, crsCoord, 0, 1);
crsCoord[0] = (crsCoord[0] - area.getMinimum(0)) / objCellSize;
crsCoord[1] = (crsCoord[1] - area.getMinimum(1)) / objCellSize;
x = (int) crsCoord[0];
y = (int) crsCoord[1];
ite.getPixel(pixel);
for (i = 0; i < nbBand; i++) {
if (stats[i][y][x] == null)
stats[i][y][x] = new Statistics("");
if (!Double.isNaN(pixel[i])) {
stats[i][y][x].accept(pixel[i]);
}
}
}
} catch (TransformException ex) {
ExceptionPresentation ep = new ExceptionPresentation(ex);
ep.setLayer(layer);
ep.setResource(resource);
return Stream.of(ep);
}
// prepare the cell feature type
final FeatureType cellType = CellSymbolizer.buildCellType(coverage);
final Rule rule = symbol.getSource().getRule();
final List<Feature> features = new ArrayList<>();
for (y = 0; y < nby; y++) {
for (x = 0; x < nbx; x++) {
if (stats[0][y][x] == null) {
for (i = 0; i < nbBand; i++) {
stats[i][y][x] = new Statistics("");
}
}
double cx = area.getMinimum(0) + (0.5 + x) * objCellSize;
double cy = area.getMinimum(1) + (0.5 + y) * objCellSize;
final Feature feature = cellType.newInstance();
feature.setPropertyValue(CellSymbolizer.PROPERY_GEOM_CENTER, GF.createPoint(new Coordinate(cx, cy)));
int k = 0;
for (int b = 0, n = nbBand; b < n; b++) {
feature.setPropertyValue("band_" + b + CellSymbolizer.PROPERY_SUFFIX_COUNT, (double) stats[b][y][x].count());
feature.setPropertyValue("band_" + b + CellSymbolizer.PROPERY_SUFFIX_MIN, stats[b][y][x].minimum());
feature.setPropertyValue("band_" + b + CellSymbolizer.PROPERY_SUFFIX_MEAN, stats[b][y][x].mean());
feature.setPropertyValue("band_" + b + CellSymbolizer.PROPERY_SUFFIX_MAX, stats[b][y][x].maximum());
feature.setPropertyValue("band_" + b + CellSymbolizer.PROPERY_SUFFIX_RANGE, stats[b][y][x].span());
feature.setPropertyValue("band_" + b + CellSymbolizer.PROPERY_SUFFIX_RMS, stats[b][y][x].rms());
feature.setPropertyValue("band_" + b + CellSymbolizer.PROPERY_SUFFIX_SUM, stats[b][y][x].sum());
}
features.add(feature);
}
}
final InMemoryFeatureSet fs = new InMemoryFeatureSet(cellType, features);
final MapLayer subLayer = MapBuilder.createLayer(fs);
subLayer.setStyle(GO2Utilities.STYLE_FACTORY.style(rule.symbolizers().toArray(new Symbolizer[0])));
return DefaultPortrayalService.present(subLayer, fs, renderingContext);
}
use of org.geotoolkit.storage.memory.InMemoryFeatureSet in project geotoolkit by Geomatys.
the class ClusterHullProcess method toFeatureSet.
private FeatureSet toFeatureSet(final Set<Geometry> geometries, final CoordinateReferenceSystem crs) throws DataStoreException {
final FeatureType type = createSimpleType(crs);
final List<Feature> features = new ArrayList<>();
for (Geometry geometry : geometries) {
features.add(createDefaultFeatureFromGeometry(geometry, type));
}
return new InMemoryFeatureSet(type, features);
}
use of org.geotoolkit.storage.memory.InMemoryFeatureSet in project geotoolkit by Geomatys.
the class WFSFeatureSet method add.
@Override
public void add(Iterator<? extends Feature> newFeatures) throws DataStoreException {
final List<Feature> features = new ArrayList<>();
newFeatures.forEachRemaining(features::add);
if (features.isEmpty()) {
// nothing to add
return;
}
final TransactionRequest request = store.createTransaction();
final Insert insert = store.createInsertElement();
insert.setInputFormat("text/xml; subtype=\"gml/3.1.1\"");
final FeatureType featureType = getType();
final FeatureSet col = new InMemoryFeatureSet(NamesExt.create("id"), featureType, features);
insert.setFeatures(col);
request.elements().add(insert);
InputStream response = null;
try {
response = request.getResponseStream();
Unmarshaller unmarshal = WFSMarshallerPool.getInstance().acquireUnmarshaller();
Object obj = unmarshal.unmarshal(response);
WFSMarshallerPool.getInstance().recycle(unmarshal);
if (obj instanceof JAXBElement) {
obj = ((JAXBElement) obj).getValue();
}
if (obj instanceof TransactionResponse) {
final TransactionResponse tr = (TransactionResponse) obj;
// TODO send event
// tr.getInsertedFID();
} else {
throw new DataStoreException("Unexpected response : " + obj.getClass());
}
} catch (IOException ex) {
throw new DataStoreException(ex);
} catch (JAXBException ex) {
throw new DataStoreException(ex);
} finally {
if (response != null) {
try {
response.close();
} catch (IOException ex) {
store.getLogger().log(Level.SEVERE, null, ex);
}
}
}
}
use of org.geotoolkit.storage.memory.InMemoryFeatureSet in project geotoolkit by Geomatys.
the class CachedPatternSymbolizer method getMasks.
public Map.Entry<FeatureSet, MutableStyle> getMasks(final GridCoverage coverage) throws IOException, TransformException {
final List<Feature> features = new ArrayList<>();
final Map<NumberRange, List<Symbolizer>> styles = new LinkedHashMap<>();
final Map<NumberRange, Integer> stylesIndexes = new LinkedHashMap<>();
final Map<Expression, List<Symbolizer>> categorizes = styleElement.getRanges();
final Expression[] steps = categorizes.keySet().toArray(new Expression[categorizes.size()]);
Arrays.sort(steps, new Comparator<Expression>() {
@Override
public int compare(Expression t1, Expression t2) {
if (t1 == null) {
return -1;
} else if (t2 == null) {
return +1;
}
double d1 = ((Number) t1.apply(null)).doubleValue();
double d2 = ((Number) t2.apply(null)).doubleValue();
double res = d1 - d2;
if (res < 0) {
return -1;
} else if (res > 0) {
return +1;
} else {
return 0;
}
}
});
// fill the numberranges ------------------------------------------------
double last = Double.NEGATIVE_INFINITY;
double end = Double.POSITIVE_INFINITY;
NumberRange interval;
int i = 0;
int index = 0;
for (; i < steps.length - 1; i++) {
end = ((Number) steps[i + 1].apply(null)).doubleValue();
interval = NumberRange.create(last, true, end, false);
styles.put(interval, categorizes.get(steps[i]));
stylesIndexes.put(interval, index++);
last = end;
}
// last element
end = Double.POSITIVE_INFINITY;
NumberRange<Double> lastRange = NumberRange.create(last, true, end, true);
styles.put(lastRange, categorizes.get(steps[i]));
stylesIndexes.put(lastRange, index++);
// calculate the polygons -----------------------------------------------
final ProcessDescriptor descriptor = CoverageToVectorDescriptor.INSTANCE;
final Integer band = ((Number) styleElement.getChannel().apply(null)).intValue();
final ParameterValueGroup input = descriptor.getInputDescriptor().createValue();
input.parameter(CoverageToVectorDescriptor.COVERAGE.getName().getCode()).setValue(coverage);
final Set<NumberRange> nrs = styles.keySet();
input.parameter(CoverageToVectorDescriptor.RANGES.getName().getCode()).setValue(nrs.toArray(new NumberRange[nrs.size()]));
input.parameter(CoverageToVectorDescriptor.BAND.getName().getCode()).setValue(band);
final Process process = descriptor.createProcess(input);
final Geometry[] polygons;
try {
polygons = (Geometry[]) process.call().parameter(CoverageToVectorDescriptor.GEOMETRIES.getName().getCode()).getValue();
} catch (ProcessException ex) {
Logger.getLogger("org.geotoolkit.display2d.ext.pattern").log(Level.WARNING, null, ex);
throw new IOException(ex.getMessage(), ex);
}
// build the global style -----------------------------------------------
final MutableStyle style = GO2Utilities.STYLE_FACTORY.style();
final MutableFeatureTypeStyle fts = GO2Utilities.STYLE_FACTORY.featureTypeStyle();
style.featureTypeStyles().add(fts);
int idx = 0;
for (List<Symbolizer> lst : styles.values()) {
MutableRule rule = GO2Utilities.STYLE_FACTORY.rule();
rule.symbolizers().addAll(lst);
rule.setFilter(GO2Utilities.FILTER_FACTORY.equal(GO2Utilities.FILTER_FACTORY.property("category"), GO2Utilities.FILTER_FACTORY.literal(idx), true, MatchAction.ANY));
fts.rules().add(rule);
idx++;
}
// build the features ---------------------------------------------------
final CoordinateReferenceSystem crs = coverage.getCoordinateReferenceSystem();
final FeatureTypeBuilder sftBuilder = new FeatureTypeBuilder();
final String geometryField = "geometry";
sftBuilder.setName("DynamicFeature");
sftBuilder.addAttribute(Integer.class).setName("id").addRole(AttributeRole.IDENTIFIER_COMPONENT);
sftBuilder.addAttribute(Geometry.class).setName(geometryField).setCRS(crs).addRole(AttributeRole.DEFAULT_GEOMETRY);
sftBuilder.addAttribute(Integer.class).setName("category");
final FeatureType sft = sftBuilder.build();
int id = 0;
for (Geometry entry : polygons) {
if (entry == null)
continue;
NumberRange numberRange = (NumberRange) entry.getUserData();
idx = stylesIndexes.get(numberRange);
entry.setUserData(crs);
final Feature feature = sft.newInstance();
feature.setPropertyValue(geometryField, entry);
feature.setPropertyValue("id", id++);
feature.setPropertyValue("category", idx);
features.add(feature);
}
return new AbstractMap.SimpleEntry<>(new InMemoryFeatureSet(sft, features), style);
}
Aggregations