[출처] http://blog.naver.com/PostView.nhn?blogId=korn123&logNo=30135388874


참고 :  헤더 정보 ( http://www.garykessler.net/library/file_sigs.html )


1. HeaderCheck.java :: shape 파일의 header정보를 파싱합니다.

package com.appspot.hihanjoong.shp;


import java.io.File;
import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.channels.FileChannel;

/**
 * shape file정보를 HeaderInfo 객체에 저장함
 * 
 * @author 조한중
 *
 */
public class HeaderCheck {

 private FileInputStream fileInputStream; 
 private HeaderInfo headerInfo;          
 
 /**
  * 
  * @param path 파일경로
  * @param fileName 파일명
  */
 public HeaderCheck(String path,String fileName) {
  this(path + fileName);
 }
 
 /**
  * 
  * @param fullPath 파일명을 포함한 full path
  */
 public HeaderCheck(String fullPath) {
  
  headerInfo = new HeaderInfo();
  File shpFile = new File(fullPath);
  ByteBuffer buf = ByteBuffer.allocate(100);
  

  try {
   if(!shpFile.exists()) System.err.println("파일 없음");   
   fileInputStream = new FileInputStream(shpFile);
   FileChannel channel = fileInputStream.getChannel();
   channel.read(buf);
   buf.flip();

   int fileCode = buf.getInt(0);
   headerInfo.setFileCode(fileCode);

   int filelength = buf.getInt(24);
   headerInfo.setFilelength(filelength);
 
   buf.order(ByteOrder.LITTLE_ENDIAN);
   
   int version = buf.getInt(28);
   headerInfo.setVersion(version);
   
   int shapeType = buf.getInt(32);
   headerInfo.setShapeType(shapeType);
   
   double minX = buf.getDouble(36);
   headerInfo.setMinX(minX);
   
   double minY = buf.getDouble(44);
   headerInfo.setMinY(minY);
   
   double minZ = buf.getDouble(68);
   headerInfo.setMinZ(minZ);
   
   double minM = buf.getDouble(84);
   headerInfo.setMinM(minM);
   
   double maxX = buf.getDouble(52);
   headerInfo.setMaxX(maxX);
   
   double maxY = buf.getDouble(60);
   headerInfo.setMaxY(maxY);
   
   double maxZ = buf.getDouble(76);
   headerInfo.setMaxZ(maxZ);
   
   double maxM = buf.getDouble(92);
   headerInfo.setMaxM(maxM);
   
  } catch(Exception e) {
   e.printStackTrace();
  }  
 }
 
 /**
  * header 정보를 반환
  * @return HeaderInfo
  */
 public HeaderInfo getHeaderInfo(){
  return headerInfo;
 }
  

}

2. HeaderInfo.java :: 파싱된 header정보를 저장합니다.

package com.appspot.hihanjoong.shp;

/**
 * header정보를 저장
 * 
 * <h1>Shape file header</h1>
 * <ul>
 *  <li>fileCode
 *  <li>filelength
 *  <li>version
 *  <li>shapeType
 *  <li>minX
 *  <li>minY
 *  <li>minZ
 *  <li>minM
 *  <li>maxX
 *  <li>maxY
 *  <li>maxZ
 *  <li>maxM
 * </ul>
 * 
 * @author 조한중
 *
 */
public class HeaderInfo {
 
 private int fileCode;
 private int filelength;
 private int version;
 private int shapeType;
 private double minX;
 private double minY;
 private double minZ;
 private double minM;
 private double maxX;
 private double maxY;
 private double maxZ;
 private double maxM;
 private String shapeTypeNm;
 
 public int getFileCode() {
  return fileCode;
 }


 public void setFileCode(int fileCode) {
  this.fileCode = fileCode;
 }


 public int getFilelength() {
  return filelength;
 }


 public void setFilelength(int filelength) {
  this.filelength = filelength;
 }


 public int getVersion() {
  return version;
 }


 public void setVersion(int version) {
  this.version = version;
 }


 public int getShapeType() {
  return shapeType;
 }


 public void setShapeType(int shapeType) {
  this.shapeType = shapeType;
  shapeTypeNm = getTypeName(this.shapeType);
 }

 public double getMinX() {
  return minX;
 }


 public void setMinX(double minX) {
  this.minX = minX;
 }


 public double getMinY() {
  return minY;
 }


 public void setMinY(double minY) {
  this.minY = minY;
 }


 public double getMinZ() {
  return minZ;
 }


 public void setMinZ(double minZ) {
  this.minZ = minZ;
 }


 public double getMinM() {
  return minM;
 }


 public void setMinM(double minM) {
  this.minM = minM;
 }


 public double getMaxX() {
  return maxX;
 }


 public void setMaxX(double maxX) {
  this.maxX = maxX;
 }


 public double getMaxY() {
  return maxY;
 }


 public void setMaxY(double maxY) {
  this.maxY = maxY;
 }


 public double getMaxZ() {
  return maxZ;
 }


 public void setMaxZ(double maxZ) {
  this.maxZ = maxZ;
 }


 public double getMaxM() {
  return maxM;
 }


 public void setMaxM(double maxM) {
  this.maxM = maxM;
 }

 private String getTypeName(int code){
  String type = "";
  
  switch(code){
  case 0 : type = "Null Shape"; break;
  case 1 : type = "Point"; break;
  case 3 : type = "PolyLine"; break;
  case 5 : type = "PolyGon"; break;
  case 8 : type = "MultiPoint"; break;
  case 11 : type = "PointZ"; break;
  case 13 : type = "PolyLineZ"; break;
  case 15 : type = "PolyGonZ"; break;
  case 18 : type = "MultiPointZ"; break;
  case 21 : type = "PointM"; break;
  case 23 : type = "PolyLineM"; break;
  case 25 : type = "PolyGonM"; break;
  case 28 : type = "MultiPointM"; break;
  case 31 : type = "MultiPatch"; break;
  }
  
  return type;
 }

 @Override
 public String toString() {
  StringBuffer sb = new StringBuffer();
  sb.append("fileCode : " + fileCode + "\n");
  sb.append("filelength : " + filelength + "\n");
  sb.append("version : " + version + "\n");
  sb.append("shapeType : " + shapeType + "\n");
  sb.append("shapeTypeNm : " + shapeTypeNm + "\n");
  sb.append("minX : " + minX + "\n");
  sb.append("minY : " + minY + "\n");
  sb.append("minZ : " + minZ + "\n");
  sb.append("minM : " + minM + "\n");
  sb.append("maxX : " + maxX + "\n");
  sb.append("maxY : " + maxY + "\n");
  sb.append("maxZ : " + maxZ + "\n");
  sb.append("maxM : " + maxM + "\n");
  return sb.toString();
 }
 

}

3. ShpLoaderMain.java :: 예제를 실행시키는 메인메서드 

package com.appspot.hihanjoong.shp;

public class ShpLoaderMain {
 
 public static void main(String[] args) {
  HeaderCheck check = new HeaderCheck("","test.shp");
  System.out.println(check.getHeaderInfo().toString());
 }

}

4. 실행결과 


+ Recent posts