You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@baremaps.apache.org by bc...@apache.org on 2023/10/08 14:42:31 UTC

[incubator-baremaps] 02/10: Improve tile store

This is an automated email from the ASF dual-hosted git repository.

bchapuis pushed a commit to branch gdal
in repository https://gitbox.apache.org/repos/asf/incubator-baremaps.git

commit 41407c56da500852a2d61d34f29b171491ae619f
Author: Bertil Chapuis <bc...@gmail.com>
AuthorDate: Sun Apr 23 11:03:36 2023 +0200

    Improve tile store
---
 .run/ContourTileStore.run.xml                      |  16 +++
 .../apache/baremaps/raster/ContourTileStore.java   | 116 ++++++++++++---------
 .../main/java/org/apache/baremaps/raster/Main.java |  35 +++----
 .../org/apache/baremaps/tilestore/TileCoord.java   |   4 +-
 4 files changed, 100 insertions(+), 71 deletions(-)

diff --git a/.run/ContourTileStore.run.xml b/.run/ContourTileStore.run.xml
new file mode 100644
index 00000000..307d971d
--- /dev/null
+++ b/.run/ContourTileStore.run.xml
@@ -0,0 +1,16 @@
+<component name="ProjectRunConfigurationManager">
+  <configuration default="false" name="ContourTileStore" type="Application" factoryName="Application" nameIsGenerated="true">
+    <option name="MAIN_CLASS_NAME" value="org.apache.baremaps.raster.ContourTileStore" />
+    <module name="baremaps-core" />
+    <option name="VM_PARAMETERS" value="-Djava.library.path=$PROJECT_DIR$/../../osgeo/gdal/build/swig/java" />
+    <extension name="coverage">
+      <pattern>
+        <option name="PATTERN" value="org.apache.baremaps.raster.*" />
+        <option name="ENABLED" value="true" />
+      </pattern>
+    </extension>
+    <method v="2">
+      <option name="Make" enabled="true" />
+    </method>
+  </configuration>
+</component>
\ No newline at end of file
diff --git a/baremaps-core/src/main/java/org/apache/baremaps/raster/ContourTileStore.java b/baremaps-core/src/main/java/org/apache/baremaps/raster/ContourTileStore.java
index a352fa20..9e2b093b 100644
--- a/baremaps-core/src/main/java/org/apache/baremaps/raster/ContourTileStore.java
+++ b/baremaps-core/src/main/java/org/apache/baremaps/raster/ContourTileStore.java
@@ -12,73 +12,79 @@
 
 package org.apache.baremaps.raster;
 
-import java.net.URL;
 import java.nio.ByteBuffer;
-import java.nio.file.Files;
 import java.nio.file.Paths;
 import java.util.List;
 import java.util.Vector;
+import java.util.stream.IntStream;
+import java.util.stream.LongStream;
+
 import org.apache.baremaps.database.tile.Tile;
 import org.apache.baremaps.database.tile.TileStore;
 import org.apache.baremaps.database.tile.TileStoreException;
+import org.apache.baremaps.openstreetmap.utils.GeometryUtils;
+import org.apache.baremaps.openstreetmap.utils.ProjectionTransformer;
+import org.gdal.gdal.Dataset;
+import org.gdal.gdal.WarpOptions;
 import org.gdal.gdal.gdal;
 import org.gdal.gdalconst.gdalconstConstants;
 import org.gdal.ogr.FieldDefn;
 import org.gdal.ogr.ogr;
 import org.gdal.osr.SpatialReference;
+import org.locationtech.jts.geom.util.GeometryTransformer;
+import org.locationtech.proj4j.ProjCoordinate;
 
