Skip to content

Commit

Permalink
minor change
Browse files Browse the repository at this point in the history
  • Loading branch information
clausnagel committed Oct 25, 2024
1 parent 2346e90 commit 38bd1c2
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 123 deletions.
27 changes: 24 additions & 3 deletions citydb-tiling/src/main/java/org/citydb/tiling/TileMatrix.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,15 @@

public class TileMatrix {
private final Envelope extent;
private final DatabaseAdapter adapter;
private final int columns;
private final int rows;
private final double tileWidth;
private final double tileHeight;
private final DatabaseAdapter adapter;
private final double[] columnOffsets;
private final double[] rowOffsets;
private final boolean swapAxes;
private TileMatrixOrigin tileMatrixOrigin = TileMatrixOrigin.TOP_LEFT;

TileMatrix(Coordinate lowerCorner, Coordinate upperCorner, int columns, int rows, double tileWidth,
double tileHeight, DatabaseAdapter adapter) throws TilingException {
Expand Down Expand Up @@ -82,11 +83,31 @@ public double getTileHeight() {
return tileHeight;
}

TileIterator getTileIterator(TileMatrixOrigin origin) {
public TileMatrixOrigin getOrigin() {
return tileMatrixOrigin;
}

TileMatrix setTileMatrixOrigin(TileMatrixOrigin tileMatrixOrigin) {
if (tileMatrixOrigin != null) {
this.tileMatrixOrigin = tileMatrixOrigin;
}

return this;
}

public TileIterator getTileIterator() {
return new TileIterator(this, getOrigin());
}

public TileIterator getTileIterator(TileMatrixOrigin origin) {
return new TileIterator(this, origin);
}

Tile getTileAt(int column, int row, TileMatrixOrigin origin) {
public Tile getTileAt(int column, int row) {
return getTileAt(column, row, getOrigin());
}

public Tile getTileAt(int column, int row, TileMatrixOrigin origin) {
if (column < 0 || column >= columns || row < 0 || row >= rows) {
throw new IndexOutOfBoundsException("Tile index (" + column + "," + row + ") is out of bounds.");
}
Expand Down
92 changes: 54 additions & 38 deletions citydb-tiling/src/main/java/org/citydb/tiling/Tiling.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,70 +21,86 @@

package org.citydb.tiling;

import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.annotation.JSONField;
import org.citydb.config.SerializableConfig;
import org.citydb.database.adapter.DatabaseAdapter;
import org.citydb.database.geometry.GeometryException;
import org.citydb.database.srs.SpatialReference;
import org.citydb.database.srs.SrsException;
import org.citydb.model.geometry.Envelope;
import org.citydb.tiling.encoding.ExtentReader;
import org.citydb.tiling.encoding.ExtentWriter;
import org.citydb.tiling.options.TileMatrixOrigin;

import java.sql.SQLException;
import java.util.Objects;
import java.util.Optional;

@SerializableConfig(name = "tiling")
public class Tiling {
private final Envelope extent;
private final TileMatrix tileMatrix;
private final TilingOptions options;
@JSONField(serializeUsing = ExtentWriter.class, deserializeUsing = ExtentReader.class)
private Envelope extent;
private TilingScheme scheme;
@JSONField(serializeFeatures = JSONWriter.Feature.WriteEnumUsingToString)
private TileMatrixOrigin tileMatrixOrigin = TileMatrixOrigin.TOP_LEFT;

private Tiling(Envelope extent, TileMatrix tileMatrix, TilingOptions options) {
this.extent = extent;
this.tileMatrix = tileMatrix;
this.options = options;
public static Tiling newInstance() {
return new Tiling();
}

public static Tiling of(DatabaseAdapter adapter, TilingOptions options) throws TilingException {
Objects.requireNonNull(adapter, "The database adapter must not be null.");
Objects.requireNonNull(options, "The tiling options must not be null.");

Envelope extent = options.getExtent()
.orElseThrow(() -> new TilingException("No tiling extent specified."));
TilingScheme scheme = options.getScheme()
.orElseThrow(() -> new TilingException("No tiling scheme specified."));

try {
SpatialReference reference = adapter.getGeometryAdapter().getSpatialReference(extent)
.orElse(adapter.getDatabaseMetadata().getSpatialReference());
if (reference.getSRID() != adapter.getDatabaseMetadata().getSpatialReference().getSRID()) {
extent = adapter.getGeometryAdapter().transform(extent);
}
} catch (GeometryException | SrsException | SQLException e) {
throw new TilingException("Failed to transform the tiling extent to the database SRS.", e);
}
public static Tiling of(Envelope extent, TilingScheme scheme) {
return new Tiling().setExtent(extent).setScheme(scheme);
}

return new Tiling(extent, scheme.createTileMatrix(extent, adapter), options);
public Optional<Envelope> getExtent() {
return Optional.ofNullable(extent);
}

public Envelope getExtent() {
return extent;
public Tiling setExtent(Envelope extent) {
this.extent = extent;
return this;
}

public TileMatrix getTileMatrix() {
return tileMatrix;
public Optional<TilingScheme> getScheme() {
return Optional.ofNullable(scheme);
}

public TileIterator getTileIterator() {
return getTileIterator(options.getTileMatrixOrigin());
public Tiling setScheme(TilingScheme scheme) {
this.scheme = scheme;
return this;
}

public TileIterator getTileIterator(TileMatrixOrigin origin) {
return tileMatrix.getTileIterator(origin);
public TileMatrixOrigin getTileMatrixOrigin() {
return tileMatrixOrigin != null ? tileMatrixOrigin : TileMatrixOrigin.TOP_LEFT;
}

public Tile getTileAt(int column, int row) {
return getTileAt(column, row, options.getTileMatrixOrigin());
public Tiling setTileMatrixOrigin(TileMatrixOrigin tileMatrixOrigin) {
this.tileMatrixOrigin = tileMatrixOrigin;
return this;
}

public Tile getTileAt(int column, int row, TileMatrixOrigin origin) {
return tileMatrix.getTileAt(column, row, origin);
public TileMatrix buildTileMatrix(DatabaseAdapter adapter) throws TilingException {
Objects.requireNonNull(adapter, "The database adapter must not be null.");

if (extent == null) {
throw new TilingException("No tiling extent specified.");
} else if (scheme == null) {
throw new TilingException("No tiling scheme specified.");
}

Envelope extent;
try {
SpatialReference reference = adapter.getGeometryAdapter().getSpatialReference(this.extent)
.orElse(adapter.getDatabaseMetadata().getSpatialReference());
extent = reference.getSRID() != adapter.getDatabaseMetadata().getSpatialReference().getSRID() ?
adapter.getGeometryAdapter().transform(this.extent) :
this.extent;
} catch (GeometryException | SrsException | SQLException e) {
throw new TilingException("Failed to transform the tiling extent to the database SRS.", e);
}

return scheme.buildTileMatrix(extent, adapter)
.setTileMatrixOrigin(getTileMatrixOrigin());
}
}
72 changes: 0 additions & 72 deletions citydb-tiling/src/main/java/org/citydb/tiling/TilingOptions.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@
seeAlso = {DimensionScheme.class, MatrixScheme.class},
seeAlsoDefault = MatrixScheme.class)
public abstract class TilingScheme {
protected abstract TileMatrix createTileMatrix(Envelope extent, DatabaseAdapter adapter) throws TilingException;
protected abstract TileMatrix buildTileMatrix(Envelope extent, DatabaseAdapter adapter) throws TilingException;

protected TileMatrix createTileMatrix(Coordinate lowerCorner, Coordinate upperCorner, int columns, int rows,
double tileWidth, double tileHeight, DatabaseAdapter adapter) throws TilingException {
protected TileMatrix buildTileMatrix(Coordinate lowerCorner, Coordinate upperCorner, int columns, int rows,
double tileWidth, double tileHeight, DatabaseAdapter adapter) throws TilingException {
return new TileMatrix(lowerCorner, upperCorner, columns, rows, tileWidth, tileHeight, adapter);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public DimensionScheme setGridPoint(Point gridPoint) {
}

@Override
protected TileMatrix createTileMatrix(Envelope extent, DatabaseAdapter adapter) throws TilingException {
protected TileMatrix buildTileMatrix(Envelope extent, DatabaseAdapter adapter) throws TilingException {
if (width == null) {
throw new TilingException("No tile width provided for the dimension tiling scheme.");
} else if (height == null) {
Expand Down Expand Up @@ -137,13 +137,12 @@ protected TileMatrix createTileMatrix(Envelope extent, DatabaseAdapter adapter)
Coordinate lowerCorner = Coordinate.of(minX, minY);
Coordinate upperCorner = Coordinate.of(minX + columns * tileWidth, minY + rows * tileHeight);

return createTileMatrix(lowerCorner, upperCorner, columns, rows, tileWidth, tileHeight, adapter);
return buildTileMatrix(lowerCorner, upperCorner, columns, rows, tileWidth, tileHeight, adapter);
}

private double getNearestNeighbor(double gridValue, double candidate, double offset) {
double distance = Math.sqrt((gridValue - candidate) * (gridValue - candidate));
return gridValue >= candidate ?
gridValue - Math.ceil(distance / offset) * offset :
gridValue + Math.floor(distance / offset) * offset;
gridValue - Math.ceil((gridValue - candidate) / offset) * offset :
gridValue + Math.floor((candidate - gridValue) / offset) * offset;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ public MatrixScheme setRows(int rows) {
}

@Override
protected TileMatrix createTileMatrix(Envelope extent, DatabaseAdapter adapter) throws TilingException {
return createTileMatrix(extent.getLowerCorner(), extent.getUpperCorner(), getColumns(), getRows(),
protected TileMatrix buildTileMatrix(Envelope extent, DatabaseAdapter adapter) throws TilingException {
return buildTileMatrix(extent.getLowerCorner(), extent.getUpperCorner(), getColumns(), getRows(),
(extent.getUpperCorner().getX() - extent.getLowerCorner().getX()) / getColumns(),
(extent.getUpperCorner().getY() - extent.getLowerCorner().getY()) / getRows(),
adapter);
Expand Down

0 comments on commit 38bd1c2

Please sign in to comment.