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 EquiSurface {
/**
* 生成等值面
*
* @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 geojsonpogylon = "";
try {
double _undefData = -9999.0;
SimpleFeatureCollection polygonCollection = 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];
List<Border> _borders = Contour.tracingBorders(_gridData, _X, _Y,
S1, _undefData);
cPolylineList = Contour.tracingContourLines(_gridData, _X, _Y, nc,
dataInterval, _undefData, _borders, S1);// 生成等值线
cPolylineList = Contour.smoothLines(cPolylineList);// 平滑
cPolygonList = Contour.tracingPolygons(_gridData, cPolylineList,
_borders, dataInterval);
geojsonpogylon = getPolygonGeoJson(cPolygonList);
if (isclip) {
polygonCollection = GeoJSONUtil.readGeoJsonByString(geojsonpogylon);
FeatureSource dc = clipFeatureCollection(fc, polygonCollection);
geojsonpogylon = getPolygonGeoJson(dc.getFeatures());
}
} catch (Exception e) {
e.printStackTrace();
}
return geojsonpogylon;
}
private 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();
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;
}
private 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 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;
没有合适的资源?快使用搜索试试~ 我知道了~
Java+Geotools+WContour+Openlayers实现等值线等值面功能
共16个文件
java:3个
shp:2个
shx:2个
需积分: 12 15 下载量 90 浏览量
2022-09-28
10:32:15
上传
评论 1
收藏 88.78MB ZIP 举报
温馨提示
Java+Geotools+WContour+Openlayers:生成等值线等值面主要核心jar包,及代码
资源详情
资源评论
资源推荐
收起资源包目录
等值线等值面功能.zip (16个子文件)
openlayers3代码.txt 3KB
geotools-18.1-bin.zip 87.91MB
openlayers3.html 3KB
Geotools代码.txt 12KB
国界shp
bou1_4p.dbf 71KB
bou1_4l.shx 11KB
bou1_4p.shp 1.02MB
bou1_4p.shx 7KB
bou1_4l.dbf 115KB
bou1_4l.shp 1.09MB
util.rar 7KB
wContour-lib
wContour.dll 69KB
wContour.jar 142KB
util
CommonMethod.java 8KB
GeoJSONUtil.java 13KB
FeaureUtil.java 8KB
共 16 条
- 1
银河系0405
- 粉丝: 4
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0