我想在java-gdal中编写ogr2ogr -f "GeoJSON" destination.geojson source.geojson -s_srs EPSG:3068 -t_srs EPSG:4326
代码。我试着通过查看this example来了解它应该如何工作,但由于ogr2ogr有很多用途我无法确定哪些与我相关。这是我的尝试:
public static void ogr2ogr(String name, String sourceSRS, String destSRS) {
ogr.RegisterAll();
String pszFormat = "GeoJSON";
DataSource poDS = ogr.Open(name + "_" + sourceSRS+".geojson", false);
/* -------------------------------------------------------------------- */
/* Try opening the output datasource as an existing, writable */
/* -------------------------------------------------------------------- */
DataSource poODS = ogr.Open(name + "_" + destSRS+".geojson", 0);
Driver poDriver = ogr.GetDriverByName(pszFormat);
SpatialReference poOutputSRS = new SpatialReference();
poOutputSRS.SetFromUserInput( destSRS );
SpatialReference poSourceSRS = new SpatialReference();
poSourceSRS.SetFromUserInput( sourceSRS );
CoordinateTransformation poCT = CoordinateTransformation.CreateCoordinateTransformation( poSourceSRS, poOutputSRS );
Layer poDstLayer = poODS.GetLayerByName("bla");
Feature poDstFeature = new Feature( poDstLayer.GetLayerDefn() );
Geometry poDstGeometry = poDstFeature.GetGeometryRef();
int eErr = poDstGeometry.Transform( poCT );
poDstLayer.CommitTransaction();
poDstGeometry.AssignSpatialReference(poOutputSRS);
}
我在poOutputSRS.SetFromUserInput( destSRS );
(即第99行)收到此异常:
ERROR 3: Cannot open file 'C:\Users\Users\Desktop\test\target_EPSG4326.geojson'
Exception in thread "main" java.lang.RuntimeException: OGR Error: Corrupt data
at org.gdal.osr.osrJNI.SpatialReference_SetFromUserInput(Native Method)
at org.gdal.osr.SpatialReference.SetFromUserInput(SpatialReference.java:455)
at wmsRasterToGeojsonVector.GdalJava.ogr2ogr(GdalJava.java:99)
at wmsRasterToGeojsonVector.GdalJava.main(GdalJava.java:30)