当前位置:网站首页>Geotools: common tools for mutual conversion of wkt, geojason, feature and featurecollection
Geotools: common tools for mutual conversion of wkt, geojason, feature and featurecollection
2022-06-30 01:32:00 【liuccn】
Reference article :
GeoTools:WKT、GeoJson、Feature、FeatureCollection transformation
Conversion tool class
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> * Space processing tools * </p> * **/
public class GeoTools {
private static final WKTReader READER = new WKTReader();
/** * Element set root node */
private static final String[] COLLECTION_TYPE = new String[]{
"FeatureCollection"};
/** * Geographic feature types */
private static final String[] FEATURES_TYPE = new String[]{
"Feature"};
/** * Geographic data types * spot 、 Line 、 Noodles 、 Geometric set */
private static final String[] GEO_TYPE = new String[]{
"Geometry", "Point", "LineString", "Polygon", "MultiPoint", "MultiLineString", "MultiPolygon", "GeometryCollection"};
/** * obtain Geo Geometric type * @param wktStr WKT character string * @return Geo Geometric type */
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(" Nonstandard WKT character string :"+ e);
e.printStackTrace();
}
}
return type;
}
/** * It's standard WKT * @param wktStr WKT character string * @return yes 、 no */
public static boolean isWkt(String wktStr) {
for (String s : GEO_TYPE) {
if (wktStr.toLowerCase().startsWith(s.toLowerCase())) {
return true;
}
}
return false;
}
/** * Not standard WKT * @param wktStr WKT character string * @return yes 、 no */
public static boolean isNotWkt(String wktStr) {
return !isWkt(wktStr);
}
/** * It's standard GeoJson * @param geoJsonStr GeoJson character string * @return yes 、 no */
public static boolean isGeoJson(String geoJsonStr) {
try {
JSONObject jsonObject = JSONUtil.parseObj(geoJsonStr);
return isGeoJson(jsonObject);
}catch (Exception e) {
return false;
}
}
/** * Not standard GeoJson * @param geoJsonStr GeoJson character string * @return yes 、 no */
public static boolean isNotGeoJson(String geoJsonStr) {
return !isGeoJson(geoJsonStr);
}
/** * It's standard GeoJson * @param geoJson GeoJson object * @return yes 、 no */
public static boolean isGeoJson(JSONObject geoJson) {
String type = geoJson.getStr("type");
boolean mark = false;
// Judge the root node
if (ArrayUtil.containsIgnoreCase(COLLECTION_TYPE, type)) {
JSONArray jsonArray = geoJson.get("features", JSONArray.class);
for (Object jsonStr : jsonArray) {
JSONObject jsonObject = JSONUtil.parseObj(jsonStr);
type = jsonObject.getStr("type");
// Judge geographical elements
if (ArrayUtil.containsIgnoreCase(FEATURES_TYPE, type)) {
type = jsonObject.get("geometry", JSONObject.class).getStr("type");
// Judge geometric elements
mark = ArrayUtil.containsIgnoreCase(GEO_TYPE, type);
}
if (!mark) {
return false;
}
}
}else {
// Judge geographical elements
if (ArrayUtil.containsIgnoreCase(FEATURES_TYPE, type)) {
type = geoJson.get("geometry", JSONObject.class).getStr("type");
}
// Data is geometric data
mark = ArrayUtil.containsIgnoreCase(GEO_TYPE, type);
}
return mark;
}
/** * Not standard GeoJson * @param geoJson GeoJson object * @return yes 、 no */
public static boolean isNotGeoJson(JSONObject geoJson) {
return !isGeoJson(geoJson);
}
/** * GeoJson turn WKT * @param geoJson GeoJson object * @return WKT character string */
public static String geoJsonToWkt(JSONObject geoJson) {
String wkt = null;
GeometryJSON gJson = new GeometryJSON();
try {
if(isGeoJson(geoJson)){
String type = geoJson.getStr("type");
// Determine whether the root node
if (ArrayUtil.containsIgnoreCase(COLLECTION_TYPE, type)) {
JSONArray geometriesArray = geoJson.get("features", JSONArray.class);
// Define a number assembly graphic object
int size = geometriesArray.size();
Geometry[] geometries = new Geometry[size];
for (int i = 0; i < size; i++){
String str = JSONUtil.parseObj(geometriesArray.get(i)).getStr("geometry");
// Use GeoUtil Read out str
Reader reader = GeoJSONUtil.toReader(str);
Geometry geometry = gJson.read(reader);
geometries[i] = geometry;
}
GeometryCollection geometryCollection = new GeometryCollection(geometries, new GeometryFactory());
wkt = geometryCollection.toText();
}else {
String geoStr = geoJson.toString();
// Determine whether the geographical element node
if (ArrayUtil.containsIgnoreCase(FEATURES_TYPE, type)) {
geoStr = geoJson.getStr("geometry");
}
Reader reader = GeoJSONUtil.toReader(geoStr);
Geometry read = gJson.read(reader);
wkt = read.toText();
}
}
} catch (IOException e){
System.out.println("GeoJson turn WKT Something unusual happened :"+ e);
e.printStackTrace();
}
return wkt;
}
/** * WKT turn GeoJson * @param wktStr WKT character string * @return GeoJson object */
public static JSONObject wktToGeoJson(String wktStr) {
JSONObject jsonObject = new JSONObject();
try {
Geometry geometry = READER.read(wktStr);
StringWriter writer = new StringWriter();
GeometryJSON geometryJson = new GeometryJSON();
geometryJson.write(geometry, writer);
jsonObject = JSONUtil.parseObj(writer.toString());
} catch (Exception e) {
System.out.println("WKT turn GeoJson Something unusual happened :"+ e);
e.printStackTrace();
}
return jsonObject;
}
/** * WKT turn Feature * @param wktStr WKT character string * @return Feature JSON object */
public static JSONObject wktToFeature(String wktStr) {
JSONObject jsonObject = new JSONObject();
try {
SimpleFeatureType type = DataUtilities.createType("Link", "geometry:"+getGeometryType(wktStr));
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(type);
// according to TYPE You can assign values to attributes in the order declared in , I haven't tried other methods yet
featureBuilder.add(READER.read(wktStr));
SimpleFeature feature = featureBuilder.buildFeature(null);
StringWriter writer = new StringWriter();
FeatureJSON fJson = new FeatureJSON();
fJson.writeFeature(feature, writer);
jsonObject = JSONUtil.parseObj(writer.toString());
}catch (Exception e) {
System.out.println("WKT turn Feature Something unusual happened :"+ e);
e.printStackTrace();
}
return jsonObject;
}
/** * WKT turn FeatureCollection * @param wktStr WKT character string * @return FeatureCollection JSON object */
public static JSONObject wktToFeatureCollection(String wktStr) {
JSONObject jsonObject = new JSONObject();
try {
String geometryType = getGeometryType(wktStr);
if (StrUtil.isNotEmpty(geometryType)) {
SimpleFeatureType type = DataUtilities.createType("Link", "geometry:" + geometryType);
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(type);
List<SimpleFeature> features = new ArrayList<>();
SimpleFeatureCollection collection = new ListFeatureCollection(type, features);
featureBuilder.add(READER.read(wktStr));
SimpleFeature feature = featureBuilder.buildFeature(null);
features.add(feature);
StringWriter writer = new StringWriter();
FeatureJSON fJson = new FeatureJSON();
fJson.writeFeatureCollection(collection, writer);
jsonObject = JSONUtil.parseObj(writer.toString());
}
}catch (Exception e) {
System.out.println("WKT turn FeatureCollection Something unusual happened :"+ e);
e.printStackTrace();
}
return jsonObject;
}
}
test
import cn.hutool.json.JSONUtil;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class GeoToolsTest {
String geoJsonStr = "{\n" +
" \"type\":\"Point\",\n" +
" \"coordinates\":[105.380859375,31.57853542647338]\n" +
" }";
String geoJsonStr2 = "{\"type\":\"Feature\",\n" +
" \"properties\":{},\n" +
" \"geometry\":{\n" +
" \"type\":\"Point\",\n" +
" \"coordinates\":[105.380859375,31.57853542647338]\n" +
" }\n" +
" }";
String geoJsonStr3 = "{\n" +
" \"type\": \"FeatureCollection\",\n" +
" \"features\": [\n" +
" {\"type\":\"Feature\",\n" +
" \"properties\":{},\n" +
" \"geometry\":{\n" +
" \"type\":\"Point\",\n" +
" \"coordinates\":[105.380859375,31.57853542647338]\n" +
" }\n" +
" },\n" +
"\t\t{\"type\":\"Feature\",\n" +
" \"properties\":{},\n" +
" \"geometry\":{\n" +
" \"type\":\"Point\",\n" +
" \"coordinates\":[105.380859375,31.57853542647338]\n" +
" }\n" +
" }\n" +
" ]\n" +
"}";
String[] wktStr = new String[]{
"POINT(6 10)", "LINESTRING(3 4,10 50,20 25)", "POLYGON((1 1,5 1,5 5,1 5,1 1),(2 2,2 3,3 3,3 2,2 2))",
"MULTIPOINT(3.5 5.6, 4.8 10.5)", "MULTILINESTRING((3 4,10 50,20 25),(-5 -8,-10 -8,-15 -4))", "MULTIPOLYGON(((1 1,5 1,5 5,1 5,1 1),(2 2,2 3,3 3,3 2,2 2)),((6 3,9 2,9 4,6 3)))",
"GEOMETRYCOLLECTION(POINT(4 6),LINESTRING(4 6,7 10))", "POINT ZM (1 1 5 60)", "POINT M (1 1 80)"};
@Test
void getGeometryType() {
for (String s : wktStr) {
System.out.println("Geo Geometric type :" + GeoTools.getGeometryType(s));
}
}
@Test
void isWkt() {
for (String s : wktStr) {
System.out.println(" It's standard WKT:" + GeoTools.isWkt(s));
}
}
@Test
void isNotWkt() {
for (String s : wktStr) {
System.out.println(" Not standard WKT:" + GeoTools.isNotWkt(geoJsonStr2));
}
}
@Test
void isGeoJson() {
System.out.println(" It's standard GeoJson:" + GeoTools.isGeoJson(geoJsonStr3));
}
@Test
void isNotGeoJson() {
System.out.println(" Not standard GeoJson:" + GeoTools.isNotGeoJson(geoJsonStr3));
}
@Test
void geoJsonToWkt() {
System.out.println("GeoJson turn WKT:" + GeoTools.geoJsonToWkt(JSONUtil.parseObj(geoJsonStr)));
System.out.println("GeoJson turn WKT:" + GeoTools.geoJsonToWkt(JSONUtil.parseObj(geoJsonStr2)));
System.out.println("GeoJson turn WKT:" + GeoTools.geoJsonToWkt(JSONUtil.parseObj(geoJsonStr3)));
}
@Test
void wktToGeoJson() {
for (String s : wktStr) {
System.out.println("WKT turn GeoJson:" + GeoTools.wktToGeoJson(s));
}
}
@Test
void wktToFeature() {
for (String s : wktStr) {
System.out.println("WKT turn Feature:" + GeoTools.wktToFeature(s));
}
}
@Test
void wktToFeatureCollection() {
for (String s : wktStr) {
System.out.println("WKT turn FeatureCollection:" + GeoTools.wktToFeatureCollection(s));
}
}
}
边栏推荐
- Sorting out the usage of transforms in pytoch
- Gesture digital enlightenment learning machine
- 工具与生活服务
- JS recursive summation 1-100
- Varnish foundation overview 2
- 对深度网络模型量化工作的总结
- Pytorch中transforms的用法整理
- 【机器学习Q&A】余弦相似度、余弦距离、欧式距离以及机器学习中距离的含义
- cookie加密9
- Sentinel source code analysis Part 8 - core process - sphu Entry current limiting execution
猜你喜欢

