Java Code Examples for mil.nga.sf.proj.ProjectionTransform#transform()

The following examples show how to use mil.nga.sf.proj.ProjectionTransform#transform() . 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: DefaultFeatureTiles.java    From geopackage-android with MIT License 6 votes vote down vote up
/**
 * Add the linestring to the path
 *
 * @param simplifyTolerance simplify tolerance in meters
 * @param boundingBox       bounding box
 * @param transform         projection transform
 * @param path              path
 * @param lineString        line string
 */
private void addLineString(double simplifyTolerance, BoundingBox boundingBox, ProjectionTransform transform, Path path, LineString lineString) {

    List<Point> points = lineString.getPoints();

    if (points.size() >= 2) {

        // Try to simplify the number of points in the LineString
        points = simplifyPoints(simplifyTolerance, points);

        for (int i = 0; i < points.size(); i++) {
            Point point = points.get(i);
            Point webMercatorPoint = transform.transform(point);
            float x = TileBoundingBoxUtils.getXPixel(tileWidth, boundingBox,
                    webMercatorPoint.getX());
            float y = TileBoundingBoxUtils.getYPixel(tileHeight, boundingBox,
                    webMercatorPoint.getY());
            if (i == 0) {
                path.moveTo(x, y);
            } else {
                path.lineTo(x, y);
            }
        }
    }
}
 
Example 2
Source File: DefaultFeatureTiles.java    From geopackage-android with MIT License 6 votes vote down vote up
/**
 * Add a ring
 *
 * @param simplifyTolerance simplify tolerance in meters
 * @param boundingBox       bounding box
 * @param transform         projection transform
 * @param path              path
 * @param points            points
 */
private void addRing(double simplifyTolerance, BoundingBox boundingBox, ProjectionTransform transform, Path path, List<Point> points) {

    // Try to simplify the number of points in the LineString
    points = simplifyPoints(simplifyTolerance, points);

    for (int i = 0; i < points.size(); i++) {
        Point point = points.get(i);
        Point webMercatorPoint = transform.transform(point);
        float x = TileBoundingBoxUtils.getXPixel(tileWidth, boundingBox,
                webMercatorPoint.getX());
        float y = TileBoundingBoxUtils.getYPixel(tileHeight, boundingBox,
                webMercatorPoint.getY());
        if (i == 0) {
            path.moveTo(x, y);
        } else {
            path.lineTo(x, y);
        }
    }
    path.close();
}
 
Example 3
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 4
Source File: FeatureTiles.java    From geopackage-android with MIT License 5 votes vote down vote up
/**
 * When the simplify tolerance is set, simplify the points to a similar
 * curve with fewer points.
 *
 * @param simplifyTolerance simplify tolerance in meters
 * @param points            ordered points
 * @return simplified points
 * @since 2.0.0
 */
protected List<Point> simplifyPoints(double simplifyTolerance,
                                     List<Point> points) {

    List<Point> simplifiedPoints = null;
    if (simplifyGeometries) {

        // Reproject to web mercator if not in meters
        if (projection != null && !projection.isUnit(Units.METRES)) {
            ProjectionTransform toWebMercator = projection
                    .getTransformation(WEB_MERCATOR_PROJECTION);
            points = toWebMercator.transform(points);
        }

        // Simplify the points
        simplifiedPoints = GeometryUtils.simplifyPoints(points,
                simplifyTolerance);

        // Reproject back to the original projection
        if (projection != null && !projection.isUnit(Units.METRES)) {
            ProjectionTransform fromWebMercator = WEB_MERCATOR_PROJECTION
                    .getTransformation(projection);
            simplifiedPoints = fromWebMercator.transform(simplifiedPoints);
        }
    } else {
        simplifiedPoints = points;
    }

    return simplifiedPoints;
}
 
Example 5
Source File: DefaultFeatureTiles.java    From geopackage-java with MIT License 5 votes vote down vote up
/**
 * Get the path of the line string
 *
 * @param simplifyTolerance
 *            simplify tolerance in meters
 * @param boundingBox
 * @param transform
 * @param lineString
 */
