Search in sources :

Example 1 with GeometryTransform

use of mil.nga.sf.proj.GeometryTransform in project geopackage-android-map by ngageoint.

the class FeatureInfoBuilder method projectGeometry.

/**
 * 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) {
        SpatialReferenceSystemDao srsDao = SpatialReferenceSystemDao.create(featureDao.getDb());
        try {
            int srsId = geometryData.getSrsId();
            SpatialReferenceSystem srs = srsDao.queryForId((long) srsId);
            if (!projection.equals(srs.getOrganization(), srs.getOrganizationCoordsysId())) {
                Projection geomProjection = srs.getProjection();
                GeometryTransform transform = GeometryTransform.create(geomProjection, 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);
        }
    }
}
Also used : GeometryTransform(mil.nga.sf.proj.GeometryTransform) Geometry(mil.nga.sf.Geometry) SpatialReferenceSystemDao(mil.nga.geopackage.srs.SpatialReferenceSystemDao) SQLException(java.sql.SQLException) SpatialReferenceSystem(mil.nga.geopackage.srs.SpatialReferenceSystem) Projection(mil.nga.proj.Projection) GeoPackageException(mil.nga.geopackage.GeoPackageException) Point(mil.nga.sf.Point)

Example 2 with GeometryTransform

use of mil.nga.sf.proj.GeometryTransform in project geopackage-android-map by ngageoint.

the class FeatureOverlayQueryTest method testTileBounds.

/**
 * Test Tile Bounds
 */
@Test
public void testTileBounds() {
    double longitude = -77.196785;
    double latitude = 38.753195;
    int zoom = 6;
    LatLng coordinate = new LatLng(latitude, longitude);
    BoundingBox boundingBox = FeatureOverlayQuery.tileBounds(coordinate, zoom);
    TestCase.assertEquals(-78.75000000000001, boundingBox.getMinLongitude(), 0.0);
    TestCase.assertEquals(36.597889133070204, boundingBox.getMinLatitude(), 0.0);
    TestCase.assertEquals(-73.125, boundingBox.getMaxLongitude(), 0.0);
    TestCase.assertEquals(40.97989806962014, boundingBox.getMaxLatitude(), 0.0);
    Point point = new Point(longitude, latitude);
    BoundingBox boundingBox2 = FeatureOverlayQuery.tileBounds(point, zoom);
    TestCase.assertEquals(boundingBox, boundingBox2);
    Projection projection = ProjectionFactory.getProjection(ProjectionConstants.EPSG_WEB_MERCATOR);
    GeometryTransform transform = GeometryTransform.create(ProjectionConstants.EPSG_WORLD_GEODETIC_SYSTEM, projection);
    point = transform.transform(point);
    boundingBox = FeatureOverlayQuery.tileBounds(projection, point, zoom);
    TestCase.assertEquals(-8766409.899970295, boundingBox.getMinLongitude(), 0.0);
    TestCase.assertEquals(4383204.9499851465, boundingBox.getMinLatitude(), 0.0);
    TestCase.assertEquals(-8140237.7642581295, boundingBox.getMaxLongitude(), 0.0);
    TestCase.assertEquals(5009377.085697312, boundingBox.getMaxLatitude(), 0.0);
    longitude = 151.215026;
    latitude = -33.856686;
    zoom = 15;
    coordinate = new LatLng(latitude, longitude);
    boundingBox = FeatureOverlayQuery.tileBounds(coordinate, zoom);
    TestCase.assertEquals(151.20483398437506, boundingBox.getMinLongitude(), 0.0);
    TestCase.assertEquals(-33.86129311351553, boundingBox.getMinLatitude(), 0.0);
    TestCase.assertEquals(151.21582031250003, boundingBox.getMaxLongitude(), 0.0);
    TestCase.assertEquals(-33.852169701407426, boundingBox.getMaxLatitude(), 0.0);
    point = new Point(longitude, latitude);
    boundingBox2 = FeatureOverlayQuery.tileBounds(point, zoom);
    TestCase.assertEquals(boundingBox, boundingBox2);
    point = transform.transform(point);
    boundingBox = FeatureOverlayQuery.tileBounds(projection, point, zoom);
    TestCase.assertEquals(16832045.124622095, boundingBox.getMinLongitude(), 0.0);
    TestCase.assertEquals(-4010192.2519534864, boundingBox.getMinLatitude(), 0.0);
    TestCase.assertEquals(16833268.117074657, boundingBox.getMaxLongitude(), 0.0);
    TestCase.assertEquals(-4008969.2595009245, boundingBox.getMaxLatitude(), 0.0);
}
Also used : GeometryTransform(mil.nga.sf.proj.GeometryTransform) BoundingBox(mil.nga.geopackage.BoundingBox) Projection(mil.nga.proj.Projection) LatLng(com.google.android.gms.maps.model.LatLng) Point(mil.nga.sf.Point) Point(mil.nga.sf.Point) Test(org.junit.Test)

