一、发布png到geoserver的imagepyramid
// translate png to tif epsg:4326 File png = new File(pngPathStr); BufferedImage sourcePng = ImageIO.read(new FileInputStream(png)); int pngWidth = sourcePng.getWidth(); int pngHeight = sourcePng.getHeight(); String outGeoTiffFormTransName = pngPathStr + "out.tif"; String comdTransPngToGTifWith4326 = "gdal_translate -a_srs EPSG:4326 -gcp 0 0 "+ west +" "+ north +" -gcp " + pngWidth + " 0 "+ east +" "+ north +" -gcp "+ pngWidth +" " + pngHeight +" " + east +" "+ south +" "+ pngPathStr +" "+ outGeoTiffFormTransName; //gdal_translate -a_srs EPSG:4326 -gcp 0 0 -90 0 -gcp 256 0 -45 0 -gcp 256 256 -45 45 Lena.tif Lean.tiff System.out.println(comdTransPngToGTifWith4326); Process process1 = Runtime.getRuntime().exec(comdTransPngToGTifWith4326); process1.waitFor(); process1.destroy(); //warp tif to geotiff epsg:4326 String warpGeotiffName = outGeoTiffFormTransName + "warp.tiff"; String comdWarpGEOtiff = "gdalwarp -s_srs EPSG:4326 -t_srs EPSG:4326 " + outGeoTiffFormTransName +" "+ warpGeotiffName; //gdalwarp -s_srs EPSG:4326 -t_srs EPSG:4326 Lean.tiff System.out.println(comdWarpGEOtiff); Process process2 = Runtime.getRuntime().exec(comdWarpGEOtiff); process2.waitFor(); process2.destroy(); //retile geoTiff in 4 level String cutPngDirName = pngPathStr.substring(0, pngPathStr.lastIndexOf(".")); String addCutTiffDirCmd = "mkdir " +cutPngDirName; Process process3 = Runtime.getRuntime().exec(addCutTiffDirCmd); process3.waitFor(); process3.destroy(); String retileGeotiffCmd = "gdal_retile.py -v -r bilinear -levels 4 -ps 2048 2048 -co \"TILED=YES\" -targetDir "+ cutPngDirName +" "+warpGeotiffName; //gdal_retile.py -v -r bilinear -levels 4 -ps 2048 2048 -co "TILED=YES" -targetDir lean LEAN.tiff Process process4 = Runtime.getRuntime().exec(retileGeotiffCmd); process4.waitFor(); process4.destroy(); //use geoserver restapi to add a workspace String curlAddWorkSpanceCmd = "curl -v -u admin:geoserver -XPOST -H \"content-type: text/xml\" -d \"
// translate png to tif epsg:4326 File png = new File(pngPathStr); BufferedImage sourcePng = ImageIO.read(new FileInputStream(png)); int pngWidth = sourcePng.getWidth(); int pngHeight = sourcePng.getHeight(); String outGeoTiffFormTransName = pngPathStr + "out.tif"; String comdTransPngToGTifWith4326 = "gdal_translate -a_srs EPSG:4326 -gcp 0 0 "+ west +" "+ north +" -gcp " + pngWidth + " 0 "+ east +" "+ north +" -gcp "+ pngWidth +" " + pngHeight +" " + east +" "+ south +" "+ pngPathStr +" "+ outGeoTiffFormTransName; //gdal_translate -a_srs EPSG:4326 -gcp 0 0 -90 0 -gcp 256 0 -45 0 -gcp 256 256 -45 45 Lena.tif Lean.tiff System.out.println(comdTransPngToGTifWith4326); Process process1 = Runtime.getRuntime().exec(comdTransPngToGTifWith4326); process1.waitFor(); process1.destroy(); //warp tif to geotiff epsg:4326 String warpGeotiffName = outGeoTiffFormTransName + "warp.tiff"; String comdWarpGEOtiff = "gdalwarp -s_srs EPSG:4326 -t_srs EPSG:4326 " + outGeoTiffFormTransName +" "+ warpGeotiffName; //gdalwarp -s_srs EPSG:4326 -t_srs EPSG:4326 Lean.tiff System.out.println(comdWarpGEOtiff); Process process2 = Runtime.getRuntime().exec(comdWarpGEOtiff); process2.waitFor(); process2.destroy(); //retile geoTiff in 4 level String cutPngDirName = pngPathStr.substring(0, pngPathStr.lastIndexOf(".")); String addCutTiffDirCmd = "mkdir " +cutPngDirName; Process process3 = Runtime.getRuntime().exec(addCutTiffDirCmd); process3.waitFor(); process3.destroy(); String retileGeotiffCmd = "gdal_retile.py -v -r bilinear -levels 4 -ps 2048 2048 -co \"TILED=YES\" -targetDir "+ cutPngDirName +" "+warpGeotiffName; //gdal_retile.py -v -r bilinear -levels 4 -ps 2048 2048 -co "TILED=YES" -targetDir lean LEAN.tiff Process process4 = Runtime.getRuntime().exec(retileGeotiffCmd); process4.waitFor(); process4.destroy(); //use geoserver restapi to add a workspace String curlAddWorkSpanceCmd = "curl -v -u admin:geoserver -XPOST -H \"content-type: text/xml\" -d \"\" \"http://127.0.0.1:8090/geoserver/rest/workspaces\""; //curl -v -u admin geoserver -XPOST -H "content-type: text/xml" -d " toby " "http://127.0.0.1:8090/geoserver/rest/workspaces" Process process5 = Runtime.getRuntime().exec(curlAddWorkSpanceCmd); process5.waitFor(); process5.destroy(); //add a imagepyramid WMS String addImagepyramidCmd = "curl -v -u admin:geoserver -XPUT -H \"Context-type: text/plain\" -d \""+ cutPngDirName +"\" \"http://localhost:8090/geoserver/rest/workspaces/toby/coveragestores/poly-incremental/external.imagepyramid\""; //curl -v -u admin:geoserver -XPUT -H "Context-type: text/plain" -d "file:/home/xschen/Downloads/lean" "http://localhost:8090/geoserver/rest/workspaces/toby/coveragestores/poly-incremental/external.imagepyramid" Process process6 = Runtime.getRuntime().exec(addImagepyramidCmd); process6.waitFor(); process6.destroy(); toby \" \"http://127.0.0.1:8090/geoserver/rest/workspaces\""; //curl -v -u admin geoserver -XPOST -H "content-type: text/xml" -d " toby " "http://127.0.0.1:8090/geoserver/rest/workspaces" Process process5 = Runtime.getRuntime().exec(curlAddWorkSpanceCmd); process5.waitFor(); process5.destroy(); //add a imagepyramid WMS String addImagepyramidCmd = "curl -v -u admin:geoserver -XPUT -H \"Context-type: text/plain\" -d \""+ cutPngDirName +"\" \"http://localhost:8090/geoserver/rest/workspaces/toby/coveragestores/poly-incremental/external.imagepyramid\""; //curl -v -u admin:geoserver -XPUT -H "Context-type: text/plain" -d "file:/home/xschen/Downloads/lean" "http://localhost:8090/geoserver/rest/workspaces/toby/coveragestores/poly-incremental/external.imagepyramid" Process process6 = Runtime.getRuntime().exec(addImagepyramidCmd); process6.waitFor(); process6.destroy(); toby
二、前台的geoserverTerrainProvider配置使用
var terrainProvider = new Cesium.GeoserverTerrainProvider({
service: "WMS",
url : "http://127.0.0.1:8888/geoserver/kjtest/wms",//上面java中得到的WMSurl
layerName: "gt30e100n40",//上面java中的cutPngDirName
//layerName: "lean",
heightMapWidth: 65,
heightMapHeight: 65,
offset: 0,
highest: 40000,
hasStyledImage: true,
waterMask: true,
cesiumViewer: viewer,
formatArray: {
format : "image/bil",
postProcessArray : function(bufferIn, size,highest,lowest,offset) {
var resultat;
var viewerIn = new DataView(bufferIn);
var littleEndianBuffer = new ArrayBuffer(size.height * size.width * 2);
var viewerOut = new DataView(littleEndianBuffer);
if (littleEndianBuffer.byteLength === bufferIn.byteLength) {
var temp, goodCell = 0, somme = 0;
for (var i = 0; i < littleEndianBuffer.byteLength; i += 2) {
temp = viewerIn.getInt16(i, false)-offset ;
if (temp > lowest && temp < highest) {
viewerOut.setInt16(i, temp, true);
somme += temp;
goodCell++;
} else {
var val = (goodCell == 0 ? 1 : somme / goodCell);
viewerOut.setInt16(i, val, true);
}
}
resultat = new Int16Array(littleEndianBuffer);
console.log(resultat);
}
return resultat;
}
}
});
viewer.terrainProvider = terrainProvider;
或者使用cesium的
new Cesium.WebMapServiceImageryProvider({
url: geoserverWMSurl,//?号之前
layers: workspace:layerName//工作空间 : 发布图层的名称
});
最后使用nginx解决跨域问题。
nginx.config的配置信息如下:
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
listen 8888;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location ^~/em {
proxy_pass http://127.0.0.1:8081/em;
proxy_set_header Host $host:8888;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ^~/demWordData {
proxy_pass http://127.0.0.1:8080/demWordData;
proxy_set_header Host $host:8888;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl;
# listen [::]:443 ssl;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers PROFILE=SYSTEM;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# location / {
# }
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}
欢迎大家来我的新家看一看 3wwang个人博客-记录走过的技术之路