当前位置:   article > 正文

geotools导入maven项目_org.geotools.data.shapefile.shapefiledatastore mav

org.geotools.data.shapefile.shapefiledatastore maven

1.pom.xml

增加下列代码

  1. <repositories>
  2. <repository>
  3. <id>osgeo</id>
  4. <name>OSGeo Release Repository</name>
  5. <url>https://repo.osgeo.org/repository/release/</url>
  6. <snapshots><enabled>false</enabled></snapshots>
  7. <releases><enabled>true</enabled></releases>
  8. </repository>
  9. <repository>
  10. <id>osgeo-snapshot</id>
  11. <name>OSGeo Snapshot Repository</name>
  12. <url>https://repo.osgeo.org/repository/snapshot/</url>
  13. <snapshots><enabled>true</enabled></snapshots>
  14. <releases><enabled>false</enabled></releases>
  15. </repository>
  16. </repositories>
  17. <dependencies>
  18. <dependency>
  19. <groupId>org.geotools</groupId>
  20. <artifactId>gt-shapefile</artifactId>
  21. <version>20.3</version>
  22. </dependency>
  23. <dependency>
  24. <groupId>org.geotools</groupId>
  25. <artifactId>gt-api</artifactId>
  26. <version>20.3</version>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.geotools</groupId>
  30. <artifactId>gt-geojson</artifactId>
  31. <version>20.3</version>
  32. </dependency>
  33. <dependency>
  34. <groupId>org.geotools</groupId>
  35. <artifactId>gt-geometry</artifactId>
  36. <version>20.3</version>
  37. </dependency>
  38. <dependency>
  39. <groupId>org.geotools</groupId>
  40. <artifactId>gt-jts-wrapper</artifactId>
  41. <version>20.3</version>
  42. </dependency>
  43. <dependency>
  44. <groupId>org.geotools</groupId>
  45. <artifactId>gt-main</artifactId>
  46. <version>20.3</version>
  47. </dependency>
  48. <dependency>
  49. <groupId>org.geotools</groupId>
  50. <artifactId>gt-epsg-hsql</artifactId>
  51. <version>20.3</version>
  52. </dependency>
  53. <dependency>
  54. <groupId>org.geotools</groupId>
  55. <artifactId>gt-opengis</artifactId>
  56. <version>20.3</version>
  57. </dependency>
  58. <dependency>
  59. <groupId>org.geotools</groupId>
  60. <artifactId>gt-data</artifactId>
  61. <version>20.3</version>
  62. </dependency>
  63. <dependency>
  64. <groupId>org.geotools</groupId>
  65. <artifactId>gt-referencing</artifactId>
  66. <version>20.3</version>
  67. </dependency>
  68. </dependencies>

2.geotools中shp和geojson格式的相互转换

