Search in sources :

Example 16 with PathName

use of com.revolsys.io.PathName in project com.revolsys.open by revolsys.

the class OracleSdoGeometryFieldAdder method initialize.

@Override
public void initialize(final JdbcRecordStoreSchema schema) {
    try (final JdbcConnection connection = this.recordStore.getJdbcConnection()) {
        final String schemaName = schema.getDbName();
        final String sridSql = "select M.TABLE_NAME, M.COLUMN_NAME, M.SRID, M.DIMINFO, C.GEOMETRY_TYPE " + "from ALL_SDO_GEOM_METADATA M " + "LEFT OUTER JOIN ALL_GEOMETRY_COLUMNS C ON (M.OWNER = C.F_TABLE_SCHEMA AND M.TABLE_NAME = C.F_TABLE_NAME AND M.COLUMN_NAME = C.F_GEOMETRY_COLUMN) " + "where OWNER = ?";
        try (final PreparedStatement statement = connection.prepareStatement(sridSql)) {
            statement.setString(1, schemaName);
            try (final ResultSet resultSet = statement.executeQuery()) {
                while (resultSet.next()) {
                    final String tableName = resultSet.getString(1);
                    final String columnName = resultSet.getString(2);
                    final PathName typePath = schema.getPathName().newChild(tableName);
                    int srid = resultSet.getInt(3);
                    if (resultSet.wasNull() || srid < 0) {
                        srid = 0;
                    }
                    final Object[] dimInfo = (Object[]) resultSet.getArray("DIMINFO").getArray();
                    int axisCount = dimInfo.length;
                    setColumnProperty(schema, typePath, columnName, AXIS_COUNT, axisCount);
                    if (axisCount < 2) {
                        axisCount = 2;
                    } else if (axisCount > 4) {
                        axisCount = 4;
                    }
                    final double[] scales = new double[axisCount];
                    for (int i = 0; i < scales.length; i++) {
                        scales[i] = getScale(dimInfo, i);
                    }
                    final GeometryFactory geometryFactory = this.recordStore.getGeometryFactory(srid, axisCount, scales);
                    setColumnProperty(schema, typePath, columnName, GEOMETRY_FACTORY, geometryFactory);
                    setColumnProperty(schema, typePath, columnName, ORACLE_SRID, srid);
                    final int geometryType = resultSet.getInt(5);
                    DataType geometryDataType;
                    if (resultSet.wasNull()) {
                        geometryDataType = DataTypes.GEOMETRY;
                    } else {
                        geometryDataType = ID_TO_DATA_TYPE.get(geometryType);
                        if (geometryDataType == null) {
                            geometryDataType = DataTypes.GEOMETRY;
                        }
                    }
                    setColumnProperty(schema, typePath, columnName, GEOMETRY_TYPE, geometryDataType);
                }
            }
        } catch (final SQLException e) {
            Logs.error(this, "Unable to initialize", e);
        }
    }
}
Also used : GeometryFactory(com.revolsys.geometry.model.GeometryFactory) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) DataType(com.revolsys.datatype.DataType) JdbcConnection(com.revolsys.jdbc.JdbcConnection) PreparedStatement(java.sql.PreparedStatement) PathName(com.revolsys.io.PathName)

Example 17 with PathName

use of com.revolsys.io.PathName in project com.revolsys.open by revolsys.

the class OgrRecordStore method newLayerRecordDefinition.

