在Java中,可以使用一些第三方庫來實現GeoTIFF與其他格式之間的轉換。以下是一些建議的庫和方法:
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-main</artifactId>
<version>${geotools.version}</version>
</dependency>
然后,可以使用GeoTools的OutputFormat
和RasterReader
類將GeoTIFF文件轉換為其他格式。以下是一個簡單的示例,將GeoTIFF文件轉換為GeoJSON格式:
import org.geotools.data.*;
import org.geotools.data.shapefile.ShapefileDataStoreFactory;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.geometry.jts.JTS;
import org.geotools.referencing.CRS;
import org.locationtech.jts.geom.Geometry;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import java.io.File;
import java.io.IOException;
public class GeoTiffToGeoJson {
public static void main(String[] args) throws IOException {
File inputTiff = new File("path/to/input.tiff");
File outputGeoJson = new File("path/to/output.geojson");
CoordinateReferenceSystem crs = CRS.decode("EPSG:4326");
SimpleFeatureCollection collection = loadGeoTiff(inputTiff, crs);
SimpleFeatureIterator iterator = collection.features();
// Convert the feature collection to GeoJSON format
// This is a simplified example, you may need to customize the output format
StringBuilder json = new StringBuilder();
json.append("{\"type\":\"FeatureCollection\",\"features\":[\n");
while (iterator.hasNext()) {
SimpleFeature feature = iterator.next();
Geometry geometry = (Geometry) feature.getDefaultGeometryProperty().getValue();
json.append("{\"type\":\"Feature\",\"geometry\":");
json.append(JTS.toJSON(geometry));
json.append(",\"properties\":{}}\n");
}
json.append("]}\n");
// Write the GeoJSON output to a file
// You can use any Java I/O library to write the JSON content to a file
}
private static SimpleFeatureCollection loadGeoTiff(File tiffFile, CoordinateReferenceSystem crs) throws IOException {
ShapefileDataStoreFactory factory = new ShapefileDataStoreFactory();
Map<String, Serializable> params = new HashMap<>();
params.put("url", tiffFile.toURI().toURL());
params.put("create spatial index", Boolean.TRUE);
params.put("geometry type", "GEOMETRY");
params.put("CRS", crs.toWKT());
DataStore store = factory.createNewDataStore(params);
SimpleFeatureCollection collection = store.getFeatureSource().getFeatures();
return collection;
}
}
這個示例僅適用于將GeoTIFF文件轉換為GeoJSON格式。要將GeoTIFF文件轉換為其他格式,您需要查找相應的庫和方法,并根據需要進行定制。
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-geometry</artifactId>
<version>${commons-geometry.version}</version>
</dependency>
然后,可以使用Apache Commons Geo的GeometryFormat
類將GeoTIFF文件轉換為其他格式。以下是一個簡單的示例,將GeoTIFF文件轉換為GeoJSON格式:
import org.apache.commons.geometry.geometry.Geometry;
import org.apache.commons.geometry.io.geojson.GeoJsonWriter;
import org.apache.commons.geometry.io.geojson.GeoJsonWriterFactory;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class GeoTiffToGeoJson {
public static void main(String[] args) throws IOException {
Path inputTiff = Paths.get("path/to/input.tiff");
Path outputGeoJson = Paths.get("path/to/output.geojson");
// Read the GeoTIFF file as a Geometry
Geometry geometry = GeometryFormat.read(inputTiff.toFile());
// Write the Geometry to GeoJSON format
try (FileWriter out = Files.newOutputStream(outputGeoJson);
GeoJsonWriter writer = GeoJsonWriterFactory.create(out)) {
writer.write(geometry);
}
}
}
這個示例僅適用于將單個GeoTIFF文件轉換為GeoJSON格式。要將GeoTIFF文件轉換為其他格式,您需要查找相應的庫和方法,并根據需要進行定制。