Java Code Examples for mil.nga.geopackage.factory.GeoPackageFactory#getManager()

The following examples show how to use mil.nga.geopackage.factory.GeoPackageFactory#getManager() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestSetupTeardown.java    From geopackage-android-map with MIT License 6 votes vote down vote up
/**
 * Set up the import database
 * 
 * @param activity
 * @param testContext
 * @return
 */
public static GeoPackage setUpImport(Activity activity, Context testContext) {

	GeoPackageManager manager = GeoPackageFactory.getManager(activity);

	// Delete
	manager.delete(TestConstants.IMPORT_DB_NAME);

	// Copy the test db file from assets to the internal storage
	TestUtils.copyAssetFileToInternalStorage(activity, testContext,
			TestConstants.IMPORT_DB_FILE_NAME);

	// Import
	String importLocation = TestUtils.getAssetFileInternalStorageLocation(
			activity, TestConstants.IMPORT_DB_FILE_NAME);
	manager.importGeoPackage(new File(importLocation));

	// Open
	GeoPackage geoPackage = manager.open(TestConstants.IMPORT_DB_NAME);
	if (geoPackage == null) {
		throw new GeoPackageException("Failed to open database");
	}

	return geoPackage;
}
 
Example 2
Source File: ImportCoverageDataTiffGeoPackageTestCase.java    From geopackage-android with MIT License 6 votes vote down vote up
@Override
protected GeoPackage getGeoPackage() throws Exception {

    GeoPackageManager manager = GeoPackageFactory.getManager(activity);

    // Delete
    manager.delete(TestConstants.IMPORT_COVERAGE_DATA_TIFF_DB_NAME);

    // Copy the test db file from assets to the internal storage
    TestUtils.copyAssetFileToInternalStorage(activity, testContext,
            TestConstants.IMPORT_COVERAGE_DATA_TIFF_DB_FILE_NAME);

    // Import
    String importLocation = TestUtils.getAssetFileInternalStorageLocation(
            activity, TestConstants.IMPORT_COVERAGE_DATA_TIFF_DB_FILE_NAME);
    manager.importGeoPackage(new File(importLocation));

    // Open
    GeoPackage geoPackage = manager.open(TestConstants.IMPORT_COVERAGE_DATA_TIFF_DB_NAME);
    if (geoPackage == null) {
        throw new GeoPackageException("Failed to open database");
    }

    return geoPackage;
}
 
Example 3
Source File: ImportCoverageDataGeoPackageTestCase.java    From geopackage-android with MIT License 6 votes vote down vote up
@Override
protected GeoPackage getGeoPackage() throws Exception {

    GeoPackageManager manager = GeoPackageFactory.getManager(activity);

    // Delete
    manager.delete(TestConstants.IMPORT_COVERAGE_DATA_DB_NAME);

    // Copy the test db file from assets to the internal storage
    TestUtils.copyAssetFileToInternalStorage(activity, testContext,
            TestConstants.IMPORT_COVERAGE_DATA_DB_FILE_NAME);

    // Import
    String importLocation = TestUtils.getAssetFileInternalStorageLocation(
            activity, TestConstants.IMPORT_COVERAGE_DATA_DB_FILE_NAME);
    manager.importGeoPackage(new File(importLocation));

    // Open
    GeoPackage geoPackage = manager.open(TestConstants.IMPORT_COVERAGE_DATA_DB_NAME);
    if (geoPackage == null) {
        throw new GeoPackageException("Failed to open database");
    }

    return geoPackage;
}
 
Example 4
Source File: TestSetupTeardown.java    From geopackage-android with MIT License 5 votes vote down vote up
/**
 * Tear down the create database
 *
 * @param activity
 * @param geoPackage
 */
public static void tearDownCreate(Activity activity, GeoPackage geoPackage) {

    // Close
    if (geoPackage != null) {
        geoPackage.close();
    }

    // Delete
    GeoPackageManager manager = GeoPackageFactory.getManager(activity);
    manager.delete(TestConstants.TEST_DB_NAME);
}
 
