1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > GeoTools-WKT\GeoJson相互转换

GeoTools-WKT\GeoJson相互转换

时间:2023-04-16 01:57:28

相关推荐

GeoTools-WKT\GeoJson相互转换

GeoTools-WKT\GeoJson相互转换

介绍

先简单介绍两种类型

WKT

三种基本类型

点(POINT),线(LINESTRING), 面(POLYGON)

POINT(0 0)POINT Z (0 0 0) Z 一般代表高程信息POINT ZM (0 0 0 0) Z 一般代表高程信息,M一般代表其他信息如距离、温度、浓度等LINESTRING(0 0,1 1,1 2)POLYGON((0 0,4 0,4 4,0 4,0 0),(1 1, 2 1, 2 2, 1 2,1 1))

三种扩展类型

多点(MULTIPOINT), 多线(MULTILINESTRING), 多个多边形(MULTIPOLYGON)

MULTIPOINT((0 0),(1 2))MULTIPOINT Z ((0 0 0),(1 2 3))MULTILINESTRING((0 0,1 1,1 2),(2 3,3 2,5 4))MULTIPOLYGON(((0 0,4 0,4 4,0 4,0 0),(1 1,2 1,2 2,1 2,1 1)), ((-1 -1,-1 -2,-2 -2,-2 -1,-1 -1)))GEOMETRYCOLLECTION(POINT(2 3),LINESTRING(2 3,3 4))

GeoJson

GeoJSON是一种对各种地理数据结构进行编码的格式,基于Javascript对象表示法(JavaScript Object Notation, 简称JSON)的地理空间信息数据交换格式。GeoJSON对象可以表示几何、特征或者特征集合。GeoJSON支持下面几何类型:点、线、面、多点、多线、多面和几何集合。GeoJSON里的特征包含一个几何对象和其他属性,特征集合表示一系列特征。

详细的介绍可以看我的另一个博文:GeoJson介绍_孙霸天的博客-CSDN博客_geojson

转换工具

我使用的是java,这里我选用GeoTools工具类

项目使用maven管理

pom.xml

这个引入gt-geojson包就行

<dependency><groupId>org.geotools</groupId><artifactId>gt-geojson</artifactId><version>22.5</version></dependency>

GeoUtils.java

