利用Geotools来转换影像的坐标系

目录

背景

若有一幅航拍的原始影像,需要自动产品化,那就必须要在后端进行坐标系的统一转换,这时用到开源的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);

转载自:https://blog.csdn.net/u013323965/article/details/79453383

You may also like...