Search in sources :

Example 1 with DataSource

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

the class OgrRecordStore method newDataSource.

protected DataSource newDataSource(final boolean update) {
    final String path = FileUtil.getCanonicalPath(this.file);
    DataSource dataSource;
    if (this.file.exists()) {
        dataSource = ogr.Open(path, update);
    } else {
        final Driver driver = ogr.GetDriverByName(this.driverName);
        dataSource = driver.CreateDataSource(path);
    }
    return dataSource;
}
Also used : Driver(org.gdal.ogr.Driver) DataSource(org.gdal.ogr.DataSource)

Example 2 with DataSource

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

the class OgrQueryIterator method initDo.

@Override
protected synchronized void initDo() {
    if (this.recordStore != null) {
        final DataSource dataSource = this.recordStore.getDataSource();
        if (dataSource != null) {
            final String sql = this.recordStore.getSql(this.query);
            this.layer = dataSource.ExecuteSQL(sql);
            this.recordStore.addLayerToClose(this.layer);
        }
    }
}
Also used : LineString(com.revolsys.geometry.model.LineString) DataSource(org.gdal.ogr.DataSource)

Example 3 with DataSource

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

the class OgrRecordStore method refreshSchemaElements.

@Override
protected synchronized Map<PathName, ? extends RecordStoreSchemaElement> refreshSchemaElements(final RecordStoreSchema schema) {
    final Map<PathName, RecordStoreSchemaElement> elementsByPath = new TreeMap<>();
    if (!isClosed()) {
        final DataSource dataSource = getDataSource();
        if (dataSource != null) {
            for (int layerIndex = 0; layerIndex < dataSource.GetLayerCount(); layerIndex++) {
                final Layer layer = dataSource.GetLayer(layerIndex);
                if (layer != null) {
                    try {
                        final RecordDefinitionImpl recordDefinition = newLayerRecordDefinition(schema, layer);
                        final PathName typePath = recordDefinition.getPathName();
                        final String layerName = layer.GetName();
                        this.layerNameToPathMap.put(layerName.toUpperCase(), typePath);
                        this.pathToLayerNameMap.put(typePath, layerName);
                        elementsByPath.put(typePath, recordDefinition);
                    } finally {
                        layer.delete();
                    }
                }
            }
        }
    }
    return elementsByPath;
}
Also used : RecordDefinitionImpl(com.revolsys.record.schema.RecordDefinitionImpl) PathName(com.revolsys.io.PathName) TreeMap(java.util.TreeMap) Layer(org.gdal.ogr.Layer) RecordStoreSchemaElement(com.revolsys.record.schema.RecordStoreSchemaElement) DataSource(org.gdal.ogr.DataSource)

Example 4 with DataSource

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

Aggregations

DataSource (org.gdal.ogr.DataSource)4 Layer (org.gdal.ogr.Layer)2 LineString (com.revolsys.geometry.model.LineString)1 PathName (com.revolsys.io.PathName)1 RecordDefinition (com.revolsys.record.schema.RecordDefinition)1 RecordDefinitionImpl (com.revolsys.record.schema.RecordDefinitionImpl)1 RecordStoreSchemaElement (com.revolsys.record.schema.RecordStoreSchemaElement)1 TreeMap (java.util.TreeMap)1 Driver (org.gdal.ogr.Driver)1 Feature (org.gdal.ogr.Feature)1