wkt面数据转geojson
import com.alibaba.fastjson.JSONException; import com.alibaba.fastjson.JSONObject; import org.locationtech.jts.geom.Coordinate; import org.locationtech.jts.geom.Geometry; import org.locationtech.jts.io.ParseException; import org.locationtech.jts.io.WKTReader; public class WKTToGeoJSONConverter { public static void main(String[] args) { // 示例 WKT 数据 String wkt = "POLYGON ((0 0, 0 10, 10 10, 10 0, 0 0))"; // String wkt = "MULTIPOLYGON (((43876460.5886 4557211.8423999995, 43876830.28126473 4556955.511663466, 43876986.7596 4557164.5297, 43877421.5759 4556935.826300001, 43877187.270899996 4556494.7279, 43876662.615 4556731.5492, 43876685.96516568 4556762.739497202, 43876381.637099996 4556848.7905, 43876460.5886 4557211.8423999995)), ((43876345.0617 4554530.7226, 43876456.4056 4554102.8003, 43876505.4387 4553914.3882, 43876174.8098 4553986.221999999, 43876345.0617 4554530.7226)), ((43870876.6665 4555441.290999999, 43871569.0309 4554423.7804000005, 43870343.883 4554950.150800001, 43870876.6665 4555441.290999999)), ((43868943.252 4557351.4333, 43869426.9282 4556516.4758, 43868810.4922 4556664.367900001, 43868943.252 4557351.4333)), ((43869754.1936 4557739.0306, 43870215.0965 4557386.9695999995, 43870052.7795 4557198.6493, 43869594.6533 4557321.0469, 43869754.1936 4557739.0306)), ((43870813.5107 4556876.008400001, 43871375.0251 4556910.135600001, 43871326.7948 4556450.8159, 43870814.3933 4556562.5789, 43870813.5107 4556876.008400001)), ((43872151.6271 4556024.8948, 43872675.3091 4555329.136499999, 43872605.5269 4555008.774, 43871926.1364 4554910.698999999, 43872151.6271 4556024.8948)), ((43872488.9108 4557651.6379, 43873378.8445 4557412.302100001, 43873087.9918 4556990.192399999, 43872425.9887 4557258.852499999, 43872488.9108 4557651.6379)))"; // 将 WKT 转换为 GeoJSON String geoJson = convertWKTToGeoJSON(wkt); // 打印转换后的 GeoJSON System.out.println(geoJson); } //返回值{"coordinates":{"coordinates":[[0.0,0.0],[0.0,10.0],[10.0,10.0],[10.0,0.0],[0.0,0.0]]},"type":"Polygon"} public static String convertWKTToGeoJSON(String wkt) { WKTReader reader = new WKTReader(); try { Geometry geometry = reader.read(wkt); // 创建 GeoJSON 对象 JSONObject geoJson = new JSONObject(); geoJson.put("type", geometry.getGeometryType()); // 添加坐标信息 JSONObject coordinates = new JSONObject(); coordinates.put("coordinates", geometryToJSONArray(geometry)); geoJson.put("coordinates", coordinates); return geoJson.toString(); } catch (ParseException | JSONException e) { e.printStackTrace(); return null; } } private static Object geometryToJSONArray(Geometry geometry) { int numGeometries = geometry.getNumGeometries(); if (numGeometries == 1) { return coordinatesToJSONArray(geometry.getCoordinates()); } else { Object[] geometries = new Object[numGeometries]; for (int i = 0; i < numGeometries; i++) { geometries[i] = coordinatesToJSONArray(geometry.getGeometryN(i).getCoordinates()); } return geometries; } } private static Object coordinatesToJSONArray(Coordinate[] coordinates) { Object[] jsonArray = new Object[coordinates.length]; for (int i = 0; i < coordinates.length; i++) { double[] coordArray = new double[]{coordinates[i].getX(), coordinates[i].getY()}; jsonArray[i] = coordArray; } return jsonArray; } }
wkt转elasticsearch的shape数据
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import org.locationtech.jts.geom.Geometry; import org.locationtech.jts.io.ParseException; import org.locationtech.jts.io.WKTReader; import org.locationtech.jts.io.WKTWriter; import java.util.ArrayList; import java.util.List; /** * @Author: luojie * @Date: 2022/11/14 9:07 */ public class WKTUtil { public static void main(String[] args) { String wkt = "POLYGON ((0 0, 0 10, 10 10, 10 0, 0 0))"; System.out.println(wkt2Shape(wkt)); } public static String geomToWkt(Geometry geometry) { if(geometry == null){ return null; } String wkt = null; WKTWriter writer = new WKTWriter(); wkt = writer.write(geometry); return wkt; } public static Geometry wktToGeom(String wkt) { Geometry geometry = null; WKTReader reader = new WKTReader(); try { geometry = reader.read(wkt); } catch (ParseException e) { e.printStackTrace(); } return geometry; } public static JSONObject wkt2Shape(String wkt){ String geoJSONStr = WKTToGeoJSONConverter.convertWKTToGeoJSON(wkt); if(geoJSONStr == null){ return null; } JSONObject jsonObject = new JSONObject(); jsonObject.put("type", "Polygon"); if(geoJSONStr.contains("[[[")){ JSONObject data = JSONObject.parseObject(geoJSONStr); JSONObject coordinates = data.getJSONObject("coordinates"); JSONArray array = coordinates.getJSONArray("coordinates"); List<List<List<Double>>> listList = new ArrayList<>(); for(int i = 0; i < array.size(); i++){ List<List<Double>> lists = new ArrayList<>(); JSONArray second = array.getJSONArray(i); for(int j = 0; j < second.size(); j++){ List<Double> list = JSONArray.parseArray(second.getString(j), Double.class); lists.add(list); } listList.add(lists); } jsonObject.put("coordinates", listList); }else if(geoJSONStr.contains("[[")){ JSONObject data = JSONObject.parseObject(geoJSONStr); JSONObject coordinates = data.getJSONObject("coordinates"); JSONArray array = coordinates.getJSONArray("coordinates"); List<List<List<Double>>> listList = new ArrayList<>(); List<List<Double>> lists = new ArrayList<>(); for(int i = 0; i < array.size(); i++){ List<Double> list = JSONArray.parseArray(array.getString(i), Double.class); lists.add(list); } listList.add(lists); jsonObject.put("coordinates", listList); } return jsonObject; } }