Java Code Examples for mil.nga.geopackage.features.user.FeatureCursor#moveToNext()

The following examples show how to use mil.nga.geopackage.features.user.FeatureCursor#moveToNext() . 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: FeatureTableIndex.java    From geopackage-android with MIT License 5 votes vote down vote up
/**
 * Index the feature rows in the cursor
 *
 * @param tableIndex table index
 * @param cursor     feature cursor
 * @return count, -1 if no results or canceled
 */
private int indexRows(TableIndex tableIndex, FeatureCursor cursor) {

    int count = -1;

    try {
        while ((progress == null || progress.isActive())
                && cursor.moveToNext()) {
            if (count < 0) {
                count++;
            }
            try {
                FeatureRow row = cursor.getRow();
                if (row.isValid()) {
                    boolean indexed = index(tableIndex,
                            row.getId(), row.getGeometry());
                    if (indexed) {
                        count++;
                    }
                    if (progress != null) {
                        progress.addProgress(1);
                    }
                }
            } catch (Exception e) {
                Log.e(FeatureTableIndex.class.getSimpleName(), "Failed to index feature. Table: "
                        + tableIndex.getTableName() + ", Position: " + cursor.getPosition(), e);
            }
        }
    } finally {
        cursor.close();
    }

    return count;
}
 
Example 2
Source File: FeatureIndexer.java    From geopackage-android with MIT License 5 votes vote down vote up
/**
 * Index the feature rows in the cursor
 *
 * @param geoPackageId GeoPackage id
 * @param cursor       feature cursor
 * @return count, -1 if no results or canceled
 */
private int indexRows(long geoPackageId, FeatureCursor cursor) {

    int count = -1;

    try {
        while ((progress == null || progress.isActive())
                && cursor.moveToNext()) {
            if (count < 0) {
                count++;
            }
            try {
                FeatureRow row = cursor.getRow();
                if (row.isValid()) {
                    boolean indexed = index(geoPackageId, row, false);
                    if (indexed) {
                        count++;
                    }
                    if (progress != null) {
                        progress.addProgress(1);
                    }
                }
            } catch (Exception e) {
                Log.e(FeatureIndexer.class.getSimpleName(), "Failed to index feature. Table: "
                        + featureDao.getTableName() + ", Position: " + cursor.getPosition(), e);
            }
        }
    } finally {
        cursor.close();
    }

    return count;
}
 
Example 3
Source File: DefaultFeatureTiles.java    From geopackage-android with MIT License 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public Bitmap drawTile(int zoom, BoundingBox boundingBox, FeatureCursor cursor) {

    FeatureTileCanvas canvas = new FeatureTileCanvas(tileWidth, tileHeight);

    ProjectionTransform transform = getProjectionToWebMercatorTransform(featureDao.getProjection());
    BoundingBox expandedBoundingBox = expandBoundingBox(boundingBox);

    boolean drawn = false;
    while (cursor.moveToNext()) {
        FeatureRow row = cursor.getRow();
        if (drawFeature(zoom, boundingBox, expandedBoundingBox, transform, canvas, row)) {
            drawn = true;
        }
    }
    cursor.close();

    Bitmap bitmap = null;
    if (drawn) {
        bitmap = canvas.createBitmap();
        bitmap = checkIfDrawn(bitmap);
    } else {
        canvas.recycle();
    }

    return bitmap;
}
 