private Path2D getPath(double simplifyTolerance, BoundingBox boundingBox,
		ProjectionTransform transform, LineString lineString) {

	Path2D path = null;

	// Try to simplify the number of points in the LineString
	List<Point> lineStringPoints = simplifyPoints(simplifyTolerance,
			lineString.getPoints());

	for (Point point : lineStringPoints) {

		Point projectedPoint = transform.transform(point);

		float x = TileBoundingBoxUtils.getXPixel(tileWidth, boundingBox,
				projectedPoint.getX());
		float y = TileBoundingBoxUtils.getYPixel(tileHeight, boundingBox,
				projectedPoint.getY());

		if (path == null) {
			path = new Path2D.Double();
			path.moveTo(x, y);
		} else {
			path.lineTo(x, y);
		}

	}

	return path;
}
 
Example 6
Source File: FeatureTiles.java    From geopackage-java with MIT License 5 votes vote down vote up
/**
 * When the simplify tolerance is set, simplify the points to a similar
 * curve with fewer points.
 * 
 * @param simplifyTolerance
 *            simplify tolerance in meters
 * @param points
 *            ordered points
 * @return simplified points
 * @since 2.0.0
 */
protected List<Point> simplifyPoints(double simplifyTolerance,
		List<Point> points) {

	List<Point> simplifiedPoints = null;
	if (simplifyGeometries) {

		// Reproject to web mercator if not in meters
		if (projection != null && !projection.isUnit(Units.METRES)) {
			ProjectionTransform toWebMercator = projection
					.getTransformation(WEB_MERCATOR_PROJECTION);
			points = toWebMercator.transform(points);
		}

		// Simplify the points
		simplifiedPoints = GeometryUtils.simplifyPoints(points,
				simplifyTolerance);

		// Reproject back to the original projection
		if (projection != null && !projection.isUnit(Units.METRES)) {
			ProjectionTransform fromWebMercator = WEB_MERCATOR_PROJECTION
					.getTransformation(projection);
			simplifiedPoints = fromWebMercator.transform(simplifiedPoints);
		}
	} else {
		simplifiedPoints = points;
	}

	return simplifiedPoints;
}
 
Example 7
Source File: TileCreator.java    From geopackage-android with MIT License 4 votes vote down vote up
/**
 * Reproject the tile to the requested projection
 *
 * @param tile                    tile in the tile matrix projection
 * @param requestedTileWidth      requested tile width
 * @param requestedTileHeight     requested tile height
 * @param requestBoundingBox      request bounding box in the request projection
 * @param transformRequestToTiles transformation from request to tiles
 * @param tilesBoundingBox        request bounding box in the tile matrix projection
 * @return projected tile
 */
private Bitmap reprojectTile(Bitmap tile, int requestedTileWidth, int requestedTileHeight, BoundingBox requestBoundingBox, ProjectionTransform transformRequestToTiles, BoundingBox tilesBoundingBox) {

    final double requestedWidthUnitsPerPixel = (requestBoundingBox.getMaxLongitude() - requestBoundingBox.getMinLongitude()) / requestedTileWidth;
    final double requestedHeightUnitsPerPixel = (requestBoundingBox.getMaxLatitude() - requestBoundingBox.getMinLatitude()) / requestedTileHeight;

    final double tilesDistanceWidth = tilesBoundingBox.getMaxLongitude() - tilesBoundingBox.getMinLongitude();
    final double tilesDistanceHeight = tilesBoundingBox.getMaxLatitude() - tilesBoundingBox.getMinLatitude();

    final int width = tile.getWidth();
    final int height = tile.getHeight();

    // Tile pixels of the tile matrix tiles
    int[] pixels = new int[width * height];
    tile.getPixels(pixels, 0, width, 0, 0, width, height);

    // Projected tile pixels to draw the reprojected tile
    int[] projectedPixels = new int[requestedTileWidth * requestedTileHeight];

    // Retrieve each pixel in the new tile from the unprojected tile
    for (int y = 0; y < requestedTileHeight; y++) {
        for (int x = 0; x < requestedTileWidth; x++) {

            double longitude = requestBoundingBox.getMinLongitude() + (x * requestedWidthUnitsPerPixel);
            double latitude = requestBoundingBox.getMaxLatitude() - (y * requestedHeightUnitsPerPixel);
            ProjCoordinate fromCoord = new ProjCoordinate(longitude, latitude);
            ProjCoordinate toCoord = transformRequestToTiles.transform(fromCoord);
            double projectedLongitude = toCoord.x;
            double projectedLatitude = toCoord.y;

            int xPixel = (int) Math.round(((projectedLongitude - tilesBoundingBox.getMinLongitude()) / tilesDistanceWidth) * width);
            int yPixel = (int) Math.round(((tilesBoundingBox.getMaxLatitude() - projectedLatitude) / tilesDistanceHeight) * height);

            xPixel = Math.max(0, xPixel);
            xPixel = Math.min(width - 1, xPixel);

            yPixel = Math.max(0, yPixel);
            yPixel = Math.min(height - 1, yPixel);

            int color = pixels[(yPixel * width) + xPixel];
            projectedPixels[(y * requestedTileWidth) + x] = color;
        }
    }

    // Draw the new tile bitmap
    Bitmap projectedTileBitmap = Bitmap.createBitmap(requestedTileWidth,
            requestedTileHeight, tile.getConfig());
    projectedTileBitmap.setPixels(projectedPixels, 0, requestedTileWidth, 0, 0, requestedTileWidth, requestedTileHeight);

    return projectedTileBitmap;
}
 
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: CoverageDataCore.java    From geopackage-core-java with MIT License 4 votes vote down vote up
/**
 * Reproject the coverage data to the requested projection
 *
 * @param values
 *            coverage data values
 * @param requestedCoverageWidth
 *            requested coverage data width
 * @param requestedCoverageHeight
 *            requested coverage data height
 * @param requestBoundingBox
 *            request bounding box in the request projection
 * @param transformRequestToCoverage
 *            transformation from request to coverage data
 * @param coverageBoundingBox
 *            coverage data bounding box
 * @return projected coverage data
 */
