在做gis产品的过程中,或多或少的都会用到地形的高度数据
前端地形采样
cesium在线地形高度采样,
基础版与高精度版
getMostDetailedTerrainHeight为高精度版本,耗费时间要长些
function getMostDetailedTerrainHeight(lng, lat) {
return new Promise(function (resolve, reject) {
let terrainHeight = 0;
let terrainProvider = Cesium.createWorldTerrain();
let positions = [
Cesium.Cartographic.fromDegrees(lng, lat)
];
let promise2 = Cesium.sampleTerrainMostDetailed(terrainProvider, positions);
Cesium.when(promise2, function (updatedPositions) {
terrainHeight = updatedPositions[0].height;
resolve(terrainHeight);
});
});
}
getSimpleTerrainHeight为基础版,时长要短些,Cesium.sampleTerrain(terrainProvider, 11, positions)中的11为请求精度。
function getSimpleTerrainHeight(lng, lat) {
return new Promise(function (resolve, reject) {
let terrainHeight = 0;
let terrainProvider = Cesium.createWorldTerrain();
let positions = [
Cesium.Cartographic.fromDegrees(lng, lat)
];
let promise1 = Cesium.sampleTerrain(terrainProvider, 11, positions);
Cesium.when(promise1, function (updatedPositions) {
terrainHeight = updatedPositions[0].height;
resolve(terrainHeight);
});
});
}
后端地形采样
1、采用调用Google Evalation API 获取地形高度
public static String getGoogleElevation(List<LatLngModel> requetList) {
String requestUrl = "https://maps.googleapis.com/maps/api/elevation/json?locations=39.7391536,-104.9847034|36.455556,-116.866667&key=YOUR_API_KEY";
String requestUlrNew = "https://maps.googleapis.com/maps/api/elevation/json?locations=39.7391536,-104.9847034|36.455556,-116.866667&key=YOUR_API_KEY";
String s = HttpRequestUtil.requestUrl(requestUlrNew);
return s;
}
public static String requestUrl(String url) {
StringBuilder result = new StringBuilder();
try {
URL reqUrl = new URL(url);
HttpURLConnection connection = (HttpURLConnection) reqUrl.openConnection();
connection.connect();
int responseCode = connection.getResponseCode();
String responseMessage = connection.getResponseMessage();
if (responseCode == HttpURLConnection.HTTP_OK) {
InputStream inputStream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String temp;
while ((temp = reader.readLine()) != null) {
result.append(temp);
}
}
} catch (Exception e) {
throw new BusinessValidationException(e.getMessage());
}
return result.toString();
}
2、发布DEM的Geoserver服务,通过调用Geoserver的服务计算地形高度(java版)
/**
* 获取地形高度
*
* @param points_data (86.001,41.001;86.002,41.002;86.003,41.003) (lon,lat;lon,lat;lon,lat)
* @return
* @throws IOException
*/
public static List<LatLngModel> getSimpleTerrainHeight(String points_data) throws IOException {
List<Double> lonArray = new ArrayList<Double>();
List<Double> latArray = new ArrayList<Double>();
String[] points = points_data.split(";");
for (int i = 0; i < points.length; i++) {
String strLonlat = points[i];
String[] strLonlats = strLonlat.split(",");
double lon = Double.parseDouble(strLonlats[0]);
double lat = Double.parseDouble(strLonlats[1]);
lonArray.add(lon);
latArray.add(lat);
}
Double rangValue = 0.0001;
Double maxLng = Collections.max(lonArray) + rangValue;
Double minLng = Collections.min(lonArray) - rangValue;
Double maxLat = Collections.max(latArray) + rangValue;
Double minLat = Collections.min(latArray) - rangValue;
String demRequestXML = getDemRequestXML(minLng, minLat, maxLng, maxLat);
String geoServer = (ObjectUtils.isEmpty(appSetting) ? "http://192.168.1.103:8088/geoserver" : appSetting.geoserver) + "/wcs";
InputStream inputStream = HttpRequestUtil.demPost(geoServer, demRequestXML);
GeoTiffReader tifReader = new GeoTiffReader(inputStream);
GridCoverage2D coverage = tifReader.read(null);
CoordinateReferenceSystem crs = coverage.getCoordinateReferenceSystem2D();
List<LatLngModel> list = new ArrayList();
for (int i = 0; i < points.length; i++) {
String strLonlat = points[i];
String[] strLonlats = strLonlat.split(",");
double lon = Double.parseDouble(strLonlats[0]);
double lat = Double.parseDouble(strLonlats[1]);
try {
DirectPosition position = new DirectPosition2D(crs, lon, lat);
int[] results = (int[]) coverage.evaluate(position);
results = coverage.evaluate(position, results);
LatLngModel model = new LatLngModel();
model.lng = lon;
model.lat = lat;
model.dem = results[0];
list.add(model);
} catch (Exception e) {
System.out.println(e.getMessage());
continue;
}
}
return list;
}
/**
* 请求geoserver返回dem的tif数据
* author yaohui
*
* @param url
* @param xmlStr
* @return
*/
public static InputStream demPost(String url, String xmlStr) {
InputStream content = null;
try {
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Content-Type", "application/xml");
StringEntity stringEntity2 = new StringEntity(xmlStr.toString(), HTTP.UTF_8);
stringEntity2.setContentType("application/xml");
httpPost.setEntity(stringEntity2);
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(150000).setConnectTimeout(150000).setConnectionRequestTimeout(150000).build();
CloseableHttpClient httpClient2 = HttpClients.custom().setDefaultRequestConfig(requestConfig).build();
CloseableHttpResponse response = httpClient2.execute(httpPost);
content = response.getEntity().getContent();
} catch (Exception e) {
System.out.println("发送 POST 请求出现异常!" + e);
e.printStackTrace();
}
return content;
}
public static String getDemRequestXML(double minLng, double minLat, double maxLng, double maxLat) {
String retXmlStr = "";
retXmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><GetCoverage version=\"1.0.0\" service=\"WCS\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://www.opengis.net/wcs\" xmlns:ows=\"http://www.opengis.net/ows/1.1\" xmlns:gml=\"http://www.opengis.net/gml\" xmlns:ogc=\"http://www.opengis.net/ogc\" xsi:schemaLocation=\"http://www.opengis.net/wcs http://schemas.opengis.net/wcs/1.0.0/getCoverage.xsd\">\n" +
" <sourceCoverage>STRM:ChinaSTRM</sourceCoverage>\n" +
" <domainSubset>\n" +
" <spatialSubset>\n" +
" <gml:Envelope srsName=\"EPSG:4326\">\n" +
" <gml:pos>" + minLng + " " + minLat + "</gml:pos>\n" +
" <gml:pos>" + maxLng + " " + maxLat + "</gml:pos>\n" +
" </gml:Envelope>\n" +
" <gml:Grid dimension=\"2\">\n" +
" <gml:limits>\n" +
" <gml:GridEnvelope>\n" +
" <gml:low>0 0</gml:low>\n" +
" <gml:high>119 120</gml:high>\n" +
" </gml:GridEnvelope>\n" +
" </gml:limits>\n" +
" <gml:axisName>x</gml:axisName>\n" +
" <gml:axisName>y</gml:axisName>\n" +
" </gml:Grid>\n" +
" </spatialSubset>\n" +
" </domainSubset>\n" +
" <output>\n" +
" <crs>EPSG:4326</crs>\n" +
" <format>GeoTIFF</format>\n" +
" </output>\n" +
"</GetCoverage>";
return retXmlStr;
}