private EXGeographicBoundingBox getGeographicBoundingBox(Envelope env, String epsgCode) {
   Envelope newEnvelope;
   if ("EPSG:4326".equals(epsgCode)) {
     newEnvelope = env;
   } else {
     try {
       GeometryFactory gf = new GeometryFactory();
       Polygon poly = (Polygon) gf.toGeometry(env);
       ST_Transform transformFunction = new ST_Transform();
       CoordinateReferenceSystem inputCRS = DataSourceFactory.getCRSFactory().getCRS(epsgCode);
       Value val =
           transformFunction.evaluate(
               null,
               ValueFactory.createValue(poly, inputCRS),
               ValueFactory.createValue("EPSG:4326"));
       newEnvelope = val.getAsGeometry().getEnvelopeInternal();
     } catch (FunctionException fe) {
       return getDummyGeographic();
     } catch (CRSException ex) {
       return getDummyGeographic();
     }
   }
   EXGeographicBoundingBox ret = new EXGeographicBoundingBox();
   ret.setEastBoundLongitude(newEnvelope.getMaxX());
   ret.setWestBoundLongitude(newEnvelope.getMinX());
   ret.setNorthBoundLatitude(newEnvelope.getMaxY());
   ret.setSouthBoundLatitude(newEnvelope.getMinY());
   return ret;
 }
  private void testIndexRealData(String source, double checkPeriod, DiskRTree tree)
      throws NoSuchTableException, DataSourceCreationException, DriverException, IOException,
          Exception {
    DataSource ds = dsf.getDataSource(source);
    String fieldName = "the_geom";

    ds.open();
    int fieldIndex = ds.getFieldIndexByName(fieldName);
    for (int i = 0; i < ds.getRowCount(); i++) {
      if (i / (int) checkPeriod == i / checkPeriod) {
        tree.checkTree();
        tree.close();
        tree.openIndex(indexFile);
        tree.checkTree();
        checkLookUp(tree, ds, fieldIndex);
      }
      Envelope value = ds.getFieldValue(i, fieldIndex).getAsGeometry().getEnvelopeInternal();
      tree.insert(value, i);
    }
    for (int i = 0; i < ds.getRowCount(); i++) {
      if (i / (int) checkPeriod == i / checkPeriod) {
        tree.checkTree();
        tree.save();
        tree.checkTree();
        checkLookUp(tree, ds, fieldIndex);
      }
      Value value = ds.getFieldValue(i, fieldIndex);
      tree.delete(value.getAsGeometry().getEnvelopeInternal(), i);
    }

    ds.close();
  }
 public static void failIfNull(Value... values) throws FunctionException {
   for (Value value : values) {
     if (value.getType() == Type.NULL) {
       throw new FunctionException("Cannot operate in null values");
     }
   }
 }
 private Value castValue(Type type, Value value) {
   Value newValue = value;
   if (!value.isNull()
       && type.getTypeCode() != value.getType()
       && TypeFactory.canBeCastTo(value.getType(), type.getTypeCode())) {
     newValue = value.toType(type.getTypeCode());
   }
   return newValue;
 }
 @Override
 public String check(Value value) {
   if (!value.isNull()) {
     Matcher m = pattern.matcher(value.toString());
     if (!m.matches()) {
       return "The value doesn't match the pattern '" + constraintValue + "'";
     }
   }
   return null;
 }
  @Override
  public Number[] getScope(int dimension) throws DriverException {
    if (cachedScope == null) {
      boolean open = isOpen();
      if (!open) {
        open();
      }
      for (int i = 0; i < getRowCount(); i++) {
        Metadata m = getMetadata();
        for (int j = 0; j < m.getFieldCount(); j++) {
          int typeCode = m.getFieldType(j).getTypeCode();
          Envelope r = null;
          if ((typeCode & Type.GEOMETRY) != 0) {
            Value v = getFieldValue(i, j);
            if ((v != null) && (!v.isNull())) {
              r = v.getAsGeometry().getEnvelopeInternal();
            }
          } else if (typeCode == Type.RASTER) {
            Value v = getFieldValue(i, j);
            if ((v != null) && (!v.isNull())) {
              r = v.getAsRaster().getMetadata().getEnvelope();
            }
          } else if (typeCode == Type.STREAM) {
            Value v = getFieldValue(i, j);
            if ((v != null) && (!v.isNull())) {
              r = v.getAsStream().getEnvelope();
            }
          }
          if (r != null) {
            if (cachedScope == null) {
              cachedScope = new Envelope(r);
            } else {
              cachedScope.expandToInclude(r);
            }
          }
        }
      }
      if (!open) {
        close();
      }
    }

    if (cachedScope == null) {
      return new Number[] {0, 0};
    } else {
      if (dimension == DataSet.X) {
        return new Number[] {cachedScope.getMinX(), cachedScope.getMaxX()};
      } else if (dimension == DataSet.Y) {
        return new Number[] {cachedScope.getMinY(), cachedScope.getMaxY()};
      } else {
        throw new UnsupportedOperationException("Not yet implemented");
      }
    }
  }
  @Override
  public String check(int fieldId, Value value) throws DriverException {
    // In order to build pk indexes
    initializeEdition();
    // Check special case of auto-increment not-null fields
    Type type = getMetadata().getFieldType(fieldId);
    boolean autoIncrement = type.getBooleanConstraint(Constraint.AUTO_INCREMENT);
    if (autoIncrement && type.getBooleanConstraint(Constraint.NOT_NULL) && value.isNull()) {
      return null;
    }
    int fieldType = type.getTypeCode();
    // Test geometry types.
    if (TypeFactory.isVectorial(type.getTypeCode())) {
      int valueType = value.getType();

      if (!checkGeometry(valueType, fieldType)) {
        return "Can't put a "
            + TypeFactory.getTypeName(valueType)
            + " in a "
            + TypeFactory.getTypeName(fieldType)
            + " column.";
      }
    }
    // Cast value
    Value val = castValue(type, value);
    int broadType = TypeFactory.getBroaderType(fieldType, val.getType());
    if (val.getType() != broadType
        && val.getType() != Type.NULL
        && !checkGeometry(val.getType(), fieldType)
        && fieldType != Type.STRING) {
      return "Can't cast a "
          + TypeFactory.getTypeName(value.getType())
          + " to a "
          + TypeFactory.getTypeName(fieldType);
    }

    // Check constraints
    String fieldName = getMetadata().getFieldName(fieldId);
    String error = type.check(value);
    if (error != null) {
      return "Value at field " + getFieldName(fieldId) + " is not valid:" + error;
    }

    // Check uniqueness
    if (type.getBooleanConstraint(Constraint.UNIQUE) || type.getBooleanConstraint(Constraint.PK)) {
      // We assume a geometry field can't have unique constraint
      IndexQuery iq = new DefaultAlphaQuery(fieldName, value);
      Iterator<Integer> it = queryIndex(iq);
      while (it.hasNext()) {
        if (getFieldValue(it.next(), fieldId).equals(value).getAsBoolean()) {
          return fieldName + " column doesn't admit duplicates: " + value;
        }
      }
    }

    return null;
  }
