Java生成Shp文件-点(Point)
生成面相关Shp:
1. 使用的依赖和面相同
同上
2. 待处理的JSON数据格式
[
{"yield":"226.82","coordinates":[123.6705525,48.434689999999996],"wybm":"050116802"},
{"yield":"763.78","coordinates":[123.67096099999999,48.484783],"wybm":"050116801"},
{"yield":"221.35","coordinates":[123.6707525,48.431956500000005],"wybm":"050116800"},
{"yield":"764.45","coordinates":[123.671904,48.585055499999996],"wybm":"050116809"},
{"yield":"753.23","coordinates":[123.66973899999999,48.4300209996],"wybm":"050116805"},
{"yield":"252.20","coordinates":[120.545404,50.141742666666666],"wybm":"0501041678"},{"yield":"490.28","coordinates":[120.56136329855504,50.30223093866174],"wybm":"0501041679"},
{"yield":"771.18","coordinates":[123.662247,48.707646000000004],"wybm":"0501161576"}
]
3.生成shp示例方法
/**
* 生成shape文件
*
* @param shpPath 生成shape文件路径(包含文件名称)
* @param encode 编码
* @param geoType 图幅类型,Point和Rolygon
* @param shpKey data中图幅的key
* @param attrKeys 属性key集合
* @param data 图幅和属性集合
*/
public static void write2Shape(String shpPath, String encode, String geoType, String shpKey, List<String> attrKeys, List<Map<String, Object>> data) {
try {
if (data == null || data.size() == 0) {
return;
}
//创建shape文件对象
File file = new File(shpPath);
Map<String, Serializable> params = new HashMap<>();
params.put(ShapefileDataStoreFactory.URLP.key, file.toURI().toURL());
ShapefileDataStore ds = (ShapefileDataStore) new ShapefileDataStoreFactory().createNewDataStore(params);
//定义图形信息和属性信息
SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder();
tb.setCRS(DefaultGeographicCRS.WGS84);
tb.setName("shapefile");
if ("Polygon".equals(geoType)) {
tb.add("the_geom", Polygon.class);
} else if ("MultiPolygon".equals(geoType)) {
tb.add("the_geom", MultiPolygon.class);
} else if ("Point".equals(geoType)) {
tb.add("the_geom", Point.class);
} else if ("MultiPoint".equals(geoType)) {
tb.add("the_geom", MultiPoint.class);
} else if ("LineString".equals(geoType)) {
tb.add("the_geom", LineString.class);
} else if ("MultiLineString".equals(geoType)) {
tb.add("the_geom", MultiLineString.class);
} else {
throw new Exception("Geometry中没有该类型:" + geoType);
}
for (String field : attrKeys) {
tb.add(field, String.class);
// tb.add(field.toUpperCase(), String.class);
}
ds.createSchema(tb.buildFeatureType());
//设置编码
Charset charset = Charset.forName(encode);
ds.setCharset(charset);
//设置Writer
FeatureWriter<SimpleFeatureType, SimpleFeature> writer = ds.getFeatureWriter(ds.getTypeNames()[0], Transaction.AUTO_COMMIT);
//写入文件信息
for (int i = 0; i < data.size(); i++) {
SimpleFeature feature = writer.next();
Map<String, Object> row = data.get(i);
Geometry geom = (Geometry) row.get(shpKey);
feature.setAttribute("the_geom", geom);
for (String key : row.keySet()) {
if (!key.equals(shpKey)) {
if (row.get(key) != null) {
feature.setAttribute(key, row.get(key).toString());
} else {
feature.setAttribute(key, "");
}
}
}
}
writer.write();
writer.close();
ds.dispose();
//添加到压缩文件
//zipShapeFile(shpPath);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 处理 点
*
* @param rangeArray
* @param yield
* @return
*/
private Map<String, Object> getMapForPoint(JSONArray rangeArray, String yield, String wybm) {
// 创建点.并设置经纬度
Point point = new GeometryFactory().createPoint(new Coordinate(rangeArray.getDouble(0), rangeArray.getDouble(1)));
Map<String, Object> map = new HashMap<>();
// 设置属性设置值
map.put("yield", yield);
map.put("geom", point);
map.put("wybm", wybm);
return map;
}
/**
* 生成地块的shp文件
*/
@Test
public void getSharp() {
// 最终图层List 存储每个点的数据
List<Map<String, Object>> data = new ArrayList<>();
String json = FileUtil.readString("C:\\Users\\57589\\Desktop\\merge\\finalData.json", StandardCharsets.UTF_8);
JSONArray baseArray = JSONObject.parseArray(json);
for (int i = 0; i < baseArray.size(); i++) {
JSONObject jsonObject = baseArray.getJSONObject(i);
// 获取产量
String yield = jsonObject.get("yield").toString();
// 获取属性
String wybm = jsonObject.get("wybm").toString();
// 获取坐标
JSONArray range = jsonObject.getJSONArray("coordinates");
// 获取处理后的点
Map<String, Object> map = getMapForPoint(range, yield, wybm);
data.add(map);
}
// 指定属性
List<String> list = new ArrayList<>();
list.add("yield");
list.add("wybm");
// geoType, 这里选择MultiPoint. 即多个点
write2Shape("C:\\Users\\xxx\\Desktop\\wybm-yield\\wybm-yield.shp", "utf-8", "MultiPoint", "geom", list, data);
}
在指定路径可以查看到如下文件:
在arcmap中可以查看一下具体的情况. 并选择 想要展示的属性
具体详细的操作方法在这个笔记中
到此"点"的shp文件就生成了