Search in sources :

Example 1 with DoubleValue

use of org.exist.xquery.value.DoubleValue in project exist by eXist-db.

the class FunRemove method eval.

public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().start(this);
        context.getProfiler().message(this, Profiler.DEPENDENCIES, "DEPENDENCIES", Dependency.getDependenciesName(this.getDependencies()));
        if (contextSequence != null) {
            context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence);
        }
        if (contextItem != null) {
            context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT ITEM", contextItem.toSequence());
        }
    }
    Sequence result;
    Sequence seq = getArgument(0).eval(contextSequence, contextItem);
    if (seq.isEmpty()) {
        result = Sequence.EMPTY_SEQUENCE;
    } else {
        // TODO : explain this Double conversion -pb
        int pos = ((DoubleValue) getArgument(1).eval(contextSequence, contextItem).convertTo(Type.DOUBLE)).getInt();
        if (pos < 1 || pos > seq.getItemCount()) {
            result = seq;
        } else {
            pos--;
            if (seq instanceof NodeSet) {
                result = new ExtArrayNodeSet();
                result.addAll((NodeSet) seq);
                result = ((NodeSet) result).except((NodeSet) seq.itemAt(pos));
            } else {
                result = new ValueSequence();
                for (int i = 0; i < seq.getItemCount(); i++) {
                    if (i != pos) {
                        result.add(seq.itemAt(i));
                    }
                }
            }
        }
    }
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().end(this, "", result);
    }
    return result;
}
Also used : ExtArrayNodeSet(org.exist.dom.persistent.ExtArrayNodeSet) NodeSet(org.exist.dom.persistent.NodeSet) ExtArrayNodeSet(org.exist.dom.persistent.ExtArrayNodeSet) DoubleValue(org.exist.xquery.value.DoubleValue) ValueSequence(org.exist.xquery.value.ValueSequence) ValueSequence(org.exist.xquery.value.ValueSequence) Sequence(org.exist.xquery.value.Sequence)

Example 2 with DoubleValue

use of org.exist.xquery.value.DoubleValue in project exist by eXist-db.

the class GetUploadedFileSize method eval.

@Override
public Sequence eval(final Sequence[] args, @Nonnull final RequestWrapper request) throws XPathException {
    final String uploadParamName = args[0].getStringValue();
    final List<Path> files = request.getFileUploadParam(uploadParamName);
    if (files == null || files.isEmpty()) {
        return Sequence.EMPTY_SEQUENCE;
    }
    final ValueSequence result = new ValueSequence();
    for (final Path file : files) {
        result.add(new DoubleValue(FileUtils.sizeQuietly(file)));
    }
    return result;
}
Also used : Path(java.nio.file.Path) DoubleValue(org.exist.xquery.value.DoubleValue) ValueSequence(org.exist.xquery.value.ValueSequence)

Example 3 with DoubleValue

use of org.exist.xquery.value.DoubleValue in project exist by eXist-db.

