基于GeoTools做GeoJson,PostGIS,Shapefile的转换
github:https://github.com/yieryi/geotools4postgis/
下面的geojson导入postgis,shp导入postgis,postgis导出shp,postgis导出geojson已经封装好在GitHub上。maven里导入对应的jar(复制pom相应代码),复制PostgisUtility.java和PostgisDataStore.java文件,就可以按照app.java里的方式调用这两个类文件里封装完的四个方法。这四个方法代码如下:
validateshp,validategeojson方法为验证路径有效性,可不调用或者自己判断,否则java可能会报文件路径异常
转载请说明来源。shp文件导入到postgis数据库中
public static boolean importShp(String shppath, String tablename) throws IOException { if (!validateShp(shppath, true)) return false; DataStore pgDatastore = postgisDataStore.getInstance(); ShapefileDataStore shapefileDataStore = null; shapefileDataStore = new ShapefileDataStore(new File(shppath).toURI().toURL()); shapefileDataStore.setCharset(Charset.forName("utf-8")); FeatureSource featureSource = shapefileDataStore.getFeatureSource(); FeatureCollection featureCollection = featureSource.getFeatures(); SimpleFeatureType shpfeaturetype = shapefileDataStore.getSchema(); SimpleFeatureTypeBuilder typeBuilder = new SimpleFeatureTypeBuilder(); typeBuilder.init(shpfeaturetype); typeBuilder.setName(tablename); SimpleFeatureType newtype = typeBuilder.buildFeatureType(); pgDatastore.createSchema(newtype); logger.info("\npostgis创建数据表成功"); FeatureIterator iterator = featureCollection.features(); FeatureWriter<SimpleFeatureType, SimpleFeature> featureWriter = pgDatastore.getFeatureWriterAppend(tablename, Transaction.AUTO_COMMIT); while (iterator.hasNext()) { Feature feature = iterator.next(); SimpleFeature simpleFeature = featureWriter.next(); Collection<Property> properties = feature.getProperties(); Iterator<Property> propertyIterator = properties.iterator(); while (propertyIterator.hasNext()) { Property property = propertyIterator.next(); simpleFeature.setAttribute(property.getName().toString(), property.getValue()); } featureWriter.write(); } iterator.close(); featureWriter.close(); shapefileDataStore.dispose(); pgDatastore.dispose(); logger.info("\nshp导入postgis成功"); return true; }
把geojson格式数据导入到postgsi,存储坐标和所有属性
public static boolean importGeojson(String geojsonpath, String tablename) throws IOException { if (!validateGeojson(geojsonpath, true)) return false; DataStore pgDatastore = postgisDataStore.getInstance(); FeatureJSON featureJSON = new FeatureJSON(); FeatureCollection featureCollection = featureJSON.readFeatureCollection(new FileInputStream(geojsonpath)); SimpleFeatureType geojsontype = (SimpleFeatureType) featureCollection.getSchema(); SimpleFeatureTypeBuilder typeBuilder = new SimpleFeatureTypeBuilder(); typeBuilder.init(geojsontype); typeBuilder.setName(tablename); SimpleFeatureType newtype = typeBuilder.buildFeatureType(); pgDatastore.createSchema(newtype); FeatureIterator iterator = featureCollection.features(); FeatureWriter<SimpleFeatureType, SimpleFeature> featureWriter = pgDatastore.getFeatureWriterAppend(tablename, Transaction.AUTO_COMMIT); while (iterator.hasNext()) { Feature feature = iterator.next(); SimpleFeature simpleFeature = featureWriter.next(); Collection<Property> properties = feature.getProperties(); Iterator<Property> propertyIterator = properties.iterator(); while (propertyIterator.hasNext()) { Property property = propertyIterator.next(); simpleFeature.setAttribute(property.getName().toString(), property.getValue()); } featureWriter.write(); } iterator.close(); featureWriter.close(); pgDatastore.dispose(); return true; }
导出postgis数据库中的指定矢量表为shp文件
public static boolean exportShp(String tablename, String shpPath) throws IOException, FactoryException { DataStore pgDatastore = postgisDataStore.getInstance(); FeatureSource featureSource = pgDatastore.getFeatureSource(tablename); FeatureCollection featureCollection = featureSource.getFeatures(); FeatureIterator<SimpleFeature> iterator = featureCollection.features(); SimpleFeatureType pgfeaturetype = pgDatastore.getSchema(tablename); File file = new File(shpPath); if (!validateShp(shpPath, false)) return false; Map<String, Serializable> params = new HashMap<String, Serializable>(); params.put(ShapefileDataStoreFactory.URLP.key, file.toURI().toURL()); ShapefileDataStore shpDataStore = (ShapefileDataStore) new ShapefileDataStoreFactory().createNewDataStore(params); String srid = pgfeaturetype.getGeometryDescriptor().getUserData().get("nativeSRID").toString(); SimpleFeatureTypeBuilder typeBuilder = new SimpleFeatureTypeBuilder(); typeBuilder.init(pgfeaturetype); if (!srid.equals("0")) { CoordinateReferenceSystem crs = CRS.decode("EPSG:" + srid, true); typeBuilder.setCRS(crs); } pgfeaturetype = typeBuilder.buildFeatureType(); shpDataStore.setCharset(Charset.forName("utf-8")); shpDataStore.createSchema(pgfeaturetype); FeatureWriter<SimpleFeatureType, SimpleFeature> featureWriter = shpDataStore.getFeatureWriter(shpDataStore.getTypeNames()[0], AUTO_COMMIT); while (iterator.hasNext()) { Feature feature = iterator.next(); SimpleFeature simpleFeature = featureWriter.next(); Collection<Property> properties = feature.getProperties(); Iterator<Property> propertyIterator = properties.iterator(); while (propertyIterator.hasNext()) { Property property = propertyIterator.next(); if (geomfield(property.getName().toString())) { simpleFeature.setAttribute("the_geom", property.getValue()); continue; } simpleFeature.setAttribute(property.getName().toString(), property.getValue()); } featureWriter.write(); } iterator.close(); featureWriter.close(); pgDatastore.dispose(); return true; }
导出postgis数据库中的指定矢量表为geojson文件
public static boolean exportGeojson(String tablename, String geojsonPath) throws IOException, FactoryException { DataStore pgDatastore = postgisDataStore.getInstance(); FeatureSource featureSource = pgDatastore.getFeatureSource(tablename); FeatureCollection featureCollection = featureSource.getFeatures(); FeatureJSON featureJSON = new FeatureJSON(); File file = new File(geojsonPath); if (!file.exists()) { file.createNewFile(); } OutputStream outputStream = new FileOutputStream(file, false); featureJSON.writeFeatureCollection(featureCollection, outputStream); pgDatastore.dispose(); return true; }