1 功能需求
????對一副3857坐標系的衛星影像按照谷歌TMS進行切片,并將切片文件存儲在符合MBTiles規范的SQLite數據庫中
2 依賴庫
GDAL ,GeoTools
3 環境搭建
????由于個人對Java語言比較熟悉,因此使用Java語言開發環境,Java工程復用原有的SpringBoot工程,然后在pom文件中添加對GeoTools庫的依賴即可實現GeoTools的引入。
3.1 Windows環境下搭建GDAL Java Binding開發環境
????GDAL是當前最流行的遙感影像處理庫,提供了一系列對遙感影像數據的操作函數,支持常見的影像數據格式,并提供了相應的抽象結構方便進行算法操作。GDAL的源代碼是用C和C++編寫的,并通過SWIG發布其他語言版本的bindings。
????首先訪問http://www.gisinternals.com/下載對應版本的編譯好的GDAL庫,下載并解壓到本地文件夾下,注意路徑不能有中文。然后在計算機-環境變量Path中添加對GDAL DLL的引用。E:\soft\GIS\gdal\bin;E:\soft\GIS\gdal\bin\gdal\java
????在E:\soft\GIS\gdal\bin\gdal\java即可找到GDAL對應的JAR包,在工程中引用后即完成了GDAL的引入。完成后運行測試代碼,成功顯示影像信息后即代表GDAL庫引入成功。
public class GDALTest {
public static void main(String[] args) {
gdal.AllRegister();
String fileName = "D:\\images\\2011_output_cai.tif";
Dataset dataset = gdal.Open(fileName, gdalconstConstants.GA_ReadOnly);
if (dataset == null) {
System.err.println("GDALOpen failed - " + gdal.GetLastErrorNo());
System.err.println(gdal.GetLastErrorMsg());
System.exit(1);
}
double[] ori_transform = dataset.GetGeoTransform();
System.out.println(String.format("Origin = (%s, %s)", ori_transform[0], ori_transform[3]));
System.out.println(String.format("Pixel Size = (%s, %s)", ori_transform[1], ori_transform[5]));
}
}
4 切片處理流程
4.1 首先獲取原始影像的地理坐標范圍
double[] ori_transform = dataset.GetGeoTransform();
double latMin = ori_transform[3];
double latMax = ori_transform[3] + (yCount * ori_transform[1]);
double lonMin = ori_transform[0];
double lonMax = ori_transform[0] + (xCount * ori_transform[1]);
ReferencedEnvelope imageBound = new ReferencedEnvelope(lonMin, lonMax, latMin, latMax, crs);
4.2 獲取原始影像的像素分辨率
int xCount = dataset.getRasterXSize();
int yCount = dataset.getRasterYSize();
// 原始圖像東西方向像素分辨率
double src_w_e_pixel_resolution = (lonMax - lonMin) / xCount;
// 原始圖像南北方向像素分辨率
double src_n_s_pixel_resolution = (latMax - latMin) / yCount;
4.3 根據原始影像地理范圍求解切片行列號
int tileRowMax = Utils.getOSMTileYFromLatitude(latMin, zoom);
int tileRowMin = Utils.getOSMTileYFromLatitude(latMax, zoom);
int tileColMin = Utils.getOSMTileXFromLongitude(lonMin, zoom);
int tileColMax = Utils.getOSMTileXFromLongitude(lonMax, zoom);
4.4 求原始影像地理范圍與指定縮放級別指定行列號的切片交集
double tempLatMin = tile2lat(row + 1, zoom);
double tempLatMax = tile2lat(row, zoom);
double tempLonMin = tile2lon(col, zoom);
double tempLonMax = tile2lon(col + 1, zoom);
ReferencedEnvelope tileBound = new ReferencedEnvelope(tempLonMin, tempLonMax, tempLatMin,tempLatMax, crs);
ReferencedEnvelope intersect = tileBound.intersection(imageBound);
4.5 求解當前切片的像素分辨率(默認切片大小為256*256)
// 切片東西方向像素分辨率
double dst_w_e_pixel_resolution = (tempLonMax - tempLonMin) / 256;
// 切片南北方向像素分辨率
double dst_n_s_pixel_resolution = (tempLatMax - tempLatMin) / 256;
4.6 計算交集的像素信息
// 求切圖范圍和原始圖像交集的起始點像素坐標
int offset_x = (int) ((intersect.getMinX() - lonMin) / src_w_e_pixel_resolution);
int offset_y = (int) Math.abs((intersect.getMaxY() - latMax) / src_n_s_pixel_resolution);
// 求在切圖地理范圍內的原始圖像的像素大小
int block_xsize = (int) ((intersect.getMaxX() - intersect.getMinX()) / src_w_e_pixel_resolution);
int block_ysize = (int) ((intersect.getMaxY() - intersect.getMinY()) / src_n_s_pixel_resolution);
// 求原始圖像在切片內的像素大小
int image_Xbuf = (int) Math.ceil((intersect.getMaxX() - intersect.getMinX()) / dst_w_e_pixel_resolution);
int image_Ybuf = (int) Math.ceil(Math.abs((intersect.getMaxY() - intersect.getMinY()) / dst_n_s_pixel_resolution));
// 求原始圖像在切片中的偏移坐標
int imageOffsetX = (int) ((intersect.getMinX() - tempLonMin) / dst_w_e_pixel_resolution);
int imageOffsetY = (int) Math.abs((intersect.getMaxY() - tempLatMax) / dst_n_s_pixel_resolution);
imageOffsetX = imageOffsetX > 0 ? imageOffsetX : 0;
imageOffsetY = imageOffsetY > 0 ? imageOffsetY : 0;
4.7 使用GDAL的ReadRaster方法對影像指定范圍進行讀取與壓縮。
推薦在切片前建立原始影像的金字塔文件,ReadRaster在內部實現中可直接讀取相應級別的金字塔文件,提高效率。
Band in_band1 = dataset.GetRasterBand(1);
Band in_band2 = dataset.GetRasterBand(2);
Band in_band3 = dataset.GetRasterBand(3);
int[] band1BuffData = new int[256 * 256 * gdalconst.GDT_Int32];
int[] band2BuffData = new int[256 * 256 * gdalconst.GDT_Int32];
int[] band3BuffData = new int[256 * 256 * gdalconst.GDT_Int32];
in_band1.ReadRaster(offset_x, offset_y, block_xsize, block_ysize, image_Xbuf, image_Ybuf,gdalconst.GDT_Int32, band1BuffData, 0, 0);
in_band2.ReadRaster(offset_x, offset_y, block_xsize, block_ysize, image_Xbuf, image_Ybuf,gdalconst.GDT_Int32, band2BuffData, 0, 0);
in_band3.ReadRaster(offset_x, offset_y, block_xsize, block_ysize, image_Xbuf, image_Ybuf,gdalconst.GDT_Int32, band3BuffData, 0, 0);
4.8 將切片數據寫入文件
// 使用gdal的MEM驅動在內存中創建一塊區域存儲圖像數組
Driver memDriver = gdal.GetDriverByName("MEM");
Dataset msmDS = memDriver.Create("msmDS", 256, 256, 4);
Band dstBand1 = msmDS.GetRasterBand(1);
Band dstBand2 = msmDS.GetRasterBand(2);
Band dstBand3 = msmDS.GetRasterBand(3);
// 設置alpha波段數據,實現背景透明
Band alphaBand = msmDS.GetRasterBand(4);
int[] alphaData = new int[256 * 256 * gdalconst.GDT_Int32];
for (int index = 0; index < alphaData.length; index++) {
if (band1BuffData[index] > 0) {
alphaData[index] = 255;
}
}
// 寫各個波段數據
dstBand1.WriteRaster(imageOffsetX, imageOffsetY, image_Xbuf, image_Ybuf, band1BuffData);
dstBand2.WriteRaster(imageOffsetX, imageOffsetY, image_Xbuf, image_Ybuf, band2BuffData);
dstBand3.WriteRaster(imageOffsetX, imageOffsetY, image_Xbuf, image_Ybuf, band3BuffData);
alphaBand.WriteRaster(imageOffsetX, imageOffsetY, image_Xbuf, image_Ybuf, alphaData);
String tileFolder = System.getProperty("java.io.tmpdir");
String pngPath = tileFolder + File.separator + zoom + "c" + col + "r" + row +".png";
// 使用PNG驅動將內存中的圖像數組寫入文件
Driver pngDriver = gdal.GetDriverByName("PNG");
Dataset pngDs = pngDriver.CreateCopy(pngPath, msmDS);
// 釋放內存
msmDS.FlushCache();
pngDs.delete();
4.8 讀取臨時文件,并轉換成二進制存儲到SQLite
try {
InputStream fis = new FileInputStream(tileFile);
byte[] imgBytes = toByteArray(fis);
String uuid = UUID.randomUUID().toString();
PreparedStatement mapPs = conn.prepareStatement("insert into map(zoom_level,tile_column,tile_row,tile_id) values (?,?,?,?)");
PreparedStatement imagesPs = conn.prepareStatement("insert into images(tile_data,tile_id) values (?,?)");
imagesPs.setBytes(1, imgBytes);
imagesPs.setString(2, uuid);
mapPs.setInt(1, zoom);
mapPs.setInt(2, col);
mapPs.setInt(3, row);
mapPs.setString(4, uuid);
imagesPs.executeUpdate();
mapPs.executeUpdate();
imagesPs.close();
mapPs.close();
fis.close();
tileFile.delete();
} catch (Exception e) {
e.printStackTrace();
}