天天看點

利用Geotools來轉換影像的坐标系背景處理Tips

背景

若有一幅航拍的原始影像,需要自動産品化,那就必須要在後端進行坐标系的統一轉換,這時用到開源的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);
           

繼續閱讀