-public class ContourTileStore implements TileStore {
-  @Override
-  public ByteBuffer read(Tile tile) throws TileStoreException {
-    var file = Paths.get(String.format("%s/%s/%s.tif", tile.z(), tile.x(), tile.y()));
-    var source = String.format("https://s3.amazonaws.com/elevation-tiles-prod/geotiff/%s", file);
-
-    try {
-      Files.deleteIfExists(file);
-      Files.createDirectories(file.getParent());
-      Files.createFile(file);
-      try (var stream = new URL(source).openStream()) {
-        Files.copy(stream, file);
-      }
-    } catch (Exception e) {
-      e.printStackTrace();
-    }
-
-    var dataset = gdal.Open(file.toString(), gdalconstConstants.GA_ReadOnly);
-
-    dataset.GetGeoTransform();
-    dataset.GetRasterXSize();
-    dataset.GetRasterYSize();
-
-
-    var band = dataset.GetRasterBand(1);
-
-    band.ReadRaster_Direct(0, 0, 100, 100);
-
-    var wkt = dataset.GetProjection();
-    var srs = new SpatialReference(wkt);
-
-    var driver = ogr.GetDriverByName("Memory");
-    var dataSource = driver.CreateDataSource("memory_name");
+public class ContourTileStore implements TileStore, AutoCloseable {
 
-    var layer = dataSource.CreateLayer("contour", srs, ogr.wkbLineString);
+  static {
+    gdal.AllRegister();
+    ogr.RegisterAll();
+  }
 
-    var field = new FieldDefn("ID", ogr.OFTInteger);
-    field.SetWidth(8);
-    layer.CreateField(field, 0);
-    field.delete();
+  private final Dataset sourceDataset;
 
-    gdal.ContourGenerateEx(band, layer, new Vector<>(List.of(
-        "LEVEL_INTERVAL=" + 10)));
+  public ContourTileStore() {
+    var dem = Paths.get("examples/contour/dem.xml").toAbsolutePath().toString();
+    sourceDataset = gdal.Open(dem, gdalconstConstants.GA_ReadOnly);
+  }
 
-    for (int i = 0; i < layer.GetFeatureCount(); i++) {
-      var feature = layer.GetFeature(i);
-      var geometry = feature.GetGeometryRef();
-      // System.out.println(geometry.ExportToWkt());
-    }
+  @Override
+  public ByteBuffer read(Tile tile) throws TileStoreException {
+    var sourceBand = sourceDataset.GetRasterBand(1);
+    var envelope = tile.envelope();
+
+    // Warp the raster to the requested extent
+    var rasterOptions = new WarpOptions(new Vector<>(List.of(
+            "-of", "MEM",
+            "-te", Double.toString(envelope.getMinX()), Double.toString(envelope.getMinY()), Double.toString(envelope.getMaxX()), Double.toString(envelope.getMaxY()),
+            "-te_srs", "EPSG:4326")));
+    var rasterDataset = gdal.Warp("", new Dataset[]{sourceDataset}, rasterOptions);
+    var rasterBand = rasterDataset.GetRasterBand(1);
+
+    // Generate the contours
+    //var wkt = rasterDataset.GetProjection();
+    //var srs = new SpatialReference(wkt);
+    var srs = new SpatialReference("EPSG:4326");
+    var vectorDriver = ogr.GetDriverByName("Memory");
+    var vectorDataSource = vectorDriver.CreateDataSource("vector");
+    var vectorLayer = vectorDataSource.CreateLayer("vector", srs, ogr.wkbLineString);
+    gdal.ContourGenerateEx(rasterBand, vectorLayer, new Vector<>(List.of("LEVEL_INTERVAL=" + 10)));
+
+    // return the contours
+    var geometries = LongStream.range(0, vectorLayer.GetFeatureCount())
+            .mapToObj(vectorLayer::GetFeature)
+            .map(feature -> feature.GetGeometryRef())
+            .map(geometry -> GeometryUtils.deserialize(geometry.ExportToWkb()))
+            .toList();
+
+    var transformer = GeometryUtils.coordinateTransform(4326, 3857);
+    var min = transformer.transform(new ProjCoordinate(envelope.getMinX(), envelope.getMinY()), new ProjCoordinate());
+    var max = transformer.transform(new ProjCoordinate(envelope.getMaxX(), envelope.getMaxY()), new ProjCoordinate());
+
+    rasterBand.delete();
+    rasterDataset.delete();
+    sourceBand.delete();
 
-    dataSource.delete();
-    dataset.delete();
     return null;
   }
 
@@ -91,4 +97,16 @@ public class ContourTileStore implements TileStore {
   public void delete(Tile tile) throws TileStoreException {
     throw new UnsupportedOperationException();
   }
+
+  @Override
+  public void close() throws Exception {
+    sourceDataset.delete();
+  }
+
+  public static void main(String[] args) throws Exception {
+    var store = new ContourTileStore();
+    store.read(new Tile(8492, 5792, 14).parent());
+  }
+
+
 }
diff --git a/baremaps-core/src/main/java/org/apache/baremaps/raster/Main.java b/baremaps-core/src/main/java/org/apache/baremaps/raster/Main.java
index 4e37b4d5..f447f5c5 100644
--- a/baremaps-core/src/main/java/org/apache/baremaps/raster/Main.java
+++ b/baremaps-core/src/main/java/org/apache/baremaps/raster/Main.java
@@ -26,17 +26,15 @@ import org.gdal.osr.SpatialReference;
 public class Main {
 
   public static void main(String[] args) {
-
-    // var sourceFilename = Paths.get("examples/contour/liecthenstein-aster-dem-v2-3857.tif")
-    // .toAbsolutePath().toString();
-    // var hillshadeFilename =
-    // Paths.get("examples/contour/liecthenstein-aster-dem-v2-3857-hillshade.tif").toAbsolutePath()
-    // .toString();
-    // var outputFilename = Paths.get("examples/contour/liecthenstein-aster-dem-v2-3857.shp")
-    // .toAbsolutePath().toString();
-    // var warpFilename = Paths.get("examples/contour/liecthenstein-aster-dem-v2-3857-warp.tif")
-    // .toAbsolutePath().toString();
-
+     var sourceFilename = Paths.get("examples/contour/liecthenstein-aster-dem-v2-3857.tif")
+     .toAbsolutePath().toString();
+     var hillshadeFilename =
+     Paths.get("examples/contour/liecthenstein-aster-dem-v2-3857-hillshade.tif").toAbsolutePath()
+     .toString();
+     var outputFilename = Paths.get("examples/contour/liecthenstein-aster-dem-v2-3857.shp")
+     .toAbsolutePath().toString();
+     var warpFilename = Paths.get("examples/contour/liecthenstein-aster-dem-v2-3857-warp.tif")
+     .toAbsolutePath().toString();
 
     var dem = Paths.get("examples/contour/dem.xml")
         .toAbsolutePath().toString();
@@ -44,15 +42,15 @@ public class Main {
     gdal.AllRegister();
     ogr.RegisterAll();
 
-    planetContour("https://s3.amazonaws.com/elevation-tiles-prod/geotiff/%s/%s/%s.tif");
+    planetContour();
 
-    // hillshade(sourceFilename, 1, hillshadeFilename, 45d, 315d);
-    // contourEx(hillshadeFilename, 1, outputFilename, 50, 0);
-    // warp(sourceFilename, warpFilename);
-    // shadow(hillshadeFilename, outputFilename);
+   hillshade(sourceFilename, 1, hillshadeFilename, 45d, 315d);
+   contourEx(hillshadeFilename, 1, outputFilename, 50, 0);
+   warp(sourceFilename, warpFilename);
+   shadow(hillshadeFilename, outputFilename);
   }
 
-  public static void planetContour(String source) {
+  public static void planetContour() {
     var file = Paths.get(String.format("%s/%s/%s.tif", 14, 8514, 5816));
     var url = String.format("https://s3.amazonaws.com/elevation-tiles-prod/geotiff/%s", file);
     System.out.println(url);
@@ -69,7 +67,6 @@ public class Main {
       e.printStackTrace();
     }
 
-
     var dataset = gdal.Open(file.toString(), gdalconstConstants.GA_ReadOnly);
 
     var band = dataset.GetRasterBand(1);
@@ -231,10 +228,8 @@ public class Main {
 
     dataSource.delete();
     dataset.delete();
-
   }
 
-
   public static void hillshade(String source, Integer sourceBand, String target, Double azimuth,
       Double altitude) {
     var options = new Vector<>(List.of(
diff --git a/baremaps-core/src/main/java/org/apache/baremaps/tilestore/TileCoord.java b/baremaps-core/src/main/java/org/apache/baremaps/tilestore/TileCoord.java
index a9dbf97e..1953e634 100644
--- a/baremaps-core/src/main/java/org/apache/baremaps/tilestore/TileCoord.java
+++ b/baremaps-core/src/main/java/org/apache/baremaps/tilestore/TileCoord.java
@@ -198,8 +198,8 @@ public final class TileCoord implements Comparable<TileCoord> {
   public Envelope envelope() {
     double x1 = tile2lon(x, z);
     double x2 = tile2lon(x + 1, z);
-    double y1 = tile2lat(y + 1, z);
-    double y2 = tile2lat(y, z);
+    double y1 = tile2lat(y, z);
+    double y2 = tile2lat(y + 1, z);
     return new Envelope(x1, x2, y1, y2);
   }