Search in sources :

Example 21 with Hints

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);
    }
}
Also used : DataStoreException(org.apache.sis.storage.DataStoreException) Query(org.apache.sis.storage.Query) SQLQuery(org.geotoolkit.storage.feature.query.SQLQuery) Hints(org.geotoolkit.factory.Hints) Filter(org.opengis.filter.Filter) UnsupportedQueryException(org.apache.sis.storage.UnsupportedQueryException) IOException(java.io.IOException)

Example 22 with Hints

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);
}
Also used : FeatureType(org.opengis.feature.FeatureType) Query(org.apache.sis.storage.Query) Hints(org.geotoolkit.factory.Hints) UnsupportedQueryException(org.apache.sis.storage.UnsupportedQueryException) Filter(org.opengis.filter.Filter) FeatureReader(org.geotoolkit.storage.feature.FeatureReader)

Example 23 with Hints

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);
}
Also used : FeatureType(org.opengis.feature.FeatureType) Query(org.apache.sis.storage.Query) Hints(org.geotoolkit.factory.Hints) UnsupportedQueryException(org.apache.sis.storage.UnsupportedQueryException) PropertyType(org.opengis.feature.PropertyType) Envelope(org.locationtech.jts.geom.Envelope) GenericName(org.opengis.util.GenericName) LooseBBox(org.geotoolkit.filter.binaryspatial.LooseBBox) AttributeType(org.opengis.feature.AttributeType) ShapefileFeatureReader(org.geotoolkit.data.shapefile.ShapefileFeatureReader) FeatureReader(org.geotoolkit.storage.feature.FeatureReader) DataStoreException(org.apache.sis.storage.DataStoreException) IOException(java.io.IOException) AbstractOperation(org.apache.sis.feature.AbstractOperation) FilterAttributeExtractor(org.geotoolkit.filter.visitor.FilterAttributeExtractor) Filter(org.opengis.filter.Filter) ResourceId(org.opengis.filter.ResourceId) CloseableCollection(org.geotoolkit.index.CloseableCollection)

Example 24 with Hints

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);
            }
        }
    }
}
Also used : Hints(org.geotoolkit.factory.Hints) GO2Hints(org.geotoolkit.display2d.GO2Hints) Color(java.awt.Color) OutputStream(java.io.OutputStream) J2DCanvasSVG(org.geotoolkit.display2d.canvas.J2DCanvasSVG) IIOException(javax.imageio.IIOException) IOException(java.io.IOException) Dimension(java.awt.Dimension) Presentation(org.apache.sis.internal.map.Presentation) ExceptionPresentation(org.apache.sis.internal.map.ExceptionPresentation) Envelope(org.opengis.geometry.Envelope) BufferedImage(java.awt.image.BufferedImage) GridCoverage(org.apache.sis.coverage.grid.GridCoverage) GridCoverageBuilder(org.apache.sis.coverage.grid.GridCoverageBuilder) OutputStreamWriter(java.io.OutputStreamWriter) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) ImageWriter(javax.imageio.ImageWriter) ImageCoverageWriter(org.geotoolkit.coverage.io.ImageCoverageWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) PortrayalException(org.geotoolkit.display.PortrayalException)

Example 25 with Hints

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);
}
Also used : Hints(org.geotoolkit.factory.Hints) RenderingHints(java.awt.RenderingHints)

Aggregations

Hints (org.geotoolkit.factory.Hints)30 RenderingHints (java.awt.RenderingHints)10 DataStoreException (org.apache.sis.storage.DataStoreException)10 GO2Hints (org.geotoolkit.display2d.GO2Hints)10 Dimension (java.awt.Dimension)9 MapLayers (org.apache.sis.portrayal.MapLayers)9 Test (org.junit.Test)9 BufferedImage (java.awt.image.BufferedImage)8 FeatureType (org.opengis.feature.FeatureType)8 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)7 GeneralEnvelope (org.apache.sis.geometry.GeneralEnvelope)7 Filter (org.opengis.filter.Filter)7 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)7 MapLayer (org.apache.sis.portrayal.MapLayer)6 PortrayalException (org.geotoolkit.display.PortrayalException)6 CanvasDef (org.geotoolkit.display2d.service.CanvasDef)6 SceneDef (org.geotoolkit.display2d.service.SceneDef)6 Query (org.apache.sis.storage.Query)5 FeatureReader (org.geotoolkit.storage.feature.FeatureReader)5