Gdal Support mac,linux,win os

This commit is contained in:
DanielLee
2025-12-16 18:05:59 +09:00
parent 827c056d02
commit 9f0c55fd0c
8 changed files with 359 additions and 42 deletions

View File

@@ -152,55 +152,97 @@ public class FIleChecker {
File file = new File(filePath);
if (!file.exists()) {
System.err.println("파일이 존재하지 않습니다: " + filePath);
return false;
}
boolean hasDriver = false;
// 운영체제 감지
String osName = System.getProperty("os.name").toLowerCase();
boolean isWindows = osName.contains("win");
boolean isMac = osName.contains("mac");
boolean isUnix = osName.contains("nix") || osName.contains("nux") || osName.contains("aix");
// gdalinfo 경로 찾기 (일반적인 설치 경로 우선 확인)
String gdalinfoPath = findGdalinfoPath();
if (gdalinfoPath == null) {
System.err.println("gdalinfo 명령어를 찾을 수 없습니다. GDAL이 설치되어 있는지 확인하세요.");
System.err.println("macOS: brew install gdal");
System.err.println("Ubuntu/Debian: sudo apt-get install gdal-bin");
System.err.println("CentOS/RHEL: sudo yum install gdal");
return false;
}
List<String> command = new ArrayList<>();
// 윈도우용
command.add("cmd.exe"); // 윈도우 명령 프롬프트 실행
command.add("/c"); // 명령어를 수행하고 종료한다는 옵션
command.add("gdalinfo");
command.add(filePath);
command.add("|");
command.add("findstr");
command.add("/i");
command.add("Geo");
/*
command.add("sh"); // 리눅스,맥 명령 프롬프트 실행
command.add("-c"); // 명령어를 수행하고 종료한다는 옵션
command.add("gdalinfo");
command.add(filePath);
command.add("|");
command.add("grep");
command.add("-i");
command.add("Geo");
*/
if (isWindows) {
// 윈도우용
command.add("cmd.exe");
command.add("/c");
command.add(gdalinfoPath + " \"" + filePath + "\" | findstr /i Geo");
} else if (isMac || isUnix) {
// 리눅스, 맥용
command.add("sh");
command.add("-c");
command.add(gdalinfoPath + " \"" + filePath + "\" | grep -i Geo");
} else {
System.err.println("지원하지 않는 운영체제: " + osName);
return false;
}
ProcessBuilder processBuilder = new ProcessBuilder(command);
processBuilder.redirectErrorStream(true);
Process process = null;
BufferedReader reader = null;
try {
Process process = processBuilder.start();
System.out.println("gdalinfo 명령어 실행 시작: " + filePath);
process = processBuilder.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println("line == " + line);
System.out.println("gdalinfo 출력: " + line);
if (line.contains("Driver: GTiff/GeoTIFF")) {
hasDriver = true;
break;
}
}
process.waitFor();
int exitCode = process.waitFor();
System.out.println("gdalinfo 종료 코드: " + exitCode);
} catch (Exception e) {
// 프로세스가 정상 종료되지 않았고 Driver를 찾지 못한 경우
if (exitCode != 0 && !hasDriver) {
System.err.println("gdalinfo 명령 실행 실패. Exit code: " + exitCode);
}
} catch (IOException e) {
System.err.println("gdalinfo 실행 중 I/O 오류 발생: " + e.getMessage());
e.printStackTrace();
return false;
} catch (InterruptedException e) {
System.err.println("gdalinfo 실행 중 인터럽트 발생: " + e.getMessage());
Thread.currentThread().interrupt();
return false;
} catch (Exception e) {
System.err.println("gdalinfo 실행 중 예상치 못한 오류 발생: " + e.getMessage());
e.printStackTrace();
return false;
} finally {
// 리소스 정리
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
System.err.println("BufferedReader 종료 중 오류: " + e.getMessage());
}
}
if (process != null) {
process.destroy();
}
}
return hasDriver;
@@ -349,4 +391,54 @@ public class FIleChecker {
return nameComparator;
}
}
/**
* gdalinfo 실행 파일 경로를 찾습니다.
*
* @return gdalinfo 경로 (찾지 못하면 null)
*/
private static String findGdalinfoPath() {
// 일반적인 설치 경로 확인
String[] possiblePaths = {
"/usr/local/bin/gdalinfo", // Homebrew (macOS)
"/opt/homebrew/bin/gdalinfo", // Homebrew (Apple Silicon macOS)
"/usr/bin/gdalinfo", // Linux
"gdalinfo" // PATH에 있는 경우
};
for (String path : possiblePaths) {
if (isCommandAvailable(path)) {
return path;
}
}
return null;
}
/**
* 명령어가 사용 가능한지 확인합니다.
*
* @param command 명령어 경로
* @return 사용 가능 여부
*/
private static boolean isCommandAvailable(String command) {
try {
ProcessBuilder pb = new ProcessBuilder(command, "--version");
pb.redirectErrorStream(true);
Process process = pb.start();
// 프로세스 완료 대기 (최대 5초)
boolean finished = process.waitFor(5, java.util.concurrent.TimeUnit.SECONDS);
if (!finished) {
process.destroy();
return false;
}
// 종료 코드가 0이면 정상 (일부 명령어는 --version에서 다른 코드 반환할 수 있음)
return process.exitValue() == 0 || process.exitValue() == 1;
} catch (Exception e) {
return false;
}
}
}