游戏电视苹果数码历史美丽
投稿投诉
美丽时装
彩妆资讯
历史明星
乐活安卓
数码常识
驾车健康
苹果问答
网络发型
电视车载
室内电影
游戏科学
音乐整形

java使用GeoTools工具geojson与shp相互转

  记录使用geotools工具,实现shp和geojson数据互转
  爬坑:不使用依赖:vividsolutions,因为1。8与geotools20以后版本jts不一致,会报错。com。vividsolutions
  jtsartifactId
  1。8
  pom
  dependency
  1。引起pom依赖jarorg。geotools
  gtshapefileartifactId
  20。3
  dependency
  org。geotools
  gtapiartifactId
  20。3
  dependency
  org。geotools
  gtgeojsonartifactId
  20。3
  dependency
  org。geotools
  gtgeometryartifactId
  20。3
  dependency
  org。geotools
  gtjtswrapperartifactId
  20。3
  dependency
  org。geotools
  gtmainartifactId
  20。3
  dependency
  org。geotools
  gtepsghsqlartifactId
  20。3
  dependency
  org。geotools
  gtopengisartifactId
  20。3
  dependency
  org。geotools
  gtdataartifactId
  20。3
  dependency
  org。geotools
  gtreferencingartifactId
  20。3
  dependency
  com。alibaba
  fastjsonartifactId
  1。2。83
  dependency
  dependencies
  org。apache。maven。plugins
  mavencompilerpluginartifactId
  3。0
  1。8source
  1。8
  true
  configuration
  plugin
  plugins
  build
  2。上代码,自动识别坐标系,默认为84坐标importcom。alibaba。fastjson。JSON;
  importcom。alibaba。fastjson。JSONArray;
  importcom。alibaba。fastjson。JSONObject;
  importorg。geotools。referencing。CRS;
  importorg。locationtech。jts。geom。;
  importorg。geotools。data。FeatureWriter;
  importorg。geotools。data。Transaction;
  importorg。geotools。data。shapefile。ShapefileDataStore;
  importorg。geotools。data。shapefile。ShapefileDataStoreFactory;
  importorg。geotools。data。simple。SimpleFeatureCollection;
  importorg。geotools。data。simple。SimpleFeatureIterator;
  importorg。geotools。data。simple。SimpleFeatureSource;
  importorg。geotools。feature。simple。SimpleFeatureTypeBuilder;
  importorg。geotools。geojson。feature。FeatureJSON;
  importorg。geotools。geojson。geom。GeometryJSON;
  importorg。geotools。referencing。crs。DefaultGeographicCRS;
  importorg。opengis。feature。simple。SimpleFeature;
  importorg。opengis。feature。simple。SimpleFeatureType;
  importorg。opengis。referencing。crs。CoordinateReferenceSystem;
  importjava。io。;
  importjava。nio。charset。Charset;
  importjava。util。;
  authorqiaobing1226
  since20221028上午10:52
  参考:https:blog。csdn。netqiaobing1226articledetails127612665
  publicclassShp2GeojsonUtils{
  privatestaticfinalStringPOINTPoint;
  privatestaticfinalStringMULTIPOINTMultiPoint;
  privatestaticfinalStringLINESTRINGLineString;
  privatestaticfinalStringMULTILINESTRINGMultiLineString;
  privatestaticfinalStringPOLYGONPolygon;
  privatestaticfinalStringMULTIPOLYGONMultiPolygon;
  privatestaticfinalStringTHEGEOMthegeom;
  privatestaticfinalStringPROPERTIESproperties;
  privatestaticfinalStringGEOMETRYgeometry;
  privatestaticfinalStringGBKGBK;
  geoJson转换为shp文件
  paramjsonPath
  paramshpPath
  return
  publicstaticMapString,ObjectgeoJson2Shape(StringjsonPath,StringshpPath){
  MapString,ObjectmapnewHashMap();
  GeometryJSONgeoJsonnewGeometryJSON();
  try{
  JSONObjectjsonreadGeoJsonFile(jsonPath);
  JSONArrayfeatures(JSONArray)json。get(features);
  JSONObjectfeature0JSONObject。parseObject(features。get(0)。toString());
  获取属性名称
  SetpropertiesJSONObject。parseObject(feature0。getString(PROPERTIES))。keySet();
  StringstrType((JSONObject)feature0。get(GEOMETRY))。getString(type);
  StringstrCrsjson。getJSONObject(crs)。getJSONObject(PROPERTIES)。getString(name);
  CoordinateReferenceSystemcrsCRS。decode(strCrs);
  ShapefileDataStoreshapefileDataStoredataStore(properties,strType,shpPath,crs);
  if(shapefileDataStorenull){
  returnmap;
  }
  设置Writer
  FeatureWriterSimpleFeatureType,SimpleFeaturewritershapefileDataStore。getFeatureWriter(shapefileDataStore。getTypeNames()〔0〕,
  Transaction。AUTOCOMMIT);
  for(inti0,lenfeatures。size();ilen;i){
  StringstrFeaturefeatures。get(i)。toString();
  ReaderreadernewStringReader(strFeature);
  SimpleFeaturefeaturewriter。next();
  switch(strType){
  casePOINT:
  feature。setAttribute(THEGEOM,geoJson。readPoint(reader));
  break;
  caseMULTIPOINT:
  feature。setAttribute(THEGEOM,geoJson。readMultiPoint(reader));
  break;
  caseLINESTRING:
  feature。setAttribute(THEGEOM,geoJson。readLine(reader));
  break;
  caseMULTILINESTRING:
  feature。setAttribute(THEGEOM,geoJson。readMultiLine(reader));
  break;
  casePOLYGON:
  feature。setAttribute(THEGEOM,geoJson。readPolygon(reader));
  break;
  caseMULTIPOLYGON:
  feature。setAttribute(THEGEOM,geoJson。readMultiPolygon(reader));
  break;
  }
  Iteratoriteratorproperties。iterator();
  while(iterator。hasNext()){
  Stringstriterator。next()。toString();
  JSONObjectelementJSONObject。parseObject(features。get(i)。toString());
  feature。setAttribute(str,JSONObject。parseObject(element。getString(PROPERTIES))。get(str));
  }
  writer。write();
  }
  writer。close();
  shapefileDataStore。dispose();
  map。put(status,200);
  map。put(message,shp转换success);
  }catch(Exceptione){
  map。put(status,400);
  map。put(message,e。getMessage());
  e。printStackTrace();
  }
  returnmap;
  }
  读取geojosn文件
  paramjsonPath
  return
  privatestaticJSONObjectreadGeoJsonFile(StringjsonPath){
  读文件到Stringbuffer
  StringBuffersbnewStringBuffer();
  BufferedReaderbrnull;
  try{
  brnewBufferedReader(newFileReader(jsonPath));
  Stringstr;
  while((strbr。readLine())!null){逐行读取
  sb。append(strr);
  }
  br。close();
  }catch(Exceptione){
  if(br!null){
  try{
  br。close();
  }catch(Exceptionexception){
  exception。printStackTrace();
  }
  }
  e。printStackTrace();
  }
  returnJSONObject。parseObject(sb。toString());
  }
  设置shp文件属性
  paramproperties
  paramstrType
  paramshpPath
  return
  privatestaticShapefileDataStoredataStore(Setproperties,StringstrType,StringshpPath,CoordinateReferenceSystemcrs){
  try{
  Classlt;?geoTypenull;
  switch(strType){
  casePOINT:
  geoTypePoint。class;
  break;
  caseMULTIPOINT:
  geoTypeMultiPoint。class;
  break;
  caseLINESTRING:
  geoTypeLineString。class;
  break;
  caseMULTILINESTRING:
  geoTypeMultiLineString。class;
  break;
  casePOLYGON:
  geoTypePolygon。class;
  break;
  caseMULTIPOLYGON:
  geoTypeMultiPolygon。class;
  break;
  }
  创建shape文件对象
  FilefilenewFile(shpPath);
  MapString,SerializableparamsnewHashMapString,Serializable();
  params。put(ShapefileDataStoreFactory。URLP。key,file。toURI()。toURL());
  ShapefileDataStoreds(ShapefileDataStore)newShapefileDataStoreFactory()。createNewDataStore(params);
  定义图形信息和属性信息
  SimpleFeatureTypeBuildertbnewSimpleFeatureTypeBuilder();
  默认84坐标
  tb。setCRS(crsnull?DefaultGeographicCRS。WGS84:crs);
  tb。setName(shapefile);
  类型,PointMultiPointLineStringMultiLineStringPolygonMultiPolygon
  tb。add(thegeom,geoType);
  IteratorpropertiesIterproperties。iterator();
  设置属性
  while(propertiesIter。hasNext()){
  StringstrpropertiesIter。next()。toString();
  此处设置为string
  tb。add(str,String。class);
  }
  ds。createSchema(tb。buildFeatureType());
  设置编码
  CharsetcharsetCharset。forName(GBK);
  ds。setCharset(charset);
  returnds;
  }catch(Exceptionex){
  ex。printStackTrace();
  }
  returnnull;
  }
  shp文件转换geojson数据
  paramshpPath
  return
  publicstaticMapString,Objectshp2Geojson(StringshpPath,StringjsonPath){
  MapString,ObjectmapnewHashMap();
  新建json对象
  JSONObjectgeojsonObjectnewJSONObject();
  geojsonObject。put(type,FeatureCollection);
  try{
  JSONArrayarraynewJSONArray();
  StringfileNamereadShpContent(shpPath,array);
  geojsonObject。put(features,array);
  geojsonObject。put(name,fileName);
  StringcrsgetCoordinateSystemWKT(shpPath);
  GEOGCS表示这个是地址坐标系,PROJCS则表示是平面投影坐标系
  JSONObjectcrsJsonnewJSONObject();
  JSONObjectproJsonnewJSONObject();
  crsJson。put(type,name);
  if(crs。startsWith(PROJCS)){
  proJson。put(name,urn:ogc:def:crs:EPSG::3857);
  crsJson。put(properties,proJson);
  }else{
  proJson。put(name,urn:ogc:def:crs:OGC:1。3:CRS84);
  crsJson。put(properties,proJson);
  }
  geojsonObject。put(crs,crsJson);
  itertor。close();
  longstartTimeSystem。currentTimeMillis();
  将json字符串使用字符流写入文件
  FileoutputfilenewFile(jsonPath);
  BufferedWriterbufferedWriternewBufferedWriter(newFileWriter(outputfile));
  bufferedWriter。write(JSON。toJSONString(geojsonObject));
  bufferedWriter。flush();
  bufferedWriter。close();
  FileoutputfilenewFile(jsonPath);
  FileOutputStreamfileOutputStreamnewFileOutputStream(outputfile);
  OutputStreamWriteroutputStreamWriternewOutputStreamWriter(fileOutputStream,utf8);
  outputStreamWriter。write(JSON。toJSONString(geojsonObject));
  outputStreamWriter。flush();
  outputStreamWriter。close();
  将json字符串使用字节流写入文件
  FileoutputfilenewFile(jsonPath);
  BufferedOutputStreambufferedOutputStreamnewBufferedOutputStream(newFileOutputStream(outputfile));
  byte〔〕bytesJSON。toJSONString(geojsonObject)。getBytes(utf8);
  bufferedOutputStream。write(bytes);
  fileOutputStream。write(JSON。toJSONString(geojsonObject));
  bufferedOutputStream。flush();
  bufferedOutputStream。close();
  longendTimeSystem。currentTimeMillis();
  System。out。println(当前程序耗时:(endTimestartTime)ms);
  }catch(Exceptione){
  map。put(status,failure);
  map。put(message,e。getMessage());
  e。printStackTrace();
  }
  returngeojsonObject;
  }
  privatestaticStringreadShpContent(StringshpPath,JSONArrayarray){
  StringfileName;
  try{
  FeatureJSONfjsonnewFeatureJSON();
  获取featurecollection
  FilefilenewFile(shpPath);
  ShapefileDataStoreshpDataStorenull;
  shpDataStorenewShapefileDataStore(file。toURL());
  设置编码
  CharsetcharsetCharset。forName(GBK);
  shpDataStore。setCharset(charset);
  fileNameshpDataStore。getTypeNames()〔0〕;
  SimpleFeatureSourcefeatureSourcenull;
  featureSourceshpDataStore。getFeatureSource(fileName);
  SimpleFeatureCollectionresultfeatureSource。getFeatures();
  SimpleFeatureIteratoritertorresult。features();
  遍历feature转为json对象
  while(itertor。hasNext()){
  SimpleFeaturefeatureitertor。next();
  StringWriterwriternewStringWriter();
  fjson。writeFeature(feature,writer);
  Stringtempwriter。toString();
  ObjectgeometryJSONObject。parseObject(temp)。getString(GEOMETRY);
  byte〔〕btemp。getBytes(iso88591);
  tempnewString(b,GBK);
  JSONObjectjsonJSON。parseObject(temp);
  array。add(json);
  }
  itertor。close();
  }catch(Exceptione){
  e。printStackTrace();
  }
  returnfileName;
  }
  获取Shape文件的坐标系信息,GEOGCS表示这个是地址坐标系,PROJCS则表示是平面投影坐标系
  shpPath
  publicstaticStringgetCoordinateSystemWKT(StringshpPath){
  ShapefileDataStoreFactoryfactorynewShapefileDataStoreFactory();
  ShapefileDataStoredataStorenull;
  try{
  dataStore(ShapefileDataStore)factory。createDataStore(newFile(shpPath)。toURI()。toURL());
  returndataStore。getSchema()。getCoordinateReferenceSystem()。toWKT();
  }catch(UnsupportedOperationExceptionIOExceptione){
  e。printStackTrace();
  }finally{
  dataStore。dispose();
  }
  return;
  }
  工具类测试方法
  paramargs
  publicstaticvoidmain(String〔〕args)throwsException{
  Shp2GeojsonUtilsshpToGeojsonnewShp2GeojsonUtils();
  shape2Geojson
  StringshpPathUsersecarxDesktopgeojson上下高架mct123ADRoad。shp;
  StringjsonPathUsersecarxDesktopgeojsonADRoad。geojson;
  MapString,ObjectmapshpToGeojson。shp2Geojson(shpPath,jsonPath);
  geojson2Shape
  StringshpPathUsersecarxDesktopgeojson上下高架mct123ADRoad。shp;
  StringjsonPathUsersecarxDesktopgeojson上下高架mctADRoad。geojson;
  MapString,ObjectmapshpToGeojson。geoJson2Shape(jsonPath,shpPath);
  System。out。println(map。toString());
  }
  }
  版权声明:本文为CSDN博主qiaobing1226的原创文章,遵循CC4。0BYSA版权协议,转载请附上原文出处链接及本声明。
  原文链接:https:blog。csdn。netqiaobing1226articledetails127612665

