ฉันต้องการสร้างแผนที่ด้วย GeoTools และบันทึกลงในภาพ (เช่น JPEG) ความต้องการของฉันเรียบง่าย:
- สร้างแผนที่ของโลกด้วย 2 เลเยอร์: ขอบเขตทางการเมืองและกริด เลเยอร์มาจากแหล่งที่แตกต่างกันและการคาดการณ์ที่แตกต่างกัน
- ส่งออกแผนที่ไปยังการคาดการณ์ที่แตกต่างกัน (เช่น "EPSG: 5070", "EPSG: 4326", "EPSG: 54012", "EPSG: 54009" ฯลฯ )
- คลิปเอาต์พุตไปยัง AOIs อื่น (เช่น -124.79 ถึง -66.9 lon, 24.4 ถึง 49.4 lat)
ฉันต้องการทำสิ่งนี้โดยทางโปรแกรมผ่าน API จนถึงตอนนี้ฉันประสบความสำเร็จอย่าง จำกัด ฉันเรียนรู้ที่จะสร้างแผนที่และเอาท์พุทในการฉายภาพต่าง ๆ โดยใช้วิธีนี้:
//Step 1: Create map
MapContent map = new MapContent();
map.setTitle("World");
//Step 2: Set projection
CoordinateReferenceSystem crs = CRS.decode("EPSG:5070"); //Conic projection over US
MapViewport vp = map.getViewport();
vp.setCoordinateReferenceSystem(crs);
//Step 3: Add layers to map
CoordinateReferenceSystem mapCRS = map.getCoordinateReferenceSystem();
map.addLayer(reproject(getPoliticalBoundaries(), mapCRS));
map.addLayer(reproject(getGraticules(), mapCRS));
//Step 4: Save image
saveImage(map, "/temp/graticules.jpg", 800);วิธีการบันทึกตรงจากเว็บไซต์ GeoTools :
public void saveImage(final MapContent map, final String file, final int imageWidth) {
    GTRenderer renderer = new StreamingRenderer();
    renderer.setMapContent(map);
    Rectangle imageBounds = null;
    ReferencedEnvelope mapBounds = null;
    try {
        mapBounds = map.getMaxBounds();
        double heightToWidth = mapBounds.getSpan(1) / mapBounds.getSpan(0);
        imageBounds = new Rectangle(
                0, 0, imageWidth, (int) Math.round(imageWidth * heightToWidth));
    } catch (Exception e) {
        // failed to access map layers
        throw new RuntimeException(e);
    }
    BufferedImage image = new BufferedImage(imageBounds.width, imageBounds.height, BufferedImage.TYPE_INT_RGB);
    Graphics2D gr = image.createGraphics();
    gr.setPaint(Color.WHITE);
    gr.fill(imageBounds);
    try {
        renderer.paint(gr, imageBounds, mapBounds);
        File fileToSave = new File(file);
        ImageIO.write(image, "jpeg", fileToSave);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}วิธีการปฏิเสธคือสิ่งประดิษฐ์ของฉัน มันเป็นบิตของการแฮ็ค แต่วิธีเดียวที่ฉันสามารถค้นหาเพื่อส่งภาพไปยังการฉายภาพที่เฉพาะเจาะจง
private static Layer reproject(Layer layer, CoordinateReferenceSystem mapCRS) throws Exception {
    SimpleFeatureSource featureSource = (SimpleFeatureSource) layer.getFeatureSource();
  //Define coordinate transformation
    CoordinateReferenceSystem dataCRS = featureSource.getSchema().getCoordinateReferenceSystem();
    boolean lenient = true; // allow for some error due to different datums
    MathTransform transform = CRS.findMathTransform(dataCRS, mapCRS, lenient);
  //Create new feature collection
    SimpleFeatureCollection copy = FeatureCollections.newCollection("internal");
    SimpleFeatureType featureType = SimpleFeatureTypeBuilder.retype(featureSource.getSchema(), mapCRS);
    SimpleFeatureIterator iterator = featureSource.getFeatures().features();
    try {
        while (iterator.hasNext()) {
            SimpleFeature feature = iterator.next();
            Geometry geometry = (Geometry) feature.getDefaultGeometry();
            Geometry geometry2 = JTS.transform(geometry, transform);
            copy.add( SimpleFeatureBuilder.build( featureType, new Object[]{ geometry2 }, null) );
        }
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    finally {
        iterator.close();
    }
  //Return new layer
    Style style = SLD.createLineStyle(Color.BLACK, 1);
    layer = new FeatureLayer(copy, style);
    layer.setTitle("Graticules");
    return layer;
}ผลลัพธ์ไม่ดีจริง ๆ :

ดังนั้นฉันคิดว่าฉันมีคำถามสองสามข้อที่แตกต่างกัน:
- นี่คือการอนุมัติที่ถูกต้องหรือไม่ ฉันจำเป็นต้องปฏิเสธเลเยอร์ใหม่ด้วยตนเองหรือ MapViewport ควรจะทำสิ่งนี้ให้ฉันหรือไม่
- ฉันจะคลิปผลลัพธ์ไปยัง AOI ที่เฉพาะเจาะจงได้อย่างไร ฉันได้ลองตั้งค่าขอบเขตโดยใช้วิธี MapViewport.setBounds (ซองจดหมาย) แต่วิธี saveImage ดูเหมือนจะไม่สนใจขอบเขต
- ฉันจะให้เส้นละติจูดของฉันแสดงเป็นส่วนโค้งได้อย่างไร มีการตั้งค่าการแปลงที่ฉันหายไปหรือไม่?
ฉันใช้ GeoTools 8.7



