1.问题背景:

需要解析nc文件的数据源,获取一个三维数据,并计算器开发值。

java 后台处理:

定以一个实例来接收解析的数据并返回给前端。

package cn.edu.shou.domain;

import cn.edu.shou.domain.tbjhship;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;

import com.fasterxml.jackson.annotation.JsonManagedReference;

import com.fasterxml.jackson.annotation.ObjectIdGenerators;

import lombok.Getter;

import lombok.Setter;

import javax.persistence.*;

/**

* Created by seky on 16/3/25.

*/

@Entity

@Table(name = "tbjhshipdata")

public class tbjhshipdata {

@Id

@GeneratedValue(strategy = GenerationType.AUTO)

@Setter @Getter

private int id;

@Setter @Getter

private float windspeed;//风速

@Setter @Getter

private float winddir;//方向

@Setter @Getter

private float cwindspeed;//c风速

@Setter @Getter

private float cwinddir;//c风向

@Setter @Getter

private float watertemp;//水温

@Setter @Getter

private float airpressure;//气压

@ManyToOne(cascade = CascadeType.ALL,fetch = FetchType.EAGER)

@JoinColumn(name = "tbjhshipid")

@JsonManagedReference

public cn.edu.shou.domain.tbjhship tbjhship;

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public float getWindspeed() {

return windspeed;

}

public void setWindspeed(float windspeed) {

this.windspeed = windspeed;

}

public float getWinddir() {

return winddir;

}

public void setWinddir(float winddir) {

this.winddir = winddir;

}

public float getCwindspeed() {

return cwindspeed;

}

public void setCwindspeed(float cwindspeed) {

this.cwindspeed = cwindspeed;

}

public float getCwinddir() {

return cwinddir;

}

public void setCwinddir(float cwinddir) {

this.cwinddir = cwinddir;

}

public float getWatertemp() {

return watertemp;

}

public void setWatertemp(float watertemp) {

this.watertemp = watertemp;

}

public float getAirpressure() {

return airpressure;

}

public void setAirpressure(float airpressure) {

this.airpressure = airpressure;

}

public tbjhship getTbjhship() {

return tbjhship;

}

public void setTbjhship(tbjhship tbjhship) {

this.tbjhship = tbjhship;

}

}

定义restapi接口:

package cn.edu.shou.web.api;

import cn.edu.shou.domain.tbjhshipdata;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.*;

import ucar.ma2.Array;

import ucar.nc2.NetcdfFile;

import ucar.nc2.Variable;

import java.io.IOException;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

/**

* Created by Administrator on 2016/4/2.

*/

@RestController

@RequestMapping(value = "/api/config")

public class ReadNetcdfController {

NetcdfFile ncfile = null;

String filename = "D:\\jidi\\Wind_wq_sugon_wrf_2015121620.nc";

//获取u v 数据值

// lat 纬度 lon 经度

@RequestMapping(value = "/getSqrt", method =RequestMethod.GET)

public List> getNetCdfPredictData(int lat,int lon) throws Exception{

List> list = new ArrayList>();

Map map =new HashMap();

try{

ncfile = NetcdfFile.open(filename);

String variable = "u10";

String variable10 = "v10";

Variable varu10 = ncfile.findVariable(variable);

Variable varv10 = ncfile.findVariable(variable10);

if (null != varu10 && null != varv10) {

//第一个参数为时间编号,表示第几个时刻,

//第二个参数为经度的数据编号,表示从哪个经度数据开始

//第三个参数为维度的数据编号,表示从哪个维度数据开始

int[] origin = new int[]{4, 0, 0};//位置

origin[1]=lat;

origin[2]=lon;

//第一个参数表示时间的范围,72表示要读取72个时刻的数据

//第二个表示经度的数据范围,1表示只读去一个点的数据

//第三个表示纬度的数据范围,1表示只读一个点的数据

int[] size = new int[]{72, lat, lon};//

Array data2D = varu10.read(origin, size);

//v10 read

Array data3D = varv10.read(origin, size);

//计算开方值

Double netcdfSqrt = Math.sqrt((Math.pow(data2D.getDouble(0), 2) + Math.pow(data3D.getDouble(0), 2)));

System.out.println("netcdfsqrt"+netcdfSqrt);

map.put("windSpeed", netcdfSqrt.toString());

map.put("windDir", netcdfSqrt.toString());

list.add(map);

}

}catch (Exception e){

e.printStackTrace();

}finally {

if (null != ncfile)

try {

ncfile.close();

} catch (IOException ioe) {

ioe.printStackTrace();

}

}

return list;

}

@RequestMapping(value = "/PostCoordinates/{lat}/{lon}",method =RequestMethod.GET)

public List postCoordiantes( @PathVariable int lat,

@PathVariable int lon){

List> list1 = new ArrayList>();

try {

Map map=getLatAndLonIndex(lat,lon);

//return list

list1 = getNetCdfPredictData(map.get("latindex"),map.get("lonindex"));

System.out.println("list1 is"+list1);

}catch (Exception e){

e.printStackTrace();

}

List results=new ArrayList();

tbjhshipdata shiData=new tbjhshipdata();

for (Maplist:list1){

shiData.setWindspeed(Float.parseFloat(list.get("windSpeed")));

shiData.setWinddir(Float.parseFloat(list.get("windDir")));

results.add(shiData);

}

return results;

}

private MapgetLatAndLonIndex(int lat,int lon){

Map latAndLonIndex =new HashMap();//接收鼠标点击经纬度对应nc文件的编号

latAndLonIndex.put("latindex",lat);

latAndLonIndex.put("lonindex",lon);

return latAndLonIndex;

}

}