the class FunGMLProducers method eval.

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    Sequence result = null;
    try {
        AbstractGMLJDBCIndexWorker indexWorker = (AbstractGMLJDBCIndexWorker) context.getBroker().getIndexController().getWorkerByIndexId(AbstractGMLJDBCIndex.ID);
        if (indexWorker == null) {
            logger.error("Unable to find a spatial index worker");
            throw new XPathException(this, "Unable to find a spatial index worker");
        }
        Geometry geometry = null;
        String targetSRS = null;
        if (isCalledAs("transform")) {
            if (args[0].isEmpty())
                result = Sequence.EMPTY_SEQUENCE;
            else {
                NodeValue geometryNode = (NodeValue) args[0].itemAt(0);
                // Try to get the geometry from the index
                String sourceSRS = null;
                if (geometryNode.getImplementationType() == NodeValue.PERSISTENT_NODE) {
                    sourceSRS = indexWorker.getGeometricPropertyForNode(context, (NodeProxy) geometryNode, "SRS_NAME").getStringValue();
                    geometry = indexWorker.getGeometryForNode(context.getBroker(), (NodeProxy) geometryNode, false);
                    hasUsedIndex = true;
                }
                // Otherwise, build it
                if (geometry == null) {
                    sourceSRS = ((Element) geometryNode.getNode()).getAttribute("srsName").trim();
                    geometry = indexWorker.streamNodeToGeometry(context, geometryNode);
                }
                if (geometry == null) {
                    logger.error("Unable to get a geometry from the node");
                    throw new XPathException("Unable to get a geometry from the node");
                }
                targetSRS = args[1].itemAt(0).getStringValue().trim();
                geometry = indexWorker.transformGeometry(geometry, sourceSRS, targetSRS);
            }
        } else if (isCalledAs("WKTtoGML")) {
            if (args[0].isEmpty())
                result = Sequence.EMPTY_SEQUENCE;
            else {
                String wkt = args[0].itemAt(0).getStringValue();
                WKTReader wktReader = new WKTReader();
                try {
                    geometry = wktReader.read(wkt);
                } catch (ParseException e) {
                    logger.error(e.getMessage());
                    throw new XPathException(e);
                }
                if (geometry == null) {
                    logger.error("Unable to get a geometry from the node");
                    throw new XPathException("Unable to get a geometry from the node");
                }
                targetSRS = args[1].itemAt(0).getStringValue().trim();
            }
        } else if (isCalledAs("buffer")) {
            if (args[0].isEmpty())
                result = Sequence.EMPTY_SEQUENCE;
            else {
                NodeValue geometryNode = (NodeValue) args[0].itemAt(0);
                // Try to get the geometry from the index
                if (geometryNode.getImplementationType() == NodeValue.PERSISTENT_NODE) {
                    targetSRS = indexWorker.getGeometricPropertyForNode(context, (NodeProxy) geometryNode, "SRS_NAME").getStringValue();
                    geometry = indexWorker.getGeometryForNode(context.getBroker(), (NodeProxy) geometryNode, false);
                    hasUsedIndex = true;
                }
                // Otherwise, build it
                if (geometry == null) {
                    targetSRS = ((Element) geometryNode.getNode()).getAttribute("srsName").trim();
                    geometry = indexWorker.streamNodeToGeometry(context, geometryNode);
                }
                if (geometry == null) {
                    logger.error("Unable to get a geometry from the node");
                    throw new XPathException("Unable to get a geometry from the node");
                }
                double distance = ((DoubleValue) args[1].itemAt(0)).getDouble();
                int quadrantSegments = 8;
                int endCapStyle = BufferOp.CAP_ROUND;
                if (getArgumentCount() > 2 && Type.subTypeOf(args[2].itemAt(0).getType(), Type.INTEGER))
                    quadrantSegments = ((IntegerValue) args[2].itemAt(0)).getInt();
                if (getArgumentCount() > 3 && Type.subTypeOf(args[3].itemAt(0).getType(), Type.INTEGER))
                    endCapStyle = ((IntegerValue) args[3].itemAt(0)).getInt();
                switch(endCapStyle) {
                    case BufferOp.CAP_ROUND:
                    case BufferOp.CAP_BUTT:
                    case BufferOp.CAP_SQUARE:
                        // OK
                        break;
                    default:
                        {
                            logger.error("Invalid line end style");
                            throw new XPathException("Invalid line end style");
                        }
                }
                geometry = geometry.buffer(distance, quadrantSegments, endCapStyle);
            }
        } else if (isCalledAs("getBbox")) {
            if (args[0].isEmpty())
                result = Sequence.EMPTY_SEQUENCE;
            else {
                NodeValue geometryNode = (NodeValue) args[0].itemAt(0);
                // Try to get the geometry from the index
                if (geometryNode.getImplementationType() == NodeValue.PERSISTENT_NODE) {
                    targetSRS = indexWorker.getGeometricPropertyForNode(context, (NodeProxy) geometryNode, "SRS_NAME").getStringValue();
                    geometry = indexWorker.getGeometryForNode(context.getBroker(), (NodeProxy) geometryNode, false);
                    hasUsedIndex = true;
                }
                // Otherwise, build it
                if (geometry == null) {
                    targetSRS = ((Element) geometryNode.getNode()).getAttribute("srsName").trim();
                    geometry = indexWorker.streamNodeToGeometry(context, geometryNode);
                }
                if (geometry == null) {
                    logger.error("Unable to get a geometry from the node");
                    throw new XPathException("Unable to get a geometry from the node");
                }
                geometry = geometry.getEnvelope();
            }
        } else if (isCalledAs("convexHull")) {
            if (args[0].isEmpty())
                result = Sequence.EMPTY_SEQUENCE;
            else {
                NodeValue geometryNode = (NodeValue) args[0].itemAt(0);
                // Try to get the geometry from the index
                if (geometryNode.getImplementationType() == NodeValue.PERSISTENT_NODE) {
                    targetSRS = indexWorker.getGeometricPropertyForNode(context, (NodeProxy) geometryNode, "SRS_NAME").getStringValue();
                    geometry = indexWorker.getGeometryForNode(context.getBroker(), (NodeProxy) geometryNode, false);
                    hasUsedIndex = true;
                }
                // Otherwise, build it
                if (geometry == null) {
                    targetSRS = ((Element) geometryNode.getNode()).getAttribute("srsName").trim();
                    geometry = indexWorker.streamNodeToGeometry(context, geometryNode);
                }
                if (geometry == null) {
                    logger.error("Unable to get a geometry from the node");
                    throw new XPathException("Unable to get a geometry from the node");
                }
                geometry = geometry.convexHull();
            }
        } else if (isCalledAs("boundary")) {
            if (args[0].isEmpty())
                result = Sequence.EMPTY_SEQUENCE;
            else {
                NodeValue geometryNode = (NodeValue) args[0].itemAt(0);
                // Try to get the geometry from the index
                if (geometryNode.getImplementationType() == NodeValue.PERSISTENT_NODE) {
                    targetSRS = indexWorker.getGeometricPropertyForNode(context, (NodeProxy) geometryNode, "SRS_NAME").getStringValue();
                    geometry = indexWorker.getGeometryForNode(context.getBroker(), (NodeProxy) geometryNode, false);
                    hasUsedIndex = true;
                }
                // Otherwise, build it
                if (geometry == null) {
                    targetSRS = ((Element) geometryNode.getNode()).getAttribute("srsName").trim();
                    geometry = indexWorker.streamNodeToGeometry(context, geometryNode);
                }
                if (geometry == null) {
                    logger.error("Unable to get a geometry from the node");
                    throw new XPathException("Unable to get a geometry from the node");
                }
                geometry = geometry.getBoundary();
            }
        } else {
            Geometry geometry1 = null;
            Geometry geometry2 = null;
            if (args[0].isEmpty() && args[1].isEmpty())
                result = Sequence.EMPTY_SEQUENCE;
            else if (!args[0].isEmpty() && args[1].isEmpty())
                result = args[0].itemAt(0).toSequence();
            else if (args[0].isEmpty() && !args[1].isEmpty())
                result = args[1].itemAt(0).toSequence();
            else {
                NodeValue geometryNode1 = (NodeValue) args[0].itemAt(0);
                NodeValue geometryNode2 = (NodeValue) args[1].itemAt(0);
                String srsName1 = null;
                String srsName2 = null;
                // Try to get the geometries from the index
                if (geometryNode1.getImplementationType() == NodeValue.PERSISTENT_NODE) {
                    srsName1 = indexWorker.getGeometricPropertyForNode(context, (NodeProxy) geometryNode1, "SRS_NAME").getStringValue();
                    geometry1 = indexWorker.getGeometryForNode(context.getBroker(), (NodeProxy) geometryNode1, false);
                    hasUsedIndex = true;
                }
                if (geometryNode2.getImplementationType() == NodeValue.PERSISTENT_NODE) {
                    srsName2 = indexWorker.getGeometricPropertyForNode(context, (NodeProxy) geometryNode2, "SRS_NAME").getStringValue();
                    geometry2 = indexWorker.getGeometryForNode(context.getBroker(), (NodeProxy) geometryNode2, false);
                    hasUsedIndex = true;
                }
                // Otherwise build them
                if (geometry1 == null) {
                    srsName1 = ((Element) geometryNode1.getNode()).getAttribute("srsName").trim();
                    geometry1 = indexWorker.streamNodeToGeometry(context, geometryNode1);
                }
                if (geometry2 == null) {
                    srsName2 = ((Element) geometryNode2.getNode()).getAttribute("srsName").trim();
                    geometry2 = indexWorker.streamNodeToGeometry(context, geometryNode2);
                }
                if (geometry1 == null) {
                    logger.error("Unable to get a geometry from the first node");
                    throw new XPathException("Unable to get a geometry from the first node");
                }
                if (geometry2 == null) {
                    logger.error("Unable to get a geometry from the second node");
                    throw new XPathException("Unable to get a geometry from the second node");
                }
                if (srsName1 == null)
                    throw new XPathException("Unable to get a SRS for the first geometry");
                if (srsName2 == null)
                    throw new XPathException("Unable to get a SRS for the second geometry");
                // Transform the second geometry in the SRS of the first one if necessary
                if (!srsName1.equalsIgnoreCase(srsName2)) {
                    geometry2 = indexWorker.transformGeometry(geometry2, srsName1, srsName2);
                }
                if (isCalledAs("intersection")) {
                    geometry = geometry1.intersection(geometry2);
                } else if (isCalledAs("union")) {
                    geometry = geometry1.union(geometry2);
                } else if (isCalledAs("difference")) {
                    geometry = geometry1.difference(geometry2);
                } else if (isCalledAs("symetricDifference")) {
                    geometry = geometry1.symDifference(geometry2);
                }
                targetSRS = srsName1;
            }
        }
        if (result == null) {
            String gmlPrefix = context.getPrefixForURI(AbstractGMLJDBCIndexWorker.GML_NS);
            if (gmlPrefix == null) {
                logger.error("namespace is not defined:" + SpatialModule.PREFIX);
                throw new XPathException("'" + AbstractGMLJDBCIndexWorker.GML_NS + "' namespace is not defined");
            }
            context.pushDocumentContext();
            try {
                MemTreeBuilder builder = context.getDocumentBuilder();
                DocumentBuilderReceiver receiver = new DocumentBuilderReceiver(builder);
                result = (NodeValue) indexWorker.streamGeometryToElement(geometry, targetSRS, receiver);
            } finally {
                context.popDocumentContext();
            }
        }
    } catch (SpatialIndexException e) {
        logger.error(e.getMessage());
        throw new XPathException(e);
    }
    return result;
}
Also used : NodeValue(org.exist.xquery.value.NodeValue) XPathException(org.exist.xquery.XPathException) Element(org.w3c.dom.Element) IntegerValue(org.exist.xquery.value.IntegerValue) Sequence(org.exist.xquery.value.Sequence) SpatialIndexException(org.exist.indexing.spatial.SpatialIndexException) WKTReader(com.vividsolutions.jts.io.WKTReader) DocumentBuilderReceiver(org.exist.dom.memtree.DocumentBuilderReceiver) NodeProxy(org.exist.dom.persistent.NodeProxy) Geometry(com.vividsolutions.jts.geom.Geometry) MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) DoubleValue(org.exist.xquery.value.DoubleValue) AbstractGMLJDBCIndexWorker(org.exist.indexing.spatial.AbstractGMLJDBCIndexWorker) ParseException(com.vividsolutions.jts.io.ParseException)

