scala GridGeometry2D的世界到网格函数翻译错误

gudnpqoy  于 2023-04-06  发布在  Scala
关注(0)|答案(2)|浏览(118)

使用地理工具版本11.2

import java.awt.Rectangle

import com.vividsolutions.jts.geom.Envelope
import org.geotools.coverage.grid.{GeneralGridEnvelope, GridGeometry2D}
import org.geotools.geometry.jts.ReferencedEnvelope
import org.geotools.referencing.crs.DefaultGeographicCRS.WGS84

val minX = -78.523
val minY = 38.010
val maxX = -78.451
val maxY = 38.069
val width = 400
val height = 300

val bounds = new ReferencedEnvelope(new Envelope(minX, maxX, minY, maxY), WGS84)
val rect = new Rectangle(0, 0, width, height)
val ggEnvelope = new GeneralGridEnvelope(rect, bounds.getDimension)
val gm = new GridGeometry2D(ggEnvelope, bounds)
////////////////// This should be [0, 299] but is [-1, 299]//////////////////////////////
gm.worldToGrid(bounds.getLowerCorner)
laik7k3q

laik7k3q1#

在www.example.com worldToGrid函数的源代码中,可以看到与maxX和minY类似的内容GridGeometry2D.java

val TOL = 1.0E-6
  val values = Array(0.0)

  def safeWorldAccess(grid: GridCoverage2D, point: DirectPosition2D): Double = {
    val trPoint = point.clone()
    val gridGeom = grid.getGridGeometry
    if (Math.abs(trPoint.getX - gridGeom.getEnvelope2D.getMinX) <= TOL) {
      trPoint.setLocation(trPoint.getX + TOL, trPoint.getY)
    }

    if (Math.abs(trPoint.getY - gridGeom.getEnvelope2D.getMaxY) <= TOL) {
     trPoint.setLocation(trPoint.getX, trPoint.getY - TOL)
    }
    val gc: GridCoordinates2D = gridGeom.worldToGrid(trPoint)
    grid.evaluate(gc, values).head
  }
wecizke3

wecizke32#

此代码在geotool v29中返回[0,299]

double minX = -78.523;
        double minY = 38.010;
        double maxX = -78.451;
        double maxY = 38.069;
        int width = 400;
        int height = 300;

        ReferencedEnvelope bounds = new ReferencedEnvelope(new Envelope(minX, maxX, minY, maxY), WGS84);
        Rectangle rect = new Rectangle(0, 0, width, height);
        GeneralGridEnvelope ggEnvelope = new GeneralGridEnvelope(rect, bounds.getDimension());
        GridGeometry2D gm = new GridGeometry2D(ggEnvelope, bounds);
        ////////////////// This should be [0, 299] but is [-1, 299]//////////////////////////////
        System.out.println(gm.worldToGrid(bounds.getLowerCorner()).toString());

相关问题