Search in sources :

Example 1 with FieldDefn

use of org.gdal.ogr.FieldDefn 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 2 with FieldDefn

use of org.gdal.ogr.FieldDefn in project com.revolsys.open by revolsys.

the class OgrRecordWriter method setFieldValues.

@SuppressWarnings("deprecation")
protected void setFieldValues(final FeatureDefn featureDefinition, final Record record, final Feature feature) {
    final int fieldCount = featureDefinition.GetFieldCount();
    for (int fieldIndex = 0; fieldIndex < fieldCount; fieldIndex++) {
        final FieldDefn fieldDefinition = featureDefinition.GetFieldDefn(fieldIndex);
        final String name = fieldDefinition.GetName();
        final Object value = record.getValue(name);
        if (value != null) {
            final int fieldType = fieldDefinition.GetFieldType();
            switch(fieldType) {
                case 0:
                    final Integer intValue = Integers.toValid(value);
                    if (intValue != null) {
                        feature.SetField(fieldIndex, intValue);
                    }
                    break;
                case 1:
                    // value = feature.GetFieldAsIntegerList(fieldIndex);
                    break;
                case 2:
                    final Double doubleValue = DataTypes.DOUBLE.toObject(value);
                    if (doubleValue != null) {
                        feature.SetField(fieldIndex, doubleValue);
                    }
                    break;
                case 3:
                    // value = feature.GetFieldAsDoubleList(fieldIndex);
                    break;
                case 4:
                case 6:
                    final String string = DataTypes.toString(value);
                    feature.SetField(fieldIndex, string);
                    break;
                case 5:
                case 7:
                    // value = feature.GetFieldAsStringList(fieldIndex);
                    break;
                case 8:
                    // binary
                    break;
                case 9:
                case 10:
                case 11:
                    final java.util.Date date = DataTypes.DATE_TIME.toObject(value);
                    final int year = 1900 + date.getYear();
                    final int month = date.getMonth();
                    final int day = date.getDay();
                    final int hours = date.getHours();
                    final int minutes = date.getMinutes();
                    final int seconds = date.getSeconds();
                    final int timezoneOffset = date.getTimezoneOffset();
                    feature.SetField(fieldIndex, year, month, day, hours, minutes, seconds, timezoneOffset);
                    break;
                default:
                    final String string2 = DataTypes.toString(value);
                    feature.SetField(fieldIndex, string2);
                    break;
            }
        }
    }
}
Also used : GeomFieldDefn(org.gdal.ogr.GeomFieldDefn) FieldDefn(org.gdal.ogr.FieldDefn) LineString(com.revolsys.geometry.model.LineString) Point(com.revolsys.geometry.model.Point)

Aggregations

FieldDefn (org.gdal.ogr.FieldDefn)2 GeomFieldDefn (org.gdal.ogr.GeomFieldDefn)2 DataType (com.revolsys.datatype.DataType)1 GeometryFactory (com.revolsys.geometry.model.GeometryFactory)1 LineString (com.revolsys.geometry.model.LineString)1 Point (com.revolsys.geometry.model.Point)1 PathName (com.revolsys.io.PathName)1 FieldDefinition (com.revolsys.record.schema.FieldDefinition)1 RecordDefinitionImpl (com.revolsys.record.schema.RecordDefinitionImpl)1 FeatureDefn (org.gdal.ogr.FeatureDefn)1 SpatialReference (org.gdal.osr.SpatialReference)1