public static void failIfNotNumeric(Function function, Type type) throws IncompatibleTypesException { if (!TypeFactory.isNumerical(type.getTypeCode())) { throw new IncompatibleTypesException( "Function " + function.getName() + " only operates with numerical types. " + TypeFactory.getTypeName(type.getTypeCode()) + " found"); } }
public static void failIfNotOfType(CustomQuery customQuery, Type type, int typeCode) { if (type.getTypeCode() != typeCode) { throw new IncompatibleTypesException( "Function " + customQuery.getName() + " only operates with " + TypeFactory.getTypeName(typeCode) + " types. " + TypeFactory.getTypeName(type.getTypeCode()) + " found"); } }
public static void failIfNotNumeric( final CustomQuery customQuery, final Type type, final int argNumber) throws IncompatibleTypesException { if (!TypeFactory.isNumerical(type.getTypeCode())) { throw new IncompatibleTypesException( customQuery.getName() + " requires a numerical type as argument number " + argNumber + ". " + TypeFactory.getTypeName(type.getTypeCode()) + " found"); } }
@Test public void testMetadataEditionListenerTest() throws Exception { dsf.getSourceManager().remove("big"); dsf.getSourceManager() .register("big", new FileSourceCreation(super.getAnyNonSpatialResource(), null)); testMetadataEditionListenerTest("big", TypeFactory.createType(Type.STRING, "STRING")); }
@Test public void testAddField() throws Exception { sm.register( "landcover", getTempCopyOf(new File(TestResourceHandler.TESTRESOURCES, "landcover2000.shp"))); testAddField("landcover", TypeFactory.createType(Type.STRING, "String")); }
protected Value evaluate(Function function, Value... args) throws FunctionException { Type[] types = new Type[args.length]; for (int i = 0; i < types.length; i++) { types[i] = TypeFactory.createType(args[i].getType()); } FunctionValidator.failIfTypesDoNotMatchSignature(types, function.getFunctionSignatures()); return evaluateFunction(function, args); }
@Test public void testObjectDriverType() throws Exception { MemoryDataSetDriver driver = new MemoryDataSetDriver( new String[] {"pk", "geom"}, new Type[] {TypeFactory.createType(Type.INT), TypeFactory.createType(Type.GEOMETRY)}); sm.register("spatial", driver); Source src = sm.getSource("spatial"); assertEquals((src.getType() & SourceManager.MEMORY), SourceManager.MEMORY); assertEquals((src.getType() & SourceManager.VECTORIAL), SourceManager.VECTORIAL); driver = new MemoryDataSetDriver(new String[] {"pk"}, new Type[] {TypeFactory.createType(Type.INT)}); sm.register("alpha", driver); src = sm.getSource("alpha"); assertEquals((src.getType() & SourceManager.MEMORY), SourceManager.MEMORY); assertEquals((src.getType() & SourceManager.VECTORIAL), 0); }
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; }
public static void failIfNotOfTypes(CustomQuery customQuery, Type type, int... typesCodes) { for (int typeCode : typesCodes) { if (type.getTypeCode() == typeCode) { return; } } throw new IncompatibleTypesException( TypeFactory.getTypeName(type.getTypeCode()) + " is not allowed with custom query " + customQuery.getName()); }
public static void failIfNotOfTypes(Function function, Type type, int... typesCodes) throws IncompatibleTypesException { for (int typeCode : typesCodes) { if (type.getTypeCode() == typeCode) { return; } } throw new IncompatibleTypesException( TypeFactory.getTypeName(type.getTypeCode()) + " is not allowed with function " + function.getName()); }
public void setUp() throws Exception { byte[] rasterData = new byte[4]; RasterMetadata rasterMetadata = new RasterMetadata(0, 0, 1, 1, 2, 2); GeoRaster gr = GeoRasterFactory.createGeoRaster(rasterData, rasterMetadata); dsf = new DataSourceFactory(); dsf.setTempDir("src/test/resources/backup"); DefaultMetadata metadata = new DefaultMetadata( new Type[] {TypeFactory.createType(Type.RASTER)}, new String[] {"raster"}); ObjectMemoryDriver omd = new ObjectMemoryDriver(metadata); omd.addValues(new Value[] {ValueFactory.createValue(gr)}); dsf.getSourceManager().register("raster", omd); }
@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; }
@Test public void testCreationFileAlreadyExists() throws Exception { DefaultMetadata metadata = new DefaultMetadata(); metadata.addField("mystr", TypeFactory.createType(Type.STRING)); File file = File.createTempFile("temp", ".gdms", currentWorkspace); file.delete(); FileSourceCreation sc = new FileSourceCreation(file, metadata); dsf.createDataSource(sc); try { dsf.createDataSource(sc); fail(); } catch (DriverException e) { } }
@Test public void testImplicitGeometryCasts() { assertTrue(TypeFactory.canBeCastTo(Type.GEOMETRYCOLLECTION, Type.GEOMETRY)); assertTrue(TypeFactory.canBeCastTo(Type.LINESTRING, Type.GEOMETRY)); assertTrue(TypeFactory.canBeCastTo(Type.MULTILINESTRING, Type.GEOMETRY)); assertTrue(TypeFactory.canBeCastTo(Type.MULTILINESTRING, Type.GEOMETRYCOLLECTION)); assertFalse(TypeFactory.canBeCastTo(Type.MULTILINESTRING, Type.LINESTRING)); assertFalse(TypeFactory.canBeCastTo(Type.POINT, Type.POLYGON)); }
@Test public void testEditionWithFieldAdded() throws Exception { sm.register("toto", getTempCopyOf(super.getAnyNonSpatialResource())); DataSource d = dsf.getDataSource("toto", DataSourceFactory.EDITABLE); d.open(); d.addField("extra", TypeFactory.createType(Type.STRING)); int fi = d.getFieldIndexByName("extra"); new UndoRedoTest().testAlphanumericEditionUndoRedo(d); Value newValue = ValueFactory.createValue("hi"); d.setFieldValue(0, fi, newValue); d.undo(); d.redo(); d.commit(); d.close(); d.open(); assertTrue(equals(d.getFieldValue(0, d.getFieldIndexByName("extra")), newValue)); d.close(); }
public static void failIfFieldIsNotOfType( final CustomQuery customQuery, final String fieldName, final int fieldIndex, final int typeCodeOfField, final Metadata metadata) throws DriverException, SemanticException { failIfFieldDoesNotExist(customQuery, fieldName, fieldIndex, metadata); final Type[] fieldTypes = MetadataUtilities.getFieldTypes(metadata); if (typeCodeOfField != fieldTypes[fieldIndex].getTypeCode()) { throw new IncompatibleTypesException( customQuery.getName() + ": " + fieldName + " is not of type " + TypeFactory.getTypeName(typeCodeOfField)); } }
@Test public void testFieldInsertionEditionWhileEdition() throws Exception { sm.register("toto", getTempCopyOf(super.getAnyNonSpatialResource())); DataSource d = dsf.getDataSource("toto"); d.open(); String nouveau = "nouveau"; Value newValue = ValueFactory.createValue(nouveau); Value testValue = d.getFieldValue(2, 1); int lastField = d.getMetadata().getFieldCount(); d.deleteRow(0); d.setFieldValue(0, 1, d.getFieldValue(1, 1)); d.addField(nouveau, TypeFactory.createType(Type.STRING)); d.setFieldValue(0, lastField, newValue); assertTrue(equals(d.getFieldValue(0, lastField), newValue)); d.commit(); d.close(); d.open(); assertEquals(d.getMetadata().getFieldName(lastField).toLowerCase(), nouveau); assertTrue(equals(d.getFieldValue(0, lastField), newValue)); assertTrue(equals(d.getFieldValue(0, 1), testValue)); d.close(); }
@Before public void setUp() throws Exception { super.setUpTestsWithoutEdition(); wktReader = new WKTReader(); JTSMultiPolygon2D = wktReader.read("MULTIPOLYGON (((0 0, 1 1, 0 1, 0 0)))"); JTSMultiLineString2D = wktReader.read("MULTILINESTRING ((0 0, 1 1, 0 1, 0 0))"); JTSMultiPoint2D = wktReader.read("MULTIPOINT (0 0, 1 1, 0 1, 0 0)"); JTSPolygon2D = wktReader.read( "POLYGON ((181 124, 87 162, 76 256, 166 315, 286 325, 373 255, 387 213, 377 159, 351 121, 298 101, 234 56, 181 124), (165 244, 227 219, 234 300, 168 288, 165 244), (244 130, 305 135, 324 186, 306 210, 272 206, 206 174, 244 130))"); JTSLineString2D = wktReader.read("LINESTRING (1 1, 2 1, 2 2, 1 2, 1 1)"); JTSLineString3D = wktReader.read("LINESTRING (1 1 1, 2 1 2, 2 2 3, 1 2 4, 1 1 5)"); JTSPoint3D = wktReader.read("POINT(0 10 20)"); JTSPoint2D = wktReader.read("POINT(0 10)"); JTSPolygonWith2Holes = wktReader.read( "POLYGON ((85 55, 85 306, 366 306, 366 55, 85 55), (153 205, 212 173, 241 190, 251 253, 235 278, 147 254, 153 205), (262 88, 321 97, 324 153, 303 177, 240 138, 262 88))"); GeometryFactory gf = new GeometryFactory(); JTSGeometryCollection = gf.createGeometryCollection( new Geometry[] {JTSMultiPolygon2D, JTSMultiLineString2D, JTSPolygon2D}); JTS3DCollection = gf.createGeometryCollection(new Geometry[] {JTSMultiPolygon2D, JTSLineString3D}); // first datasource final MemoryDataSetDriver driver1 = new MemoryDataSetDriver( new String[] {"pk", "geom"}, new Type[] { TypeFactory.createType(Type.INT, new PrimaryKeyConstraint()), TypeFactory.createType(Type.GEOMETRY) }); // insert all filled rows... driver1.addValues( new Value[] {ValueFactory.createValue(1), ValueFactory.createValue(JTSMultiPolygon2D)}); driver1.addValues( new Value[] {ValueFactory.createValue(2), ValueFactory.createValue(JTSMultiLineString2D)}); driver1.addValues( new Value[] {ValueFactory.createValue(3), ValueFactory.createValue(JTSLineString2D)}); driver1.addValues( new Value[] {ValueFactory.createValue(4), ValueFactory.createValue(JTSPolygon2D)}); // and register this new driver... dsf.getSourceManager().register("ds1", driver1); // second datasource final MemoryDataSetDriver driver2 = new MemoryDataSetDriver( new String[] {"pk", "geom"}, new Type[] { TypeFactory.createType(Type.INT, new PrimaryKeyConstraint()), TypeFactory.createType(Type.GEOMETRY) }); driver1.addValues( new Value[] {ValueFactory.createValue(1), ValueFactory.createValue(JTSMultiPolygon2D)}); // and register this new driver... dsf.getSourceManager().register("ds2", driver2); }
@Override public Type getType(Type[] types) { return TypeFactory.createType(Type.DOUBLE); }
@Override public void actionPerformed(ActionEvent e) { if ("modify".equals(e.getActionCommand())) { try { File globals = new File(configPath); // Table creation GdmsWriter globalsGW = new GdmsWriter(globals); String[] fieldNames1 = { "bufferSize", "amenitiesWeighting", "constructibilityWeighting", "idealhousingWeighting", "gaussDeviation", "segregationThreshold", "segregationTolerance", "householdMemory", "movingThreshold", "immigrantNumber", "numberOfTurns", "year", "threshold_1", "threshold_2", "threshold_3", "threshold_4" }; org.gdms.data.types.Type integ = TypeFactory.createType(64); org.gdms.data.types.Type doubl = TypeFactory.createType(16); org.gdms.data.types.Type[] fieldTypes1 = { doubl, doubl, doubl, doubl, doubl, doubl, doubl, integ, doubl, integ, integ, integ, doubl, doubl, doubl, doubl }; Metadata m1 = new DefaultMetadata(fieldTypes1, fieldNames1); globalsGW.writeMetadata(0, m1); // Table filling Map<String, JSpinner> sp = spp.getSpinners(); Double hM = (Double) sp.get("householdMemory").getValue(); Integer hMi = hM.intValue(); Double iN = (Double) sp.get("immigrantNumber").getValue(); Integer iNi = iN.intValue(); Double nOT = (Double) sp.get("numberOfTurns").getValue(); Integer nOTi = nOT.intValue(); Double y = (Double) sp.get("year").getValue(); Integer yi = y.intValue(); globalsGW.addValues( new Value[] { ValueFactory.createValue((Double) sp.get("bufferSize").getValue()), ValueFactory.createValue((Double) sp.get("amenitiesWeighting").getValue()), ValueFactory.createValue((Double) sp.get("constructibilityWeighting").getValue()), ValueFactory.createValue((Double) sp.get("idealhousingWeighting").getValue()), ValueFactory.createValue((Double) sp.get("gaussDeviation").getValue()), ValueFactory.createValue((Double) sp.get("segregationThreshold").getValue()), ValueFactory.createValue((Double) sp.get("segregationTolerance").getValue()), ValueFactory.createValue(hMi), ValueFactory.createValue((Double) sp.get("movingThreshold").getValue()), ValueFactory.createValue(iNi), ValueFactory.createValue(nOTi), ValueFactory.createValue(yi), ValueFactory.createValue((Double) sp.get("threshold_1").getValue()), ValueFactory.createValue((Double) sp.get("threshold_2").getValue()), ValueFactory.createValue((Double) sp.get("threshold_3").getValue()), ValueFactory.createValue((Double) sp.get("threshold_4").getValue()), }); // Table closing globalsGW.writeRowIndexes(); globalsGW.writeExtent(); globalsGW.writeWritenRowCount(); globalsGW.close(); try { if (spp.getSelections().get("statistical").isSelected()) { new LaunchFrame(configPath, "statistical"); } else if (spp.getSelections().get("schelling").isSelected()) { new LaunchFrame(configPath, "schelling"); } } catch (DriverException ex) { Services.getErrorManager().error("Driver Exception", ex); JOptionPane.showMessageDialog( this, "Some driver error has occurred.", "Driver Error", JOptionPane.WARNING_MESSAGE); return; } catch (DataSourceCreationException ex) { Services.getErrorManager().error("DataSourceCreation Exception", ex); JOptionPane.showMessageDialog( this, "Some DataSource creation error has occurred.", "DataSource Creation Error", JOptionPane.WARNING_MESSAGE); return; } dispose(); } catch (IOException ex) { Services.getErrorManager().error("I/O Exception", ex); JOptionPane.showMessageDialog( this, "Some I/O error has occurred.", "I/O Error", JOptionPane.WARNING_MESSAGE); return; } catch (DriverException ex) { Services.getErrorManager().error("Driver Exception", ex); JOptionPane.showMessageDialog( this, "Some driver error has occurred.", "Driver Error", JOptionPane.WARNING_MESSAGE); return; } } else { try { new LaunchFrame(configPath, oldChoice); } catch (DriverException ex) { Services.getErrorManager().error("Driver Exception", ex); JOptionPane.showMessageDialog( this, "Some driver error has occurred.", "Driver Error", JOptionPane.WARNING_MESSAGE); return; } catch (DataSourceCreationException ex) { Services.getErrorManager().error("DataSourceCreation Exception", ex); JOptionPane.showMessageDialog( this, "Some DataSource creation error has occurred.", "DataSource Creation Error", JOptionPane.WARNING_MESSAGE); return; } dispose(); } }
public SumDriver(double n) { super( new DefaultMetadata( new Type[] {TypeFactory.createType(Type.DOUBLE)}, new String[] {"sum"})); sum = n; }