protected RecordDefinitionImpl newLayerRecordDefinition(final RecordStoreSchema schema, final Layer layer) {
    final String layerName = layer.GetName();
    final PathName typePath = PathName.newPathName(layerName);
    /**
     * This primes the layer so that the fidColumn is loaded correctly.
     */
    layer.GetNextFeature();
    final RecordDefinitionImpl recordDefinition = new RecordDefinitionImpl(schema, typePath);
    String idFieldName = layer.GetFIDColumn();
    if (!Property.hasValue(idFieldName)) {
        idFieldName = "rowid";
    }
    this.idFieldNames.put(typePath.getUpperPath(), idFieldName);
    final FeatureDefn layerDefinition = layer.GetLayerDefn();
    if (SQLITE.equals(this.driverName) || GEO_PAKCAGE.equals(this.driverName)) {
        recordDefinition.addField(idFieldName, DataTypes.LONG, true);
        recordDefinition.setIdFieldName(idFieldName);
    }
    for (int fieldIndex = 0; fieldIndex < layerDefinition.GetFieldCount(); fieldIndex++) {
        final FieldDefn fieldDefinition = layerDefinition.GetFieldDefn(fieldIndex);
        final String fieldName = fieldDefinition.GetName();
        final int fieldType = fieldDefinition.GetFieldType();
        final int fieldWidth = fieldDefinition.GetWidth();
        final int fieldPrecision = fieldDefinition.GetPrecision();
        DataType fieldDataType;
        switch(fieldType) {
            case 0:
                fieldDataType = DataTypes.INT;
                break;
            case 2:
                fieldDataType = DataTypes.DOUBLE;
                break;
            case 4:
            case 6:
                fieldDataType = DataTypes.STRING;
                break;
            case 9:
                fieldDataType = DataTypes.DATE;
                break;
            case 11:
                fieldDataType = DataTypes.DATE_TIME;
                break;
            default:
                fieldDataType = DataTypes.STRING;
                final String fieldTypeName = fieldDefinition.GetFieldTypeName(fieldType);
                Logs.error(this, "Unsupported field type " + this.file + " " + fieldName + ": " + fieldTypeName);
                break;
        }
        final FieldDefinition field = new FieldDefinition(fieldName, fieldDataType, fieldWidth, fieldPrecision, false);
        recordDefinition.addField(field);
    }
    for (int fieldIndex = 0; fieldIndex < layerDefinition.GetGeomFieldCount(); fieldIndex++) {
        final GeomFieldDefn fieldDefinition = layerDefinition.GetGeomFieldDefn(fieldIndex);
        final String fieldName = fieldDefinition.GetName();
        final int geometryFieldType = fieldDefinition.GetFieldType();
        DataType geometryFieldDataType;
        int axisCount = 2;
        switch(geometryFieldType) {
            case 1:
                geometryFieldDataType = DataTypes.POINT;
                break;
            case 2:
                geometryFieldDataType = DataTypes.LINE_STRING;
                break;
            case 3:
                geometryFieldDataType = DataTypes.POLYGON;
                break;
            case 4:
                geometryFieldDataType = DataTypes.MULTI_POINT;
                break;
            case 5:
                geometryFieldDataType = DataTypes.MULTI_LINE_STRING;
                break;
            case 6:
                geometryFieldDataType = DataTypes.MULTI_POLYGON;
                break;
            case 7:
                geometryFieldDataType = DataTypes.GEOMETRY_COLLECTION;
                break;
            case 101:
                geometryFieldDataType = DataTypes.LINEAR_RING;
                break;
            case 0x80000000 + 1:
                geometryFieldDataType = DataTypes.POINT;
                axisCount = 3;
                break;
            case 0x80000000 + 2:
                geometryFieldDataType = DataTypes.LINE_STRING;
                axisCount = 3;
                break;
            case 0x80000000 + 3:
                geometryFieldDataType = DataTypes.POLYGON;
                axisCount = 3;
                break;
            case 0x80000000 + 4:
                geometryFieldDataType = DataTypes.MULTI_POINT;
                axisCount = 3;
                break;
            case 0x80000000 + 5:
                geometryFieldDataType = DataTypes.MULTI_LINE_STRING;
                axisCount = 3;
                break;
            case 0x80000000 + 6:
                geometryFieldDataType = DataTypes.MULTI_POLYGON;
                axisCount = 3;
                break;
            case 0x80000000 + 7:
                geometryFieldDataType = DataTypes.GEOMETRY_COLLECTION;
                axisCount = 3;
                break;
            default:
                geometryFieldDataType = DataTypes.GEOMETRY;
                break;
        }
        final SpatialReference spatialReference = fieldDefinition.GetSpatialRef();
        final GeometryFactory geometryFactory = Gdal.getGeometryFactory(spatialReference, axisCount);
        final FieldDefinition field = new FieldDefinition(fieldName, geometryFieldDataType, false);
        field.setProperty(FieldProperties.GEOMETRY_FACTORY, geometryFactory);
        recordDefinition.addField(field);
    }
    return recordDefinition;
}
Also used : GeometryFactory(com.revolsys.geometry.model.GeometryFactory) FeatureDefn(org.gdal.ogr.FeatureDefn) GeomFieldDefn(org.gdal.ogr.GeomFieldDefn) FieldDefn(org.gdal.ogr.FieldDefn) FieldDefinition(com.revolsys.record.schema.FieldDefinition) GeomFieldDefn(org.gdal.ogr.GeomFieldDefn) SpatialReference(org.gdal.osr.SpatialReference) RecordDefinitionImpl(com.revolsys.record.schema.RecordDefinitionImpl) DataType(com.revolsys.datatype.DataType) PathName(com.revolsys.io.PathName)

Example 18 with PathName

