项目上需要根据降雨量数据,生成geojson数据返回给前端,之前也没有做过类似的需求,故百度一番,发现资料不少,有些基本上是源码都贡献出来了,简直是手把手教你生成geojson,遂copy到本地验证,结果发现出来的效果不尽人意,试了几个不同案例,效果都差强人意,苦思了多日还是不行,遂作罢,暂且放下吧,后面通过查看源码,调整参数,修改shp文件的方式最终生成了效果还过得去的geojson数据,
下面贴上核心代码,方便自己查看,也方便有相关需求的开发者参考
ps:插值部分主要是利用wContour包里面的IDW插值方法,算是一种比较经典的插值方法,简单高效,效果可能没克里金法好,不过也够用了
@Override
public String calculateIsoSurface(String start, String end) throws Exception {
String geojsonpogylon = "";
try {
double _undefData = -9999.0;
int[] size = new int[]{200,200};
double[] dataInterval = new double[]{0, 1, 10.0, 25.0, 50.0, 100.0, 250.0};
List<PolyLine> cPolylineList;
List<Polygon> cPolygonList;
int width = size[0], height = size[1];
double[] _X = new double[width];
double[] _Y = new double[height];
List<Map<String, Object>> rains = stpptnrMapper.getRainI(start, end);
//如果查询到降雨为空,
if (rains.size() <= 0) {
return null;
}
File file = new File(shpPath+ File.separator+"XXXX.shp");
ShapefileDataStore shpDataStore = null;
shpDataStore = new ShapefileDataStore(file.toURI().toURL());
//设置编码
Charset charset = Charset.forName("GBK");
shpDataStore.setCharset(charset);
String typeName = shpDataStore.getTypeNames()[0];
SimpleFeatureSource featureSource = null;
featureSource = shpDataStore.getFeatureSource(typeName);
SimpleFeatureCollection fc = featureSource.getFeatures();
//获取shp边界,生成格网点,注意网格范围
double minX = fc.getBounds().getMinX();
double minY = fc.getBounds().getMinY();
double maxX = fc.getBounds().getMaxX();
double maxY = fc.getBounds().getMaxY();
Interpolate.createGridXY_Num(minX, minY, maxX, maxY, _X, _Y);
double[][] _gridData = new double[width][height];
int nc = dataInterval.length;
double[][] trainData = new double[rains.size()][3];
for (int i = 0; i < rains.size(); i++) {
Map<String, Object> rain = rains.get(i);
trainData[