Search in sources :

Example 1 with WithinDistance

use of com.revolsys.record.query.functions.WithinDistance in project com.revolsys.open by revolsys.

the class RecordStoreLayer method getRecords.

@Override
public <R extends LayerRecord> List<R> getRecords(final Geometry geometry, final double distance) {
    if (Property.isEmpty(geometry) || !hasGeometryField()) {
        return Collections.emptyList();
    } else {
        final RecordDefinition recordDefinition = getRecordDefinition();
        final FieldDefinition geometryField = getGeometryField();
        final WithinDistance where = F.dWithin(geometryField, geometry, distance);
        final Query query = new Query(recordDefinition, where);
        return getRecords(query);
    }
}
Also used : Query(com.revolsys.record.query.Query) FieldDefinition(com.revolsys.record.schema.FieldDefinition) WithinDistance(com.revolsys.record.query.functions.WithinDistance) RecordDefinition(com.revolsys.record.schema.RecordDefinition)

Example 2 with WithinDistance

use of com.revolsys.record.query.functions.WithinDistance in project com.revolsys.open by revolsys.

the class FileGdbRecordStore method appendQueryValue.

@Override
public void appendQueryValue(final Query query, final StringBuilder buffer, final QueryValue condition) {
    if (condition instanceof Like || condition instanceof ILike) {
        final BinaryCondition like = (BinaryCondition) condition;
        final QueryValue left = like.getLeft();
        final QueryValue right = like.getRight();
        buffer.append("UPPER(CAST(");
        appendQueryValue(query, buffer, left);
        buffer.append(" AS VARCHAR(4000))) LIKE ");
        if (right instanceof Value) {
            final Value valueCondition = (Value) right;
            final Object value = valueCondition.getValue();
            buffer.append("'");
            if (value != null) {
                final String string = DataTypes.toString(value);
                buffer.append(string.toUpperCase().replaceAll("'", "''"));
            }
            buffer.append("'");
        } else {
            appendQueryValue(query, buffer, right);
        }
    } else if (condition instanceof LeftUnaryCondition) {
        final LeftUnaryCondition unaryCondition = (LeftUnaryCondition) condition;
        final String operator = unaryCondition.getOperator();
        final QueryValue right = unaryCondition.getValue();
        buffer.append(operator);
        buffer.append(" ");
        appendQueryValue(query, buffer, right);
    } else if (condition instanceof RightUnaryCondition) {
        final RightUnaryCondition unaryCondition = (RightUnaryCondition) condition;
        final QueryValue left = unaryCondition.getValue();
        final String operator = unaryCondition.getOperator();
        appendQueryValue(query, buffer, left);
        buffer.append(" ");
        buffer.append(operator);
    } else if (condition instanceof BinaryCondition) {
        final BinaryCondition binaryCondition = (BinaryCondition) condition;
        final QueryValue left = binaryCondition.getLeft();
        final String operator = binaryCondition.getOperator();
        final QueryValue right = binaryCondition.getRight();
        appendQueryValue(query, buffer, left);
        buffer.append(" ");
        buffer.append(operator);
        buffer.append(" ");
        appendQueryValue(query, buffer, right);
    } else if (condition instanceof AbstractMultiCondition) {
        final AbstractMultiCondition multipleCondition = (AbstractMultiCondition) condition;
        buffer.append("(");
        boolean first = true;
        final String operator = multipleCondition.getOperator();
        for (final QueryValue subCondition : multipleCondition.getQueryValues()) {
            if (first) {
                first = false;
            } else {
                buffer.append(" ");
                buffer.append(operator);
                buffer.append(" ");
            }
            appendQueryValue(query, buffer, subCondition);
        }
        buffer.append(")");
    } else if (condition instanceof In) {
        final In in = (In) condition;
        if (in.isEmpty()) {
            buffer.append("1==0");
        } else {
            final QueryValue left = in.getLeft();
            appendQueryValue(query, buffer, left);
            buffer.append(" IN (");
            appendQueryValue(query, buffer, in.getValues());
            buffer.append(")");
        }
    } else if (condition instanceof Value) {
        final Value valueCondition = (Value) condition;
        Object value = valueCondition.getValue();
        if (value instanceof Identifier) {
            final Identifier identifier = (Identifier) value;
            value = identifier.getValue(0);
        }
        appendValue(buffer, value);
    } else if (condition instanceof CollectionValue) {
        final CollectionValue collectionValue = (CollectionValue) condition;
        final List<Object> values = collectionValue.getValues();
        boolean first = true;
        for (final Object value : values) {
            if (first) {
                first = false;
            } else {
                buffer.append(", ");
            }
            appendValue(buffer, value);
        }
    } else if (condition instanceof Column) {
        final Column column = (Column) condition;
        final Object name = column.getName();
        buffer.append(name);
    } else if (condition instanceof SqlCondition) {
        final SqlCondition sqlCondition = (SqlCondition) condition;
        final String where = sqlCondition.getSql();
        final List<Object> parameters = sqlCondition.getParameterValues();
        if (parameters.isEmpty()) {
            if (where.indexOf('?') > -1) {
                throw new IllegalArgumentException("No arguments specified for a where clause with placeholders: " + where);
            } else {
                buffer.append(where);
            }
        } else {
            final Matcher matcher = PLACEHOLDER_PATTERN.matcher(where);
            int i = 0;
            while (matcher.find()) {
                if (i >= parameters.size()) {
                    throw new IllegalArgumentException("Not enough arguments for where clause with placeholders: " + where);
                }
                final Object argument = parameters.get(i);
                final StringBuffer replacement = new StringBuffer();
                matcher.appendReplacement(replacement, DataTypes.toString(argument));
                buffer.append(replacement);
                appendValue(buffer, argument);
                i++;
            }
            final StringBuffer tail = new StringBuffer();
            matcher.appendTail(tail);
            buffer.append(tail);
        }
    } else if (condition instanceof EnvelopeIntersects) {
        buffer.append("1 = 1");
    } else if (condition instanceof WithinDistance) {
        buffer.append("1 = 1");
    } else {
        condition.appendDefaultSql(query, this, buffer);
    }
}
Also used : In(com.revolsys.record.query.In) Matcher(java.util.regex.Matcher) ILike(com.revolsys.record.query.ILike) VectorOfWString(com.revolsys.gis.esri.gdb.file.capi.swig.VectorOfWString) QueryValue(com.revolsys.record.query.QueryValue) SqlCondition(com.revolsys.record.query.SqlCondition) RightUnaryCondition(com.revolsys.record.query.RightUnaryCondition) CollectionValue(com.revolsys.record.query.CollectionValue) Like(com.revolsys.record.query.Like) ILike(com.revolsys.record.query.ILike) Identifier(com.revolsys.identifier.Identifier) SingleIdentifier(com.revolsys.identifier.SingleIdentifier) BinaryCondition(com.revolsys.record.query.BinaryCondition) Column(com.revolsys.record.query.Column) EnvelopeIntersects(com.revolsys.record.query.functions.EnvelopeIntersects) WithinDistance(com.revolsys.record.query.functions.WithinDistance) CollectionValue(com.revolsys.record.query.CollectionValue) Value(com.revolsys.record.query.Value) QueryValue(com.revolsys.record.query.QueryValue) List(java.util.List) LeftUnaryCondition(com.revolsys.record.query.LeftUnaryCondition) AbstractMultiCondition(com.revolsys.record.query.AbstractMultiCondition)