Example 4
Source File: GeoPackageExample.java    From geopackage-android with MIT License 5 votes vote down vote up
private static void createFeatureStylesGeometry2(GeoPackage geoPackage,
                                                 List<StyleRow> styles, List<IconRow> icons) throws IOException {

    FeatureDao featureDao = geoPackage.getFeatureDao("geometry2");
    FeatureTableStyles geometry2Styles = new FeatureTableStyles(geoPackage,
            featureDao.getTable());

    geometry2Styles.setTableStyle(GeometryType.POINT, styles.get(0));
    geometry2Styles.setTableStyle(GeometryType.LINESTRING, styles.get(1));
    geometry2Styles.setTableStyle(GeometryType.POLYGON, styles.get(0));
    geometry2Styles.setTableStyle(GeometryType.GEOMETRY, styles.get(2));

    geometry2Styles.createStyleRelationship();
    geometry2Styles.createIconRelationship();

    FeatureCursor features = featureDao.queryForAll();
    while (features.moveToNext()) {
        FeatureRow featureRow = features.getRow();
        switch (featureRow.getGeometryType()) {
            case POINT:
                geometry2Styles.setIcon(featureRow, icons.get(0));
                break;
            case LINESTRING:
                geometry2Styles.setStyle(featureRow, styles.get(0));
                break;
            case POLYGON:
                geometry2Styles.setStyle(featureRow, styles.get(1));
                break;
            default:
        }
    }
    features.close();

}
 
Example 5
Source File: GoogleMapShapeConverterUtils.java    From geopackage-android-map with MIT License 4 votes vote down vote up
/**
 * Test shapes
 * 
 * @param geoPackage
 * @throws SQLException
 */
public static void testShapes(GeoPackage geoPackage) throws SQLException {

	GeometryColumnsDao geometryColumnsDao = geoPackage
			.getGeometryColumnsDao();

	if (geometryColumnsDao.isTableExists()) {
		List<GeometryColumns> results = geometryColumnsDao.queryForAll();

		for (GeometryColumns geometryColumns : results) {

			FeatureDao dao = geoPackage.getFeatureDao(geometryColumns);

			GoogleMapShapeConverter converter = new GoogleMapShapeConverter(
					dao.getProjection());
			converter.setExteriorOrientation(null);
			converter.setHoleOrientation(null);

			// Query for all
			FeatureCursor cursor = dao.queryForAll();
			while (cursor.moveToNext()) {
				FeatureRow featureRow = cursor.getRow();

				GeoPackageGeometryData geometryData = featureRow
						.getGeometry();

				if (geometryData != null) {
					Geometry geometry = geometryData.getGeometry();
					GeometryType geometryType = geometry.getGeometryType();

					switch (geometryType) {
					case POINT:
						convertPoint(converter, (Point) geometry);
						break;
					case LINESTRING:
						convertLineString(converter, (LineString) geometry);
						break;
					case POLYGON:
						convertPolygon(converter, (Polygon) geometry);
						break;
					case MULTIPOINT:
						convertMultiPoint(converter, (MultiPoint) geometry);
						break;
					case MULTILINESTRING:
						convertMultiLineString(converter,
								(MultiLineString) geometry);
						break;
					case MULTIPOLYGON:
						convertMultiPolygon(converter,
								(MultiPolygon) geometry);
						break;
					case CIRCULARSTRING:
						convertLineString(converter,
								(CircularString) geometry);
						break;
					case COMPOUNDCURVE:
						convertCompoundCurve(converter,
								(CompoundCurve) geometry);
						break;
					case POLYHEDRALSURFACE:
						convertMultiPolygon(converter,
								(PolyhedralSurface) geometry);
						break;
					case TIN:
						convertMultiPolygon(converter, (TIN) geometry);
						break;
					case TRIANGLE:
						convertPolygon(converter, (Triangle) geometry);
						break;
					default:
					}
				}

			}
			cursor.close();

		}
	}

}
 
