一、java代码

package cn.cepec.talroad.air.common;import cn.cepec.talroad.air.common.geoutil.FeaureUtil;
import cn.cepec.talroad.air.common.geoutil.GeoJSONUtil;
import com.vividsolutions.jts.geom.Geometry;
import net.sf.json.JSONObject;
import org.geotools.data.FeatureSource;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureIterator;
import org.geotools.geojson.feature.FeatureJSON;
import org.opengis.feature.Feature;
import org.opengis.feature.simple.SimpleFeature;
import wcontour.Contour;
import wcontour.global.Border;
import wcontour.global.PointD;
import wcontour.global.PolyLine;
import wcontour.global.Polygon;
import wcontour.Interpolate;import java.io.File;
import java.io.StringWriter;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;public class InterpolationUtils {/*** 生成等值面** @param trainData    训练数据double[size][3],r,lon、lat、value* @param dataInterval 数据间隔double[0.0001,25,50,100,200,300]* @param size         大小,宽,高new int[]{100, 100};* @param boundryFile  四至.shp* @param isclip       是否裁剪* @return*/// 解析Shapefile文件public static String calEquiSurface(double[][] trainData, double[] dataInterval, int[] size, String boundryFile,boolean isclip) {String geojsonpogylon = "";try {double _undefData = -9999.0;// 多边形集合SimpleFeatureCollection polygonCollection = null;// 多边形线集合List<PolyLine> cPolylineList = new ArrayList<PolyLine>();// 多边形ListList<Polygon> cPolygonList = new ArrayList<Polygon>();int width = size[0], height = size[1];double[] _X = new double[width];double[] _Y = new double[height];File file = new File(boundryFile);// 源shape文件ShapefileDataStore shpDataStore = null;shpDataStore = new ShapefileDataStore(file.toURL());// 设置编码Charset charset = Charset.forName("GBK");shpDataStore.setCharset(charset);// 获取文件后缀名String typeName = shpDataStore.getTypeNames()[0];// SimpleFeatureSource相当于AE中的featureClass,设置属性SimpleFeatureSource featureSource = null;featureSource = shpDataStore.getFeatureSource();SimpleFeatureCollection fc = featureSource.getFeatures();// 最大最小经纬度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;// IDW插值 (训练数据(离散数据阵列)、宽(栅格X阵)、高(栅格Y阵)、默认数(最近邻居数))_gridData = Interpolate.interpolation_IDW_Neighbor(trainData, _X, _Y, 12, _undefData);int[][] S1 = new int[_gridData.length][_gridData[0].length];// 边界List<Border> _borders = Contour.tracingBorders(_gridData, _X, _Y, S1, _undefData);// 生成等值线(IDW插值、宽、高、数据间隔长度、数据间隔、默认值、边界、IDW长度)cPolylineList = Contour.tracingContourLines(_gridData, _X, _Y, nc, dataInterval, _undefData, _borders, S1);// 平滑cPolylineList = Contour.smoothLines(cPolylineList);cPolygonList = Contour.tracingPolygons(_gridData, cPolylineList, _borders, dataInterval);// 多边形Jsongeojsonpogylon = getPolygonGeoJson(cPolygonList);if (isclip) {// 读取GeoJSON字符串 返回 SimpleFeatureCollection 要素集合polygonCollection = GeoJSONUtil.readGeoJsonByString(geojsonpogylon);// 裁剪等值面FeatureSource dc = clipFeatureCollection(fc, polygonCollection);// 得到多边形GeoJsongeojsonpogylon = getPolygonGeoJson(dc.getFeatures());}} catch (Exception e) {e.printStackTrace();}return geojsonpogylon;}/*** 生成等值线** @param trainData    训练数据* @param dataInterval 数据间隔* @param size         大小,宽,高* @param boundryFile  四至* @param isclip       是否裁剪* @return*/public static String calEquiSurfaceLine(double[][] trainData, double[] dataInterval, int[] size, String boundryFile,boolean isclip) {String geojsonline = "";try {double _undefData = -9999.0;SimpleFeatureCollection polylineCollection = null;List<PolyLine> cPolylineList = new ArrayList<PolyLine>();List<Polygon> cPolygonList = new ArrayList<Polygon>();int width = size[0],height = size[1];double[] _X = new double[width];double[] _Y = new double[height];File file = new File(boundryFile);ShapefileDataStore shpDataStore = null;shpDataStore = new ShapefileDataStore(file.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();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;_gridData = Interpolate.interpolation_IDW_Neighbor(trainData,_X, _Y, 12, _undefData);// IDW插值int[][] S1 = new int[_gridData.length][_gridData[0].length];/*** double[][] S0,* double[] X,* double[] Y,* int[][] S1,* double undefData*/List<Border> _borders = Contour.tracingBorders(_gridData, _X, _Y,S1, _undefData);/*** double[][] S0,* double[] X,* double[] Y,* int nc,* double[] contour,* double undefData,* List<Border> borders,* int[][] S1*/cPolylineList = Contour.tracingContourLines(_gridData, _X, _Y, nc,dataInterval, _undefData, _borders, S1);// 生成等值线cPolylineList = Contour.smoothLines(cPolylineList);// 平滑geojsonline = getPolylineGeoJson(cPolylineList);if (isclip) {polylineCollection = GeoJSONUtil.readGeoJsonByString(geojsonline);FeatureSource dc = clipFeatureLineCollection(fc, polylineCollection);geojsonline = getPolylineGeoJson(dc.getFeatures());}} catch (Exception e) {e.printStackTrace();}return geojsonline;}// 等值面private static String getPolygonGeoJson(List<Polygon> cPolygonList) {String geo = null;String geometry = " { \"type\":\"Feature\",\"geometry\":";String properties = ",\"properties\":{ \"hvalue\":";String head = "{\"type\": \"FeatureCollection\"," + "\"features\": [";String end = "  ] }";if (cPolygonList == null || cPolygonList.size() == 0) {return null;}try {for (Polygon pPolygon : cPolygonList) {List<Object> ptsTotal = new ArrayList<Object>();List<Object> pts = new ArrayList<Object>();PolyLine pline = pPolygon.OutLine;for (PointD ptD : pline.PointList) {List<Double> pt = new ArrayList<Double>();pt.add(ptD.X);pt.add(ptD.Y);pts.add(pt);}ptsTotal.add(pts);if (pPolygon.HasHoles()) {for (PolyLine cptLine : pPolygon.HoleLines) {List<Object> cpts = new ArrayList<Object>();for (PointD ccptD : cptLine.PointList) {List<Double> pt = new ArrayList<Double>();pt.add(ccptD.X);pt.add(ccptD.Y);cpts.add(pt);}if (cpts.size() > 0) {ptsTotal.add(cpts);}}}JSONObject js = new JSONObject();js.put("type", "Polygon");js.put("coordinates", ptsTotal);double hv = pPolygon.HighValue;double lv = pPolygon.LowValue;if (hv == lv) {if (pPolygon.IsClockWise) {if (!pPolygon.IsHighCenter) {hv = hv - 0.1;lv = lv - 0.1;}} else {if (!pPolygon.IsHighCenter) {hv = hv - 0.1;lv = lv - 0.1;}}} else {if (!pPolygon.IsClockWise) {lv = lv + 0.1;} else {if (pPolygon.IsHighCenter) {hv = hv - 0.1;}}}geo = geometry + js.toString() + properties + hv + ", \"lvalue\":" + lv + "} }" + "," + geo;}if (geo.contains(",")) {geo = geo.substring(0, geo.lastIndexOf(","));}geo = head + geo + end;} catch (Exception e) {e.printStackTrace();return geo;}return geo;}private static String getPolygonGeoJson(FeatureCollection fc) {FeatureJSON fjson = new FeatureJSON();StringBuffer sb = new StringBuffer();try {sb.append("{\"type\": \"FeatureCollection\",\"features\": ");FeatureIterator itertor = fc.features();List<String> list = new ArrayList<String>();while (itertor.hasNext()) {SimpleFeature feature = (SimpleFeature) itertor.next();// 用流写StringWriter writer = new StringWriter();fjson.writeFeature(feature, writer);list.add(writer.toString());}itertor.close();sb.append(list.toString());sb.append("}");} catch (Exception e) {e.printStackTrace();}return sb.toString();}// 裁剪等值面private static FeatureSource clipFeatureCollection(FeatureCollection fc, SimpleFeatureCollection gs) {FeatureSource cs = null;try {List<Map<String, Object>> values = new ArrayList<Map<String, Object>>();// 多边形FeatureIterator contourFeatureIterator = gs.features();// 数据FeatureIterator dataFeatureIterator = fc.features();// 迭代读取数据记录while (dataFeatureIterator.hasNext()) {// 数据下一条数据Feature dataFeature = dataFeatureIterator.next();// 数据"the_geom"属性的值Geometry dataGeometry = (Geometry) dataFeature.getProperty("the_geom").getValue();while (contourFeatureIterator.hasNext()) {Feature contourFeature = contourFeatureIterator.next();Geometry contourGeometry = (Geometry) contourFeature.getProperty("geometry").getValue();double lv = (Double) contourFeature.getProperty("lvalue").getValue();double hv = (Double) contourFeature.getProperty("hvalue").getValue();if (dataGeometry.intersects(contourGeometry)) {Geometry geo = dataGeometry.intersection(contourGeometry);Map<String, Object> map = new HashMap<String, Object>();map.put("the_geom", geo);map.put("lvalue", lv);map.put("hvalue", hv);values.add(map);}}}contourFeatureIterator.close();dataFeatureIterator.close();SimpleFeatureCollection sc = FeaureUtil.creatSimpleFeatureByFeilds("polygons","crs:4326,the_geom:MultiPolygon,lvalue:double,hvalue:double", values);cs = FeaureUtil.creatFeatureSourceByCollection(sc);} catch (Exception e) {e.printStackTrace();return cs;}return cs;}public static String getPolylineGeoJson(FeatureCollection fc) {FeatureJSON fjson = new FeatureJSON();StringBuffer sb = new StringBuffer();try {sb.append("{\"type\": \"FeatureCollection\",\"features\": ");FeatureIterator itertor = fc.features();List<String> list = new ArrayList<String>();while (itertor.hasNext()) {SimpleFeature feature = (SimpleFeature) itertor.next();StringWriter writer = new StringWriter();fjson.writeFeature(feature, writer);list.add(writer.toString());}itertor.close();sb.append(list.toString());sb.append("}");} catch (Exception e) {e.printStackTrace();}return sb.toString();}public static String getPolylineGeoJson(List<PolyLine> cPolylineList) {String geo = null;String geometry = " { \"type\":\"Feature\",\"geometry\":";String properties = ",\"properties\":{ \"value\":";String head = "{\"type\": \"FeatureCollection\"," + "\"features\": [";String end = "  ] }";if (cPolylineList == null || cPolylineList.size() == 0) {return null;}try {for (PolyLine pPolyline : cPolylineList) {List<Object> ptsTotal = new ArrayList<Object>();for (PointD ptD : pPolyline.PointList) {List<Double> pt = new ArrayList<Double>();pt.add(ptD.X);pt.add(ptD.Y);ptsTotal.add(pt);}JSONObject js = new JSONObject();js.put("type", "LineString");js.put("coordinates", ptsTotal);geo = geometry + js.toString() + properties + pPolyline.Value + "} }" + "," + geo;}if (geo.contains(",")) {geo = geo.substring(0, geo.lastIndexOf(","));}geo = head + geo + end;} catch (Exception e) {e.printStackTrace();return geo;}return geo;}public static FeatureSource clipFeatureLineCollection(FeatureCollection fc,SimpleFeatureCollection gs) {FeatureSource cs = null;try {List<Map<String, Object>> values = new ArrayList<Map<String, Object>>();FeatureIterator contourFeatureIterator = gs.features();FeatureIterator dataFeatureIterator = fc.features();while (dataFeatureIterator.hasNext()) {Feature dataFeature = dataFeatureIterator.next();Geometry dataGeometry = (Geometry) dataFeature.getProperty("the_geom").getValue();while (contourFeatureIterator.hasNext()) {Feature contourFeature = contourFeatureIterator.next();Geometry contourGeometry = (Geometry) contourFeature.getProperty("geometry").getValue();double v = (Double) contourFeature.getProperty("value").getValue();if (dataGeometry.intersects(contourGeometry)) {Geometry geo = dataGeometry.intersection(contourGeometry);Map<String, Object> map = new HashMap<String, Object>();map.put("the_geom", geo);map.put("value", v);values.add(map);}}}contourFeatureIterator.close();dataFeatureIterator.close();SimpleFeatureCollection sc = FeaureUtil.creatSimpleFeatureByFeilds("polygons","crs:4326,the_geom:LineString,value:double",values);cs = FeaureUtil.creatFeatureSourceByCollection(sc);} catch (Exception e) {e.printStackTrace();return cs;}return cs;}
}

