当前位置:   article > 正文

基于GeoTools的GeoJson导入到PostGis实战_gt-geojson

gt-geojson

          GeoJson是一种对各种地理数据结构进行编码的格式,基于json的地理空间信息数据交换格式。GeoJson对象可以用来表示几何,特征或者特征集合。支持地理点、线、面、多点、多线、多面及几何集合。GeoJson不是本文的重点,因此不再赘述。

         PostGIS是在对象关系型数据库PostgreSQL上增加了存储管理空间数据的能力的开源GIS数据库。本文的例子不能直接运行在PostgreSql的数据库上,需要装扩展依赖才行(具体可参考某度的详细教程)。

        本文将重点介绍如何基于GeoTools将GeoJson数据导入到PostGis中,并通过数据库客户端软件查询到导入结果。

        本实例运行环境:

1:win7+jdk8+GeoTools 24+POSTGIS: 3.1+PostgreSQL 9.6.14

       第一步:准备GeoTools依赖的包,详情见pom.xml

  1. <dependencies>
  2. <dependency>
  3. <groupId>junit</groupId>
  4. <artifactId>junit</artifactId>
  5. <version>4.11</version>
  6. <scope>test</scope>
  7. </dependency>
  8. <dependency>
  9. <groupId>org.geotools</groupId>
  10. <artifactId>gt-shapefile</artifactId>
  11. <version>${geotools.version}</version>
  12. </dependency>
  13. <dependency>
  14. <groupId>org.geotools</groupId>
  15. <artifactId>gt-swing</artifactId>
  16. <version>${geotools.version}</version>
  17. </dependency>
  18. <dependency>
  19. <groupId>org.geotools</groupId>
  20. <artifactId>gt-main</artifactId>
  21. <version>${geotools.version}</version>
  22. </dependency>
  23. <dependency>
  24. <groupId>org.geotools.jdbc</groupId>
  25. <artifactId>gt-jdbc-postgis</artifactId>
  26. <version>${geotools.version}</version>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.geotools</groupId>
  30. <artifactId>gt-metadata</artifactId>
  31. <version>${geotools.version}</version>
  32. </dependency>
  33. <dependency>
  34. <groupId>org.geotools</groupId>
  35. <artifactId>gt-referencing</artifactId>
  36. <version>${geotools.version}</version>
  37. </dependency>
  38. <dependency>
  39. <groupId>org.geotools</groupId>
  40. <artifactId>gt-geojson</artifactId>
  41. <version>${geotools.version}</version>
  42. </dependency>
  43. <dependency>
  44. <groupId>org.geotools</groupId>
  45. <artifactId>gt-epsg-hsql</artifactId>
  46. <version>${geotools.version}</version>
  47. </dependency>
  48. <dependency>
  49. <groupId>log4j</groupId>
  50. <artifactId>log4j</artifactId>
  51. <version>1.2.17</version>
  52. </dependency>
  53. <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
  54. <dependency>
  55. <groupId>com.alibaba</groupId>
  56. <artifactId>fastjson</artifactId>
  57. <version>1.2.47</version>
  58. </dependency>
  59. <!--非必需,简化I/O操作-->
  60. <dependency>
  61. <groupId>commons-io</groupId>
  62. <artifactId>commons-io</artifactId>
  63. <version>2.6</version>
  64. </dependency>
  65. <dependency>
  66. <groupId>org.postgis</groupId>
  67. <artifactId>postgis-driver</artifactId>
  68. <version>1.0</version>
  69. </dependency>
  70. </dependencies>

         第二步:创建PostGis数据源操作对象,相当于JDBC 数据源

  1. public class PostgisDataStore {
  2. private static Logger logger = Logger.getLogger(PostgisDataStore.class);
  3. private static DataStore postgisDataStore = null;
  4. private static String dbtype = null;
  5. private static String host = null;
  6. private static String port = null;
  7. private static String database = null;
  8. private static String schema = null;
  9. private static String username = null;
  10. private static String password = null;
  11. public PostgisDataStore() {
  12. }
  13. public static DataStore getInstance() {
  14. if (postgisDataStore == null) {
  15. Map<String, Object> params = new HashMap<String, Object>();
  16. params.put(PostgisNGDataStoreFactory.DBTYPE.key, dbtype);
  17. params.put(PostgisNGDataStoreFactory.HOST.key, host);
  18. params.put(PostgisNGDataStoreFactory.PORT.key, new Integer(port));
  19. params.put(PostgisNGDataStoreFactory.DATABASE.key, database);
  20. params.put(PostgisNGDataStoreFactory.SCHEMA.key, schema);
  21. params.put(PostgisNGDataStoreFactory.USER.key, username);
  22. params.put(PostgisNGDataStoreFactory.PASSWD.key, password);
  23. try {
  24. postgisDataStore = DataStoreFinder.getDataStore(params);
  25. logger.info("\nPostgisDataStore 初始化geotools中的 Datastore成功\n");
  26. } catch (IOException e) {
  27. logger.error("\nPostgisDataStore 初始化geotools中的 Datastore失败\n");
  28. logger.error(e.getMessage());
  29. }
  30. }
  31. return postgisDataStore;
  32. }
  33. }

        以上代码仅供参考,如需在实战中使用,还是尽可能利用已有的连接池技术提高应用性能。

        第三步:准备待带入的GeoJson数据,以Line为例

  1. {
  2. "type": "FeatureCollection",
  3. "features": [
  4. {
  5. "type": "Feature",
  6. "properties": {
  7. "name": "north",
  8. "linedesc": "A"
  9. },
  10. "geometry": {
  11. "type": "LineString",
  12. "coordinates": [
  13. [
  14. 476.7884016036988,
  15. 39.9663655061331
  16. ],
  17. [
  18. 476.7959976196289,
  19. 39.96679309103126
  20. ],
  21. [
  22. 476.80423736572268,
  23. 39.96613526700328
  24. ]
  25. ]
  26. }
  27. }
  28. ]
  29. }

        第四步:将GeoJson数据写入到数据库中,关键代码如下:

  1. /**
  2. * geojson 文件导入的Postgis数据库
  3. *
  4. * @param geojsonpath geojson文件存储路径,包括文件拓展名.json或者.geojson
  5. * @param tablename 自定义存储到postgis数据库存储的表的名称
  6. * @return 导入结果
  7. * @throws IOException
  8. */
  9. public static boolean importGeojson(String geojsonpath, String tablename) throws IOException {
  10. if (!validateGeojson(geojsonpath, true)) return false;
  11. DataStore pgDatastore = postgisDataStore.getInstance();
  12. FeatureJSON featureJSON = new FeatureJSON();
  13. FeatureCollection featureCollection = featureJSON.readFeatureCollection(new FileInputStream(geojsonpath));
  14. SimpleFeatureType geojsontype = (SimpleFeatureType) featureCollection.getSchema();
  15. SimpleFeatureTypeBuilder typeBuilder = new SimpleFeatureTypeBuilder();
  16. typeBuilder.init(geojsontype);
  17. typeBuilder.setName(tablename);
  18. SimpleFeatureType newtype = typeBuilder.buildFeatureType();
  19. pgDatastore.createSchema(newtype);
  20. FeatureIterator iterator = featureCollection.features();
  21. FeatureWriter<SimpleFeatureType, SimpleFeature> featureWriter = pgDatastore.getFeatureWriterAppend(tablename, Transaction.AUTO_COMMIT);
  22. while (iterator.hasNext()) {
  23. Feature feature = iterator.next();
  24. SimpleFeature simpleFeature = featureWriter.next();
  25. Collection<Property> properties = feature.getProperties();
  26. Iterator<Property> propertyIterator = properties.iterator();
  27. while (propertyIterator.hasNext()) {
  28. Property property = propertyIterator.next();
  29. simpleFeature.setAttribute(property.getName().toString(), property.getValue());
  30. }
  31. featureWriter.write();
  32. }
  33. iterator.close();
  34. featureWriter.close();
  35. pgDatastore.dispose();
  36. return true;
  37. }

        第五步:测试调用

  1. PostgisDataStore postgisDataStore = new PostgisDataStore();
  2. PostgisDataStore.setHost("127.0.0.1");
  3. PostgisDataStore.setPort("5432");
  4. // 扩展数据库类型,读取postgis该参数类型设置为postgis
  5. // 其余设置对应的数据库类型,要求jdbc支持并且引入相关库
  6. PostgisDataStore.setDbtype("postgis");
  7. PostgisDataStore.setDatabase("postgis_31_sample");
  8. PostgisDataStore.setSchema("public");
  9. PostgisDataStore.setUsername("asus");
  10. PostgisDataStore.setPassword("postgres");
  11. PostgisUtility.setPostgisDataStore(postgisDataStore);
  12. boolean flag = PostgisUtility.importGeojson("D:\\wzh_workspace_20210320\\geotools4postgis\\src\\main\\java\\iwuang\\line.geojson", "wzh_line2");
  13. System.out.println(flag);

        使用数据客户端软件打开后可以查询到导入的数据

图片

       总结,通过以上程序可以实现GeoJson数据的导入,如果遇到数据库连不上,请查找是不是数据连接信息没有正确配置,比如用户名密码等有没有正确输入。应用程序处理通用序列图如下:

图片

        本文代码参考至https://github.com/yieryi/geotools4postgis,有兴趣的各位可以到github上将代码下载下来进行调试。

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/102537
推荐阅读
相关标签
  

闽ICP备14008679号