我国有600吨黄金存在美国,不运回来的真实原因自从2019年初,德国向美国申请运回在美存下的1236吨黄金储备,美国断然拒绝德国请求,引发世界各国担忧,美国信用再次受到挑战,美国让各国储存黄金就是为了维护美元霸权,近年来美……10!点杀夺冠!中国女足奥运对手暴露短板,水庆霞有望率队复仇正文近日结束的女足美洲杯决赛上,中国女足奥运对手之一的巴西女足10点杀哥伦比亚女足夺冠,此役巴西女足暴露出致命短板,水庆霞未来有望率领新一期中国女足复仇奥运05惨败于巴西……体重120,避免前两种裤子,显胖又难看,后三种显瘦还显高现在很多女孩子的体重都属于微胖界,很多女孩子因为掌握了合理的穿搭技巧,穿出了穿衣显瘦、脱衣有肉的小心机。尤其是体重在120斤左右的女孩子,穿对衣服看起来就像100斤左右,……重大突破!人造太阳新世界纪录科技日报实习记者张佳欣据欧洲核聚变研发创新联盟(EUROfusion)、英国原子能管理局(UKAEA)和国际热核聚变实验堆(ITER)9日联合召开新闻发布会称,欧洲科学家……胜利重要还是保护对手重要?郭艾伦讲话理太偏1月13日CBA辽粤大战,火花四溅!韩德君、威姆斯冲突,胡明轩垫脚,让CBA一下子就热闹非凡,甚至盖过了马布里和李春江的骂战!赛后,郭艾伦呼吁球员之间应互相保护!那么是胜利重要……华为Mate50将在7月发布4000多起售首发鸿蒙OS3。0前不久余承东在官方场合透露,华为手机供应得到了极大的改善,华为售价开始回来了。随后在今天上午有,行业中又传出了华为Mate50系列的消息,消息该机将最快在7月正式发布,而……快乐和喜悦有什么不同快乐是激烈而短暂的,喜悦是平静而长久的。快乐是因为得到,是做加法,得到之后会有短暂的快乐,之后会空虚,会若有所失,为了再次体会到快乐,会激发出进一步的欲望和焦虑,来促使人……这波天秀!奇瑞QQ上半年销量超10万辆!同比增长220。7随着疫情形势逐步缓解,尽管补贴退坡、缺芯等危机仍然存在,但新能源汽车产业整体仍然呈现出蓬勃发展的火爆场面。奇瑞QQ同样销量可观,以小蚂蚁和冰淇淋为代表的新能源乘用车6月销量22……相同的裙子如何穿出新花样?换个颜色就能做到,秒变百变少女如果说有这样一件衣服是所有女生都会忍不住想穿的话,那么它的名字一定叫做连衣裙,没错,没有任何一位仙女能够从内心中抵挡得住小裙子带来的诱惑力,不过,很多女生都会因为选款而发愁,其……华为小艺建议全新升级,化身贴心的私人管家人工智能有多厉害?以前觉得人工智能离我们还很遥远,即便是目前应用最广泛的AI技术语音识别,也不过就是个摆设。但是今天看到HarmonyOS3及华为全场景新品发布会上全新升级的小……vista系统重装下载安装教程平常我们使用的电脑操作系统基本都是windows系统,win7、10、11甚至是xp都是较为常用的操作系统,而其它的心态比如说vista系统也是有用户在使用的,那么该如何安装这……音乐才子高枫一生只爱章子怡,生前遗作是我对世界的告别英年早逝,是天妒英才还是飞来横祸。娱乐圈中有才有貌的人并不少,但越是这样的人,命运就越坎坷。像李小龙、黄家驹、张国荣、陈百强等人,哪个不是娱乐圈中的佼佼者,哪个不是……
联想回应重启乐檬手机品牌账号并非官方所有IT之家2月14日消息日前一个名为Lemeng手机的账号出现在微博上,其简介为Lemeng乐檬手机官方微博且和联想手机官方微博有互动,尽管未获微博加V认证,但不由得让人怀疑联想……泰国乡下的老奶奶面馆,皮皮虾比面还多,我一口气吃3碗大家好,我是定居曼谷的二小姐。刚刚过去的清明小长假,大家过得都还好吗?因为二小姐人在泰国,所以更加清楚相比泰国日增5万的躺平政策,国内的防控管理真的好了一万倍不止。……MotoG4Plus安卓8。1系统更新正式推送IT之家2月10日消息2017年9月份,联想宣布将为MotoZ、MotoG5、MotoX4等多款手机带来Android8。0Oreo更新,而对于MotoG4和MotoG4Plu……联想Z5ProGT骁龙855版跑分曝光多核心破万感谢IT之家网友IT之家高端用户的线索投递!IT之家1月9日消息今天一款型号为联想L78032的新机现身Geekbench,该机正是此前发布的联想Z5ProGT855版,……该扬名立万的时候碌碌无为,这一届足坛90后堪忧,金球奖还是个在80后渐渐淡出足坛的主流,90后成为主力军登上了历史舞台,年龄最大的一批90后也已经32岁,最年轻的90后则是22岁,正是最年富力强,渴望建功立业的年龄!然而当今足坛90后的……金庸群侠传3D重制版全流程图文攻略(3)上期我们收狄云,这期我们继续冒险。来到神雕山洞,偶遇神雕大战巨蟒,我们上前触发剧情。对战巨蟒的要领:给张无忌装备乌蚕衣,在前排用九阳神功输出。段誉用北冥神功吸取内力降低巨……湖南怀化也有那么多温泉去处,你知道吗?没想到,怀化还有那么仙气的温泉你知道吗赶紧(一)鹤城叠翠兰亭怀化环城路以东约8公里处,有一山庄,名叠翠兰亭,庄园内仿四合院徽派建筑错落别致,几汪养生温泉……陈赫带俩女儿游迪士尼,安安小麦肤色像极张子萱,裙子包包买不停10月13日,陈赫在社交平台晒出一段视频,自曝带着两个孩子同游迪士尼。陈赫穿着黑色T恤,打扮十分简单随意,看上去瘦了不少。他和大女儿安安坐在一起乘坐游乐项目,小女儿则和阿……防溺水!防溺水!防溺水!这些安全知识要牢记夏季是溺水、中暑等安全事故的高发期,在享受快乐暑假的同时,家长们一定要重视孩子的安全防护,跟小编一起来看这份安全指南1。防溺水安全要点要牢记这些情况容易导致溺水……华为Mate30Pro5G版现可在部分线下体验店预约感谢IT之家网友薛定谔杀猫的线索投递!IT之家10月17日消息据IT之家网友反馈,华为Mate30Pro5G版现可在部分线下体验店预约。据介绍,一些体量大的华为体验店现已……华为Mate30Pro有线无线快充实测IT之家10月17日消息AndroidAuthority对华为Mate30Pro的快充速度进行了测试,其测试类型为华为27W无线快充、华为40W有线快充及12WPDQC3。0快……华为官方释出Mate30Pro4K延时摄影样片一瞬即永恒IT之家10月14日消息今日华为官方通过微博正式放出了由Mate30Pro录制的4K延时样片,一起来看一下。配置方面,华为Mate30Pro采用了由双4000万像素双主摄……
友情链接:易事利快生活快传网聚热点七猫云快好知快百科中准网快好找文好找中准网快软网