Search in sources :

Example 6 with Layer

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

use of org.gdal.ogr.Layer 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 8 with Layer

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

Layer (org.gdal.ogr.Layer)8 LineString (com.revolsys.geometry.model.LineString)4 RecordDefinition (com.revolsys.record.schema.RecordDefinition)4 Feature (org.gdal.ogr.Feature)3 PathName (com.revolsys.io.PathName)2 FieldDefinition (com.revolsys.record.schema.FieldDefinition)2 DataSource (org.gdal.ogr.DataSource)2 FeatureDefn (org.gdal.ogr.FeatureDefn)2 GeometryFactory (com.revolsys.geometry.model.GeometryFactory)1 RecordDefinitionImpl (com.revolsys.record.schema.RecordDefinitionImpl)1 RecordStoreSchema (com.revolsys.record.schema.RecordStoreSchema)1 RecordStoreSchemaElement (com.revolsys.record.schema.RecordStoreSchemaElement)1 TreeMap (java.util.TreeMap)1 SpatialReference (org.gdal.osr.SpatialReference)1