import com.alibaba.fastjson.JSONObject;import org.geotools.geojson.geom.GeometryJSON;import org.locationtech.jts.geom.Geometry;import org.locationtech.jts.io.WKTReader;import java.io.IOException;import java.io.Reader;import java.io.StringReader;import java.io.StringWriter;import java.util.HashMap;import java.util.Map;public class GeoUtils {/*** wkt转GeoJson包括属性值(feature)* @param wkt wkt* @param map 属性值* @return GeoJson*/public static HashMap<Object, Object> wktToJson(String wkt, Map<String, Object> map) {String json = null;// geoJsonHashMap<Object, Object> feature = new HashMap<>();try {WKTReader reader = new WKTReader();Geometry geometry = reader.read(wkt);StringWriter writer = new StringWriter();GeometryJSON g = new GeometryJSON();g.write(geometry, writer);JSONObject jsonObject = JSONObject.parseObject(writer.toString());feature.put("type", "Feature");feature.put("geometry", jsonObject);feature.put("properties", map);} catch (Exception e) {e.printStackTrace();}return feature;}/*** wkt转GeoJson(geometry) 不包括属性* @param wkt wkt* @return geoJson*/public static JSONObject wktToJson(String wkt) {JSONObject jsonObject = null;try {WKTReader reader = new WKTReader();Geometry geometry = reader.read(wkt);StringWriter writer = new StringWriter();GeometryJSON g = new GeometryJSON();g.write(geometry, writer);jsonObject = JSONObject.parseObject(writer.toString());} catch (Exception e) {e.printStackTrace();}return jsonObject;}public static String jsonToWkt(String geoJson) {String wkt = null;GeometryJSON gJson = new GeometryJSON();Reader reader = new StringReader(geoJson);try {Geometry geometry = gJson.read(reader);wkt = geometry.toText();} catch (IOException e) {e.printStackTrace();}return wkt;}}

测试方法

public static void main(String[] args) {String wkt = "MULTIPOLYGON (((120.22452128700002 32.566831636000074, 119.50959153000008 32.458417937000036, 119.10844561400006 32.481514920000052, 119.07520520000003 32.452880795000056, 119.03290032700011 32.518119566000053, 118.98780268200005 32.504359306000026, 118.92580064200001 32.56200029300004, 118.88197783500004 32.556636972000035, 118.90400399000009 32.593799503000071, 118.84150398400004 32.569171840000024, 118.81550038400007 32.606095375000052, 118.7862000002 32.583500968000067, 118.71629154700008 32.617443698000045, 118.62963377800008 32.576794737000057, 118.58571502400002 32.598674052000035, 118.55775861600011 32.572150347000047, 118.61143082800004 32.517253606000054, 118.58500004600012 32.482521182000028, 118.68339829700005 32.473899767000034, 118.69959959900007 32.357428972000037, 118.6510942220001 32.304752081000061, 118.66549809900005 32.246841048000078, 118.61908851200008 32.206496283000035, 118.50160157400001 32.196529386000066, 118.49205625100001 32.122242626000059, 118.37759755900004 32.060101033000024, 118.35780272700003 31.932368262000068, 118.49419916500005 31.844931660000043, 118.47619156700011 31.778659271000038, 118.53849606500012 31.765881300000046, 118.52900400800002 31.736719448000031, 118.62979487300004 31.762350468000079, 118.67610366100007 31.729489533000049, 118.68740250200005 31.708019447000027, 118.63830068100003 31.678359137000029, 118.65019528800008 31.645878210000035, 118.72830076000002 31.630100723000055, 118.76850589800006 31.685778089000053, 118.7929981310001 31.623019805000069, 118.85799801400003 31.619609904000072, 118.85947950000002 31.422155278000048, 118.69570300900011 31.300368343000059, 118.78730479900003 31.233179477000078, 119.09470688200008 31.238702120000028, 119.17759786500005 31.303199921000044, 119.25919903100009 31.252451582000049, 119.33490260100007 31.262639013000069, 119.34818509000002 31.301483340000061, 119.3721972940001 31.268281286000047, 119.35395665800002 31.205916262000073, 119.38459985400004 31.171208997000065, 119.52571624500001 31.162264709000056, 119.57360360200005 31.122031837000065, 119.66410125500011 31.169848178000052, 119.91040045400007 31.172069754000063, 119.98821120900004 31.034763786000042, 120.13388450800005 30.943786881000051, 120.36903284000005 30.952368267000054, 120.3631932620001 30.886669745000063, 120.42290033500001 30.9287900004, 120.45780278400002 30.812000373000046, 120.50859351600002 30.765439607000076, 120.58419947100003 30.858969566000042, 120.69570283500002 30.871599010000068, 120.6860940470001 30.968411235000076, 120.88569333000009 31.006478505000075, 120.89185233500007 31.08970037000006, 120.84969738700011 31.106550877000075, 120.86799787000007 31.135110616000077, 121.06639648000009 31.160998764000055, 121.05219731900002 31.270338848000051, 121.1508982900001 31.28761946700007, 121.10019507000004 31.35988078400004, 121.15320284300003 31.410571573000027, 121.14160176300004 31.446911913000065, 121.38030260400001 31.547729314000037, 121.09639642000002 31.764149589000056, 121.19348325300007 31.832955883000068, 121.32063385700008 31.858526222000023, 121.44332814900008 31.761568666000073, 121.56265593400008 31.71497322700003, 121.71122903600008 31.721018578000042, 121.87414431600007 31.685496048000061, 121.90816374200006 31.72122825100007, 121.84540803100003 31.933577275000061, 121.72171715000002 32.031693843000028, 121.56881965400009 32.09877743900006, 121.42473532700001 32.110048579000079, 121.43172816000003 32.166039918000024, 121.37774926700001 32.166556932000049, 121.36515666200012 32.238196769000069, 121.40474805600002 32.256168971000079, 121.36896916000001 32.274733171000037, 121.39778190700008 32.321098418000076, 121.37359581600003 32.399037645000078, 121.05824182800006 32.535245714000041, 120.9914879800001 32.520912822000071, 121.0004439600001 32.58107857400006, 120.92536791300006 32.606614395000065, 120.93853697600002 32.645071986000062, 120.90427896300002 32.657330940000065, 120.87331583300011 32.732171156000049, 120.88955477900004 32.783140035000031, 120.22452128700002 32.566831636000074)))";JSONObject jsonObject = wktToJson(wkt);System.out.println(jsonObject);}

转换结果如下:

{"coordinates":[[[[120.2245,32.5668],[119.5096,32.4584],[119.1084,32.4815],[119.0752,32.4529],[119.0329,32.5181],[118.9878,32.5044],[118.9258,32.562],[118.882,32.5566],[118.904,32.5938],[118.8415,32.5692],[118.8155,32.6061],[118.7862,32.5835],[118.7163,32.6174],[118.6296,32.5768],[118.5857,32.5987],[118.5578,32.5722],[118.6114,32.5173],[118.585,32.4825],[118.6834,32.4739],[118.6996,32.3574],[118.6511,32.3048],[118.6655,32.2468],[118.6191,32.2065],[118.5016,32.1965],[118.4921,32.1222],[118.3776,32.0601],[118.3578,31.9324],[118.4942,31.8449],[118.4762,31.7787],[118.5385,31.7659],[118.529,31.7367],[118.6298,31.7624],[118.6761,31.7295],[118.6874,31.708],[118.6383,31.6784],[118.6502,31.6459],[118.7283,31.6301],[118.7685,31.6858],[118.793,31.623],[118.858,31.6196],[118.8595,31.4222],[118.6957,31.3004],[118.7873,31.2332],[119.0947,31.2387],[119.1776,31.3032],[119.2592,31.2525],[119.3349,31.2626],[119.3482,31.3015],[119.3722,31.2683],[119.354,31.2059],[119.3846,31.1712],[119.5257,31.1623],[119.5736,31.122],[119.6641,31.1698],[119.9104,31.1721],[119.9882,31.0348],[120.1339,30.9438],[120.369,30.9524],[120.3632,30.8867],[120.4229,30.9282],[120.4578,30.812],[120.5086,30.7654],[120.5842,30.859],[120.6957,30.8716],[120.6861,30.9684],[120.8857,31.0065],[120.8919,31.0897],[120.8497,31.1066],[120.868,31.1351],[121.0664,31.161],[121.0522,31.2703],[121.1509,31.2876],[121.1002,31.3599],[121.1532,31.4106],[121.1416,31.4469],[121.3803,31.5477],[121.0964,31.7641],[121.1935,31.833],[121.3206,31.8585],[121.4433,31.7616],[121.5627,31.715],[121.7112,31.721],[121.8741,31.6855],[121.9082,31.7212],[121.8454,31.9336],[121.7217,32.0317],[121.5688,32.0988],[121.4247,32.11],[121.4317,32.166],[121.3777,32.1666],[121.3652,32.2382],[121.4047,32.2562],[121.369,32.2747],[121.3978,32.3211],[121.3736,32.399],[121.0582,32.5352],[120.9915,32.5209],[121.0004,32.5811],[120.9254,32.6066],[120.9385,32.6451],[120.9043,32.6573],[120.8733,32.7322],[120.8896,32.7831],[120.2245,32.5668]]]],"type":"MultiPolygon"}

后面我会开一个小坑,GeoTools的使用

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。