Example #8
0
  protected Value testSpatialFunction(
      ScalarFunction function, int[] valueType, int parameterCount, Value... normalValue)
      throws Exception {
    // Test null input
    ColumnValue[] vals = new ColumnValue[parameterCount];
    for (int i = 0; i < parameterCount; i++) {
      vals[i] = new ColumnValue(valueType[i], ValueFactory.createNullValue());
    }
    Value res = evaluate(function, vals);
    assertTrue(res.isNull());

    // Test too many parameters
    Value[] args = new Value[parameterCount + 1];
    for (int i = 0; i < args.length; i++) {
      args[i] = normalValue[0];
    }
    try {
      res = evaluate(function, args);
      fail();
    } catch (IncompatibleTypesException e) {
    }

    // Test wrong parameter type
    try {
      args = new Value[parameterCount];
      for (int i = 0; i < args.length; i++) {
        args[i] = ValueFactory.createValue(new Value[0]);
      }
      res = evaluate(function, args);
      fail();
    } catch (IncompatibleTypesException e) {
    }

    // Test normal input value and type
    res = evaluate(function, normalValue);
    return res;
  }
 public static void failIfNotOfType(Value value, int type) throws FunctionException {
   if (type != value.getType()) {
     throw new FunctionException(value.toString() + " is not of type " + type);
   }
 }