protected Double[][] reprojectCoverageData(Double[][] values,
		int requestedCoverageWidth, int requestedCoverageHeight,
		BoundingBox requestBoundingBox,
		ProjectionTransform transformRequestToCoverage,
		BoundingBox coverageBoundingBox) {

	final double requestedWidthUnitsPerPixel = (requestBoundingBox
			.getMaxLongitude() - requestBoundingBox.getMinLongitude())
			/ requestedCoverageWidth;
	final double requestedHeightUnitsPerPixel = (requestBoundingBox
			.getMaxLatitude() - requestBoundingBox.getMinLatitude())
			/ requestedCoverageHeight;

	final double tilesDistanceWidth = coverageBoundingBox.getMaxLongitude()
			- coverageBoundingBox.getMinLongitude();
	final double tilesDistanceHeight = coverageBoundingBox.getMaxLatitude()
			- coverageBoundingBox.getMinLatitude();

	final int width = values[0].length;
	final int height = values.length;

	Double[][] projectedValues = new Double[requestedCoverageHeight][requestedCoverageWidth];

	// Retrieve each coverage data value in the unprojected coverage data
	for (int y = 0; y < requestedCoverageHeight; y++) {
		for (int x = 0; x < requestedCoverageWidth; x++) {

			double longitude = requestBoundingBox.getMinLongitude()
					+ (x * requestedWidthUnitsPerPixel);
			double latitude = requestBoundingBox.getMaxLatitude()
					- (y * requestedHeightUnitsPerPixel);
			ProjCoordinate fromCoord = new ProjCoordinate(longitude,
					latitude);
			ProjCoordinate toCoord = transformRequestToCoverage
					.transform(fromCoord);
			double projectedLongitude = toCoord.x;
			double projectedLatitude = toCoord.y;

			int xPixel = (int) Math
					.round(((projectedLongitude - coverageBoundingBox
							.getMinLongitude()) / tilesDistanceWidth)
							* width);
			int yPixel = (int) Math
					.round(((coverageBoundingBox.getMaxLatitude() - projectedLatitude) / tilesDistanceHeight)
							* height);

			xPixel = Math.max(0, xPixel);
			xPixel = Math.min(width - 1, xPixel);

			yPixel = Math.max(0, yPixel);
			yPixel = Math.min(height - 1, yPixel);

			Double coverageData = values[yPixel][xPixel];
			projectedValues[y][x] = coverageData;
		}
	}

	return projectedValues;
}
 
