瀏覽代碼

Merge branch 'f-slop' into f-xiaogu

gushoubang 8 月之前
父節點
當前提交
e517e378e7
共有 2 個文件被更改,包括 67 次插入0 次删除
  1. 5 0
      onemap-modules/onemap-spatial/pom.xml
  2. 62 0
      onemap-modules/onemap-spatial/src/test/java/OnlineMap.java

+ 5 - 0
onemap-modules/onemap-spatial/pom.xml

@@ -114,6 +114,11 @@
             <artifactId>gt-shapefile</artifactId>
             <version>${org.geotools.version}</version>
         </dependency>
+        <dependency>
+            <groupId>org.geotools</groupId>
+            <artifactId>gt-tile-client</artifactId>
+            <version>${org.geotools.version}</version>
+        </dependency>
         <dependency>
             <groupId>org.locationtech.jts</groupId>
             <artifactId>jts-core</artifactId>

+ 62 - 0
onemap-modules/onemap-spatial/src/test/java/OnlineMap.java

@@ -0,0 +1,62 @@
+import org.geotools.geometry.jts.ReferencedEnvelope;
+import org.geotools.map.MapContent;
+
+import org.geotools.geometry.jts.ReferencedEnvelope;
+import org.geotools.map.MapContent;
+import org.geotools.map.TileLayer;
+import org.geotools.ows.ServiceException;
+import org.geotools.swing.JMapFrame;
+import org.geotools.tile.TileService;
+import org.geotools.tile.TileServiceInfo;
+import org.geotools.tile.impl.WebMercatorTileService;
+import org.opengis.referencing.crs.CoordinateReferenceSystem;
+import org.opengis.referencing.FactoryException;
+import org.opengis.referencing.NoSuchAuthorityCodeException;
+import org.geotools.referencing.CRS;
+
+import java.awt.*;
+import java.awt.image.BufferedImage;
+import java.io.File;
+import javax.imageio.ImageIO;
+
+public class OnlineMap {
+
+    public static void main(String[] args) throws Exception {
+        // Set up the tile service URL
+        String urlTemplate = "http://ecn.t3.tiles.virtualearth.net/tiles/a{q}.jpeg?g=1";
+        TileService service = new WebMercatorTileService("Bing Aerial", urlTemplate, 18, 2, 256, 256);
+
+        // Create map content
+        MapContent mapContent = new MapContent();
+        mapContent.setTitle("Bing Aerial Map");
+
+        // Create tile layer
+        TileLayer tileLayer = new TileLayer(new TileServiceInfo(service));
+        mapContent.addLayer(tileLayer);
+
+        // Define the vector range (bounding box) for the area of interest
+        CoordinateReferenceSystem crs = CRS.decode("EPSG:3857");
+        ReferencedEnvelope bbox = new ReferencedEnvelope(-8238304.0, -8238304.0 + 100000, 4970240.0, 4970240.0 + 100000, crs);
+
+        // Display the map
+        JMapFrame mapFrame = new JMapFrame(mapContent);
+        mapFrame.setSize(800, 600);
+        mapFrame.setVisible(true);
+        mapFrame.getMapPane().setDisplayArea(bbox);
+
+        // Set up the screenshot area
+        Rectangle screenRect = new Rectangle(800, 600);
+        BufferedImage bufferedImage = new BufferedImage(screenRect.width, screenRect.height, BufferedImage.TYPE_INT_RGB);
+        Graphics2D graphics = bufferedImage.createGraphics();
+
+        // Render the map to the image
+        mapFrame.getMapPane().paint(graphics);
+
+        // Save the screenshot
+        ImageIO.write(bufferedImage, "png", new File("screenshot.png"));
+
+        // Release resources
+        graphics.dispose();
+        mapContent.dispose();
+    }
+}