Example 3 with WithinDistance

use of com.revolsys.record.query.functions.WithinDistance in project com.revolsys.open by revolsys.

the class OgrRecordStore method appendQueryValue.

@Override
public void appendQueryValue(final Query query, final StringBuilder sql, final QueryValue condition) {
    if (condition instanceof Like || condition instanceof ILike) {
        final BinaryCondition like = (BinaryCondition) condition;
        final QueryValue left = like.getLeft();
        final QueryValue right = like.getRight();
        sql.append("UPPER(");
        appendQueryValue(query, sql, left);
        sql.append(") LIKE ");
        if (right instanceof Value) {
            final Value valueCondition = (Value) right;
            final Object value = valueCondition.getValue();
            sql.append("'");
            if (value != null) {
                final String string = DataTypes.toString(value);
                sql.append(string.toUpperCase());
            }
            sql.append("'");
        } else {
            appendQueryValue(query, sql, right);
        }
    } else if (condition instanceof LeftUnaryCondition) {
        final LeftUnaryCondition unaryCondition = (LeftUnaryCondition) condition;
        final String operator = unaryCondition.getOperator();
        final QueryValue right = unaryCondition.getValue();
        sql.append(operator);
        sql.append(" ");
        appendQueryValue(query, sql, right);
    } else if (condition instanceof RightUnaryCondition) {
        final RightUnaryCondition unaryCondition = (RightUnaryCondition) condition;
        final QueryValue left = unaryCondition.getValue();
        final String operator = unaryCondition.getOperator();
        appendQueryValue(query, sql, left);
        sql.append(" ");
        sql.append(operator);
    } else if (condition instanceof BinaryCondition) {
        final BinaryCondition binaryCondition = (BinaryCondition) condition;
        final QueryValue left = binaryCondition.getLeft();
        final String operator = binaryCondition.getOperator();
        final QueryValue right = binaryCondition.getRight();
        appendQueryValue(query, sql, left);
        sql.append(" ");
        sql.append(operator);
        sql.append(" ");
        appendQueryValue(query, sql, right);
    } else if (condition instanceof AbstractMultiCondition) {
        final AbstractMultiCondition multipleCondition = (AbstractMultiCondition) condition;
        sql.append("(");
        boolean first = true;
        final String operator = multipleCondition.getOperator();
        for (final QueryValue subCondition : multipleCondition.getQueryValues()) {
            if (first) {
                first = false;
            } else {
                sql.append(" ");
                sql.append(operator);
                sql.append(" ");
            }
            appendQueryValue(query, sql, subCondition);
        }
        sql.append(")");
    } else if (condition instanceof Value) {
        final Value valueCondition = (Value) condition;
        final Object value = valueCondition.getValue();
        appendValue(sql, value);
    } else if (condition instanceof CollectionValue) {
        final CollectionValue collectionValue = (CollectionValue) condition;
        final List<Object> values = collectionValue.getValues();
        boolean first = true;
        for (final Object value : values) {
            if (first) {
                first = false;
            } else {
                sql.append(", ");
            }
            appendValue(sql, value);
        }
    } else if (condition instanceof Column) {
        final Column column = (Column) condition;
        final Object name = column.getName();
        sql.append(name);
    } else if (condition instanceof SqlCondition) {
        final SqlCondition sqlCondition = (SqlCondition) condition;
        final String where = sqlCondition.getSql();
        final List<Object> parameters = sqlCondition.getParameterValues();
        if (parameters.isEmpty()) {
            if (where.indexOf('?') > -1) {
                throw new IllegalArgumentException("No arguments specified for a where clause with placeholders: " + where);
            } else {
                sql.append(where);
            }
        } else {
            final Matcher matcher = PLACEHOLDER_PATTERN.matcher(where);
            int i = 0;
            while (matcher.find()) {
                if (i >= parameters.size()) {
                    throw new IllegalArgumentException("Not enough arguments for where clause with placeholders: " + where);
                }
                final Object argument = parameters.get(i);
                final StringBuffer replacement = new StringBuffer();
                matcher.appendReplacement(replacement, DataTypes.toString(argument));
                sql.append(replacement);
                appendValue(sql, argument);
                i++;
            }
            final StringBuffer tail = new StringBuffer();
            matcher.appendTail(tail);
            sql.append(tail);
        }
    } else if (condition instanceof EnvelopeIntersects) {
        final EnvelopeIntersects envelopeIntersects = (EnvelopeIntersects) condition;
        final QueryValue boundingBox1Value = envelopeIntersects.getBoundingBox1Value();
        final QueryValue boundingBox2Value = envelopeIntersects.getBoundingBox2Value();
        if (boundingBox1Value == null || boundingBox2Value == null) {
            sql.append("1 = 0");
        } else {
            sql.append("Intersects(");
            boundingBox1Value.appendSql(query, this, sql);
            sql.append(",");
            boundingBox2Value.appendSql(query, this, sql);
            sql.append(")");
        }
    } else if (condition instanceof WithinDistance) {
        final WithinDistance withinDistance = (WithinDistance) condition;
        final QueryValue geometry1Value = withinDistance.getGeometry1Value();
        final QueryValue geometry2Value = withinDistance.getGeometry2Value();
        final QueryValue distanceValue = withinDistance.getDistanceValue();
        if (geometry1Value == null || geometry2Value == null || distanceValue == null) {
            sql.append("1 = 0");
        } else {
            sql.append("Distance(");
            geometry1Value.appendSql(query, this, sql);
            sql.append(", ");
            geometry2Value.appendSql(query, this, sql);
            sql.append(") <= ");
            distanceValue.appendSql(query, this, sql);
            sql.append(")");
        }
    } else {
        condition.appendDefaultSql(query, this, sql);
    }
}
Also used : Matcher(java.util.regex.Matcher) ILike(com.revolsys.record.query.ILike) QueryValue(com.revolsys.record.query.QueryValue) SqlCondition(com.revolsys.record.query.SqlCondition) RightUnaryCondition(com.revolsys.record.query.RightUnaryCondition) CollectionValue(com.revolsys.record.query.CollectionValue) ILike(com.revolsys.record.query.ILike) Like(com.revolsys.record.query.Like) BinaryCondition(com.revolsys.record.query.BinaryCondition) Column(com.revolsys.record.query.Column) EnvelopeIntersects(com.revolsys.record.query.functions.EnvelopeIntersects) WithinDistance(com.revolsys.record.query.functions.WithinDistance) QueryValue(com.revolsys.record.query.QueryValue) CollectionValue(com.revolsys.record.query.CollectionValue) Value(com.revolsys.record.query.Value) LeftUnaryCondition(com.revolsys.record.query.LeftUnaryCondition) AbstractMultiCondition(com.revolsys.record.query.AbstractMultiCondition)