Example 10
Source File: TileCreator.java    From geopackage-java with MIT License 4 votes vote down vote up
/**
 * Reproject the tile to the requested projection
 *
 * @param tile
 *            tile in the tile matrix projection
 * @param requestedTileWidth
 *            requested tile width
 * @param requestedTileHeight
 *            requested tile height
 * @param requestBoundingBox
 *            request bounding box in the request projection
 * @param transformRequestToTiles
 *            transformation from request to tiles
 * @param tilesBoundingBox
 *            request bounding box in the tile matrix projection
 * @return projected tile
 */
private BufferedImage reprojectTile(BufferedImage tile,
		int requestedTileWidth, int requestedTileHeight,
		BoundingBox requestBoundingBox,
		ProjectionTransform transformRequestToTiles,
		BoundingBox tilesBoundingBox) {

	final double requestedWidthUnitsPerPixel = (requestBoundingBox
			.getMaxLongitude() - requestBoundingBox.getMinLongitude())
			/ requestedTileWidth;
	final double requestedHeightUnitsPerPixel = (requestBoundingBox
			.getMaxLatitude() - requestBoundingBox.getMinLatitude())
			/ requestedTileHeight;

	final double tilesDistanceWidth = tilesBoundingBox.getMaxLongitude()
			- tilesBoundingBox.getMinLongitude();
	final double tilesDistanceHeight = tilesBoundingBox.getMaxLatitude()
			- tilesBoundingBox.getMinLatitude();

	final int width = tile.getWidth();
	final int height = tile.getHeight();

	// Tile pixels of the tile matrix tiles
	int[] pixels = new int[width * height];
	tile.getRGB(0, 0, width, height, pixels, 0, width);

	// Projected tile pixels to draw the reprojected tile
	int[] projectedPixels = new int[requestedTileWidth
			* requestedTileHeight];

	// Retrieve each pixel in the new tile from the unprojected tile
	for (int y = 0; y < requestedTileHeight; y++) {
		for (int x = 0; x < requestedTileWidth; x++) {

			double longitude = requestBoundingBox.getMinLongitude()
					+ (x * requestedWidthUnitsPerPixel);
			double latitude = requestBoundingBox.getMaxLatitude()
					- (y * requestedHeightUnitsPerPixel);
			ProjCoordinate fromCoord = new ProjCoordinate(longitude,
					latitude);
			ProjCoordinate toCoord = transformRequestToTiles
					.transform(fromCoord);
			double projectedLongitude = toCoord.x;
			double projectedLatitude = toCoord.y;

			int xPixel = (int) Math
					.round(((projectedLongitude - tilesBoundingBox
							.getMinLongitude()) / tilesDistanceWidth)
							* width);
			int yPixel = (int) Math
					.round(((tilesBoundingBox.getMaxLatitude() - projectedLatitude) / tilesDistanceHeight)
							* height);

			xPixel = Math.max(0, xPixel);
			xPixel = Math.min(width - 1, xPixel);

			yPixel = Math.max(0, yPixel);
			yPixel = Math.min(height - 1, yPixel);

			int color = pixels[(yPixel * width) + xPixel];
			projectedPixels[(y * requestedTileWidth) + x] = color;
		}
	}

	// Draw the new image
	BufferedImage projectedTileImage = new BufferedImage(
			requestedTileWidth, requestedTileHeight, tile.getType());
	projectedTileImage.setRGB(0, 0, requestedTileWidth,
			requestedTileHeight, projectedPixels, 0, requestedTileWidth);

	return projectedTileImage;
}
 
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: TileBoundingBoxUtils.java    From geopackage-core-java with MIT License 3 votes vote down vote up
/**
 * Get the tile grid for the location specified as the projection
 * 
 * @param point
 *            point
 * @param zoom
 *            zoom level
 * @param projection
 *            projection
 * @return tile grid
 * @since 1.1.0
 */
public static TileGrid getTileGrid(Point point, int zoom,
		Projection projection) {
	ProjectionTransform toWebMercator = projection
			.getTransformation(ProjectionConstants.EPSG_WEB_MERCATOR);
	Point webMercatorPoint = toWebMercator.transform(point);
	BoundingBox boundingBox = new BoundingBox(webMercatorPoint.getX(),
			webMercatorPoint.getY(), webMercatorPoint.getX(),
			webMercatorPoint.getY());
	return getTileGrid(boundingBox, zoom);
}