参考文章:
GeoTools:WKT、GeoJson、Feature、FeatureCollection相互转换
转换工具类
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.geotools.data.DataUtilities;
import org.geotools.data.collection.ListFeatureCollection;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.geojson.GeoJSONUtil;
import org.geotools.geojson.feature.FeatureJSON;
import org.geotools.geojson.geom.GeometryJSON;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryCollection;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.io.WKTReader;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import java.io.IOException;
import java.io.Reader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
/**
* <p>
* 空间处理工具
* </p>
*
**/
public class GeoTools {
private static final WKTReader READER = new WKTReader();
/**
* 要素集合根节点
*/
private static final String[] COLLECTION_TYPE = new String[]{
"FeatureCollection"};
/**
* 地理要素类型
*/
private static final String[] FEATURES_TYPE = new String[]{
"Feature"};
/**
* 地理数据类型
* 点、线、面、几何集合
*/
private static final String[] GEO_TYPE = new String[]{
"Geometry", "Point", "LineString", "Polygon", "MultiPoint", "MultiLineString", "MultiPolygon", "GeometryCollection"};
/**
* 获取 Geo 几何类型
* @param wktStr WKT 字符串
* @return Geo 几何类型
*/
public static String getGeometryType(String wktStr) {
String type = null;
if (StrUtil.isNotEmpty(wktStr)) {
try {
Geometry read = READER.read(wktStr);
type = read.getGeometryType();
}catch (Exception e) {
System.out.println("非规范 WKT 字符串:"+ e);
e.printStackTrace();
}
}
return type;
}
/**
* 是规范的 WKT
* @param wktStr WKT 字符串
* @return 是、否
*/
public static boolean isWkt(String wktStr) {
GeoTools:WKT、GeoJSON、Feature转换实用工具

本文介绍了一个名为GeoTools的工具类,它提供了WKT、GeoJSON格式之间的转换,包括Feature和FeatureCollection的处理,适用于地理数据处理和交换。
最低0.47元/天 解锁文章
5511

被折叠的 条评论
为什么被折叠?



