ngageoint / geopackage-android

GeoPackage Android Library
http://ngageoint.github.io/geopackage-android
MIT License
94 stars 32 forks source link

FeatureDao query #58

Closed sunshine0576 closed 4 years ago

sunshine0576 commented 5 years ago

The current query method is divided into two parts. First, the index is queried, and then the data is retrieved according to ID. If there are additional supplementary queries, each data need to be judged.

public FeatureCursor query(BoundingBox boundingBox, Projection projection, String where, String[] whereArgs) {
    String box_query =buildWhere(boundingBox, projection);
    where =box_query+"and"+where;
    return super.query(where, whereArgs);
}
sunshine0576 commented 5 years ago

spatialIndexClause=String.format("ROWID IN (SELECT $s FROM %s WHERE xmin < %s AND xmax > %s AND ymin < %s AND ymax > %s)", indexId,indexTable,minX,maxX,minY,maxY);

bosborn commented 5 years ago

The bounds of the features are not stored in the geometry columns table or the feature table. To query on bounds, an index is needed. The best way to query the feature table is to use the FeatureIndexManager. The default query order based upon first indexed type found: RTree, GeoPackage Index, Metadata Index, Manual. The results can be used to iterate through the Features, in the most efficient way for each index type. The FeatureIndexManager can also be used to index the table, however RTree in Android is currently read only.

String featureTable = ...
BoundingBox boundingBox = ...
Projection projection = ...

FeatureDao featureDao = geoPackage.getFeatureDao(featureTable);
FeatureIndexManager featureIndexManager = new FeatureIndexManager(activity,
                    geoPackage, featureDao);
// Or shortcut...
// FeatureIndexManager featureIndexManager = new FeatureIndexManager(activity,
                    geoPackage, featureTable);
FeatureIndexResults featureIndexResults = featureIndexManager.query(
                    boundingBox, projection);
// Or query(), query(BoundingBox), query(GeometryEnvelope)
for (FeatureRow featureRow : featureIndexResults) {
    // ...
}
featureIndexResults.close();
sunshine0576 commented 5 years ago

@bosborn You don't understand what I'm trying to say. select * from 【FeatureTable】where rowid in ( select rowid from 【IndexTable】 where xmin < %s AND xmax > %s AND ymin < %s AND ymax > %s )

If additional field queries are required in addition to range queries, I just need to solve the problem through an SQL statement. where =box_query+"and"+where;

bosborn commented 4 years ago

Take a look at the new 3.4.0 release API with added nested queries and query params (bounding box, geometry envelope, field values, where clause, where arguments)