基于GeoTools做GeoJson,PostGIS,Shapefile的轉換
github:https://github.com/yieryi/geotools4postgis/
下面的geojson導入postgis,shp導入postgis,postgis導出shp,postgis導出geojson已經(jīng)封裝好在GitHub上。maven里導入對應的jar(復制pom相應代碼),復制PostgisUtility.java和PostgisDataStore.java文件,就可以按照app.java里的方式調用這兩個類文件里封裝完的四個方法。這四個方法代碼如下:
validateshp,validategeojson方法為驗證路徑有效性,可不調用或者自己判斷,否則java可能會報文件路徑異常
轉載請說明來源。shp文件導入到postgis數(shù)據(jù)庫中
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創(chuàng)建數(shù)據(jù)表成功");
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格式數(shù)據(jù)導入到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數(shù)據(jù)庫中的指定矢量表為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數(shù)據(jù)庫中的指定矢量表為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;
}
本文內容根據(jù)網(wǎng)絡資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點和立場。
轉載請注明,如有侵權,聯(lián)系刪除。