二、关键结构

三、代码调用

四、访问请求结果

五、iClientOL配置调用

六、展示效果

 如果对您有帮助

 感谢支持技术分享,请扫码点赞支持:

技术合作交流qq:2401315930

java实现反距离权重插值算法生成geojson矢量数据相关推荐

  1. 反距离加权插值法例题_GMS插值中的反距离权重法(Inverse distance weighted)

    反距离权重法是GMS地层插值的默认方法,了解一些关于它的原理会帮助得到更好的插值结果.这次主要介绍Shepard's method方法.反距离权重法基本思路:插值点受附近点的影响最大,而距离较远的点的 ...

  2. ArcGIS空间插值方法反距离权重法(IDW)的工作原理

    反距离权重 (IDW) 插值使用一组采样点的线性权重组合来确定像元值.权重是一种反距离函数.进行插值处理的表面应当是具有局部因变量的表面. 此方法假定所映射的变量因受到与其采样位置间的距离的影响而减小 ...

  3. GDAL+Basemap+IDW(反距离权重)代替ARCPY,制作温度、降雨分布图

    目录 一.不同差值效果对比 二.制图代码 2.1用到的模块 2.1.1遇到的问题 2.2经纬度转shp坐标点 2.2.1遇到的问题 2.3IDW(反距离权重) 2.3.1遇到的问题 2.4利用Base ...

  4. 反距离权重加权插值的理解及Python实现

    IDW(反距离里加权插值) 假设 距离较近的事物要比距离较远的事物更相似. 当为任何未测量的位置预测值时,反距离权重法会采用预测位置周围的测量值与距离预测位置较远的测量值相比,距离预测位置最近的测量值 ...

  5. python反距离权重法_反距离权重法 (Spatial Analyst)—ArcMap | 文档

    使用反距离权重法 (IDW) 获得的像元输出值限定在插值时用到的值范围之内.因为反距离权重法是加权平均距离,所以该平均值不可能大于最大输入或小于最小输入.因此,如果尚未对这些极值采样,便无法创建山脊或 ...

  6. gstat | 空间插值(一)——反距离权重插值;使用ggplot2绘制地图

    本篇既是空间插值系列的第一篇推文,也是ggplot2工具包系列推文中的一篇.空间插值使用的工具包是gstat,该工具包主要用于地统计分析. library(gstat) 示例数据来自HSAR工具包: ...

  7. 【数据可视化应用】Python反距离权重(IDW)插值计算及可视化绘制

    本文我们将介绍IDW(反距离加权法(Inverse Distance Weighted)) 插值的Python计算方法及插值结果的可视化绘制过程.主要涉及的知识点如下: IDW简介 自定义Python ...

  8. python反距离权重法_使用Python进行反距离加权(IDW)插值

    10月20日改变:这个类Invdisttree组合了反距离权重 scipy.spatial.KDTree. 忘记原来的强力回答; 这是分散数据插值的选择方法. """ i ...

  9. matlab反插值,matlab反距离权重插值

    [实例简介] 用于matlab的反距离权重插值函数,是比较方便的插值方法之一 [实例截图] [核心代码] 29468178mkriging └── mkriging ├── check.asv ├── ...

最新文章

  1. 2022-2028年中国微滤膜行业市场发展调研及投资前景分析报告
  2. iOS审核秘籍】提审资源检查大法
  3. python【力扣LeetCode算法题库】7- 整数反转
  4. Facebook 最新力作 FBNetV3来了!相比 ResNeSt 提速 5 倍,精度不输 EfficientNet
  5. hibernate 环境搭建测试
  6. linux cpu核数查看_Linux日常必备的 8 个小技能
  7. 7-21 求前缀表达式的值 (25 分)(思路详解)
  8. JavaScript之DOM对象(Event事件)
  9. mysql服务没有权限开机不会自启动_计算机服务项里没有MySQL服务,无法设置开机自启动或者关闭自启动,的解决办法...
  10. 【sql server】“已更新或删除的行值要么不能使该行成为唯一行,要么改变了多个行“ 解决方案
  11. bzoj 1854: [Scoi2010]游戏(并查集)
  12. linux vi编辑撤销,vi撤销命令(u和U),撤销上一次的操作
  13. 《数学之美》第30章 Google大脑和人工神经网络
  14. 解决H5 在安卓手机上软键盘遮盖input的问题
  15. C语言实现入门级小游戏——三子棋
  16. SAP那些事-职业篇-23-我有个梦想:做一个不加班的项目
  17. matlab中牛顿下山法实例,非线性方程的数值解法牛顿下山法matlab
  18. MySQL数据库的InnoDB引擎TableSpaceExists问题解决
  19. 一位友人做足这些准备,终于拿到了FaceBook的offer
  20. UCA-系统工程师学习-使用dpkg管理软件包

热门文章

  1. 无数据完成kaldi_lre07实验
  2. 解决***.dll没有被指定在windows上运行或者它包含错误的问题
  3. wsl使用可视化界面_win10安装子系统ubuntu附带图形化界面
  4. 汽车美容店如何提升门店营业额
  5. 使用Microsoft Hyperspace索引Spark数据
  6. 线性代数基础9--正交矩阵与行列式基础,代数余子式
  7. java 正则 小数_java用正则表达式判断是否是小数的方法
  8. 大二第一学期个人总结
  9. php 多组radiobutton,RadioButton实现多选一
  10. 利用随机函数产生并且显示 15 个小写的英文字符,显示排在最前面和最后面的字