use of com.revolsys.io.PathName in project com.revolsys.open by revolsys.

the class OgrRecordStore method newLayerRecordDefinition.

private RecordDefinition newLayerRecordDefinition(final DataSource dataSource, final RecordDefinition sourceRecordDefinition) {
    final PathName typePath = sourceRecordDefinition.getPathName();
    final String name = typePath.getName();
    final PathName parentPath = typePath.getParent();
    final RecordStoreSchema schema = getSchema(parentPath);
    Layer layer;
    if (sourceRecordDefinition.hasGeometryField()) {
        final GeometryFactory geometryFactory = sourceRecordDefinition.getGeometryFactory();
        final FieldDefinition geometryField = sourceRecordDefinition.getGeometryField();
        final int geometryFieldType = getGeometryFieldType(geometryFactory, geometryField);
        final SpatialReference spatialReference = Gdal.getSpatialReference(geometryFactory);
        layer = dataSource.CreateLayer(typePath.getPath(), spatialReference, geometryFieldType);
    } else {
        layer = dataSource.CreateLayer(name);
    }
    if (dataSource.TestCapability(ogrConstants.ODsCCreateLayer) == false) {
        System.err.println("CreateLayer not supported by driver.");
    }
    return newLayerRecordDefinition(schema, layer);
}
Also used : RecordStoreSchema(com.revolsys.record.schema.RecordStoreSchema) GeometryFactory(com.revolsys.geometry.model.GeometryFactory) FieldDefinition(com.revolsys.record.schema.FieldDefinition) SpatialReference(org.gdal.osr.SpatialReference) PathName(com.revolsys.io.PathName) Layer(org.gdal.ogr.Layer)

Example 19 with PathName

use of com.revolsys.io.PathName in project com.revolsys.open by revolsys.

the class FileGdbIoTest method doWriteReadTest.

public static void doWriteReadTest(GeometryFactory geometryFactory, final DataType dataType, Geometry geometry) {
    final int axisCount = geometryFactory.getAxisCount();
    if (geometry.isEmpty() || axisCount == 4) {
        return;
    }
    geometryFactory = GeometryFactory.fixed(geometryFactory.getCoordinateSystemId(), axisCount, GeometryFactory.newScalesFixed(axisCount, 10000000.0));
    geometry = geometry.convertGeometry(geometryFactory);
    final String geometryTypeString = dataType.toString();
    String name = "/tmp/revolsystest/io/gdb/" + geometryTypeString + "_" + axisCount;
    if (geometry.isGeometryCollection()) {
        name += "_" + geometry.getGeometryCount();
    }
    if (geometry instanceof Polygonal) {
        name += "_" + geometry.getGeometryComponents(LinearRing.class).size();
    }
    final File file = new File(name + "_" + geometry.getVertexCount() + ".gdb");
    FileUtil.deleteDirectory(file);
    file.getParentFile().mkdirs();
    try (final FileGdbRecordStore recordStore = FileGdbRecordStoreFactory.newRecordStore(file)) {
        recordStore.setCreateMissingTables(true);
        recordStore.setCreateMissingRecordStore(true);
        recordStore.initialize();
        final PathName typePath = PathName.newPathName("/" + geometryTypeString);
        final RecordDefinitionImpl recordDefinition = new RecordDefinitionImpl(typePath);
        recordDefinition.addField("ID", DataTypes.INT, true);
        recordDefinition.addField("NAME", DataTypes.STRING, 50, false);
        recordDefinition.addField("GEOMETRY", dataType, true);
        recordDefinition.setGeometryFactory(geometryFactory);
        recordStore.getRecordDefinition(recordDefinition);
        try (Writer<Record> writer = recordStore.newRecordWriter()) {
            writer.setProperty(IoConstants.GEOMETRY_FACTORY, geometryFactory);
            writer.setProperty(IoConstants.GEOMETRY_TYPE, dataType);
            final Record record = recordStore.newRecord(typePath);
            record.setValue("ID", 1);
            record.setGeometryValue(geometry);
            writer.write(record);
        }
        try (Reader<Record> reader = recordStore.getRecords(typePath)) {
            final List<Record> objects = reader.toList();
            Assert.assertEquals("Geometry Count", 1, objects.size());
            final Geometry actual = objects.get(0).getGeometry();
            Assert.assertEquals("Empty", geometry.isEmpty(), actual.isEmpty());
            if (!geometry.isEmpty()) {
                final int geometryAxisCount = geometry.getAxisCount();
                Assert.assertEquals("Axis Count", geometryAxisCount, actual.getAxisCount());
                if (!actual.equals(geometryAxisCount, geometry)) {
                    // Allow for conversion of multi part to single part
                    if (geometry.getGeometryCount() != 1 || !actual.equals(geometryAxisCount, geometry.getGeometry(0))) {
                        TestUtil.failNotEquals("Geometry Equal Exact", geometry, actual);
                    }
                }
            }
        }
    }
}
Also used : FileGdbRecordStore(com.revolsys.gis.esri.gdb.file.FileGdbRecordStore) Polygonal(com.revolsys.geometry.model.Polygonal) RecordDefinitionImpl(com.revolsys.record.schema.RecordDefinitionImpl) Geometry(com.revolsys.geometry.model.Geometry) Record(com.revolsys.record.Record) PathName(com.revolsys.io.PathName) LinearRing(com.revolsys.geometry.model.LinearRing) File(java.io.File)

