当前位置:   article > 正文

java实现geojson数据与wkt格式互转_java中geojson转gdb格式

java中geojson转gdb格式

首先奉上互转工具类和依赖

  1. import com.alibaba.fastjson.JSONArray;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.vividsolutions.jts.geom.Geometry;
  4. import com.vividsolutions.jts.geom.GeometryCollection;
  5. import com.vividsolutions.jts.geom.GeometryFactory;
  6. import com.vividsolutions.jts.io.WKTReader;
  7. import org.geotools.geojson.GeoJSONUtil;
  8. import org.geotools.geojson.geom.GeometryJSON;
  9. import java.io.IOException;
  10. import java.io.Reader;
  11. import java.io.StringWriter;
  12. public class WKTutil {
  13. private static WKTReader reader = new WKTReader();
  14. private static final String GEO_JSON_TYPE = "GeometryCollection";
  15. private static final String WKT_TYPE = "GEOMETRYCOLLECTION";
  16. public static String wktToJson(String wkt) {
  17. String json = null;
  18. try {
  19. WKTReader reader = new WKTReader();
  20. Geometry geometry = reader.read(wkt);
  21. StringWriter writer = new StringWriter();
  22. GeometryJSON g = new GeometryJSON(20);
  23. g.write(geometry, writer);
  24. json = writer.toString();
  25. } catch (Exception e) {
  26. e.printStackTrace();
  27. }
  28. return json;
  29. }
  30. public static String jsonToWkt(JSONObject jsonObject) {
  31. String wkt = null;
  32. String type = jsonObject.getString("type");
  33. GeometryJSON gJson = new GeometryJSON();
  34. try {
  35. // {"geometries":[{"coordinates":[4,6],"type":"Point"},{"coordinates":[[4,6],[7,10]],"type":"LineString"}],"type":"GeometryCollection"}
  36. if (WKTutil.GEO_JSON_TYPE.equals(type)) {
  37. // 由于解析上面的json语句会出现这个geometries属性没有采用以下办法
  38. JSONArray geometriesArray = jsonObject.getJSONArray("geometries");
  39. // 定义一个数组装图形对象
  40. int size = geometriesArray.size();
  41. Geometry[] geometries = new Geometry[size];
  42. for (int i = 0; i < size; i++) {
  43. String str = geometriesArray.get(i).toString();
  44. // 使用GeoUtil去读取str
  45. Reader reader = GeoJSONUtil.toReader(str);
  46. Geometry geometry = gJson.read(reader);
  47. geometries[i] = geometry;
  48. }
  49. GeometryCollection geometryCollection = new GeometryCollection(geometries, new GeometryFactory());
  50. wkt = geometryCollection.toText();
  51. } else {
  52. Reader reader = GeoJSONUtil.toReader(jsonObject.toString());
  53. Geometry read = gJson.read(reader);
  54. wkt = read.toText();
  55. }
  56. } catch (IOException e) {
  57. System.out.println("GeoJson转WKT出现异常");
  58. e.printStackTrace();
  59. }
  60. return wkt;
  61. }
  62. }
  1. <dependency>
  2. <groupId>org.geotools</groupId>
  3. <artifactId>gt-shapefile</artifactId>
  4. <version>19.2</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.geotools</groupId>
  8. <artifactId>gt-swing</artifactId>
  9. <version>19.2</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.geotools</groupId>
  13. <artifactId>gt-jdbc</artifactId>
  14. <version>19.2</version>
  15. </dependency>
  16. <dependency>
  17. <groupId>org.geotools.jdbc</groupId>
  18. <artifactId>gt-jdbc-postgis</artifactId>
  19. <version>19.2</version>
  20. </dependency>
  21. <dependency>
  22. <groupId>org.geotools</groupId>
  23. <artifactId>gt-epsg-hsql</artifactId>
  24. <version>19.2</version>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.geotools</groupId>
  28. <artifactId>gt-geojson</artifactId>
  29. <version>12.0</version>
  30. </dependency>
  31. <dependency>
  32. <groupId>com.vividsolutions</groupId>
  33. <artifactId>jts</artifactId>
  34. <version>1.13</version> <!--2-->
  35. <exclusions>
  36. <exclusion>
  37. <groupId>xerces</groupId>
  38. <artifactId>xercesImpl</artifactId>
  39. </exclusion>
  40. </exclusions>
  41. </dependency>

下面演示通过postman请求体传geojson格式参数,Controller层接收参数并转化wkt

  1. @RequestMapping(value = "/text", method = RequestMethod.POST)
  2. public String text(@RequestBody Object geom) {
  3. JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(geom));
  4. return WKTutil.jsonToWkt(jsonObject);
  5. }

参数

  1. {
  2. "geom": {
  3. "type": "MultiPolygon",
  4. "coordinates": [
  5. [
  6. [
  7. [
  8. -122.2694982,
  9. 37.79045922
  10. ],
  11. [
  12. -122.2693624,
  13. 37.79041125
  14. ],
  15. [
  16. -122.2693518,
  17. 37.79042521
  18. ],
  19. [
  20. -122.26899,
  21. 37.7902858
  22. ],
  23. [
  24. -122.2690027,
  25. 37.79027181
  26. ],
  27. [
  28. -122.2688602,
  29. 37.79021705
  30. ],
  31. [
  32. -122.2687222,
  33. 37.790445
  34. ],
  35. [
  36. -122.2688582,
  37. 37.79049813
  38. ],
  39. [
  40. -122.2689084,
  41. 37.79041634
  42. ],
  43. [
  44. -122.2689473,
  45. 37.79041058
  46. ],
  47. [
  48. -122.2691974,
  49. 37.79051029
  50. ],
  51. [
  52. -122.2692367,
  53. 37.7906097
  54. ],
  55. [
  56. -122.2692201,
  57. 37.79064271
  58. ],
  59. [
  60. -122.2693538,
  61. 37.79069243
  62. ],
  63. [
  64. -122.2694982,
  65. 37.79045922
  66. ]
  67. ]
  68. ]
  69. ]
  70. }
  71. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/456493
推荐阅读
相关标签
  

闽ICP备14008679号