GeoTools tif 获取元数据信息, 数据块获取,影像打开,影像保存

public String getAltitude(List<String> strPoints) throws IOException, TransformException {

    String demPath = "G:/weitu/download/xian/xian.tif";
    // String demPath = "F:/Data/yanta/YanTaDOM.tif";
    File file = new File(demPath);
    Hints tiffHints = new Hints();
    tiffHints.add(new Hints(Hints.FORCE_LONGITUDE_FIRST_AXIS_ORDER, Boolean.TRUE));
    // 默认坐标系EPSG:3857
    //tiffHints.add(new Hints(Hints.DEFAULT_COORDINATE_REFERENCE_SYSTEM, CRS.decode("EPSG:4326")));
    tiffHints.add(new Hints(Hints.DEFAULT_COORDINATE_REFERENCE_SYSTEM, DefaultGeographicCRS.WGS84));

    GeoTiffReader tifReader = new GeoTiffReader(file,tiffHints);
    GridCoverage2D coverage = tifReader.read(null);
    RenderedImage sourceImage = coverage.getRenderedImage();

    PlanarImage planarImage = (PlanarImage)sourceImage;

    //获取左上右下,包含仿射影子的左上角
    Envelope env = coverage.getEnvelope();

    //创建输出tif
    String outputPath = "D:/testTiff.tif";
    float[][] slopeData = new float[1000][1000];
    for(int i=0;i<1000;i++){
      for(int j=0;j<1000;j++)
        slopeData[i][j] = i+j;
    }
    GridCoverageFactory factory = new GridCoverageFactory();
    GridCoverage2D outputCoverage = factory.create("test", slopeData, env);
    GeoTiffWriter writer = new GeoTiffWriter(new File(outputPath));
    writer.write(outputCoverage, null);
    writer.dispose();

    int ixtiles = sourceImage.getNumXTiles();
    Raster raster = sourceImage.getTile(0,0);

    int itilerasterwidth = raster.getWidth();
    int itilerasterheight = raster.getHeight();
    int irasternumbands = raster.getNumBands();

    //获取坐标系
    CoordinateReferenceSystem crs = coverage.getCoordinateReferenceSystem2D();

    //获取图斑名称
    String [] names = tifReader.getGridCoverageNames();

    //获取影像长宽
    int iwidth = coverage.getRenderedImage().getWidth();
    int iheight = coverage.getRenderedImage().getHeight();

    //获取仿射因子其他参数
    int a  = coverage.getGridGeometry().gridDimensionX;
    int b = coverage.getGridGeometry().gridDimensionY;
    int c = coverage.getGridGeometry().axisDimensionX;
    int d = coverage.getGridGeometry().axisDimensionY;

    //获取栅格图斑个数
    int ibandcount = coverage.getNumSampleDimensions();
    String[] sampleDimensionNames = new String[ibandcount];
    for (int i = 0; i < ibandcount; i++) {
      GridSampleDimension dim = coverage.getSampleDimension(i);
      sampleDimensionNames[i] = dim.getDescription().toString();
    }

    //获取行列对应的像元值
    Raster sourceRaster = sourceImage.getData();
    float[] adsaf = {0};
    sourceRaster.getPixel(1500, 800,adsaf);
    float ibandvalue = sourceRaster.getSampleFloat(0,0,0);

    //获取源数据类型
    int iDataType = coverage.getRenderedImage().getSampleModel().getDataType();

    //??栅格转矢量
    // PolygonExtractionProcess process = new PolygonExtractionProcess();
    // SimpleFeatureCollection features = process.execute(tiffCoverage, 0, Boolean.TRUE, null, null, null, null);

    List list = new ArrayList();
    for(int i=0;i<strPoints.size();i++) {
      String strLonlat = strPoints.get(i);
      String[] strLonlats = strLonlat.split(" ");

      double lon = Double.parseDouble(strLonlats[0]),
          lat = Double.parseDouble(strLonlats[1]);

      //构建地理坐标
      DirectPosition position = new DirectPosition2D(crs, lon, lat);
      float[] results = (float[]) coverage.evaluate(position);

      //通过地理坐标获取行列号
      Point2D point2d = coverage.getGridGeometry().worldToGrid(position);

      //通过行列号获取地理坐标
      GridCoordinates2D coord= new GridCoordinates2D(0,0);
      DirectPosition tmpPos = coverage.getGridGeometry().gridToWorld(coord);
      float[] sss = (float[])coverage.evaluate(tmpPos);

      Map map = new HashMap();
      map.put("lon", lon);
      map.put("lat", lon);
      map.put("dem", results[0]);
      list.add(JSONObject.toJSONString(map));
    }
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Freedom3568

技术域不存在英雄主义,不进则退

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值