Is there any way to load a geopackage file into any mapView in Java?

Is there any library to do this?

  • 1
    Check OpenJUMP and GeoTools. – user30184 Jun 24 '17 at 20:14

This is pretty easy in GeoTools as GeoPackages are just another datastore type. So here is a modified version of the QuickStart tutorial for a GeoPackage.

public static void main(String[] args) throws Exception {

    DataStore store = GeoPackageReader(args[0]);
    SimpleFeatureSource featureSource = store.getFeatureSource(store.getTypeNames()[0]);

    // Create a map content and add our shapefile to it
    MapContent map = new MapContent();

    Style style = SLD.createSimpleStyle(featureSource.getSchema());
    Layer layer = new FeatureLayer(featureSource, style);

    // Now display the map

  public static DataStore GeoPackageReader(String filename) throws IOException {
    HashMap<String, Object> map = new HashMap<>();
    map.put(GeoPkgDataStoreFactory.DBTYPE.key, "geopkg");
    map.put(GeoPkgDataStoreFactory.DATABASE.key, filename);

    DataStore store = DataStoreFinder.getDataStore(map);
    return store;


You can run through each of the feature types in the GeoPackage by looking at the typeNames returned from the store (rather than just taking the first as I have).

  • It seems to work but when I run your code and it reaches the line: SimpleFeatureSource featureSource = store.getFeatureSource (store.getTypeNames () [0]); I get "query does not return ResultSet" and when I start to trace, in this line of geotools GeoPackage class, is where the problem starts: ResultSet rs = st.executeQuery ("PRAGMA application_id")). Do you have any idea why I get that error? I have tried with several gpkg files and with none works. – Ofir Alfonso Cantillo Jul 3 '17 at 12:47
  • can you add a link to an example GeoPackage that doesn't work? – Ian Turton Jul 3 '17 at 13:21
  • For example I have tried with geopackage.org/data/states10.gpkg – Ofir Alfonso Cantillo Jul 3 '17 at 13:34
  • works for me - just set the filename to point to that file (as command line arg in my code) – Ian Turton Jul 3 '17 at 13:40
  • Reading the file is not the problem because when I execute another query on the file it returns a response, but when geotools executes the st.executeQuery ("PRAGMA application_id") statement it returns empty resultset – Ofir Alfonso Cantillo Jul 3 '17 at 13:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.