Example 3 with GeometryTransform

use of mil.nga.sf.proj.GeometryTransform in project geopackage-java by ngageoint.

the class CoverageData method getValues.

/**
 * {@inheritDoc}
 */
@Override
public CoverageDataResults getValues(CoverageDataRequest request, Integer width, Integer height) {
    CoverageDataResults coverageDataResults = null;
    // Transform to the projection of the coverage data tiles
    GeometryTransform transformRequestToCoverage = null;
    BoundingBox requestProjectedBoundingBox = request.getBoundingBox();
    if (!sameProjection) {
        transformRequestToCoverage = GeometryTransform.create(requestProjection, coverageProjection);
        requestProjectedBoundingBox = requestProjectedBoundingBox.transform(transformRequestToCoverage);
    }
    request.setProjectedBoundingBox(requestProjectedBoundingBox);
    // Determine how many overlapping pixels to store based upon the
    // algorithm
    int overlappingPixels;
    switch(algorithm) {
        case BICUBIC:
            overlappingPixels = 3;
            break;
        default:
            overlappingPixels = 1;
    }
    // Find the tile matrix and results
    CoverageDataTileMatrixResults results = getResults(request, requestProjectedBoundingBox, overlappingPixels);
    if (results != null) {
        TileMatrix tileMatrix = results.getTileMatrix();
        TileResultSet tileResults = results.getTileResults();
        try {
            // Determine the requested coverage data dimensions, or use the
            // dimensions of a single tile matrix coverage data tile
            int requestedCoverageDataWidth = width != null ? width : (int) tileMatrix.getTileWidth();
            int requestedCoverageDataHeight = height != null ? height : (int) tileMatrix.getTileHeight();
            // Determine the size of the non projected coverage data results
            int tileWidth = requestedCoverageDataWidth;
            int tileHeight = requestedCoverageDataHeight;
            if (!sameProjection) {
                int projectedWidth = (int) Math.round((requestProjectedBoundingBox.getMaxLongitude() - requestProjectedBoundingBox.getMinLongitude()) / tileMatrix.getPixelXSize());
                if (projectedWidth > 0) {
                    tileWidth = projectedWidth;
                }
                int projectedHeight = (int) Math.round((requestProjectedBoundingBox.getMaxLatitude() - requestProjectedBoundingBox.getMinLatitude()) / tileMatrix.getPixelYSize());
                if (projectedHeight > 0) {
                    tileHeight = projectedHeight;
                }
            }
            // Retrieve the coverage data from the results
            Double[][] values = getValues(tileMatrix, tileResults, request, tileWidth, tileHeight, overlappingPixels);
            // Project the coverage data if needed
            if (values != null && !sameProjection && !request.isPoint()) {
                values = reprojectCoverageData(values, requestedCoverageDataWidth, requestedCoverageDataHeight, request.getBoundingBox(), transformRequestToCoverage, requestProjectedBoundingBox);
            }
            // Create the results
            if (values != null) {
                coverageDataResults = new CoverageDataResults(values, tileMatrix);
            }
        } finally {
            tileResults.close();
        }
    }
    return coverageDataResults;
}
Also used : GeometryTransform(mil.nga.sf.proj.GeometryTransform) BoundingBox(mil.nga.geopackage.BoundingBox) TileResultSet(mil.nga.geopackage.tiles.user.TileResultSet) TileMatrix(mil.nga.geopackage.tiles.matrix.TileMatrix)

Example 4 with GeometryTransform

use of mil.nga.sf.proj.GeometryTransform in project geopackage-java by ngageoint.

the class SQLExec method executeQuery.

/**
 * Execute the query against the database
 *
 * @param database
 *            open database
 * @param sql
 *            SQL statement
 * @param projection
 *            desired projection
 * @param maxRows
 *            max rows
 * @return results
 * @throws SQLException
 *             upon SQL error
 */
