Search in sources :

Example 1 with GeometryFieldDefinition

use of com.revolsys.gis.esri.gdb.file.capi.type.GeometryFieldDefinition in project com.revolsys.open by revolsys.

the class FileGdbRecordStore method getRecordCount.

@Override
public int getRecordCount(final Query query) {
    if (query == null) {
        return 0;
    } else {
        synchronized (this.apiSync) {
            final Geodatabase geodatabase = getGeodatabase();
            if (geodatabase == null) {
                return 0;
            } else {
                try {
                    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 BoundingBox boundingBox = QueryValue.getBoundingBox(query);
                    if (boundingBox == null) {
                        final StringBuilder sql = new StringBuilder();
                        sql.append("SELECT OBJECTID FROM ");
                        sql.append(JdbcUtils.getTableName(typePath));
                        if (whereClause.length() > 0) {
                            sql.append(" WHERE ");
                            sql.append(whereClause);
                        }
                        try (final FileGdbEnumRowsIterator rows = query(sql.toString(), false)) {
                            int count = 0;
                            for (@SuppressWarnings("unused") final Row row : rows) {
                                count++;
                            }
                            return count;
                        }
                    } else {
                        final GeometryFieldDefinition geometryField = (GeometryFieldDefinition) recordDefinition.getGeometryField();
                        if (geometryField == null || boundingBox.isEmpty()) {
                            return 0;
                        } else {
                            final StringBuilder sql = new StringBuilder();
                            sql.append("SELECT " + geometryField.getName() + " FROM ");
                            sql.append(JdbcUtils.getTableName(typePath));
                            if (whereClause.length() > 0) {
                                sql.append(" WHERE ");
                                sql.append(whereClause);
                            }
                            try (final FileGdbEnumRowsIterator rows = query(sql.toString(), false)) {
                                int count = 0;
                                for (final Row row : rows) {
                                    final Geometry geometry = (Geometry) geometryField.getValue(row);
                                    if (geometry != null) {
                                        final BoundingBox geometryBoundingBox = geometry.getBoundingBox();
                                        if (geometryBoundingBox.intersects(boundingBox)) {
                                            count++;
                                        }
                                    }
                                }
                                return count;
                            }
                        }
                    }
                } finally {
                    releaseGeodatabase();
                }
            }
        }
    }
}
Also used : Geodatabase(com.revolsys.gis.esri.gdb.file.capi.swig.Geodatabase) Geometry(com.revolsys.geometry.model.Geometry) BoundingBox(com.revolsys.geometry.model.BoundingBox) GeometryFieldDefinition(com.revolsys.gis.esri.gdb.file.capi.type.GeometryFieldDefinition) VectorOfWString(com.revolsys.gis.esri.gdb.file.capi.swig.VectorOfWString) Row(com.revolsys.gis.esri.gdb.file.capi.swig.Row) RecordDefinition(com.revolsys.record.schema.RecordDefinition)

Aggregations

BoundingBox (com.revolsys.geometry.model.BoundingBox)1 Geometry (com.revolsys.geometry.model.Geometry)1 Geodatabase (com.revolsys.gis.esri.gdb.file.capi.swig.Geodatabase)1 Row (com.revolsys.gis.esri.gdb.file.capi.swig.Row)1 VectorOfWString (com.revolsys.gis.esri.gdb.file.capi.swig.VectorOfWString)1 GeometryFieldDefinition (com.revolsys.gis.esri.gdb.file.capi.type.GeometryFieldDefinition)1 RecordDefinition (com.revolsys.record.schema.RecordDefinition)1