use of org.geotoolkit.factory.Hints in project geotoolkit by Geomatys.
the class DefaultJDBCFeatureStore method getFeatureWriter.
@Override
public FeatureWriter getFeatureWriter(Query query) throws DataStoreException {
if (!(query instanceof org.geotoolkit.storage.feature.query.Query))
throw new UnsupportedQueryException();
final org.geotoolkit.storage.feature.query.Query gquery = (org.geotoolkit.storage.feature.query.Query) query;
typeCheck(gquery.getTypeName());
final Filter filter = gquery.getSelection();
final Hints hints = gquery.getHints();
try {
if (Filter.exclude().equals(filter)) {
// append mode
return getFeatureWriterInternal(gquery.getTypeName(), Filter.exclude(), EditMode.INSERT, null, hints);
} else {
// update mode
return getFeatureWriterInternal(gquery.getTypeName(), filter, EditMode.UPDATE_AND_INSERT, null, hints);
}
} catch (IOException ex) {
throw new DataStoreException(ex);
}
}
use of org.geotoolkit.factory.Hints in project geotoolkit by Geomatys.
the class ShapefileFeatureStore method getFeatureReader.
/**
* {@inheritDoc }
*/
@Override
public FeatureReader getFeatureReader(final Query query) throws DataStoreException {
if (!(query instanceof org.geotoolkit.storage.feature.query.Query))
throw new UnsupportedQueryException();
final org.geotoolkit.storage.feature.query.Query gquery = (org.geotoolkit.storage.feature.query.Query) query;
final FeatureType baseType = getFeatureType();
final String queryTypeName = gquery.getTypeName();
final String[] queryPropertyNames = gquery.getPropertyNames();
final Hints queryHints = gquery.getHints();
final double[] queryRes = gquery.getResolution();
Filter queryFilter = gquery.getSelection();
final String typeName = baseType.getName().tip().toString();
// check if we must read the 3d values
final boolean read3D = true;
final ShapefileAttributeReader attReader = getAttributesReader(true, read3D, queryRes);
final FeatureIDReader idReader = new DefaultFeatureIDReader(typeName);
FeatureReader reader = ShapefileFeatureReader.create(attReader, idReader, baseType, queryHints);
// handle remaining query parameters ------------------------------------
final org.geotoolkit.storage.feature.query.Query qb = new org.geotoolkit.storage.feature.query.Query(queryTypeName);
qb.setProperties(queryPropertyNames);
qb.setSelection(queryFilter);
qb.setHints(queryHints);
qb.setSortBy(gquery.getSortBy());
qb.setOffset(gquery.getOffset());
gquery.getLimit().ifPresent(qb::setLimit);
return FeatureStreams.subset(reader, qb);
}
use of org.geotoolkit.factory.Hints in project geotoolkit by Geomatys.
the class IndexedShapefileFeatureStore method getFeatureReader.
/**
* Use the spatial index if available and adds a small optimization: if no
* attributes are going to be read, don't uselessly open and read the dbf
* file.
*/
@Override
public FeatureReader getFeatureReader(final Query query) throws DataStoreException {
if (!(query instanceof org.geotoolkit.storage.feature.query.Query))
throw new UnsupportedQueryException();
final org.geotoolkit.storage.feature.query.Query gquery = (org.geotoolkit.storage.feature.query.Query) query;
final FeatureType baseType = getFeatureType();
final String queryTypeName = gquery.getTypeName();
final String[] queryPropertyNames = gquery.getPropertyNames();
final Hints queryHints = gquery.getHints();
final double[] queryRes = gquery.getResolution();
Filter queryFilter = gquery.getSelection();
// check if we must read the 3d values
final boolean read3D = true;
// find the properties we will read and return --------------------------
final AttributeType idAttribute = (AttributeType) baseType.getProperty(AttributeConvention.IDENTIFIER);
Set<AttributeType> readProperties;
Set<PropertyType> returnedProperties;
if (queryPropertyNames == null) {
// return all properties. Note : preserve order by using a linked set implementation
readProperties = new LinkedHashSet<>(getAttributes(baseType, true));
returnedProperties = new LinkedHashSet<>((Collection) baseType.getProperties(true));
} else {
// return only a subset of properties. Note : preserve order by using a linked set implementation
readProperties = new LinkedHashSet<>(queryPropertyNames.length);
returnedProperties = new LinkedHashSet<>(queryPropertyNames.length);
for (String n : queryPropertyNames) {
final PropertyType cdt = baseType.getProperty(n);
if (cdt instanceof AttributeType) {
readProperties.add((AttributeType) cdt);
} else if (cdt instanceof AbstractOperation) {
final Set<String> deps = ((AbstractOperation) cdt).getDependencies();
for (String dep : deps) readProperties.add((AttributeType) baseType.getProperty(dep));
}
returnedProperties.add(cdt);
}
// add filter properties
final FilterAttributeExtractor fae = new FilterAttributeExtractor();
fae.visit(queryFilter, null);
final Set<GenericName> filterPropertyNames = fae.getAttributeNameSet();
for (GenericName n : filterPropertyNames) {
final PropertyType cdt = baseType.getProperty(n.toString());
if (cdt instanceof AttributeType) {
readProperties.add((AttributeType) cdt);
} else if (cdt instanceof AbstractOperation) {
final Set<String> deps = ((AbstractOperation) cdt).getDependencies();
for (String dep : deps) readProperties.add((AttributeType) baseType.getProperty(dep));
}
}
}
final Set<PropertyType> allProperties = new LinkedHashSet<>(returnedProperties);
allProperties.addAll(readProperties);
// create a reader ------------------------------------------------------
final FeatureType readType;
final FeatureReader reader;
try {
final GenericName[] readPropertyNames = new GenericName[allProperties.size()];
int i = 0;
for (PropertyType prop : allProperties) {
readPropertyNames[i++] = prop.getName();
}
readType = FeatureTypeExt.createSubType(baseType, readPropertyNames);
if (queryFilter.getOperatorType() == SpatialOperatorName.BBOX) {
// in case we have a BBOX filter only, which is very commun, we can speed
// the process by relying on the quadtree estimations
final Envelope bbox = ExtractBoundsFilterVisitor.bbox(queryFilter, null);
final boolean loose = (queryFilter instanceof LooseBBox);
queryFilter = Filter.include();
final List<AttributeType> attsProperties = new ArrayList<>(readProperties);
attsProperties.remove(idAttribute);
reader = createFeatureReader(getBBoxAttributesReader(attsProperties, bbox, loose, queryHints, read3D, queryRes), readType, queryHints);
} else if (queryFilter instanceof ResourceId && ((ResourceId) queryFilter).getIdentifier() == null) {
// in case we have an empty id set (TODO: should never happen, maybe we should remove this case).
return FeatureStreams.emptyReader(getFeatureType());
} else {
final List<AttributeType> attsProperties = new ArrayList<>(readProperties);
attsProperties.remove(idAttribute);
reader = createFeatureReader(getAttributesReader(attsProperties, queryFilter, read3D, queryRes), readType, queryHints);
}
} catch (IOException ex) {
throw new DataStoreException(ex);
}
// handle remaining query parameters ------------------------------------
final org.geotoolkit.storage.feature.query.Query qb = new org.geotoolkit.storage.feature.query.Query(queryTypeName);
if (readProperties.equals(returnedProperties)) {
qb.setProperties(queryPropertyNames);
}
qb.setSelection(queryFilter);
qb.setHints(queryHints);
qb.setSortBy(gquery.getSortBy());
qb.setOffset(gquery.getOffset());
gquery.getLimit().ifPresent(qb::setLimit);
return FeatureStreams.subset(reader, qb);
}
use of org.geotoolkit.factory.Hints in project geotoolkit by Geomatys.
the class DefaultPortrayalService method portray.
/**
* @param outputDef : The compression parameter will not necesarly be used
* if the mime type write can not support it.
* @throws PortrayalException
*/
public static void portray(final CanvasDef canvasDef, final SceneDef sceneDef, final OutputDef outputDef) throws PortrayalException {
final String mime = outputDef.getMime();
if (mime.contains("jpeg") || mime.contains("jpg")) {
// special case for jpeg format, the writer generate incorrect colors
// if he find out an alpha channel, so we ensure to have a opaque background
// which will result in at least an RGB palette
final Color bgColor = canvasDef.getBackground();
if (bgColor == null) {
// we set the background white
canvasDef.setBackground(Color.WHITE);
} else {
// we merge colors
canvasDef.setBackground(mergeColors(Color.WHITE, bgColor));
}
}
// directly return false if hints doesnt contain the coverage writer hint enabled
final Hints hints = sceneDef.getHints();
final Object val = (hints != null) ? hints.get(GO2Hints.KEY_COVERAGE_WRITER) : null;
final boolean useCoverageWriter = GO2Hints.COVERAGE_WRITER_ON.equals(val);
if (useCoverageWriter && portrayAsCoverage(canvasDef, sceneDef, outputDef)) {
// we succeeded in writing it with coverage writer directly.
return;
}
final PresentationWriter presWriter = PRESENTATION_FORMATS.get(mime);
if (presWriter != null) {
boolean close = false;
OutputStream outStream = null;
try {
if (outputDef.getOutput() instanceof OutputStream) {
outStream = (OutputStream) outputDef.getOutput();
} else {
outStream = IOUtilities.openWrite(outputDef.getOutput());
close = true;
}
final Stream<Presentation> stream = present(canvasDef, sceneDef);
presWriter.write(canvasDef, sceneDef, stream, outStream);
} catch (IOException ex) {
throw new PortrayalException(ex.getMessage(), ex);
} finally {
if (outStream != null && close) {
try {
outStream.close();
} catch (IOException ex) {
throw new PortrayalException(ex.getMessage(), ex);
}
}
}
} else if ("image/svg+xml".equalsIgnoreCase(mime)) {
// special canvas for svg
final Envelope contextEnv = canvasDef.getEnvelope();
final CoordinateReferenceSystem crs = contextEnv.getCoordinateReferenceSystem();
final J2DCanvasSVG canvas = new J2DCanvasSVG(crs, canvasDef.getDimension(), sceneDef.getHints());
prepareCanvas(canvas, canvasDef, sceneDef);
canvas.repaint();
boolean close = false;
OutputStream outStream = null;
try {
if (outputDef.getOutput() instanceof OutputStream) {
outStream = (OutputStream) outputDef.getOutput();
} else {
outStream = IOUtilities.openWrite(outputDef.getOutput());
close = true;
}
final Writer out = new OutputStreamWriter(outStream, "UTF-8");
canvas.getDocument(out);
} catch (IOException ex) {
throw new PortrayalException(ex.getMessage(), ex);
} finally {
if (outStream != null && close) {
try {
outStream.close();
} catch (IOException ex) {
throw new PortrayalException(ex.getMessage(), ex);
}
}
}
} else {
// use the rendering engine to generate an image
BufferedImage image = portray(canvasDef, sceneDef);
if (image == null) {
throw new PortrayalException("No image created by the canvas.");
}
if (useCoverageWriter) {
final Envelope env = canvasDef.getEnvelope();
final Dimension dim = canvasDef.getDimension();
final double[] resolution = new double[] { env.getSpan(0) / (double) dim.width, env.getSpan(1) / (double) dim.height };
// check the image color model
image = (BufferedImage) rectifyImageColorModel(image, mime);
final GridCoverageBuilder gcb = new GridCoverageBuilder();
gcb.setDomain(env);
gcb.setValues(image);
final GridCoverage coverage = gcb.build();
writeCoverage(coverage, env, resolution, outputDef, null);
} else {
try {
// image color model check is done in the writeImage method
writeImage(image, outputDef);
} catch (IOException ex) {
throw new PortrayalException(ex);
}
}
}
}
use of org.geotoolkit.factory.Hints in project geotoolkit by Geomatys.
the class DisplayElement method setRenderingHints.
public void setRenderingHints(Hints hints) {
if (hints == null)
return;
final Hints th = getHints(true);
th.add(hints);
}
Aggregations