해당 프로젝트의 경우 windows 기반 자바 어플리케이션 또는 windows 기반 자바 기반 서버 구축 시
GDAL을 사용할 경우 OS에 설치하지 않고 프로젝트 내부에 GDAL을 적용하는 방법입니다.
spring boot 기반으로 프로젝트를 만들었구요.
github를 통해 소스코드 공개해놓았으니까
참고하실분들은 참고하셔도될꺼같네요~
github
https://github.com/engintruder/gdalProject
직접 프로젝트에 적용하실분은 아래를 보시고 따라하시면됩니다 :)
1. GDAL 다운로드
http://www.gisinternals.com/
해당 사이트에서 MSVC 버전 별 다운로드 가능
여기에서는 GDAL 2.4.4 MSVC 2017 x64 로 다운로드받음
2. resources 폴더 안에 gdal24 이름으로 복사
3. maven 추가
<dependency> <groupId>org.gdal</groupId> <artifactId>gdal</artifactId> <version>2.4.0</version> </dependency> |
4. 프로젝트 메인 함수에 아래 소스 적용
public static void setPath() { Boolean sw = false; try { ClassPathResource resource = new ClassPathResource("gdal24"); String resourceAbsPath = resource.getURI().getPath(); resourceAbsPath = resourceAbsPath.substring(1, resourceAbsPath.length() ); resourceAbsPath = resourceAbsPath.replace("/", "\\");
String current = printLibPath(); String gdalLib = resourceAbsPath + "\\bin\\gdal\\java"; System.setProperty("java.library.path", gdalLib + ";" + current);
try { Field fieldSysPath = ClassLoader.class.getDeclaredField("sys_paths"); fieldSysPath.setAccessible( true ); fieldSysPath.set( null, null ); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); }
String gdal_bin = resourceAbsPath + "\\bin"; Boolean gdal_bin_sw = false; String gdal_data = resourceAbsPath + "\\bin\\gdal-data"; Boolean gdal_data_sw = false; String gdal_driver_path = resourceAbsPath + "\\bin\\gdal\\plugins"; Boolean gdal_driver_sw = false; String gdal_path = resourceAbsPath + "\\bin;"; Boolean path_sw = false;
String path = ""; ProcessBuilder pb = new ProcessBuilder("CMD", "/C", "reg query \"HKEY_CURRENT_USER\\Environment\""); Process p = pb.start(); BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); StringBuilder builder = new StringBuilder(); String line = null; while ( (line = reader.readLine()) != null) { builder.append(line); builder.append(System.getProperty("line.separator")); if (line.contains(gdal_bin)){ gdal_bin_sw = true; } if (line.contains(gdal_data)) { gdal_data_sw = true; } if (line.contains(gdal_driver_path)){ gdal_driver_sw = true; } if (line.contains(gdal_path)){ path_sw = true; } if (line.contains("Path")){ String[] sp = line.split(" "); path = sp[3]; } }
try { if (gdal_bin_sw == false) { pb = new ProcessBuilder("CMD", "/C", "setx", "GDAL_BIN", gdal_bin); p = pb.start(); p.waitFor(); logger.info("SET GDAL_BIN"); } if (gdal_data_sw == false){ pb = new ProcessBuilder("CMD", "/C", "setx", "GDAL_DATA", gdal_data); p = pb.start(); p.waitFor(); logger.info("SET GDAL_DATA"); } if (gdal_driver_sw == false) { pb = new ProcessBuilder("CMD", "/C", "setx", "GDAL_DRIVER_PATH", gdal_driver_path); p = pb.start(); p.waitFor(); logger.info("SET GDAL_DRIVER_PATH"); } if (path_sw == false){ pb = new ProcessBuilder("CMD", "/C", "setx", "path", path + gdal_path); p = pb.start(); p.waitFor(); logger.info("SET path"); }
} catch (InterruptedException e) { e.printStackTrace(); } gdal.AllRegister(); } catch (IOException e) { e.printStackTrace(); } } public static String printLibPath() { String current = System.getProperty("java.library.path"); return current; }
|