Example 6
Source File: GeoPackageExample.java    From geopackage-android with MIT License 4 votes vote down vote up
private static void insertRelatedTablesMediaExtensionRows(Activity activity, Context testContext,
                                                          GeoPackage geoPackage, ExtendedRelation relation, String query,
                                                          String name, String file, String contentType, String description,
                                                          String source) throws IOException {

    RelatedTablesExtension relatedTables = new RelatedTablesExtension(
            geoPackage);

    FeatureDao featureDao = geoPackage.getFeatureDao(relation
            .getBaseTableName());
    MediaDao mediaDao = relatedTables.getMediaDao(relation);
    UserMappingDao userMappingDao = relatedTables.getMappingDao(relation);

    MediaRow mediaRow = mediaDao.newRow();

    TestUtils.copyAssetFileToInternalStorage(activity, testContext, file);
    String mediaImageName = TestUtils.getAssetFileInternalStorageLocation(activity, file);
    Bitmap mediaImage = BitmapFactory.decodeFile(mediaImageName);

    mediaRow.setData(mediaImage, Bitmap.CompressFormat.PNG);
    mediaRow.setContentType(contentType);
    RelatedTablesUtils.populateUserRow(mediaDao.getTable(), mediaRow,
            MediaTable.requiredColumns());
    DublinCoreMetadata.setValue(mediaRow, DublinCoreType.TITLE, name);
    DublinCoreMetadata.setValue(mediaRow, DublinCoreType.DESCRIPTION,
            description);
    DublinCoreMetadata.setValue(mediaRow, DublinCoreType.SOURCE, source);
    long mediaRowId = mediaDao.create(mediaRow);

    FeatureCursor featureCursor = featureDao.queryForLike(
            TEXT_COLUMN, query);
    while (featureCursor.moveToNext()) {
        FeatureRow featureRow = featureCursor.getRow();
        UserMappingRow userMappingRow = userMappingDao.newRow();
        userMappingRow.setBaseId(featureRow.getId());
        userMappingRow.setRelatedId(mediaRowId);
        RelatedTablesUtils.populateUserRow(userMappingDao.getTable(),
                userMappingRow, UserMappingTable.requiredColumns());
        String featureName = featureRow.getValue(TEXT_COLUMN).toString();
        DublinCoreMetadata.setValue(userMappingRow, DublinCoreType.TITLE,
                featureName + " - " + name);
        DublinCoreMetadata.setValue(userMappingRow,
                DublinCoreType.DESCRIPTION, featureName + " - "
                        + description);
        DublinCoreMetadata.setValue(userMappingRow, DublinCoreType.SOURCE,
                source);
        userMappingDao.create(userMappingRow);
    }
    featureCursor.close();
}
 
Example 7
Source File: GeoPackageExample.java    From geopackage-android with MIT License 4 votes vote down vote up
private static void insertRelatedTablesFeaturesExtensionRows(
        GeoPackage geoPackage, ExtendedRelation relation) {

    RelatedTablesExtension relatedTables = new RelatedTablesExtension(
            geoPackage);
    UserMappingDao userMappingDao = relatedTables.getMappingDao(relation);

    FeatureDao featureDao1 = geoPackage.getFeatureDao(relation
            .getBaseTableName());
    FeatureDao featureDao2 = geoPackage.getFeatureDao(relation
            .getRelatedTableName());

    FeatureCursor featureCursor1 = featureDao1.queryForAll();
    while (featureCursor1.moveToNext()) {

        FeatureRow featureRow1 = featureCursor1.getRow();
        String featureName = featureRow1.getValue(TEXT_COLUMN).toString();

        FeatureCursor featureCursor2 = featureDao2.queryForEq(
                TEXT_COLUMN, featureName);
        while (featureCursor2.moveToNext()) {

            FeatureRow featureRow2 = featureCursor2.getRow();

            UserMappingRow userMappingRow = userMappingDao.newRow();
            userMappingRow.setBaseId(featureRow1.getId());
            userMappingRow.setRelatedId(featureRow2.getId());
            RelatedTablesUtils.populateUserRow(userMappingDao.getTable(),
                    userMappingRow, UserMappingTable.requiredColumns());
            DublinCoreMetadata.setValue(userMappingRow,
                    DublinCoreType.TITLE, featureName);
            DublinCoreMetadata.setValue(userMappingRow,
                    DublinCoreType.DESCRIPTION, featureName);
            DublinCoreMetadata.setValue(userMappingRow,
                    DublinCoreType.SOURCE, featureName);
            userMappingDao.create(userMappingRow);
        }
        featureCursor2.close();

    }
    featureCursor1.close();

}
 