Example 4 with DoubleValue

use of org.exist.xquery.value.DoubleValue in project exist by eXist-db.

the class NoParamFunctions method eval.

/* (non-Javadoc)
         * @see org.exist.xquery.Expression#eval(org.exist.dom.persistent.DocumentSet, org.exist.xquery.value.Sequence, org.exist.xquery.value.Item)
         */
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    Sequence result;
    final String functionName = getSignature().getName().getLocalPart();
    if (PI.equals(functionName)) {
        result = new DoubleValue(Math.PI);
    } else {
        throw new XPathException(this, "Function " + functionName + " not found.");
    }
    return result;
}
Also used : DoubleValue(org.exist.xquery.value.DoubleValue) XPathException(org.exist.xquery.XPathException) Sequence(org.exist.xquery.value.Sequence)

Example 5 with DoubleValue

use of org.exist.xquery.value.DoubleValue in project exist by eXist-db.

the class GMLHSQLIndexWorker method getGeometricPropertyForNode.

@Override
protected AtomicValue getGeometricPropertyForNode(XQueryContext context, NodeProxy p, Connection conn, String propertyName) throws SQLException, XPathException {
    PreparedStatement ps = conn.prepareStatement("SELECT " + propertyName + " FROM " + GMLHSQLIndex.TABLE_NAME + " WHERE DOCUMENT_URI = ? AND NODE_ID_UNITS = ? AND NODE_ID = ?");
    ps.setString(1, p.getOwnerDocument().getURI().toString());
    ps.setInt(2, p.getNodeId().units());
    byte[] bytes = new byte[p.getNodeId().size()];
    p.getNodeId().serialize(bytes, 0);
    ps.setBytes(3, bytes);
    ResultSet rs = null;
    try {
        rs = ps.executeQuery();
        if (!rs.next())
            // Nothing returned
            return AtomicValue.EMPTY_VALUE;
        AtomicValue result = null;
        if (rs.getMetaData().getColumnClassName(1).equals(Boolean.class.getName())) {
            result = new BooleanValue(rs.getBoolean(1));
        } else if (rs.getMetaData().getColumnClassName(1).equals(Double.class.getName())) {
            result = new DoubleValue(rs.getDouble(1));
        } else if (rs.getMetaData().getColumnClassName(1).equals(String.class.getName())) {
            result = new StringValue(rs.getString(1));
        } else if (rs.getMetaData().getColumnType(1) == java.sql.Types.BINARY) {
            result = BinaryValueFromInputStream.getInstance(context, new Base64BinaryValueType(), new UnsynchronizedByteArrayInputStream(rs.getBytes(1)));
        } else
            throw new SQLException("Unable to make an atomic value from '" + rs.getMetaData().getColumnClassName(1) + "'");
        if (rs.next()) {
            // Should be impossible
            throw new SQLException("More than one geometry for node " + p);
        }
        return result;
    } finally {
        if (rs != null)
            rs.close();
        ps.close();
    }
}
Also used : DoubleValue(org.exist.xquery.value.DoubleValue) BooleanValue(org.exist.xquery.value.BooleanValue) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) AtomicValue(org.exist.xquery.value.AtomicValue) Base64BinaryValueType(org.exist.xquery.value.Base64BinaryValueType) StringValue(org.exist.xquery.value.StringValue)