Example 5
Source File: TileOverlayPreferenceActivity.java    From mage-android with Apache License 2.0 5 votes vote down vote up
private void saveGeopackageLayers(Collection<Layer> remoteLayers) {
    Context context = getActivity().getApplicationContext();
    LayerHelper layerHelper = LayerHelper.getInstance(context);
    try {
        // get local layers
        Collection<Layer> localLayers = layerHelper.readAll("GeoPackage");

        Map<String, Layer> remoteIdToLayer = new HashMap<>(localLayers.size());
        for(Layer layer : localLayers){
            remoteIdToLayer.put(layer.getRemoteId(), layer);
        }

        GeoPackageManager manager = GeoPackageFactory.getManager(context);
        for (Layer remoteLayer : remoteLayers) {
            // Check if its loaded
            File file = new File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS),
                    String.format("MAGE/geopackages/%s/%s", remoteLayer.getRemoteId(), remoteLayer.getFileName()));
            if (file.exists() && manager.existsAtExternalFile(file)) {
                remoteLayer.setLoaded(true);
            }
            if(!localLayers.contains(remoteLayer)) {
                layerHelper.create(remoteLayer);
            }else {
                Layer localLayer = remoteIdToLayer.get(remoteLayer.getRemoteId());
                //Only remove a local layer if the even has changed
                if (!remoteLayer.getEvent().equals(localLayer.getEvent())) {
                    layerHelper.delete(localLayer.getId());
                    layerHelper.create(remoteLayer);
                }
            }
        }
    } catch (LayerException e) {
        Log.e(LOG_NAME, "Error saving geopackage layers", e);
    }
}
 
Example 6
Source File: GeoPackageExample.java    From geopackage-android with MIT License 5 votes vote down vote up
/**
 * Test making the GeoPackage example
 *
 * @throws SQLException upon error
 * @throws IOException  upon error
 */
@Test
public void testExample() throws SQLException, IOException, NameNotFoundException {

    create();

    GeoPackageManager manager = GeoPackageFactory.getManager(activity);
    GeoPackage geoPackage = manager.open(GEOPACKAGE_NAME);
    TestCase.assertNotNull(geoPackage);
    geoPackage.close();

    TestCase.assertTrue(manager.delete(GEOPACKAGE_NAME));
}
 
Example 7
Source File: GeopackageFeatureTilesOverlay.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
public GeopackageFeatureTilesOverlay(GeoPackageFeatureTileProvider provider, final Context pContext) {
    super(provider, pContext);
    Log.i(IMapView.LOGTAG, "Geopackage support is BETA. Please report any issues");
    this.ctx = pContext;

    this.provider = provider;
    // Get a manager
    manager = GeoPackageFactory.getManager(pContext);

    // Available databases
    databases = manager.databases();
}
 
Example 8
Source File: GeoPackageDownloadManager.java    From mage-android with Apache License 2.0 5 votes vote down vote up
private void loadGeopackage(long downloadId, GeoPackageDownloadListener listener) {
    synchronized (downloadLock) {
        try {
            Layer layer = layerHelper.getByDownloadId(downloadId);

            if (layer != null) {
                String relativePath = getRelativePath(layer);
                GeoPackageManager geoPackageManager = GeoPackageFactory.getManager(context);
                CacheProvider cacheProvider = CacheProvider.getInstance(context);
                File cache = new File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), relativePath);
                CacheOverlay overlay = cacheProvider.getGeoPackageCacheOverlay(context, cache, geoPackageManager);
                if (overlay != null) {
                    cacheProvider.addCacheOverlay(overlay);
                }

                layer.setRelativePath(relativePath);
                layer.setLoaded(true);
                layerHelper.update(layer);

                if (listener != null) {
                    listener.onGeoPackageDownloaded(layer, overlay);
                }
            }
        } catch (LayerException e) {
            Log.e(LOG_NAME, "Error saving layer", e);
        }
    }
}
 
Example 9
Source File: LoadGeoPackageTestCase.java    From geopackage-android with MIT License 5 votes vote down vote up
@After
public void tearDown() throws Exception {

    // Close
    if (geoPackage != null) {
        geoPackage.close();
    }

    // Delete
    GeoPackageManager manager = GeoPackageFactory.getManager(activity);
    manager.delete(name);

}
 