Example 8
Source File: GeoPackageExample.java    From geopackage-android with MIT License 4 votes vote down vote up
private static void createRelatedTablesTilesExtension(
        GeoPackage geoPackage) {

    String featureTable = "point2";
    String tileTable = "nga";

    RelatedTablesExtension relatedTables = new RelatedTablesExtension(
            geoPackage);

    List<UserCustomColumn> additionalMappingColumns = RelatedTablesUtils
            .createAdditionalUserColumns();

    UserMappingTable userMappingTable = UserMappingTable.create(
            featureTable + "_" + tileTable, additionalMappingColumns);
    ExtendedRelation relation = relatedTables.addTilesRelationship(
            featureTable, tileTable, userMappingTable);

    UserMappingDao userMappingDao = relatedTables.getMappingDao(relation);

    FeatureDao featureDao = geoPackage
            .getFeatureDao(relation.getBaseTableName());
    TileDao tileDao = geoPackage.getTileDao(relation.getRelatedTableName());

    FeatureCursor featureCursor = featureDao.queryForAll();
    while (featureCursor.moveToNext()) {

        FeatureRow featureRow = featureCursor.getRow();
        String featureName = featureRow.getValue(TEXT_COLUMN).toString();

        TileCursor tileCursor = tileDao
                .queryForTile(tileDao.getMinZoom());
        while (tileCursor.moveToNext()) {

            TileRow tileRow = tileCursor.getRow();

            UserMappingRow userMappingRow = userMappingDao.newRow();
            userMappingRow.setBaseId(featureRow.getId());
            userMappingRow.setRelatedId(tileRow.getId());
            RelatedTablesUtils.populateUserRow(userMappingDao.getTable(),
                    userMappingRow, UserMappingTable.requiredColumns());
            DublinCoreMetadata.setValue(userMappingRow,
                    DublinCoreType.TITLE, featureName);
            DublinCoreMetadata.setValue(userMappingRow,
                    DublinCoreType.DESCRIPTION, featureName);
            DublinCoreMetadata.setValue(userMappingRow,
                    DublinCoreType.SOURCE, featureName);
            userMappingDao.create(userMappingRow);
        }
        tileCursor.close();

    }
    featureCursor.close();

}
 
Example 9
Source File: GeoPackageExample.java    From geopackage-android with MIT License 4 votes vote down vote up
private static void createFeatureStylesGeometry1(GeoPackage geoPackage,
                                                 List<StyleRow> styles, List<IconRow> icons) throws IOException {

    FeatureDao featureDao = geoPackage.getFeatureDao("geometry1");
    FeatureTableStyles geometry1Styles = new FeatureTableStyles(geoPackage,
            featureDao.getTable());

    geometry1Styles.setTableStyleDefault(styles.get(0));
    geometry1Styles.setTableStyle(GeometryType.POLYGON, styles.get(1));
    geometry1Styles.setTableStyle(GeometryType.POINT, styles.get(2));

    geometry1Styles.createStyleRelationship();
    geometry1Styles.createIconRelationship();

    int pointCount = 0;
    int lineCount = 0;
    int polygonCount = 0;

    FeatureCursor features = featureDao.queryForAll();
    while (features.moveToNext()) {
        FeatureRow featureRow = features.getRow();
        switch (featureRow.getGeometryType()) {
            case POINT:
                pointCount++;
                switch (pointCount) {
                    case 1:
                        geometry1Styles.setIcon(featureRow, icons.get(0));
                        break;
                    case 2:
                        geometry1Styles.setIcon(featureRow, icons.get(1));
                        break;
                    case 3:
                        geometry1Styles.setIcon(featureRow, icons.get(2));
                        break;
                }
                break;
            case LINESTRING:
                lineCount++;
                switch (lineCount) {
                    case 2:
                        geometry1Styles.setStyle(featureRow, styles.get(1));
                        break;
                    case 3:
                        geometry1Styles.setStyle(featureRow, styles.get(2));
                        break;
                }
                break;
            case POLYGON:
                polygonCount++;
                switch (polygonCount) {
                    case 2:
                        geometry1Styles.setStyle(featureRow, styles.get(3));
                        break;
                    case 3:
                        geometry1Styles.setStyle(featureRow, styles.get(2));
                        break;
                }
                break;
            default:
        }
    }
    features.close();

}
 
