背景

若有一幅航拍的原始影像,需要自动产品化,那就必须要在后端进行坐标系的统一转换,这时用到开源的Geotools就很简单了。

处理

使用Operations类下的resample方法(重采样)可以解决这个问题,它的方法定义如下:

 Coverage org.geotools.coverage.processing.Operations.resample(Coverage source, CoordinateReferenceSystem crs) throws CoverageProcessingException

所以我们可以利用它实现栅格影像的坐标变换,例如下面代码将xxxx.tif的坐标系转换为3857坐标系。

File file = new File("xxxx.tif");
        	if(file.exists()){
	        	Reader br = new Reader();
	        	GridCoverage2D old2D = br.getGridCoverage2D(file);
	        	final CoordinateReferenceSystem WGS = CRS.decode("EPSG:3857");
	    		final CoordinateReferenceSystem sourceCRS = old2D.getCoordinateReferenceSystem();
	    		System.out.println(String.format("源坐标系为: %s", sourceCRS.getName()));
	    		GridCoverage2D new2D = (GridCoverage2D) Operations.DEFAULT.resample(old2D, WGS);
	        	System.err.println(String.format("目标坐标系为: %s", new2D.getCoordinateReferenceSystem().getName()));	
        	}

控制台打印结果如下:

源坐标系为: EPSG:WGS 84 / UTM zone 48N
目标坐标系为: EPSG:WGS 84 / Pseudo-Mercator

可见resample方法切实有效。

Tips

若涉及到4326坐标系的转换,要注意坐标轴的顺序是不固定的,需要限制顺序,否则很多后续工作会出乱子,参考Axis Order

//resample参数修改
Hints hints = new Hints(Hints.FORCE_LONGITUDE_FIRST_AXIS_ORDER, Boolean.TRUE);
CRSAuthorityFactory factory = ReferencingFactoryFinder.getCRSAuthorityFactory("EPSG", hints);
CoordinateReferenceSystem crs = factory.createCoordinateReferenceSystem("EPSG:4326");
Operations.DEFAULT.resample(xxx,crs);


Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