Maven依赖:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.76</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-geotiff</artifactId>
<version>25.3.01</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-shapefile</artifactId>
<version>25.3.01</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<!-- 中央仓库没有 gt-geotiff, gt-shapefile 指定新的仓库 -->
<repositories>
<repository>
<id>GeoSolutions</id>
<url>http://maven.geo-solutions.it/</url>
</repository>
</repositories>
指定仓库参考说明
Maven中GeoTools的引入 - Maven 的 repository 与 mirror_maven geotools-CSDN博客
import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.geotools.data.FeatureWriter;
import org.geotools.data.Transaction;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.shapefile.ShapefileDataStoreFactory;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.junit.Test;
import org.locationtech.jts.geom.*;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import java.io.File;
import java.io.Serializable;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @Author Cookie
* @Date 2024/7/30
* @Desc
*/
public class Write2Shape {
/**
* 生成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);
}
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();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 处理为几何图形(多边形)和名称的 Map 对象
* @param rangeArray
* @param name
* @return
*/
private Map<String, Object> getMap(JSONArray rangeArray, String name) {
Coordinate[] coordinates = new Coordinate[rangeArray.size()];
for (int i = 0; i < rangeArray.size(); i++) {
Coordinate coordinate = new Coordinate();
coordinate.x = rangeArray.getJSONArray(i).getDouble(0);
coordinate.y = rangeArray.getJSONArray(i).getDouble(1);
coordinates[i] = coordinate;
}
// 创建一个多边形(Polygon)对象 geometry,该多边形由 coordinates 数组中的坐标点组成。
Geometry geometry = new GeometryFactory().createPolygon(coordinates);
Map<String, Object> map1 = new HashMap<>();
// 设置属性设置值
map1.put("name", name);
map1.put("geom", geometry);
return map1;
}
/**
* 生成地块的shp文件
*/
@Test
public void getSharp() {
// 最终图层List
List<Map<String, Object>> data = new ArrayList<>();
// 获取地块列表的JSON
String json = FileUtil.readString("C:\\Users\\xxx\\Desktop\\json.json", StandardCharsets.UTF_8);
JSONArray baseArray = JSONObject.parseArray(json);
for (int i = 0; i < baseArray.size(); i++) {
// 解析数据源
JSONObject jsonObject = baseArray.getJSONObject(i);
String plotName = jsonObject.get("plot_name").toString();
// 经纬度二维数组
// 注: 需要注意的是这里需要是一个封闭的空间, ====即首尾坐标相同====
JSONArray range = jsonObject.getJSONArray("plot_range");
// 注: 这里设置地块名是因为后面想根据不同的地块名这个属性上色,所以给每个地块设置了属性。便于根据地块名设置颜色。 如果没有这个需求可以不处理为空
Map<String, Object> map = getMap(range, plotName);
data.add(map);
}
List<String> list = new ArrayList<>();
list.add("name");
write2Shape("C:\\Users\\xxx\\Desktop\\shp-new\\plot.shp", "utf-8", "MultiPolygon", "geom", list, data);
}
}
注: 以上使用到的json格式大致如下
这里提供了一个测试JSON如果需要测试的j可以一试
https://download.csdn.net/download/qq_47910339/89598370
执行可以看到在文件夹中有以下五个文件
在ArcMap中打开shp查看
选择展示属性
得到如下图片:
使用测试arcMap中查看如下: