Search in sources :

Example 6 with IsValidOp

use of com.revolsys.geometry.operation.valid.IsValidOp in project com.revolsys.open by revolsys.

the class FieldDefinition method validate.

public Object validate(Object value) {
    final String fieldName = getName();
    value = toFieldValueException(value);
    if (value == null) {
        if (isRequired()) {
            throw new IllegalArgumentException(fieldName + " is required");
        }
    } else {
        final RecordDefinition recordDefinition = getRecordDefinition();
        final CodeTable codeTable = recordDefinition.getCodeTableByFieldName(fieldName);
        if (codeTable == null) {
            final int maxLength = getLength();
            if (value instanceof Number) {
                final Number number = (Number) value;
                final BigDecimal bigNumber = new BigDecimal(number.toString());
                final int length = bigNumber.precision();
                if (maxLength > 0) {
                    if (length > maxLength) {
                        throw new IllegalArgumentException(fieldName + "=" + value + " length " + length + " > " + maxLength);
                    }
                }
                final int scale = bigNumber.scale();
                final int maxScale = getScale();
                if (maxScale > 0) {
                    if (scale > maxScale) {
                        throw new IllegalArgumentException(fieldName + "=" + value + " scale " + scale + " > " + maxScale);
                    }
                }
                final Number minValue = getMinValue();
                if (minValue != null) {
                    if (NumericComparator.numericCompare(number, minValue) < 0) {
                        throw new IllegalArgumentException(fieldName + "=" + value + " > " + minValue);
                    }
                }
                final Number maxValue = getMaxValue();
                if (maxValue != null) {
                    if (NumericComparator.numericCompare(number, maxValue) > 0) {
                        throw new IllegalArgumentException(fieldName + "=" + value + " < " + maxValue);
                    }
                }
            } else if (value instanceof String) {
                final String string = (String) value;
                final int length = string.length();
                if (maxLength > 0) {
                    if (length > maxLength) {
                        throw new IllegalArgumentException(fieldName + "=" + value + " length " + length + " > " + maxLength);
                    }
                }
            } else if (value instanceof Geometry) {
                final Geometry geometry = (Geometry) value;
                final IsValidOp validOp = new IsValidOp(geometry, false);
                if (!validOp.isValid()) {
                    final String errors = Strings.toString(validOp.getErrors());
                    throw new IllegalArgumentException("Geometry not valid: " + errors);
                }
            }
            if (!this.allowedValues.isEmpty()) {
                if (!this.allowedValues.containsKey(value)) {
                    throw new IllegalArgumentException(fieldName + "=" + value + " not in (" + Strings.toString(",", this.allowedValues) + ")");
                }
            }
        } else {
            final Identifier id = codeTable.getIdentifier(value);
            if (id == null) {
                String codeTableName;
                if (codeTable instanceof CodeTableProperty) {
                    @SuppressWarnings("resource") final CodeTableProperty property = (CodeTableProperty) codeTable;
                    codeTableName = property.getTypeName();
                } else {
                    codeTableName = codeTable.toString();
                }
                throw new IllegalArgumentException("Unable to find code for '" + value + "' in " + codeTableName);
            }
        }
    }
    return value;
}
Also used : CodeTable(com.revolsys.record.code.CodeTable) Geometry(com.revolsys.geometry.model.Geometry) Identifier(com.revolsys.identifier.Identifier) IsValidOp(com.revolsys.geometry.operation.valid.IsValidOp) BigDecimal(java.math.BigDecimal) CodeTableProperty(com.revolsys.record.code.CodeTableProperty)

Example 7 with IsValidOp

use of com.revolsys.geometry.operation.valid.IsValidOp in project com.revolsys.open by revolsys.

the class ValidClosedRingTest method checkIsValid.

private void checkIsValid(final Geometry geom, final boolean expected) {
    final IsValidOp validator = new IsValidOp(geom);
    final boolean isValid = validator.isValid();
    assertTrue(isValid == expected);
}
Also used : IsValidOp(com.revolsys.geometry.operation.valid.IsValidOp)

Example 8 with IsValidOp

use of com.revolsys.geometry.operation.valid.IsValidOp in project com.revolsys.open by revolsys.

the class ValidationFunctions method invalidGeoms.

public static Geometry invalidGeoms(final Geometry g) {
    final List invalidGeoms = new ArrayList();
    for (int i = 0; i < g.getGeometryCount(); i++) {
        final Geometry geom = g.getGeometry(i);
        final IsValidOp ivop = new IsValidOp(geom);
        final GeometryValidationError err = ivop.getValidationError();
        if (err != null) {
            invalidGeoms.add(geom);
        }
    }
    return g.getGeometryFactory().buildGeometry(invalidGeoms);
}
Also used : Geometry(com.revolsys.geometry.model.Geometry) GeometryValidationError(com.revolsys.geometry.operation.valid.GeometryValidationError) ArrayList(java.util.ArrayList) IsValidOp(com.revolsys.geometry.operation.valid.IsValidOp) List(java.util.List) ArrayList(java.util.ArrayList)

Example 9 with IsValidOp

use of com.revolsys.geometry.operation.valid.IsValidOp in project com.revolsys.open by revolsys.

the class ValidationFunctions method invalidLocations.

/**
 * Validates all geometries in a collection independently.
 * Errors are returned as points at the invalid location
 *
 * @param g
 * @return the invalid locations, if any
 */
public static Geometry invalidLocations(final Geometry g) {
    final List invalidLoc = new ArrayList();
    for (int i = 0; i < g.getGeometryCount(); i++) {
        final Geometry geom = g.getGeometry(i);
        final IsValidOp ivop = new IsValidOp(geom);
        final GeometryValidationError err = ivop.getValidationError();
        if (err != null) {
            invalidLoc.add(g.getGeometryFactory().point(err.getErrorPoint()));
        }
    }
    return g.getGeometryFactory().buildGeometry(invalidLoc);
}
Also used : Geometry(com.revolsys.geometry.model.Geometry) GeometryValidationError(com.revolsys.geometry.operation.valid.GeometryValidationError) ArrayList(java.util.ArrayList) IsValidOp(com.revolsys.geometry.operation.valid.IsValidOp) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

IsValidOp (com.revolsys.geometry.operation.valid.IsValidOp)9 Geometry (com.revolsys.geometry.model.Geometry)6 Point (com.revolsys.geometry.model.Point)3 GeometryValidationError (com.revolsys.geometry.operation.valid.GeometryValidationError)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 LineString (com.revolsys.geometry.model.LineString)1 Polygon (com.revolsys.geometry.model.Polygon)1 PointDoubleXY (com.revolsys.geometry.model.impl.PointDoubleXY)1 CoordinateNaNError (com.revolsys.geometry.operation.valid.CoordinateNaNError)1 Identifier (com.revolsys.identifier.Identifier)1 CodeTable (com.revolsys.record.code.CodeTable)1 CodeTableProperty (com.revolsys.record.code.CodeTableProperty)1 BigDecimal (java.math.BigDecimal)1