geotools中shp和geojson格式的相互转换 - LZU-GIS - 博客园

  1. package com.lzugis.geotools;
  2. import java.io.File;
  3. import java.io.Reader;
  4. import java.io.Serializable;
  5. import java.io.StringReader;
  6. import java.io.StringWriter;
  7. import java.nio.charset.Charset;
  8. import java.util.HashMap;
  9. import java.util.Map;
  10. import org.geotools.data.FeatureWriter;
  11. import org.geotools.data.Transaction;
  12. import org.geotools.data.shapefile.ShapefileDataStore;
  13. import org.geotools.data.shapefile.ShapefileDataStoreFactory;
  14. import org.geotools.data.simple.SimpleFeatureCollection;
  15. import org.geotools.data.simple.SimpleFeatureIterator;
  16. import org.geotools.data.simple.SimpleFeatureSource;
  17. import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
  18. import org.geotools.geojson.feature.FeatureJSON;
  19. import org.geotools.geojson.geom.GeometryJSON;
  20. import org.geotools.referencing.crs.DefaultGeographicCRS;
  21. import org.opengis.feature.simple.SimpleFeature;
  22. import org.opengis.feature.simple.SimpleFeatureType;
  23. import com.amazonaws.util.json.JSONArray;
  24. import com.amazonaws.util.json.JSONObject;
  25. import com.lzugis.CommonMethod;
  26. import com.vividsolutions.jts.geom.LineString;
  27. import com.vividsolutions.jts.geom.MultiLineString;
  28. import com.vividsolutions.jts.geom.MultiPoint;
  29. import com.vividsolutions.jts.geom.MultiPolygon;
  30. import com.vividsolutions.jts.geom.Point;
  31. import com.vividsolutions.jts.geom.Polygon;
  32. public class FileFormat {
  33. private static CommonMethod cm = new CommonMethod();
  34. /**
  35. * geojson转换为shp文件
  36. * @param jsonPath
  37. * @param shpPath
  38. * @return
  39. */
  40. public Map geojson2Shape(String jsonPath, String shpPath){
  41. Map map = new HashMap();
  42. GeometryJSON gjson = new GeometryJSON();
  43. try{
  44. String strJson = cm.getFileContent(jsonPath);
  45. JSONObject json = new JSONObject(strJson);
  46. JSONArray features = (JSONArray) json.get("features");
  47. JSONObject feature0 = new JSONObject(features.get(0).toString());
  48. System.out.println(feature0.toString());
  49. String strType = ((JSONObject)feature0.get("geometry")).getString("type").toString();
  50. Class<?> geoType = null;
  51. switch(strType){
  52. case "Point":
  53. geoType = Point.class;
  54. case "MultiPoint":
  55. geoType = MultiPoint.class;
  56. case "LineString":
  57. geoType = LineString.class;
  58. case "MultiLineString":
  59. geoType = MultiLineString.class;
  60. case "Polygon":
  61. geoType = Polygon.class;
  62. case "MultiPolygon":
  63. geoType = MultiPolygon.class;
  64. }
  65. //创建shape文件对象
  66. File file = new File(shpPath);
  67. Map<String, Serializable> params = new HashMap<String, Serializable>();
  68. params.put( ShapefileDataStoreFactory.URLP.key, file.toURI().toURL() );
  69. ShapefileDataStore ds = (ShapefileDataStore) new ShapefileDataStoreFactory().createNewDataStore(params);
  70. //定义图形信息和属性信息
  71. SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder();
  72. tb.setCRS(DefaultGeographicCRS.WGS84);
  73. tb.setName("shapefile");
  74. tb.add("the_geom", geoType);
  75. tb.add("POIID", Long.class);
  76. ds.createSchema(tb.buildFeatureType());
  77. //设置编码
  78. Charset charset = Charset.forName("GBK");
  79. ds.setCharset(charset);
  80. //设置Writer
  81. FeatureWriter<SimpleFeatureType, SimpleFeature> writer = ds.getFeatureWriter(ds.getTypeNames()[0], Transaction.AUTO_COMMIT);
  82. for(int i=0,len=features.length();i<len;i++){
  83. String strFeature = features.get(i).toString();
  84. Reader reader = new StringReader(strFeature);
  85. SimpleFeature feature = writer.next();
  86. feature.setAttribute("the_geom",gjson.readMultiPolygon(reader));
  87. feature.setAttribute("POIID",i);
  88. writer.write();
  89. }
  90. writer.close();
  91. ds.dispose();
  92. map.put("status", "success");
  93. map.put("message", shpPath);
  94. }
  95. catch(Exception e){
  96. map.put("status", "failure");
  97. map.put("message", e.getMessage());
  98. e.printStackTrace();
  99. }
  100. return map;
  101. }
  102. /**
  103. * shp转换为Geojson
  104. * @param shpPath
  105. * @return
  106. */
  107. public Map shape2Geojson(String shpPath, String jsonPath){
  108. Map map = new HashMap();
  109. FeatureJSON fjson = new FeatureJSON();
  110. try{
  111. StringBuffer sb = new StringBuffer();
  112. sb.append("{\"type\": \"FeatureCollection\",\"features\": ");
  113. File file = new File(shpPath);
  114. ShapefileDataStore shpDataStore = null;
  115. shpDataStore = new ShapefileDataStore(file.toURL());
  116. //设置编码
  117. Charset charset = Charset.forName("GBK");
  118. shpDataStore.setCharset(charset);
  119. String typeName = shpDataStore.getTypeNames()[0];
  120. SimpleFeatureSource featureSource = null;
  121. featureSource = shpDataStore.getFeatureSource (typeName);
  122. SimpleFeatureCollection result = featureSource.getFeatures();
  123. SimpleFeatureIterator itertor = result.features();
  124. JSONArray array = new JSONArray();
  125. while (itertor.hasNext())
  126. {
  127. SimpleFeature feature = itertor.next();
  128. StringWriter writer = new StringWriter();
  129. fjson.writeFeature(feature, writer);
  130. JSONObject json = new JSONObject(writer.toString());
  131. array.put(json);
  132. }
  133. itertor.close();
  134. sb.append(array.toString());
  135. sb.append("}");
  136. //写入文件
  137. cm.append2File(jsonPath, sb.toString());
  138. map.put("status", "success");
  139. map.put("message", sb.toString());
  140. }
  141. catch(Exception e){
  142. map.put("status", "failure");
  143. map.put("message", e.getMessage());
  144. e.printStackTrace();
  145. }
  146. return map;
  147. }
  148. /**
  149. * 工具类测试方法
  150. * @param args
  151. */
  152. public static void main(String[] args){
  153. FileFormat fileFormat = new FileFormat();
  154. long start = System.currentTimeMillis();
  155. String shpPath = "/Users/lzugis/Documents/chinadata/cityboundry.shp";
  156. String jsonPath = "/Users/lzugis/Documents/chinadata/cityboundry.geojson";
  157. Map map = fileFormat.shape2Geojson(shpPath, jsonPath);
  158. // String shpPath = "D:/data/beijing/China43262.shp";
  159. // String jsonPath = "D:/data/beijing/China4326.geojson";
  160. // Map map = fileFormat.geojson2Shape(jsonPath, shpPath);
  161. System.out.println(jsonPath+",共耗时"+(System.currentTimeMillis() - start)+"ms");
  162. }
  163. /**
  164. *
  165. * @param shpPath shp文件路径
  166. * @return geojson数据
  167. */
  168. public static Object shape2Geojson(String shpPath){
  169. FeatureJSON fjson = new FeatureJSON();
  170. JSONObject geojsonObject=new JSONObject();
  171. geojsonObject.put("type","FeatureCollection");
  172. try{
  173. File file = new File(shpPath);
  174. ShapefileDataStore shpDataStore = null;
  175. shpDataStore = new ShapefileDataStore(file.toURL());
  176. //设置编码
  177. Charset charset = Charset.forName("GBK");
  178. shpDataStore.setCharset(charset);
  179. String typeName = shpDataStore.getTypeNames()[0];
  180. SimpleFeatureSource featureSource = null;
  181. featureSource = shpDataStore.getFeatureSource (typeName);
  182. SimpleFeatureCollection result = featureSource.getFeatures();
  183. SimpleFeatureIterator itertor = result.features();
  184. JSONArray array = new JSONArray();
  185. while (itertor.hasNext())
  186. {
  187. SimpleFeature feature = itertor.next();
  188. StringWriter writer = new StringWriter();
  189. fjson.writeFeature(feature, writer);
  190. String temp=writer.toString();
  191. byte[] b=temp.getBytes("iso8859-1");
  192. temp=new String(b,"gbk");
  193. JSONObject json = JSON.parseObject(temp);
  194. array.add(json);
  195. }
  196. geojsonObject.put("features",array);
  197. itertor.close();
  198. }
  199. catch(Exception e){
  200. e.printStackTrace();
  201. }
  202. return geojsonObject;
  203. }
  204. }

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

闽ICP备14008679号