Example 10
Source File: GeoPackageMapTileModuleProvider.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
public GeoPackageMapTileModuleProvider(File[] pFile,
                                       final Context context, IFilesystemCache cache) {
    //int pThreadPoolSize, final int pPendingQueueSize
    super(Configuration.getInstance().getTileFileSystemThreads(), Configuration.getInstance().getTileFileSystemMaxQueueSize());
    Log.i(IMapView.LOGTAG, "Geopackage support is BETA. Please report any issues");
    tileWriter = cache;
    // Get a manager
    manager = GeoPackageFactory.getManager(context);
    // Available databases


    // Import database
    for (int i = 0; i < pFile.length; i++) {
        try {
            manager.importGeoPackage((pFile[i]));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    // Available databases
    List<String> databases = manager.databases();
    // Open database
    for (int i = 0; i < databases.size(); i++) {
        tileSources.add(manager.open(databases.get(i)));
    }

}
 
Example 11
Source File: TestSetupTeardown.java    From geopackage-android-map with MIT License 5 votes vote down vote up
/**
 * Tear down the create database
 * 
 * @param activity
 * @param geoPackage
 */
public static void tearDownCreate(Activity activity, GeoPackage geoPackage) {

	// Close
	if (geoPackage != null) {
		geoPackage.close();
	}

	// Delete
	GeoPackageManager manager = GeoPackageFactory.getManager(activity);
	manager.delete(TestConstants.TEST_DB_NAME);
}
 
Example 12
Source File: TestSetupTeardown.java    From geopackage-android with MIT License 4 votes vote down vote up
/**
 * Set up the create database
 *
 * @param activity
 * @param testContext
 * @param features
 * @param allowEmptyFeatures
 * @param tiles
 * @return
 * @throws SQLException
 * @throws IOException
 * @parma name
 */
public static GeoPackage setUpCreate(Activity activity,
                                     Context testContext, String name, boolean features, boolean allowEmptyFeatures, boolean tiles)
        throws SQLException, IOException {

    GeoPackageManager manager = GeoPackageFactory.getManager(activity);

    // Delete
    manager.delete(name);

    // Create
    manager.create(name);

    // Open
    GeoPackage geoPackage = manager.open(name);
    if (geoPackage == null) {
        throw new GeoPackageException("Failed to open database");
    }

    TestCase.assertEquals("Application Id", geoPackage.getApplicationId(), GeoPackageConstants.APPLICATION_ID);
    TestCase.assertEquals("User Version", geoPackage.getUserVersion(), GeoPackageConstants.USER_VERSION);
    String userVersionString = String.valueOf(geoPackage.getUserVersion());
    String majorVersion = userVersionString.substring(0, userVersionString.length() - 4);
    String minorVersion = userVersionString.substring(userVersionString.length() - 4, userVersionString.length() - 2);
    String patchVersion = userVersionString.substring(userVersionString.length() - 2);
    TestCase.assertEquals("Major User Version", geoPackage.getUserVersionMajor(), Integer.valueOf(majorVersion).intValue());
    TestCase.assertEquals("Minor User Version", geoPackage.getUserVersionMinor(), Integer.valueOf(minorVersion).intValue());
    TestCase.assertEquals("Patch User Version", geoPackage.getUserVersionPatch(), Integer.valueOf(patchVersion).intValue());

    if (features) {
        setUpCreateFeatures(geoPackage, allowEmptyFeatures);
    }

    if (tiles) {
        setUpCreateTiles(testContext, geoPackage);
    }

    setUpCreateCommon(geoPackage);

    return geoPackage;
}
 
Example 13
Source File: LoadTilesTask.java    From geopackage-mapcache-android with MIT License 4 votes vote down vote up
/**
 * Load tiles from a URL
 *
 * @param activity
 * @param callback
 * @param active
 * @param database
 * @param tableName
 * @param tileUrl
 * @param minZoom
 * @param maxZoom
 * @param compressFormat
 * @param compressQuality
 * @param googleTiles
 * @param boundingBox
 * @param scaling
 * @param authority
 * @param code
 */
public static void loadTiles(Activity activity, ILoadTilesTask callback,
                             GeoPackageDatabases active, String database, String tableName,
                             String tileUrl, int minZoom, int maxZoom,
                             CompressFormat compressFormat, Integer compressQuality,
                             boolean googleTiles, BoundingBox boundingBox, TileScaling scaling, String authority, String code) {

    GeoPackageManager manager = GeoPackageFactory.getManager(activity);
    GeoPackage geoPackage = manager.open(database);

    Projection projection = ProjectionFactory.getProjection(authority, code);
    BoundingBox bbox = transform(boundingBox, projection);

    TileGenerator tileGenerator = new UrlTileGenerator(activity, geoPackage,
            tableName, tileUrl, minZoom, maxZoom, bbox, projection);
    setTileGenerator(activity, tileGenerator, minZoom, maxZoom, compressFormat, compressQuality, googleTiles, boundingBox, scaling);

    loadTiles(activity, callback, active, geoPackage, tableName, tileGenerator);
}
 
Example 14
Source File: MapFragment.java    From mage-android with Apache License 2.0 4 votes vote down vote up
@Override
public View onCreateView(@NonNull LayoutInflater  inflater, ViewGroup container, Bundle savedInstanceState) {
	View view = inflater.inflate(R.layout.fragment_map, container, false);

	setHasOptionsMenu(true);

	staticGeometryCollection = new StaticGeometryCollection();

	availableLayerDownloadsIcon = view.findViewById(R.id.available_layer_downloads);
	zoomToLocationButton = view.findViewById(R.id.zoom_button);

	compassButton = view.findViewById(R.id.compass_button);
	compassButton.setOnClickListener(v -> resetMapBearing());

	searchButton = view.findViewById(R.id.map_search_button);
	if (Geocoder.isPresent()) {
		searchButton.setOnClickListener(v -> search());
	} else {
		searchButton.hide();
	}

	view.findViewById(R.id.new_observation_button).setOnClickListener(v -> onNewObservation());

	searchLayout = view.findViewById(R.id.search_layout);
	searchView = view.findViewById(R.id.search_view);
	searchView.setIconifiedByDefault(false);
	searchView.setIconified(false);
	searchView.clearFocus();

	MapsInitializer.initialize(context);

	ImageButton mapSettings = view.findViewById(R.id.map_settings);
	mapSettings.setOnClickListener(this);

	mapView = view.findViewById(R.id.mapView);
	Bundle mapState = (savedInstanceState != null) ? savedInstanceState.getBundle(MAP_VIEW_STATE): null;
	mapView.onCreate(mapState);

	mgrsBottomSheet = view.findViewById(R.id.mgrs_bottom_sheet);
	mgrsBottomSheetBehavior = BottomSheetBehavior.from(mgrsBottomSheet);
	mgrsCursor = view.findViewById(R.id.mgrs_grid_cursor);
	mgrsTextView = mgrsBottomSheet.findViewById(R.id.mgrs_code);
	mgrsGzdTextView = mgrsBottomSheet.findViewById(R.id.mgrs_gzd_zone);
	mgrs100dKmTextView = mgrsBottomSheet.findViewById(R.id.mgrs_grid_zone);
	mgrsEastingTextView = mgrsBottomSheet.findViewById(R.id.mgrs_easting);
	mgrsNorthingTextView = mgrsBottomSheet.findViewById(R.id.mgrs_northing);

	// Initialize the GeoPackage cache with a GeoPackage manager
	GeoPackageManager geoPackageManager = GeoPackageFactory.getManager(getActivity().getApplicationContext());
	geoPackageCache = new GeoPackageCache(geoPackageManager);

	locationProvider = locationPolicy.getBestLocationProvider();

	return view;
}
 
Example 15
Source File: GeoPackageExample.java    From geopackage-android with MIT License 4 votes vote down vote up
/**
 * Test the GeoPackage example NGA extensions
 *
 * @throws SQLException upon error
 * @throws IOException  upon error
 */
@Test
public void testExampleNGAExtensions() throws SQLException, IOException, NameNotFoundException {

    create();

    GeoPackageManager manager = GeoPackageFactory.getManager(activity);
    GeoPackage geoPackage = manager.open(GEOPACKAGE_NAME);

    validateExtensions(geoPackage, true);
    validateNGAExtensions(geoPackage, true);

    NGAExtensions.deleteExtensions(geoPackage);

    validateExtensions(geoPackage, true);
    validateNGAExtensions(geoPackage, false);

    geoPackage.close();

    TestCase.assertTrue(manager.delete(GEOPACKAGE_NAME));
}
 
Example 16
Source File: GeoPackageManagerTest.java    From geopackage-android with MIT License 4 votes vote down vote up
/**
 * Test creating a database GeoPackage file as an external link
 */
private void createExternal(int testCase) {

    GeoPackageManager manager = GeoPackageFactory.getManager(activity);

    // Verify does not exist
    assertFalse("Database already exists",
            manager.exists(TestConstants.IMPORT_DB_NAME));
    assertFalse("Database already returned in the set", manager
            .databaseSet().contains(TestConstants.IMPORT_DB_NAME));

    // Create
    File externalDirectory = activity.getExternalCacheDir();
    File createFile = new File(externalDirectory, TestConstants.IMPORT_DB_FILE_NAME);
    createFile.delete();
    createExternal(manager, externalDirectory, createFile, testCase);
    assertTrue("Database does not exist",
            manager.exists(TestConstants.IMPORT_DB_NAME));
    assertTrue("Database not returned in the set", manager.databaseSet()
            .contains(TestConstants.IMPORT_DB_NAME));
    assertTrue(manager.validate(TestConstants.IMPORT_DB_NAME));
    assertTrue(manager.validateHeader(TestConstants.IMPORT_DB_NAME));
    assertTrue(manager.validateIntegrity(TestConstants.IMPORT_DB_NAME));

    // Open
    GeoPackage geoPackage = manager.open(TestConstants.IMPORT_DB_NAME);
    assertNotNull("Failed to open database", geoPackage);
    geoPackage.close();

    // Attempt to create again
    try {
        createExternal(manager, externalDirectory, createFile, testCase);
        fail("Creating database again did not cause exception");
    } catch (GeoPackageException e) {
        // expected
    }

    // Delete
    assertTrue("Database not deleted",
            manager.delete(TestConstants.IMPORT_DB_NAME));
    assertFalse("Database exists after delete",
            manager.exists(TestConstants.IMPORT_DB_NAME));
    assertFalse("Database returned in the set after delete", manager
            .databaseSet().contains(TestConstants.IMPORT_DB_NAME));
    assertTrue("Create file could not be deleted", createFile.delete());

    // Import and Open with inverse validation
    manager.setImportHeaderValidation(!manager.isImportHeaderValidation());
    manager.setImportIntegrityValidation(!manager.isImportIntegrityValidation());
    manager.setOpenHeaderValidation(!manager.isOpenHeaderValidation());
    manager.setOpenIntegrityValidation(!manager.isOpenIntegrityValidation());

    // Create
    createExternal(manager, externalDirectory, createFile, testCase);
    assertTrue("Database does not exist",
            manager.exists(TestConstants.IMPORT_DB_NAME));
    assertTrue("Database not returned in the set", manager.databaseSet()
            .contains(TestConstants.IMPORT_DB_NAME));
    assertTrue(manager.validate(TestConstants.IMPORT_DB_NAME));
    assertTrue(manager.validateHeader(TestConstants.IMPORT_DB_NAME));
    assertTrue(manager.validateIntegrity(TestConstants.IMPORT_DB_NAME));

    // Open
    geoPackage = manager.open(TestConstants.IMPORT_DB_NAME);
    assertNotNull("Failed to open database", geoPackage);
    geoPackage.close();

    // Delete
    assertTrue("Database not deleted",
            manager.delete(TestConstants.IMPORT_DB_NAME));
    assertFalse("Database exists after delete",
            manager.exists(TestConstants.IMPORT_DB_NAME));
    assertFalse("Database returned in the set after delete", manager
            .databaseSet().contains(TestConstants.IMPORT_DB_NAME));

    // Delete the file
    assertTrue("Create file could not be deleted", createFile.delete());

}
 
Example 17
Source File: OAPIFeatureGeneratorTest.java    From geopackage-android with MIT License 4 votes vote down vote up
/**
 * Test a WFS server and create a GeoPackage
 *
 * @param server      server url
 * @param collection  collection name
 * @param name        geoPackage and table name
 * @param limit       request limit
 * @param totalLimit  total limit
 * @param boundingBox bounding box
 * @param time        time
 * @param period      period or end time
 * @throws SQLException upon error
 */
private void testServer(String server, String collection, String name,
                        Integer limit, Integer totalLimit, BoundingBox boundingBox,
                        String time, String period) throws SQLException {

    GeoPackageManager geoPackageManager = GeoPackageFactory.getManager(activity);

    geoPackageManager.delete(collection);

    geoPackageManager.create(collection);

    GeoPackage geoPackage = geoPackageManager.open(collection);

    OAPIFeatureGenerator generator = new OAPIFeatureGenerator(
            geoPackage, name, server, collection);
    generator.setLimit(limit);
    generator.setTotalLimit(totalLimit);
    generator.setBoundingBox(boundingBox);
    generator.setTime(time);
    generator.setPeriod(period);
    generator.setDownloadAttempts(3);

    int count = generator.generateFeatures();
    if (totalLimit != null) {
        TestCase.assertEquals(totalLimit.intValue(), count);
    }

    FeatureDao featureDao = generator.getFeatureDao();
    if (totalLimit != null) {
        TestCase.assertEquals(totalLimit.intValue(), featureDao.count());
    }

    FeatureIndexManager indexer = new FeatureIndexManager(activity, geoPackage, featureDao);
    indexer.setIndexLocation(FeatureIndexType.GEOPACKAGE);
    indexer.index();
    indexer.close();

    BoundingBox dataBounds = geoPackage
            .getBoundingBox(featureDao.getTableName());
    Contents contents = featureDao.getContents();
    contents.setBoundingBox(dataBounds);
    geoPackage.getContentsDao().update(contents);

    geoPackage.close();

}
 
Example 18
Source File: GeoPackageTestUtils.java    From geopackage-android with MIT License 4 votes vote down vote up
/**
 * Test the GeoPackage vacuum
 *
 * @param activity   activity
 * @param geoPackage GeoPackage
 */
public static void testVacuum(Activity activity, GeoPackage geoPackage) {

    GeoPackageManager manager = GeoPackageFactory.getManager(activity);
    long size = manager.size(geoPackage.getName());

    for (String table : geoPackage.getTables()) {

        geoPackage.deleteTable(table);

        geoPackage.vacuum();

        long newSize = manager.size(geoPackage.getName());
        TestCase.assertTrue(size > newSize);
        size = newSize;

    }

}
 
Example 19
Source File: GeoPackageExample.java    From geopackage-android with MIT License 3 votes vote down vote up
private static GeoPackage createGeoPackage(Context context) {

        GeoPackageManager manager = GeoPackageFactory.getManager(context);

        manager.delete(GEOPACKAGE_NAME);

        manager.create(GEOPACKAGE_NAME);

        GeoPackage geoPackage = manager.open(GEOPACKAGE_NAME);
        if (geoPackage == null) {
            throw new GeoPackageException("Failed to open database");
        }

        return geoPackage;
    }
 
Example 20
Source File: GeoPackageCacheUtils.java    From mage-android with Apache License 2.0 2 votes vote down vote up
/**
 * Import the GeoPackage file as an external link if it does not exist
 *
 * @param context
 * @param cacheFile
 * @return cache name when imported, null when not imported
 */
public static String importGeoPackage(Context context, File cacheFile) {
    GeoPackageManager manager = GeoPackageFactory.getManager(context);
    return importGeoPackage(manager, cacheFile);
}