Cookie加密15 登录加密

Embedded test template

Cub school learning: manual query and ADC in-depth use

JS returned content is encoded by Unicode

第八届“互联网+”大赛 | 云原生赛道邀你来挑战

Chiffrement des cookies 8

STC89C52 single chip microcomputer simple calculator design and code demonstration

Preliminary understanding of NVIDIA Jetson nano

Questions about database: database attachment

3-6sql injection website instance step 5: break through the background to obtain web administrator permissions
随机推荐
Interface Association of postman
Cookie加密12
Seata and the three platforms are working together in the summer of programming. Millions of bonuses are waiting for you
Sentinel source code analysis Part 8 - core process - sphu Entry current limiting execution
Varnish 基础概览2
js逆向请求参数加密:
Embedded exit (review and release)
Cookie加密15 登录加密
Varnish foundation overview 3
Cookie encryption 8
【图神经网络】图分类学习研究综述[2]:基于图神经网络的图分类
JS returned content is encoded by Unicode
Gesture digital enlightenment learning machine
js Array.from()的5个便捷应用
OpenCV和Image之间的转换(亲测有效)
cookie加密8
第八届“互联网+”大赛 | 云原生赛道邀你来挑战
Questions about database: database attachment
JS recursive summation 1-100
Three text to speech artifacts, each of which is very practical