Example 10
Source File: GeoPackageGeometryDataUtils.java    From geopackage-android with MIT License 4 votes vote down vote up
/**
 * Test transforming geometries between projections
 *
 * @param geoPackage
 * @throws SQLException
 * @throws IOException
 */
public static void testGeometryProjectionTransform(GeoPackage geoPackage)
        throws SQLException, IOException {

    GeometryColumnsDao geometryColumnsDao = geoPackage
            .getGeometryColumnsDao();

    if (geometryColumnsDao.isTableExists()) {
        List<GeometryColumns> results = geometryColumnsDao.queryForAll();

        for (GeometryColumns geometryColumns : results) {

            FeatureDao dao = geoPackage.getFeatureDao(geometryColumns);
            TestCase.assertNotNull(dao);

            FeatureCursor cursor = dao.queryForAll();

            while (cursor.moveToNext()) {

                GeoPackageGeometryData geometryData = cursor.getGeometry();
                if (geometryData != null) {

                    Geometry geometry = geometryData.getGeometry();

                    if (geometry != null) {

                        SpatialReferenceSystemDao srsDao = geoPackage
                                .getSpatialReferenceSystemDao();
                        long srsId = geometryData.getSrsId();
                        SpatialReferenceSystem srs = srsDao
                                .queryForId(srsId);

                        long epsg = srs.getOrganizationCoordsysId();
                        Projection projection = srs.getProjection();
                        long toEpsg = -1;
                        if (epsg == ProjectionConstants.EPSG_WORLD_GEODETIC_SYSTEM) {
                            toEpsg = ProjectionConstants.EPSG_WEB_MERCATOR;
                        } else {
                            toEpsg = ProjectionConstants.EPSG_WORLD_GEODETIC_SYSTEM;
                        }
                        ProjectionTransform transformTo = projection
                                .getTransformation(toEpsg);
                        ProjectionTransform transformFrom = srs.getTransformation(transformTo
                                .getToProjection());

                        byte[] bytes = geometryData.getWkbBytes();

                        Geometry projectedGeometry = transformTo
                                .transform(geometry);
                        GeoPackageGeometryData projectedGeometryData = new GeoPackageGeometryData(
                                -1);
                        projectedGeometryData
                                .setGeometry(projectedGeometry);
                        projectedGeometryData.toBytes();
                        byte[] projectedBytes = projectedGeometryData
                                .getWkbBytes();

                        if (epsg > 0) {
                            TestCase.assertFalse(equalByteArrays(bytes,
                                    projectedBytes));
                        }

                        Geometry restoredGeometry = transformFrom
                                .transform(projectedGeometry);

                        compareGeometries(geometry, restoredGeometry, .001);
                    }
                }

            }
            cursor.close();
        }
    }
}
 
Example 11
Source File: MapFragment.java    From mage-android with Apache License 2.0 4 votes vote down vote up
/**
 * Add the GeoPackage Feature Table Cache Overlay
 * @param enabledCacheOverlays
 * @param featureTableCacheOverlay
 * @param geoPackage
 */
