前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Geotools Image Tif 打开的影像文件,根据几何模型进行块提取,并且保存

Geotools Image Tif 打开的影像文件,根据几何模型进行块提取,并且保存

作者头像
Freedom123
发布2024-03-29 08:55:56
650
发布2024-03-29 08:55:56
举报
文章被收录于专栏:DevOpsDevOps
代码语言:javascript
复制
/**
 * 根据几何模型进行影像切割
 * @param reader 原始印象
 * @param geom 几何模型
 */
public static GridCoverage2D SplitImageByGeometry( GeoTiffReader reader, Geometry geom,ImageMetaInfo metaInfo) {
  try{
    //元数据类型
    double noDataValue = reader.read(null).getSampleDimension(0).getNoDataValues()[0];
    double iDataType = reader.read(null).getSampleDimension(0).getNoDataValues()[0];

    //包围盒
    double minX = geom.getEnvelopeInternal().getMinX();
    double minY = geom.getEnvelopeInternal().getMinY();
    double maxX = geom.getEnvelopeInternal().getMaxX();
    double maxY = geom.getEnvelopeInternal().getMaxY();

    //左上右下点
    GeometryFactory geomFactory = new GeometryFactory();
    Point rtPoint = geomFactory.createPoint( new Coordinate(minX,maxY));
    Point lbPoint = geomFactory.createPoint(new Coordinate(maxX,minY));

    //对应到行列的左上右下
    DirectPosition2D dpRT = new DirectPosition2D(rtPoint.getX(),rtPoint.getY());
    DirectPosition2D dpLB = new DirectPosition2D(lbPoint.getX(),lbPoint.getY());
    GridCoordinates2D gcRT = reader.read(null).getGridGeometry().worldToGrid(dpRT);
    GridCoordinates2D gcLB = reader.read(null).getGridGeometry().worldToGrid(dpLB);

    //计算长宽
    int width = gcLB.x - gcRT.x;
    int height = gcLB.y - gcRT.y;

    RenderedImage sourceImage = reader.read(null).getRenderedImage();

    //获取内存块
    PlanarImage planarImage = (PlanarImage)sourceImage;
    SunWritableRaster newRaster = (SunWritableRaster)planarImage.getData(new Rectangle((int)gcRT.getX(),(int)gcRT.getY(),width+1,height+1));
    metaInfo.setSplitGrid(newRaster);
    metaInfo.setSplitIdxX( (int)gcRT.getX());
    metaInfo.setSplitIdxY( (int)gcRT.getY());

    //转换为数组块
    float[][] arrBlock = new float[height][width];
    float[] block = new float[width*height];
    newRaster.getPixels((int)gcRT.getX(),(int)gcRT.getY(),width,height,block);
    for(int row =0; row < height; row ++){
        for(int col =0; col < width; col ++){
          arrBlock[row][col] = block[row*width + col];
        }
    }

    //保存输出
    Envelope2D tmEnvelope = new Envelope2D(reader.getCoordinateReferenceSystem(),minX,minY,maxX - minX,maxY - minY);
    GridCoverageFactory gridFactory = new GridCoverageFactory();
    GridCoverage2D outputCoverage = gridFactory.create("subtractTiff", arrBlock,tmEnvelope);

    //代码有问题,暂不处理
    //GridSampleDimension[] sampleDimesion = new GridSampleDimension[1];
    //sampleDimesion[0] = reader.read(null).getSampleDimension(0);
    //GridCoverage2D outputCoverage = gridFactory.create("subtractTiff", newRaster,tmEnvelope,sampleDimesion);

    return  outputCoverage;
  }catch (Exception e){
    e.printStackTrace();
    return  null;
  }

}
本文参与?腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2024-03-28,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客?前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与?腾讯云自媒体分享计划? ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
http://www.vxiaotou.com