Example 4 with WithinDistance

use of com.revolsys.record.query.functions.WithinDistance in project com.revolsys.open by revolsys.

the class QueryValue method getBoundingBox.

static BoundingBox getBoundingBox(final QueryValue queryValue) {
    BoundingBox boundingBox = null;
    if (queryValue != null) {
        for (final QueryValue childValue : queryValue.getQueryValues()) {
            if (childValue instanceof EnvelopeIntersects) {
                final EnvelopeIntersects intersects = (EnvelopeIntersects) childValue;
                boundingBox = expand(boundingBox, getBoundingBox(intersects.getBoundingBox1Value()));
                boundingBox = expand(boundingBox, getBoundingBox(intersects.getBoundingBox2Value()));
            } else if (childValue instanceof WithinDistance) {
                final WithinDistance withinDistance = (WithinDistance) childValue;
                BoundingBox withinBoundingBox = getBoundingBox(withinDistance.getGeometry1Value());
                withinBoundingBox = expand(withinBoundingBox, getBoundingBox(withinDistance.getGeometry2Value()));
                final double distance = ((Number) ((Value) withinDistance.getDistanceValue()).getValue()).doubleValue();
                boundingBox = expand(boundingBox, withinBoundingBox.expand(distance));
            } else if (childValue instanceof Value) {
                final Value valueContainer = (Value) childValue;
                final Object value = valueContainer.getValue();
                if (value instanceof BoundingBox) {
                    boundingBox = expand(boundingBox, (BoundingBox) value);
                } else if (value instanceof Geometry) {
                    final Geometry geometry = (Geometry) value;
                    boundingBox = expand(boundingBox, geometry.getBoundingBox());
                }
            }
        }
    }
    return boundingBox;
}
Also used : Geometry(com.revolsys.geometry.model.Geometry) BoundingBox(com.revolsys.geometry.model.BoundingBox) EnvelopeIntersects(com.revolsys.record.query.functions.EnvelopeIntersects) WithinDistance(com.revolsys.record.query.functions.WithinDistance) GetMapValue(com.revolsys.record.query.functions.GetMapValue)