//前端点击map地图获取底图的point经纬度值,并调用matchLatAndLon函数查询最佳的数据编号

function getPoint(evt){

console.log("evt is",evt);

//单击后获取当前点的坐标值

var point = evt.mapPoint;

console.log("point is",point);

map.graphics.clear();

getshipmessage();

//添加一个graphic在当前点的位置上

var ptGraphic = new Graphic(point, pointSymbol);

map.graphics.add(ptGraphic);

//添加一个半径当前点

var buffer = geometryEngine.geodesicBuffer(point, 10, "miles");

var bufferGraphic = new Graphic(buffer, buffSymbol);

map.graphics.add(bufferGraphic);

for(var i in extents){

if(i == (flag-1) && flag==extents[i].flag){

//判断当前点是否是陆地

if(extents[i].extent[0].XMin<=point.x && point.x<=extents[i].extent[2].XMax

&& extents[i].extent[1].YMin<=point.y&&point.y<=extents[i].extent[3].YMax){

console.log("flag is"+flag);

//弹窗数据展示

switch (flag){

case 1: setwindObserve(point);console.log("海面风数据加载成功,绘制图表");

break;

case 2: setShipObservedWin(point);console.log("海浪数据加载成功,绘制图表");break;

case 3: setflowObserver(point);console.log("海流数据加载成功,绘制图表");break;

case 4: setWaveVisibility(point);console.log("能见度数据加载成功,绘制图表");break;

default :

console.log("eeee");break;

}

}else{

//如果是陆地的话,气泡显示经纬度

console.log("point"+point.x+point.y);

var latitude = point.x;

var longitude = point.y;

var infotemplate = new InfoTemplate("该点坐标信息","lat/lon : " +latitude.toFixed(2) + ", " + longitude.toFixed(2));

var pictureSymbol = new PictureMarkerSymbol('/img/typhoon.jpg', 30, 30);

var pictureGraphic = new Graphic(point, pictureSymbol, null,infotemplate);

// map.graphics.add(pictureGraphic);

}

}

}

};

//点击当前点与已有数据的匹配

function matchLatAndLon(points){

//获取json数据

var result = null;

$.ajax({

type:"GET",

url:"/js/data/LatAndLon.json",

async:false,

success:function(data){

result = acquireMark(data,points);

},

error:function(data){

console.log(data);

}

});

function acquireMark(data,points){

var obj = new Function("return" + data)();

console.log(obj);

var i=0 ,j = 0,

pointX = points.x.toFixed(3),//lon117

pointY = points.y.toFixed(3);//lat171

console.log(pointX);

console.log(pointY);

var arrMarks =[];

//拿到所有的lon整数部分相等的数据

for(i;i

var lonParseInt =parseInt(obj.lon[i][0]);

if(lonParseInt==parseInt(pointX)){

console.log("mark is",obj.lon[i][0]);

//匹配最佳的数据位置

if(pointX==obj.lon[i][0]){

arrMarks.push(obj.lon[i][1]);

};

if(obj.lon[i][0]

console.log("obj.lon[i][1]",obj.lon[i][1]);

arrMarks.push(obj.lon[i][1]);

break;

};

if(obj.lon[i][0]>pointX){

arrMarks.push(obj.lon[i][1]);

break;

}

}

}

//拿到所有的lat整数部分相等的数据

for(j;j

var latParseInt = parseInt(obj.lat[j][0]);

if(latParseInt == parseInt(pointY)){

//匹配最佳的数据位置

if(pointY==obj.lat[j][0]){

console.log("obj.lat[i][1]",obj.lat[j][1]);

arrMarks.push(obj.lat[j][1]);

}else if(obj.lat[j][0]

console.log("obj.lat[j][1]",obj.lat[j][1]);

arrMarks.push(obj.lat[j][1]);

break;

};

if(obj.lat[j][0]>pointY){

arrMarks.push(obj.lat[j][1]);

break;

}

}

}

console.log(arrMarks);//lon + lat 编号

//将arrMarks编号post到后台,读取本地nc文件里的数据

return arrMarks;

// postArrMarks(arrMarks[1],arrMarks[0]);

}

return result;

}

ajax请求数据

var chart3 = $.ajax({

type:"GET",

url:"/api/config/PostCoordinates/"+marks[1]+"/"+marks[0],

success:function(data){

console.log(data);

},

error:function(){

console.log("error")

}

});

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