Search in sources :

Example 1 with Feature

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

the class OgrQueryIterator method getNext.

@Override
protected Record getNext() throws NoSuchElementException {
    if (this.layer == null) {
        throw new NoSuchElementException();
    } else {
        while (this.offset > 0 && this.count < this.offset) {
            final Feature feature = this.layer.GetNextFeature();
            if (feature == null) {
                throw new NoSuchElementException();
            } else {
                feature.delete();
            }
            this.count++;
        }
        if (this.count - this.offset >= this.limit) {
            throw new NoSuchElementException();
        }
        final Feature feature = this.layer.GetNextFeature();
        this.count++;
        if (feature == null) {
            throw new NoSuchElementException();
        } else {
            try {
                final Record record = this.recordFactory.newRecord(this.recordDefinition);
                record.setState(RecordState.INITIALIZING);
                if (this.labelCountMap == null) {
                    this.recordStore.addStatistic("query", record);
                } else {
                    this.labelCountMap.addCount(record);
                }
                final int fieldCount = feature.GetFieldCount();
                for (int fieldIndex = 0; fieldIndex < fieldCount; fieldIndex++) {
                    final String fieldName = feature.GetFieldDefnRef(fieldIndex).GetName();
                    if (feature.IsFieldSet(fieldIndex)) {
                        final int fieldType = feature.GetFieldType(fieldIndex);
                        Object value;
                        switch(fieldType) {
                            case 0:
                                value = feature.GetFieldAsInteger(fieldIndex);
                                break;
                            case 1:
                                value = feature.GetFieldAsIntegerList(fieldIndex);
                                break;
                            case 2:
                                value = feature.GetFieldAsDouble(fieldIndex);
                                break;
                            case 3:
                                value = feature.GetFieldAsDoubleList(fieldIndex);
                                break;
                            case 4:
                            case 6:
                                value = feature.GetFieldAsString(fieldIndex);
                                break;
                            case 5:
                            case 7:
                                value = feature.GetFieldAsStringList(fieldIndex);
                                break;
                            case 8:
                                value = null;
                                // binary
                                break;
                            case 9:
                                final Calendar date = getCalendar(feature, fieldIndex);
                                value = new Date(date.getTimeInMillis());
                                break;
                            case 10:
                                value = null;
                                // time
                                break;
                            case 11:
                                final Calendar dateTime = getCalendar(feature, fieldIndex);
                                value = new java.util.Date(dateTime.getTimeInMillis());
                                break;
                            default:
                                value = null;
                                break;
                        }
                        record.setValue(fieldName, value);
                    }
                }
                final int geometryCount = feature.GetGeomFieldCount();
                for (int geometryIndex = 0; geometryIndex < geometryCount; geometryIndex++) {
                    final String fieldName = feature.GetGeomFieldDefnRef(geometryIndex).GetName();
                    final org.gdal.ogr.Geometry ogrGeometry = feature.GetGeomFieldRef(geometryIndex);
                    final Geometry geometry = toGeometry(ogrGeometry);
                    record.setValue(fieldName, geometry);
                }
                record.setState(RecordState.PERSISTED);
                return record;
            } finally {
                feature.delete();
            }
        }
    }
}
Also used : GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar) LineString(com.revolsys.geometry.model.LineString) Feature(org.gdal.ogr.Feature) Date(java.sql.Date) Geometry(com.revolsys.geometry.model.Geometry) Record(com.revolsys.record.Record) NoSuchElementException(java.util.NoSuchElementException)

Example 2 with Feature

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

the class OgrRecordStore method getRecordCount.

@Override
public int getRecordCount(final Query query) {
    if (query == null) {
        return 0;
    } else {
        String typePath = query.getTypeName();
        RecordDefinition recordDefinition = query.getRecordDefinition();
        if (recordDefinition == null) {
            typePath = query.getTypeName();
            recordDefinition = getRecordDefinition(typePath);
            if (recordDefinition == null) {
                return 0;
            }
        } else {
            typePath = recordDefinition.getPath();
        }
        final StringBuilder whereClause = getWhereClause(query);
        final StringBuilder sql = new StringBuilder();
        sql.append("SELECT COUNT(*) FROM ");
        final String layerName = getLayerName(typePath);
        sql.append(layerName);
        if (whereClause.length() > 0) {
            sql.append(" WHERE ");
            sql.append(whereClause);
        }
        final DataSource dataSource = getDataSource();
        if (dataSource != null) {
            final Layer result = dataSource.ExecuteSQL(sql.toString());
            if (result != null) {
                addLayerToClose(result);
                try {
                    final Feature feature = result.GetNextFeature();
                    if (feature != null) {
                        try {
                            return feature.GetFieldAsInteger(0);
                        } finally {
                            feature.delete();
                        }
                    }
                } finally {
                    releaseLayerToClose(result);
                }
            }
        }
    }
    return 0;
}
Also used : Layer(org.gdal.ogr.Layer) Feature(org.gdal.ogr.Feature) RecordDefinition(com.revolsys.record.schema.RecordDefinition) DataSource(org.gdal.ogr.DataSource)

