Search code examples
androidkotlinandroid-sqliteqgisgeopackage

ngageoint/geopackage-android Writing in GPKG from android


I have a project where i have to export coordinates data into a GPKG with media as blob from my app, so it can be opened in QGIS. I have no experience at all in this field and have been battling for days to have something working.

My idea was to have an already constructed gpkg in my assets file, open it with ngageoint/geopackage-android (and ios) and fill the FeatureTable / FeatureRow / GeometryColumns.

So i have a GPKG ready with my 3 tables, and all the rest generated by QGIS.

I also have all the triggers: enter image description here

On Android i insert my feature rows like this:

private fun manageExportTrackPointsGpkg(geoPackage: GeoPackage, appPoint: AppPoint) {
   val featureDao = geoPackage.getFeatureDao("track_points")

   val point = Point(appPoint.latitude, appPoint.longitude)

   val newRow = featureDao.newRow()
   newRow.geometry = GeoPackageGeometryData.createAndWrite(point)
   /// other values...
   newRow.setValue("timeStamp", ...)
        
   featureDao.insert(newRow)
}

This part works (i think).

But the table gpkg_contents is not updated. So i do it manually:

val trackPointsFeatureDao = geoPackage.getFeatureDao("track_points")
val trackPointsContent = trackPointsFeatureDao.contents
trackPointsContent.lastChange = Date()
trackPointsContent.minY = pointMinY
trackPointsContent.maxY = pointMaxY
trackPointsContent.minX = pointMinX
trackPointsContent.maxX = pointMaxX
val update = geoPackage.contentsDao.update(trackPointsContent)

The table is now updated.

But the spatials tables' triggers are not working and thus the spatials data are not filled.

CREATE TRIGGER "rtree_track_points_geom_insert" AFTER INSERT ON "track_points" WHEN (new."geom" NOT NULL AND NOT ST_IsEmpty(NEW."geom")) BEGIN INSERT OR REPLACE INTO "rtree_track_points_geom" VALUES (NEW."fid",ST_MinX(NEW."geom"), ST_MaxX(NEW."geom"),ST_MinY(NEW."geom"), ST_MaxY(NEW."geom")); END

Apparently the ST_IsEmpty does not exists ?

So when i load my gpkg on QGIS nothing is displayed. I couldn't find any method to update the spatial tables, and the raw query is not working

(1) no such module: rtree in "INSERT INTO rtree_track_points_geom(minx, maxx, miny, maxy) VALUES (?, ?, ?, ?)"

I was unable to find a clear documentation on how to write into a GPKG with the library.

What am i doing wrong ? How to easily write into a GPKG ?

Thanks in advance.


Solution

  • I finally found the page where the library's creator have plenty example on how to create and fill a GPKG:

    https://github.com/ngageoint/geopackage-android/blob/134d0ddd1074f4de11b87ef8112530105f95f6ff/geopackage-sdk/src/androidTest/java/mil/nga/geopackage/test/GeoPackageExample.java

    Much easier to follow and it works !