Example 20 with PathName

use of com.revolsys.io.PathName in project com.revolsys.open by revolsys.

the class GeometryTest method writeTestFile.

public static void writeTestFile(final GeometryFactory geometryFactory, final String wkt) {
    final Geometry geometry = geometryFactory.geometry(wkt);
    final DataType geometryDataType = DataTypes.getDataType(geometry);
    String name = "/" + geometryDataType.getName();
    if (geometryFactory.hasZ()) {
        name += "Z";
    }
    final File file = new File("target/test-data/" + name + ".gdb");
    FileUtil.deleteDirectory(file);
    final PathName pathName = PathName.newPathName(name);
    RecordDefinitionImpl recordDefinition = new RecordDefinitionImpl(pathName);
    recordDefinition.addField("ID", DataTypes.INT, true);
    final FieldDefinition geometryField = recordDefinition.addField("Geometry", geometryDataType, true);
    geometryField.setProperty(FieldProperties.GEOMETRY_FACTORY, geometryFactory);
    recordDefinition.setIdFieldName("ID");
    final FileGdbRecordStore recordStore = FileGdbRecordStoreFactory.newRecordStore(file);
    recordStore.initialize();
    recordDefinition = (RecordDefinitionImpl) recordStore.getRecordDefinition(recordDefinition);
    final Record object = new ArrayRecord(recordDefinition);
    object.setIdentifier(Identifier.newIdentifier(1));
    object.setGeometryValue(geometry);
    recordStore.insertRecord(object);
    final Record object2 = recordStore.getRecord(pathName, Identifier.newIdentifier(1));
    if (!DataType.equal(object, object2)) {
        System.out.println("Not Equal");
        System.out.println(object);
        System.out.println(object2);
    }
    recordStore.close();
}
Also used : Geometry(com.revolsys.geometry.model.Geometry) FileGdbRecordStore(com.revolsys.gis.esri.gdb.file.FileGdbRecordStore) ArrayRecord(com.revolsys.record.ArrayRecord) FieldDefinition(com.revolsys.record.schema.FieldDefinition) DataType(com.revolsys.datatype.DataType) RecordDefinitionImpl(com.revolsys.record.schema.RecordDefinitionImpl) Record(com.revolsys.record.Record) ArrayRecord(com.revolsys.record.ArrayRecord) PathName(com.revolsys.io.PathName) File(java.io.File)

Aggregations

PathName (com.revolsys.io.PathName)64 FieldDefinition (com.revolsys.record.schema.FieldDefinition)15 RecordDefinitionImpl (com.revolsys.record.schema.RecordDefinitionImpl)14 DataType (com.revolsys.datatype.DataType)13 RecordDefinition (com.revolsys.record.schema.RecordDefinition)13 GeometryFactory (com.revolsys.geometry.model.GeometryFactory)12 RecordStoreSchema (com.revolsys.record.schema.RecordStoreSchema)11 VectorOfWString (com.revolsys.gis.esri.gdb.file.capi.swig.VectorOfWString)9 RecordStoreSchemaElement (com.revolsys.record.schema.RecordStoreSchemaElement)7 ArrayList (java.util.ArrayList)7 TreeMap (java.util.TreeMap)7 MapEx (com.revolsys.collection.map.MapEx)5 ResultSet (java.sql.ResultSet)5 Geodatabase (com.revolsys.gis.esri.gdb.file.capi.swig.Geodatabase)4 Identifier (com.revolsys.identifier.Identifier)4 JdbcConnection (com.revolsys.jdbc.JdbcConnection)4 Record (com.revolsys.record.Record)4 File (java.io.File)4 PreparedStatement (java.sql.PreparedStatement)4 Geometry (com.revolsys.geometry.model.Geometry)3