Search in sources :

Example 1 with GeomFieldDefn

use of org.gdal.ogr.GeomFieldDefn 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 GeomFieldDefn

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

the class OgrRecordWriter method setGeometries.

private void setGeometries(final FeatureDefn featureDefinition, final Record record, final Feature feature) {
    final RecordDefinition recordDefinition = record.getRecordDefinition();
    final int geometryFieldCount = featureDefinition.GetGeomFieldCount();
    for (int fieldIndex = 0; fieldIndex < geometryFieldCount; fieldIndex++) {
        final GeomFieldDefn fieldDefinition = featureDefinition.GetGeomFieldDefn(fieldIndex);
        final String name = fieldDefinition.GetName();
        Geometry geometry = record.getValue(name);
        if (geometry != null) {
            final FieldDefinition attribute = recordDefinition.getField(name);
            final GeometryFactory geometryFactory = attribute.getProperty(FieldProperties.GEOMETRY_FACTORY);
            geometry = geometry.convertGeometry(geometryFactory);
            final int geometryType = fieldDefinition.GetFieldType();
            final int axisCount = geometryFactory.getAxisCount();
            final org.gdal.ogr.Geometry ogrGeometry = toOgrGeometry(geometry, geometryType, axisCount);
            feature.SetGeomField(fieldIndex, ogrGeometry);
        }
    }
}
Also used : Geometry(com.revolsys.geometry.model.Geometry) GeometryFactory(com.revolsys.geometry.model.GeometryFactory) FieldDefinition(com.revolsys.record.schema.FieldDefinition) GeomFieldDefn(org.gdal.ogr.GeomFieldDefn) LineString(com.revolsys.geometry.model.LineString) Point(com.revolsys.geometry.model.Point) RecordDefinition(com.revolsys.record.schema.RecordDefinition)

Aggregations

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