private static SQLExecResult executeQuery(GeoPackage database, String sql, Projection projection, Integer maxRows) throws SQLException {
    SQLExecResult result = new SQLExecResult();
    if (!sql.equals(";")) {
        PreparedStatement statement = null;
        try {
            statement = database.getConnection().getConnection().prepareStatement(sql);
            if (maxRows != null) {
                statement.setMaxRows(maxRows);
                result.setMaxRows(maxRows);
            }
            boolean hasResultSet = statement.execute();
            if (hasResultSet) {
                ResultSet resultSet = statement.getResultSet();
                ResultSetMetaData metadata = resultSet.getMetaData();
                int numColumns = metadata.getColumnCount();
                int[] columnWidths = new int[numColumns];
                int[] columnTypes = new int[numColumns];
                boolean isGeoPackage = isGeoPackage(database);
                Map<String, GeometryColumns> tableGeometryColumns = new HashMap<>();
                Map<Integer, GeometryTransform> geometryColumns = new HashMap<>();
                for (int col = 1; col <= numColumns; col++) {
                    String tableName = metadata.getTableName(col);
                    result.addTable(tableName);
                    String columnName = metadata.getColumnName(col);
                    result.addColumn(columnName);
                    columnTypes[col - 1] = metadata.getColumnType(col);
                    columnWidths[col - 1] = columnName.length();
                    // Determine if the column is a GeoPackage geometry
                    if (isGeoPackage) {
                        GeometryColumns geometryColumn = null;
                        if (tableGeometryColumns.containsKey(tableName)) {
                            geometryColumn = tableGeometryColumns.get(tableName);
                        } else {
                            if (database.isFeatureTable(tableName)) {
                                geometryColumn = database.getGeometryColumnsDao().queryForTableName(tableName);
                            }
                            tableGeometryColumns.put(tableName, geometryColumn);
                        }
                        if (geometryColumn != null && geometryColumn.getColumnName().equalsIgnoreCase(columnName)) {
                            GeometryTransform transform = null;
                            if (projection != null) {
                                transform = GeometryTransform.create(geometryColumn.getProjection(), projection);
                            }
                            geometryColumns.put(col, transform);
                        }
                    }
                }
                while (resultSet.next()) {
                    List<String> row = new ArrayList<>();
                    result.addRow(row);
                    for (int col = 1; col <= numColumns; col++) {
                        String stringValue = null;
                        Object value = resultSet.getObject(col);
                        if (value != null) {
                            switch(columnTypes[col - 1]) {
                                case Types.BLOB:
                                    stringValue = BLOB_DISPLAY_VALUE;
                                    if (geometryColumns.containsKey(col)) {
                                        GeometryTransform transform = geometryColumns.get(col);
                                        byte[] bytes = (byte[]) value;
                                        try {
                                            if (transform == null) {
                                                stringValue = GeoPackageGeometryData.wkt(bytes);
                                            } else {
                                                GeoPackageGeometryData geometryData = GeoPackageGeometryData.create(bytes);
                                                stringValue = geometryData.transform(transform).getWkt();
                                            }
                                        } catch (IOException e) {
                                        }
                                    }
                                    break;
                                default:
                                    stringValue = value.toString().replaceAll("\\s*[\\r\\n]+\\s*", " ");
                            }
                            int valueLength = stringValue.length();
                            if (valueLength > columnWidths[col - 1]) {
                                columnWidths[col - 1] = valueLength;
                            }
                        }
                        row.add(stringValue);
                    }
                }
                result.addColumnWidths(columnWidths);
            } else {
                int updateCount = statement.getUpdateCount();
                if (updateCount >= 0) {
                    result.setUpdateCount(updateCount);
                }
            }
        } finally {
            SQLUtils.closeStatement(statement, sql);
        }
    }
    return result;
}
Also used : GeometryColumns(mil.nga.geopackage.features.columns.GeometryColumns) GeoPackageGeometryData(mil.nga.geopackage.geom.GeoPackageGeometryData) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) ResultSetMetaData(java.sql.ResultSetMetaData) GeometryTransform(mil.nga.sf.proj.GeometryTransform) ResultSet(java.sql.ResultSet)

Example 5 with GeometryTransform

use of mil.nga.sf.proj.GeometryTransform in project geopackage-java by ngageoint.

the class URLTileGen method generate.

/**
 * Generate the tiles
 */