Aggregations

DoubleValue (org.exist.xquery.value.DoubleValue)10 Sequence (org.exist.xquery.value.Sequence)5 UnsynchronizedByteArrayInputStream (org.apache.commons.io.input.UnsynchronizedByteArrayInputStream)3 XPathException (org.exist.xquery.XPathException)3 AtomicValue (org.exist.xquery.value.AtomicValue)3 Base64BinaryValueType (org.exist.xquery.value.Base64BinaryValueType)3 BooleanValue (org.exist.xquery.value.BooleanValue)3 StringValue (org.exist.xquery.value.StringValue)3 ValueSequence (org.exist.xquery.value.ValueSequence)3 Geometry (com.vividsolutions.jts.geom.Geometry)2 NodeProxy (org.exist.dom.persistent.NodeProxy)2 AbstractGMLJDBCIndexWorker (org.exist.indexing.spatial.AbstractGMLJDBCIndexWorker)2 SpatialIndexException (org.exist.indexing.spatial.SpatialIndexException)2 BrokerPool (org.exist.storage.BrokerPool)2 IntegerValue (org.exist.xquery.value.IntegerValue)2 NodeValue (org.exist.xquery.value.NodeValue)2 Element (org.w3c.dom.Element)2 ParseException (com.vividsolutions.jts.io.ParseException)1 WKTReader (com.vividsolutions.jts.io.WKTReader)1 BigInteger (java.math.BigInteger)1