2021年1月23日
自己简单总结了以下,在github上。链接地址 :https://github.com/yieryi/gts4vect
如果改功能对您有用,欢迎star,代码只写了第一版,有人反馈问题的话我会改进的。
该博客为原创,转载请注明链接;https://blog.csdn.net/imlang/article/details/81434652;
下面的代码介绍参考性不大,大约是18年写的。没有再检查过。建议直接github上查看代码,封装了几个非常简单的静态方法;
以下不删,仅供参考。不再回复答复。
下面的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 =<