1、环境版本
序号 | 类别 | 版本 |
---|---|---|
1 | JDK | 1.8 |
2 | Junit | 4.13.1 |
3 | Geotools | 25.4 |
4 | Collections | 3.2.2 |
2、pom文件编写
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>geotools-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>geotools-demo</name>
<description>geotools-demo</description>
<properties>
<java.version>1.8</java.version>
<geotools.version>25.4</geotools.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.2</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-shapefile</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-swing</artifactId>
<version>${geotools.version}</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>osgeo</id>
<name>OSGeo Release Repository</name>
<url>https://repo.osgeo.org/repository/release/</url>
<snapshots><enabled>false</enabled></snapshots>
<releases><enabled>true</enabled></releases>
</repository>
<repository>
<id>osgeo-snapshot</id>
<name>OSGeo Snapshot Repository</name>
<url>https://repo.osgeo.org/repository/snapshot/</url>
<snapshots><enabled>true</enabled></snapshots>
<releases><enabled>false</enabled></releases>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3、WTK读写工具类
package com.example.geotoolsdemo.utils;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.WKTReader;
import org.locationtech.jts.io.WKTWriter;
/**
* wktutil
*
* @author tree
* @date 2022/01/21
*/
public class WKTUtil {
public static String geomToWkt(Geometry geometry) {
WKTWriter writer = new WKTWriter();
return writer.write(geometry);
}
public static Geometry wktToGeom(String wkt) throws ParseException {
Geometry geometry = null;
WKTReader reader = new WKTReader();
geometry = reader.read(wkt);
return geometry;
}
}
4、Shape文件工具类
package com.example.geotoolsdemo.utils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
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.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import java.io.*;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ShapeUtil {
public static final String DEF_GEOM_KEY = "the_geom";
public static final String DEF_ENCODE = "uft-8";
/**
* 图形信息写入shp文件。shape文件中的geometry附带属性类型仅支持String(最大255)、Integer、Double、Boolean、Date(只包含日期,不包含时间);
* 附带属性的name仅支持15字符,多余的自动截取。
* @param shpPath shape文件路径,包括shp文件名称 如:D:\data\tmp\test.shp
* @param geomType 图形信息类型 Geometry类型,如Point.class、Polygon.class等
* @param data 图形信息集合
*/
public static void createShp(String shpPath, Class<?> geomType, List<Map<String, ?>> data) {
try {
createShp(shpPath, DEF_ENCODE, geomType, data);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 图形信息写入shp文件。shape文件中的geometry附带属性类型仅支持String(最大255)、Integer、Double、Boolean、Date(只包含日期,不包含时间);
* 附带属性的name仅支持15字符,多余的自动截取。
* @param shpPath shape文件路径,包括shp文件名称 如:D:\data\tmp\test.shp
* @param encode shp文件编码
* @param geomType 图形信息类型 Geometry类型,如Point.class、Polygon.class等
* @param data 图形信息集合
*/
public static void createShp(String shpPath, String encode, Class<?> geomType, List<Map<String, ?>> data) {
try {
if (StringUtils.isEmpty(shpPath)) {
throw new Exception("shp文件的路径不能为空,shpPath: " + shpPath);
}
if (StringUtils.isEmpty(encode)) {
throw new Exception("shp文件的编码不能为空,encode: " + encode);
}
if (Objects.isNull(geomType)) {
throw new Exception("shp文件的图形类型不能为空,geomType: " + geomType);
}
if (CollectionUtils.isEmpty(data)) {
throw new Exception("shp文件的图形数据不能为空,data: " + data);
}
if (!data.get(0).containsKey(DEF_GEOM_KEY)) {
throw new Exception("shp文件的图形数据中必须包含the_geom的属性,data: " + data);
}
//创建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);
//定义图形信息和属性信息
ds.createSchema(builderFeatureType(geomType, CollectionUtils.isEmpty(data) ? null : data.get(0)));
//设置编码
Charset charset = Charset.forName(encode);
ds.setCharset(charset);
//设置Writer
FeatureWriter<SimpleFeatureType, SimpleFeature> writer = ds.getFeatureWriter(ds.getTypeNames()[0], Transaction.AUTO_COMMIT);
for (Map<String, ?> map : data) {
//写下一条
SimpleFeature feature = writer.next();
for (String key : map.keySet()) {
if (DEF_GEOM_KEY.equals(key)) {
feature.setAttribute(key, map.get(key));
} else {
feature.setAttribute(key.toUpperCase(), map.get(key));
}
}
}
writer.write();
writer.close();
ds.dispose();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 构建Feature模板
* @param geomType 图形信息类型 Geometry类型,如Point.class、Polygon.class等
* @param data 图形信息具体的属性
* @return featureType
*/
public static SimpleFeatureType builderFeatureType(Class<?> geomType, Map<String, ?> data) {
//定义图形信息和属性信息
SimpleFeatureTypeBuilder ftb = new SimpleFeatureTypeBuilder();
ftb.setCRS(DefaultGeographicCRS.WGS84);
ftb.setName("shapefile");
ftb.add(DEF_GEOM_KEY, geomType);
if (MapUtils.isNotEmpty(data)) {
for (String key : data.keySet()) {
if (Objects.nonNull(data.get(key))) {
ftb.add(key.toUpperCase(), data.get(key).getClass());
}
}
}
return ftb.buildFeatureType();
}
/**
* 压缩shape文件
*
* @param shpPath shape文件路径(包含shape文件名称)
*/
public static void zipShapeFile(String shpPath) {
try {
File shpFile = new File(shpPath);
String shpRoot = shpFile.getParentFile().getPath();
String shpName = shpFile.getName().substring(0, shpFile.getName().lastIndexOf("."));
String zipPath = shpRoot + File.separator + shpName + ".zip";
File zipFile = new File(zipPath);
InputStream input = null;
ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
// zip的名称为
zipOut.setComment(shpName);
String[] shpFiles = new String[]{
shpRoot + File.separator + shpName + ".dbf",
shpRoot + File.separator + shpName + ".prj",
shpRoot + File.separator + shpName + ".shp",
shpRoot + File.separator + shpName + ".shx",
shpRoot + File.separator + shpName + ".fix"
};
for (int i = 0; i < shpFiles.length; i++) {
File file = new File(shpFiles[i]);
input = new FileInputStream(file);
zipOut.putNextEntry(new ZipEntry(file.getName()));
int temp = 0;
while ((temp = input.read()) != -1) {
zipOut.write(temp);
}
input.close();
}
zipOut.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
5、测试类
package com.example.geotoolsdemo;
import com.example.geotoolsdemo.utils.ShapeUtil;
import com.example.geotoolsdemo.utils.WKTUtil;
import org.junit.Test;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.Polygon;
import org.locationtech.jts.io.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class GeotoolsDemoApplicationTests {
@Test
public void testShp() throws ParseException {
String url = "D:\\00testdata\\tmp\\test.shp";
String str = "POLYGON ((116.21278950384274 39.90557982319698, 116.21177234433465 39.90610963061354, 116.21106912279264 39.90264172209895, 116.21399502548638 39.902612822554126, 116.21629305278306 39.905011479365406, 116.21278950384274 39.90557982319698))";
Geometry geom = WKTUtil.wktToGeom(str);
Map<String, Object> m = new HashMap<>();
m.put(ShapeUtil.DEF_GEOM_KEY, geom);
m.put("attr", "这是第1");
m.put("attr2", 456);
m.put("attr3", 23.5d);
m.put("attr4", true);
m.put("attr5", "");
String str2 = "POLYGON((113.38185597038 34.54828048706,113.38224220848 34.548355588913,113.38249970055 34.548108825684,113.38237095451 34.54787279129,113.38208127594 34.547786960602,113.38185597038 34.54828048706))";
Geometry geom2 = WKTUtil.wktToGeom(str2);
Map<String, Object> m2 = new HashMap<>();
m2.put(ShapeUtil.DEF_GEOM_KEY, geom2);
m2.put("attr", "这是第2");
m2.put("attr2", 4562);
m2.put("attr3", 23.52d);
m2.put("attr4", false);
m2.put("attr5", null);
List<Map<String, ?>> data = new ArrayList<>();
data.add(m);
data.add(m2);
//将geometry写入shape文件
ShapeUtil.createShp(url, "utf-8", Polygon.class, data);
//压缩shape文件
ShapeUtil.zipShapeFile(url);
}
}
6、测试结果
7、Demo源码链接及参考地址
源码链接:https://gitee.com/songzongqiang/geotools-demo
参考地址:https://docs.geotools.org/latest/userguide/library/data/shape.html
Geotools使用20以下版本可参考:https://blog.csdn.net/u010197332/article/details/105383393