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

The following examples show how to use mil.nga.geopackage.geom.GeoPackageGeometryData#setGeometry() . 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: FeatureTileUtils.java    From geopackage-android with MIT License 6 votes vote down vote up
public static long insertPolygon(FeatureDao featureDao, double[][]... points) {
    FeatureRow featureRow = featureDao.newRow();
    GeoPackageGeometryData geomData = new GeoPackageGeometryData(
            ProjectionConstants.EPSG_WORLD_GEODETIC_SYSTEM);
    Polygon polygon = new Polygon(false, false);
    for (double[][] ring : points) {
        LineString lineString = getLineString(ring);
        polygon.addRing(lineString);
    }
    geomData.setGeometry(polygon);
    featureRow.setGeometry(geomData);
    return featureDao.insert(featureRow);
}
 
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: TransactionTest.java    From geopackage-android with MIT License 5 votes vote down vote up
/**
 * Insert a row into the feature table
 *
 * @param featureDao feature dao
 */
private void insertRow(FeatureDao featureDao) {

    FeatureRow row = featureDao.newRow();
    GeoPackageGeometryData geometry = new GeoPackageGeometryData(
            featureDao.getGeometryColumns().getSrsId());
    geometry.setGeometry(new Point(0, 0));
    row.setGeometry(geometry);
    featureDao.insert(row);

}
 
Example 4
Source File: FeatureTileUtils.java    From geopackage-android with MIT License 5 votes vote down vote up
public static void setPoint(FeatureRow featureRow, double x, double y) {
    GeoPackageGeometryData geomData = new GeoPackageGeometryData(
            ProjectionConstants.EPSG_WORLD_GEODETIC_SYSTEM);
    Point point = new Point(false, false, x, y);
    geomData.setGeometry(point);
    featureRow.setGeometry(geomData);
}
 
Example 5
Source File: FeatureTileUtils.java    From geopackage-android with MIT License 5 votes vote down vote up
public static long insertLine(FeatureDao featureDao, double[][] points) {
    FeatureRow featureRow = featureDao.newRow();
    GeoPackageGeometryData geomData = new GeoPackageGeometryData(
            ProjectionConstants.EPSG_WORLD_GEODETIC_SYSTEM);
    LineString lineString = getLineString(points);
    geomData.setGeometry(lineString);
    featureRow.setGeometry(geomData);
    return featureDao.insert(featureRow);
}
 
Example 6
Source File: TransactionTest.java    From geopackage-java with MIT License 5 votes vote down vote up
/**
 * Insert a row into the feature table
 *
 * @param featureDao
 *            feature dao
 */
private void insertRow(FeatureDao featureDao) {

	FeatureRow row = featureDao.newRow();
	GeoPackageGeometryData geometry = new GeoPackageGeometryData(featureDao
			.getGeometryColumns().getSrsId());
	geometry.setGeometry(new Point(0, 0));
	row.setGeometry(geometry);
	featureDao.insert(row);

}
 
Example 7
Source File: FeatureTileUtils.java    From geopackage-java with MIT License 5 votes vote down vote up
public static void setPoint(FeatureRow featureRow, double x, double y) {
	GeoPackageGeometryData geomData = new GeoPackageGeometryData(
			ProjectionConstants.EPSG_WORLD_GEODETIC_SYSTEM);
	Point point = new Point(false, false, x, y);
	geomData.setGeometry(point);
	featureRow.setGeometry(geomData);
}
 
Example 8
Source File: FeatureTileUtils.java    From geopackage-java with MIT License 5 votes vote down vote up
public static long insertLine(FeatureDao featureDao, double[][] points) {
	FeatureRow featureRow = featureDao.newRow();
	GeoPackageGeometryData geomData = new GeoPackageGeometryData(
			ProjectionConstants.EPSG_WORLD_GEODETIC_SYSTEM);
	LineString lineString = getLineString(points);
	geomData.setGeometry(lineString);
	featureRow.setGeometry(geomData);
	return featureDao.insert(featureRow);
}
 
Example 9
Source File: FeatureTileUtils.java    From geopackage-java with MIT License 5 votes vote down vote up
public static long insertPolygon(FeatureDao featureDao,
		double[][]... points) {
	FeatureRow featureRow = featureDao.newRow();
	GeoPackageGeometryData geomData = new GeoPackageGeometryData(
			ProjectionConstants.EPSG_WORLD_GEODETIC_SYSTEM);
	Polygon polygon = new Polygon(false, false);
	for (double[][] ring : points) {
		LineString lineString = getLineString(ring);
		polygon.addRing(lineString);
	}
	geomData.setGeometry(polygon);
	featureRow.setGeometry(geomData);
	return featureDao.insert(featureRow);
}
 
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: 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();
		}
	}
}
 
Example 12
Source File: FeatureCoreGenerator.java    From geopackage-core-java with MIT License 3 votes vote down vote up
/**
 * Create the geometry data
 * 
 * @param geometry
 *            geometry
 * @return geometry data
 */
protected GeoPackageGeometryData createGeometryData(Geometry geometry) {
	GeoPackageGeometryData geometryData = new GeoPackageGeometryData(
			srs.getSrsId());
	geometryData.setGeometry(geometry);
	return geometryData;
}