Ticker

6/random/ticker-posts

How To Run GDAL from Spring Boot Application in Docker Container

 

 

 Here is how I run GDAL commands from Spring Boot Java application and create a Docker image.


 To keep it simple, just create a simple spring boot application with web dependencies.


@SpringBootApplication
@RestController
public class GDALonDockerApplication {
public final Logger LOGGER = LoggerFactory.getLogger(GDALonDockerApplication.class);

public static void main(String[] args) {
SpringApplication.run(GDALonDockerApplication.class, args);
}

@RequestMapping("/")
public String executeGdalCommand() {
String[] commands = "gdal_translate -of GTiff -co -COMPRESS=JPEG -co \"TILED=YES\" C46104A1.TIF output.jpeg".split(" ");
try {

Process p = Runtime.getRuntime().exec(commands);
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;

try {
while ((line = input.readLine()) != null) {
LOGGER.info(line);
}
} catch (IOException e) {
e.printStackTrace();
}

p.getErrorStream().close();
p.getInputStream().close();
p.getOutputStream().close();
p.destroy();
} catch (IOException e) {
throw new RuntimeException(e);
}
return "GDAL command executed! Well done!";
}

@GetMapping(value = "/output")
public ResponseEntity<Resource> getOutput() {
Path path = Paths.get("./");
try {
return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION,
"attachment; filename=\"output.jpeg\"").body(new UrlResource(path.resolve("output.jpeg").toUri()));
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
}
}

 To dockerize the app, use this Dockerfile.

FROM openjdk:11-jdk-slim-buster
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
COPY C46104A1.TIF C46104A1.TIF
RUN apt-get update
RUN apt-get -y install libgdal-dev
RUN apt-get -y install gdal-data
RUN apt-get -y install gdal-bin
RUN gdalinfo --version
ENTRYPOINT ["java","-jar","/app.jar"]



Yorum Gönder

0 Yorumlar