Aggregations

WithinDistance (com.revolsys.record.query.functions.WithinDistance)4 EnvelopeIntersects (com.revolsys.record.query.functions.EnvelopeIntersects)3 AbstractMultiCondition (com.revolsys.record.query.AbstractMultiCondition)2 BinaryCondition (com.revolsys.record.query.BinaryCondition)2 CollectionValue (com.revolsys.record.query.CollectionValue)2 Column (com.revolsys.record.query.Column)2 ILike (com.revolsys.record.query.ILike)2 LeftUnaryCondition (com.revolsys.record.query.LeftUnaryCondition)2 Like (com.revolsys.record.query.Like)2 QueryValue (com.revolsys.record.query.QueryValue)2 RightUnaryCondition (com.revolsys.record.query.RightUnaryCondition)2 SqlCondition (com.revolsys.record.query.SqlCondition)2 Value (com.revolsys.record.query.Value)2 Matcher (java.util.regex.Matcher)2 BoundingBox (com.revolsys.geometry.model.BoundingBox)1 Geometry (com.revolsys.geometry.model.Geometry)1 VectorOfWString (com.revolsys.gis.esri.gdb.file.capi.swig.VectorOfWString)1 Identifier (com.revolsys.identifier.Identifier)1 SingleIdentifier (com.revolsys.identifier.SingleIdentifier)1 In (com.revolsys.record.query.In)1