Example 3 with Feature

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

the class OgrRecordWriter method insert.

private void insert(final Record record) {
    final RecordDefinition sourceRecordDefinition = record.getRecordDefinition();
    final RecordDefinition recordDefinition = this.recordStore.getRecordDefinition(sourceRecordDefinition);
    final String typePath = sourceRecordDefinition.getPath();
    final List<FieldDefinition> attributes = recordDefinition.getFields();
    final List<String> idFieldNames = recordDefinition.getIdFieldNames();
    for (final FieldDefinition attribute : attributes) {
        final String name = attribute.getName();
        if (!idFieldNames.contains(name)) {
            if (attribute.isRequired()) {
                final Object value = record.getValue(name);
                if (value == null) {
                    throw new IllegalArgumentException("Atribute " + typePath + "." + name + " is required");
                }
            }
        }
    }
    try {
        final Layer layer = getLayer(sourceRecordDefinition);
        final FeatureDefn featureDefinition = layer.GetLayerDefn();
        final Feature feature = new Feature(featureDefinition);
        try {
            setFieldValues(featureDefinition, record, feature);
            setGeometries(featureDefinition, record, feature);
            layer.CreateFeature(feature);
            final String driverName = this.recordStore.getDriverName();
            if (OgrRecordStore.SQLITE.equals(driverName) || OgrRecordStore.GEO_PAKCAGE.equals(driverName)) {
                record.setValue(OgrRecordStore.ROWID, feature.GetFieldAsInteger(OgrRecordStore.ROWID));
            }
            record.setState(RecordState.PERSISTED);
        } finally {
            feature.delete();
            this.recordStore.addStatistic("Insert", record);
        }
    } catch (final IllegalArgumentException e) {
        throw new RuntimeException("Unable to insert row " + e.getMessage() + "\n" + record.toString(), e);
    } catch (final RuntimeException e) {
        Logs.debug(OgrRecordWriter.class, "Unable to insert row \n:" + record.toString());
        throw new RuntimeException("Unable to insert row", e);
    }
}
Also used : FeatureDefn(org.gdal.ogr.FeatureDefn) FieldDefinition(com.revolsys.record.schema.FieldDefinition) LineString(com.revolsys.geometry.model.LineString) Layer(org.gdal.ogr.Layer) Feature(org.gdal.ogr.Feature) RecordDefinition(com.revolsys.record.schema.RecordDefinition)

Example 4 with Feature

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

the class OgrRecordWriter method update.

private void update(final Record record) {
    final RecordDefinition recordDefinition = record.getRecordDefinition();
    final Layer layer = getLayer(recordDefinition);
    final String driverName = this.recordStore.getDriverName();
    if (OgrRecordStore.SQLITE.equals(driverName) || OgrRecordStore.GEO_PAKCAGE.equals(driverName)) {
        final Integer fid = record.getInteger(OgrRecordStore.ROWID);
        if (fid != null) {
            final Feature feature = layer.GetFeature(fid);
            if (feature != null) {
                final FeatureDefn featureDefinition = layer.GetLayerDefn();
                setFieldValues(featureDefinition, record, feature);
                layer.SetFeature(feature);
            }
        }
    }
}
Also used : FeatureDefn(org.gdal.ogr.FeatureDefn) LineString(com.revolsys.geometry.model.LineString) Layer(org.gdal.ogr.Layer) Feature(org.gdal.ogr.Feature) RecordDefinition(com.revolsys.record.schema.RecordDefinition)

Aggregations

Feature (org.gdal.ogr.Feature)4 LineString (com.revolsys.geometry.model.LineString)3 RecordDefinition (com.revolsys.record.schema.RecordDefinition)3 Layer (org.gdal.ogr.Layer)3 FeatureDefn (org.gdal.ogr.FeatureDefn)2 Geometry (com.revolsys.geometry.model.Geometry)1 Record (com.revolsys.record.Record)1 FieldDefinition (com.revolsys.record.schema.FieldDefinition)1 Date (java.sql.Date)1 Calendar (java.util.Calendar)1 GregorianCalendar (java.util.GregorianCalendar)1 NoSuchElementException (java.util.NoSuchElementException)1 DataSource (org.gdal.ogr.DataSource)1