public static void generate() {
    // If the GeoPackage does not exist create it
    if (!geoPackageFile.exists()) {
        GeoPackageManager.create(geoPackageFile);
    }
    // Open the GeoPackage
    geoPackage = GeoPackageManager.open(geoPackageFile);
    // Default the EPSG
    if (epsg == null) {
        epsg = Long.valueOf(ProjectionConstants.EPSG_WORLD_GEODETIC_SYSTEM);
    }
    // Set the projection and default bounding box as needed
    Projection bboxProjection = null;
    if (boundingBox != null) {
        bboxProjection = ProjectionFactory.getProjection(epsg);
    } else {
        boundingBox = BoundingBox.worldWGS84();
        bboxProjection = ProjectionFactory.getProjection(ProjectionConstants.EPSG_WORLD_GEODETIC_SYSTEM);
    }
    // Bound degree tiles to Web Mercator limits
    if (bboxProjection.isUnit(Units.DEGREES)) {
        boundingBox = TileBoundingBoxUtils.boundDegreesBoundingBoxWithWebMercatorLimits(boundingBox);
    }
    // Transform to the URL projection bounding box
    Projection urlProjection = ProjectionFactory.getProjection(urlEpsg);
    GeometryTransform transform = GeometryTransform.create(bboxProjection, urlProjection);
    BoundingBox urlBoundingBox = boundingBox.transform(transform);
    UrlTileGenerator tileGenerator = new UrlTileGenerator(geoPackage, tileTable, url, urlBoundingBox, urlProjection);
    for (long zoomLevel : zoomLevels) {
        tileGenerator.addZoomLevel((int) zoomLevel);
    }
    if (compressFormat != null) {
        tileGenerator.setCompressFormat(compressFormat);
        if (compressQuality != null) {
            tileGenerator.setCompressQuality(compressQuality);
        }
    }
    if (xyzTiles) {
        tileGenerator.setXYZTiles(true);
    }
    if (tms) {
        tileGenerator.setTileFormat(TileFormatType.TMS);
    }
    tileGenerator.setSkipExisting(!replaceExisting);
    int count = tileGenerator.getTileCount();
    System.out.println();
    System.out.println("GeoPackage: " + geoPackage.getName());
    System.out.println("Tile Table: " + tileTable);
    System.out.println("URL: " + url);
    System.out.println("Zoom Levels: " + zoomLevels);
    if (compressFormat != null) {
        System.out.println("Compress Format: " + compressFormat);
    }
    if (compressQuality != null) {
        System.out.println("Compress Quality: " + compressQuality);
    }
    if (xyzTiles) {
        System.out.println("Save as XYZ Tiles: true");
    }
    if (boundingBox != null) {
        System.out.println("Bounding Box:");
        System.out.println("\tMin Lon: " + boundingBox.getMinLongitude());
        System.out.println("\tMin Lat: " + boundingBox.getMinLatitude());
        System.out.println("\tMax Lon: " + boundingBox.getMaxLongitude());
        System.out.println("\tMax Lat: " + boundingBox.getMaxLatitude());
    }
    if (epsg != null) {
        System.out.println("EPSG: " + epsg);
    }
    System.out.println("URL EPSG: " + urlEpsg);
    if (tms) {
        System.out.println("URL TMS Tiles: true");
    }
    System.out.println("Existing Tiles: " + (replaceExisting ? "replace" : "skip"));
    System.out.println("Log Count Frequency: " + progress.getCountFrequency() + " tiles");
    System.out.println("Log Time Frequency: " + progress.getTimeFrequency() + " seconds");
    System.out.println("Expected Tile Count: " + count);
    System.out.println();
    tileGenerator.setProgress(progress);
    LOGGER.log(Level.INFO, "Generating Tiles...");
    int generatedCount = 0;
    try {
        generatedCount = tileGenerator.generateTiles();
    } catch (IOException | SQLException e) {
        throw new GeoPackageException("Exception while generating tiles", e);
    }
    finish(generatedCount);
}
Also used : GeometryTransform(mil.nga.sf.proj.GeometryTransform) SQLException(java.sql.SQLException) BoundingBox(mil.nga.geopackage.BoundingBox) UrlTileGenerator(mil.nga.geopackage.tiles.UrlTileGenerator) Projection(mil.nga.proj.Projection) IOException(java.io.IOException) GeoPackageException(mil.nga.geopackage.GeoPackageException)

Aggregations

GeometryTransform (mil.nga.sf.proj.GeometryTransform)60 BoundingBox (mil.nga.geopackage.BoundingBox)49 Projection (mil.nga.proj.Projection)26 Point (mil.nga.sf.Point)15 TileMatrix (mil.nga.geopackage.tiles.matrix.TileMatrix)10 FeatureDao (mil.nga.geopackage.features.user.FeatureDao)9 FeatureRow (mil.nga.geopackage.features.user.FeatureRow)9 Test (org.junit.Test)9 SpatialReferenceSystem (mil.nga.geopackage.srs.SpatialReferenceSystem)7 SpatialReferenceSystemDao (mil.nga.geopackage.srs.SpatialReferenceSystemDao)7 TileMatrixSet (mil.nga.geopackage.tiles.matrixset.TileMatrixSet)6 TileResultSet (mil.nga.geopackage.tiles.user.TileResultSet)6 GeometryEnvelope (mil.nga.sf.GeometryEnvelope)6 BufferedImage (java.awt.image.BufferedImage)5 GeoPackageException (mil.nga.geopackage.GeoPackageException)5 FeatureResultSet (mil.nga.geopackage.features.user.FeatureResultSet)5 TileDao (mil.nga.geopackage.tiles.user.TileDao)5 HashMap (java.util.HashMap)4 FeatureTable (mil.nga.geopackage.features.user.FeatureTable)4 TileMatrixSetDao (mil.nga.geopackage.tiles.matrixset.TileMatrixSetDao)4