private void addGeoPackageFeatureCacheOverlay(Map<String, CacheOverlay> enabledCacheOverlays, GeoPackageFeatureTableCacheOverlay featureTableCacheOverlay, GeoPackage geoPackage){
	// Retrieve the cache overlay if it already exists (and remove from cache overlays)
	CacheOverlay cacheOverlay = cacheOverlays.remove(featureTableCacheOverlay.getCacheName());
	if(cacheOverlay != null){
		// If the existing cache overlay is being replaced, create a new cache overlay
		if(featureTableCacheOverlay.getParent().isAdded()){
			cacheOverlay = null;
		}
		for(GeoPackageTileTableCacheOverlay linkedTileTable: featureTableCacheOverlay.getLinkedTileTables()){
			cacheOverlays.remove(linkedTileTable.getCacheName());
		}
	}
	if(cacheOverlay == null) {
		// Add the features to the map
		FeatureDao featureDao = geoPackage.getFeatureDao(featureTableCacheOverlay.getName());

		// If indexed, add as a tile overlay
		if(featureTableCacheOverlay.isIndexed()){
			FeatureTiles featureTiles = new DefaultFeatureTiles(getActivity(), geoPackage, featureDao,
					getResources().getDisplayMetrics().density);
			Integer maxFeaturesPerTile = null;
			if(featureDao.getGeometryType() == GeometryType.POINT){
				maxFeaturesPerTile = getResources().getInteger(R.integer.geopackage_feature_tiles_max_points_per_tile);
			}else{
				maxFeaturesPerTile = getResources().getInteger(R.integer.geopackage_feature_tiles_max_features_per_tile);
			}
			featureTiles.setMaxFeaturesPerTile(maxFeaturesPerTile);
			NumberFeaturesTile numberFeaturesTile = new NumberFeaturesTile(getActivity());
			// Adjust the max features number tile draw paint attributes here as needed to
			// change how tiles are drawn when more than the max features exist in a tile
			featureTiles.setMaxFeaturesTileDraw(numberFeaturesTile);
			// Adjust the feature tiles draw paint attributes here as needed to change how
			// features are drawn on tiles
			FeatureOverlay featureOverlay = new FeatureOverlay(featureTiles);
			featureOverlay.setMinZoom(featureTableCacheOverlay.getMinZoom());

			// Get the tile linked overlay
			BoundedOverlay overlay = GeoPackageOverlayFactory.getLinkedFeatureOverlay(featureOverlay, geoPackage);

			FeatureOverlayQuery featureOverlayQuery = new FeatureOverlayQuery(getActivity(), overlay, featureTiles);
			featureTableCacheOverlay.setFeatureOverlayQuery(featureOverlayQuery);
			TileOverlayOptions overlayOptions = createFeatureTileOverlayOptions(overlay);
			TileOverlay tileOverlay = map.addTileOverlay(overlayOptions);
			featureTableCacheOverlay.setTileOverlay(tileOverlay);
		}
		// Not indexed, add the features to the map
		else {
			int maxFeaturesPerTable = 0;
			if(featureDao.getGeometryType() == GeometryType.POINT){
				maxFeaturesPerTable = getResources().getInteger(R.integer.geopackage_features_max_points_per_table);
			}else{
				maxFeaturesPerTable = getResources().getInteger(R.integer.geopackage_features_max_features_per_table);
			}
			Projection projection = featureDao.getProjection();
			GoogleMapShapeConverter shapeConverter = new GoogleMapShapeConverter(projection);
			FeatureCursor featureCursor = featureDao.queryForAll();
			try {
				final int totalCount = featureCursor.getCount();
				int count = 0;
				while (featureCursor.moveToNext()) {
					try {
						FeatureRow featureRow = featureCursor.getRow();
						GeoPackageGeometryData geometryData = featureRow.getGeometry();
						if (geometryData != null && !geometryData.isEmpty()) {
							Geometry geometry = geometryData.getGeometry();
							if (geometry != null) {
								GoogleMapShape shape = shapeConverter.toShape(geometry);
								// Set the Shape Marker, PolylineOptions, and PolygonOptions here if needed to change color and style
								featureTableCacheOverlay.addShapeToMap(featureRow.getId(), shape, map);

								if (++count >= maxFeaturesPerTable) {
									if (count < totalCount) {
										Toast.makeText(getActivity().getApplicationContext(), featureTableCacheOverlay.getCacheName()
												+ "- added " + count + " of " + totalCount, Toast.LENGTH_LONG).show();
									}
									break;
								}
							}
						}
					}catch(Exception e){
						Log.e(LOG_NAME, "Failed to display feature. GeoPackage: " + geoPackage.getName()
								+ ", Table: " + featureDao.getTableName() + ", Row: " + featureCursor.getPosition(), e);
					}
				}
			} finally {
				featureCursor.close();
			}
		}

		cacheOverlay = featureTableCacheOverlay;
	}

	// Add the cache overlay to the enabled cache overlays
	enabledCacheOverlays.put(cacheOverlay.getCacheName(), cacheOverlay);
}