Java Code Examples for mil.nga.geopackage.geom.GeoPackageGeometryData#getGeometry()

The following examples show how to use mil.nga.geopackage.geom.GeoPackageGeometryData#getGeometry() . 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: FeatureRowData.java    From geopackage-android with MIT License 6 votes vote down vote up
/**
 * Build a JSON compatible object
 *
 * @param includePoints     true to include point geometries, ignored if includeGeometries is true
 * @param includeGeometries true to include all geometry types
 * @return JSON compatible object
 */
public Object jsonCompatible(boolean includePoints, boolean includeGeometries) {

    Map<String, Object> jsonValues = new HashMap<>();

    for (String key : values.keySet()) {
        Object jsonValue = null;
        Object value = values.get(key);
        if (key.equals(geometryColumn)) {
            GeoPackageGeometryData geometryData = (GeoPackageGeometryData) value;
            if (geometryData.getGeometry() != null) {
                if (includeGeometries || (includePoints && geometryData.getGeometry().getGeometryType() == GeometryType.POINT)) {
                    jsonValue = FeatureConverter.toMap(geometryData.getGeometry());
                }
            } else {
                jsonValue = value;
            }
            if (jsonValue != null) {
                jsonValues.put(key, jsonValue);
            }
        }
    }

    return jsonValues;
}
 
Example 2
Source File: FeatureInfoBuilder.java    From geopackage-android-map with MIT License 5 votes vote down vote up
/**
 * Project the geometry into the provided projection
 *
 * @param geometryData geometry data
 * @param projection   projection
 */
public void projectGeometry(GeoPackageGeometryData geometryData, Projection projection) {

    if (geometryData.getGeometry() != null) {

        try {
            SpatialReferenceSystemDao srsDao = DaoManager.createDao(featureDao.getDb().getConnectionSource(), SpatialReferenceSystem.class);
            int srsId = geometryData.getSrsId();
            SpatialReferenceSystem srs = srsDao.queryForId((long) srsId);

            if (!projection.equals(srs.getOrganization(), srs.getOrganizationCoordsysId())) {

                Projection geomProjection = srs.getProjection();
                ProjectionTransform transform = geomProjection.getTransformation(projection);

                Geometry projectedGeometry = transform.transform(geometryData.getGeometry());
                geometryData.setGeometry(projectedGeometry);
                SpatialReferenceSystem projectionSrs = srsDao.getOrCreateCode(projection.getAuthority(), Long.parseLong(projection.getCode()));
                geometryData.setSrsId((int) projectionSrs.getSrsId());
            }
        } catch (SQLException e) {
            throw new GeoPackageException("Failed to project geometry to projection with Authority: "
                    + projection.getAuthority() + ", Code: " + projection.getCode(), e);
        }
    }

}
 
Example 3
Source File: FeatureRow.java    From geopackage-android with MIT License 5 votes vote down vote up
/**
 * Get the simple features geometry value
 *
 * @return geometry
 * @since 3.1.0
 */
public Geometry getGeometryValue() {
    GeoPackageGeometryData data = getGeometry();
    Geometry geometry = null;
    if (data != null) {
        geometry = data.getGeometry();
    }
    return geometry;
}
 
Example 4
Source File: FeatureIndexer.java    From geopackage-android with MIT License 5 votes vote down vote up
/**
 * Index the feature row
 *
 * @param geoPackageId   GeoPackage id
 * @param row            feature row
 * @param possibleUpdate possible update flag
 * @return true if indexed
 */
private boolean index(long geoPackageId, FeatureRow row, boolean possibleUpdate) {

    boolean indexed = false;

    GeoPackageGeometryData geomData = row.getGeometry();
    if (geomData != null) {

        // Get the envelope
        GeometryEnvelope envelope = geomData.getEnvelope();

        // If no envelope, build one from the geometry
        if (envelope == null) {
            Geometry geometry = geomData.getGeometry();
            if (geometry != null) {
                envelope = GeometryEnvelopeBuilder.buildEnvelope(geometry);
            }
        }

        // Create the new index row
        if (envelope != null) {
            GeometryMetadata metadata = geometryMetadataDataSource.populate(geoPackageId, featureDao.getTableName(), row.getId(), envelope);
            if (possibleUpdate) {
                geometryMetadataDataSource.createOrUpdate(metadata);
            } else {
                geometryMetadataDataSource.create(metadata);
            }
            indexed = true;
        }
    }

    return indexed;
}
 
Example 5
Source File: FeatureRow.java    From geopackage-java with MIT License 5 votes vote down vote up
/**
 * Get the simple features geometry value
 * 
 * @return geometry
 * @since 3.1.0
 */
public Geometry getGeometryValue() {
	GeoPackageGeometryData data = getGeometry();
	Geometry geometry = null;
	if (data != null) {
		geometry = data.getGeometry();
	}
	return geometry;
}
 
Example 6
Source File: FeatureInfoBuilder.java    From geopackage-android-map with MIT License 4 votes vote down vote up
/**
 * Fine filter the index results verifying the click location is within the tolerance of each feature row
 *
 * @param results       feature index results
 * @param tolerance     distance tolerance
 * @param clickLocation click location
 * @return filtered feature index results
 */
private FeatureIndexResults fineFilterResults(FeatureIndexResults results, double tolerance, LatLng clickLocation) {

    FeatureIndexResults filteredResults = null;
    if (ignoreGeometryTypes.contains(geometryType)) {
        filteredResults = new FeatureIndexListResults();
    } else if (clickLocation == null && ignoreGeometryTypes.isEmpty()) {
        filteredResults = results;
    } else {

        FeatureIndexListResults filteredListResults = new FeatureIndexListResults();

        GoogleMapShapeConverter converter = new GoogleMapShapeConverter(
                featureDao.getProjection());

        for (FeatureRow featureRow : results) {

            GeoPackageGeometryData geomData = featureRow.getGeometry();
            if (geomData != null) {
                Geometry geometry = geomData.getGeometry();
                if (geometry != null) {

                    if (!ignoreGeometryTypes.contains(geometry.getGeometryType())) {

                        if (clickLocation != null) {

                            GoogleMapShape mapShape = converter.toShape(geometry);
                            if (MapUtils.isPointOnShape(clickLocation, mapShape, geodesic, tolerance)) {

                                filteredListResults.addRow(featureRow);

                            }
                        } else {
                            filteredListResults.addRow(featureRow);
                        }

                    }
                }
            }

        }

        filteredResults = filteredListResults;
    }

    return filteredResults;
}
 
Example 7
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 8
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 9
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);
}
 
Example 10
Source File: GeoPackageGeometryDataUtils.java    From geopackage-java 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);

			FeatureResultSet 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();
		}
	}
}