前文中,提到了等值面的生成,后面有人经常会问等值线的生成,本文在前文的基础上做了一点修改,完成了等值线的geotools生成。
package com.lzugis.geotools;
import com.amazonaws.util.json.JSONObject;
import com.lzugis.CommonMethod;
import com.lzugis.geotools.utils.FeaureUtil;
import com.lzugis.geotools.utils.GeoJSONUtil;
import com.vividsolutions.jts.geom.Geometry;
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.*;
/**
* Created by admin on 2017/8/29.
*/
public class EquiSurfaceLine {
private static String rootPath = System.getProperty("user.dir");
/**
* 生成等值面
*
* @param trainData 训练数据
* @param dataInterval 数据间隔
* @param size 大小,宽,高
* @param boundryFile 四至
* @param isclip 是否裁剪
* @return
*/
public String calEquiSurface(double[][] trainData,
double[] dataInterval,
int[] size,
String boundryFile,
boolean isclip) {
String geojsonline = "";
try {
double _undefData = -9999.0;
SimpleFeatureCollection polylineCollection = null;
List cPolylineList = new ArrayList();
List cPolygonList = new ArrayList();
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 _borders = Contour.tracingBorders(_gridData, _X, _Y,
S1, _undefData);
/**
* double[][] S0,
* double[] X,
* double[] Y,
* int nc,
* double[] contour,
* double undefData,
* List 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 = clipFeatureCollection(fc, polylineCollection);
geojsonline = getPolylineGeoJson(dc.getFeatures());
}
} catch (Exception e) {
e.printStackTrace();
}
return geojsonline;
}
public String getPolylineGeoJson(FeatureCollection fc) {
FeatureJSON fjson = new FeatureJSON();
StringBuffer sb = new StringBuffer();
try {
sb.append("{\"type\": \"FeatureCollection\",\"features\": ");
FeatureIterator itertor = fc.features();
List list = new ArrayList();
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 FeatureSource clipFeatureCollection(FeatureCollection fc,
SimpleFeatureCollection gs) {
FeatureSource cs = null;
try {
List
技术博客
CSDN:http://blog.csdn.NET/gisshixisheng
在线教程
https://edu.csdn.net/course/detail/799
https://edu.csdn.net/course/detail/7471
联系方式
类型 | 内容 |
---|---|
1004740957 | |
公众号 | lzugis15 |
[email protected] | |
webgis群 | 452117357 |
Android群 | 337469080 |
GIS数据可视化群 | 458292378 |