简要说明
在java利用geotools工具,将shpfile的坐标系转为指定坐标系。目前测试发现,国外坐标系转换有问题,转换处理的shpfile再去geoserver发布时,经纬度调转了,例如坐标系4547转3857;不过转国内坐标系4326/4490时,却正常。暂时还未定位到哪里问题,也欢迎留言指导一下。
maven依赖
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-shapefile</artifactId>
<version>27.0</version>
</dependency>
<dependency>
<groupId>org.geotools.jdbc</groupId>
<artifactId>gt-jdbc-postgis</artifactId>
<version>27.0</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-referencing</artifactId>
<version>27.0</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-render</artifactId>
<version>27.0</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-coverage</artifactId>
<version>27.0</version>
</dependency>
<dependency>
<groupId>it.geosolutions.imageio-ext</groupId>
<artifactId>imageio-ext-utilities</artifactId>
<version>1.4.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.conversantmedia/disruptor -->
<dependency>
<groupId>com.conversantmedia</groupId>
<artifactId>disruptor</artifactId>
<version>1.2.15</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.geotools/gt-geojson -->
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-geojson</artifactId>
<version>27.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.geotools/gt-epsg-hsql -->
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-epsg-hsql</artifactId>
<version>27.0</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-main</artifactId>
<version>27.0</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-opengis</artifactId>
<version>27.0</version>
</dependency>
<dependency>
<groupId>si.uom</groupId>
<artifactId>si-quantity</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>si.uom</groupId>
<artifactId>si-units</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>tech.uom.lib</groupId>
<artifactId>uom-lib-common</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>systems.uom</groupId>
<artifactId>systems-common</artifactId>
<version>2.0.2</version>
</dependency>
样例代码
public void changeShpSrid(String dataPath, Integer srid, String outputPath) {
ShapefileDataStore inputDataStore = null;
ShapefileDataStore outputDataStore = null;
try {
File inputFile = new File(dataPath);
if(!inputFile.exists()) {
System.out.println("源文件不存在");
return;
}
Map<String, Serializable> inputParams = new HashMap<>();
inputParams.put("url", inputFile.toURI().toURL());
inputDataStore = (ShapefileDataStore)DataStoreFinder.getDataStore(inputParams);
inputDataStore.setCharset(Charset.forName("GBK"));
String typeName = inputDataStore.getTypeNames()[0];
SimpleFeatureType schema = inputDataStore.getSchema(typeName);
File outputFile = new File(outputPath);
ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
Map<String, Serializable> outputParams = new HashMap<>();
outputParams.put("url", outputFile.toURI().toURL());
outputParams.put("create spatial index", Boolean.TRUE);
outputDataStore = (ShapefileDataStore) dataStoreFactory.createNewDataStore(outputParams);
outputDataStore.createSchema(schema);
outputDataStore.setCharset(Charset.forName("GBK"));
CoordinateReferenceSystem targetCRS = CRS.decode("EPSG:" + srid);
outputDataStore.forceSchemaCRS(targetCRS);
String outputTypeName = outputDataStore.getTypeNames()[0];
FeatureWriter<SimpleFeatureType, SimpleFeature> writer = outputDataStore.getFeatureWriterAppend(outputTypeName, Transaction.AUTO_COMMIT);
CRSAuthorityFactory factory = CRS.getAuthorityFactory(true);
SimpleFeatureIterator featureIterator = inputDataStore.getFeatureSource(typeName).getFeatures().features();
Feature firstFeature = featureIterator.next();
Geometry firstGeometry = (Geometry) ((SimpleFeature) firstFeature).getDefaultGeometry();
SimpleFeatureType sourceSimpleFeatureType = inputDataStore.getSchema();
CoordinateReferenceSystem coordinateReferenceSystem = sourceSimpleFeatureType.getCoordinateReferenceSystem();
changeNewSrid(factory, coordinateReferenceSystem, (SimpleFeature)firstFeature, firstGeometry, srid);
while (featureIterator.hasNext()) {
SimpleFeature feature = featureIterator.next();
Geometry sourceGeometry = (Geometry) feature.getDefaultGeometry();
SimpleFeature fNew = writer.next();
fNew.setAttributes(feature.getAttributes());
changeNewSrid(factory, coordinateReferenceSystem, fNew, sourceGeometry, srid);
}
featureIterator.close();
writer.close();
} catch (IOException e) {
System.out.println("文件流异常")
} catch (FactoryException e) {
System.out.println("获取geotools处理工厂异常");
} finally {
inputDataStore.dispose();
outputDataStore.dispose();
}
}
private void changeNewSrid(CRSAuthorityFactory factory, CoordinateReferenceSystem fileSrid, SimpleFeature feature
, Geometry sourceGeometry , Integer targetSrid) {
CoordinateReferenceSystem targetCoordinate = null;
try {
targetCoordinate = factory.createCoordinateReferenceSystem("EPSG:" + targetSrid);
MathTransform transform = CRS.findMathTransform(fileSrid, targetCoordinate, true);
Geometry res = JTS.transform(sourceGeometry, transform);
feature.setDefaultGeometry(res);
} catch (FactoryException e) {
System.out.println("获取转换工厂异常");
} catch (TransformException e) {
System.out.println("shpfile转换坐标系失败");
}
}