⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 featureclass.java

📁 读取shp文件,不依赖于其他架包 FeatureClass 主类
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * @(#)FeatureClass.java  15:06:17 2006-7-5 2006
 *
 * Copyright (C) 2006 Beijing Spaceware High Tech. Co., Ltd.  
 * All rights reserved.
 * 
 * Copyright (C) 2006 CyberGIS Studio, Peking University.  
 * All rights reserved.
 */
package cn.com.sdgis.gis.geobase;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;

import cn.com.sdgis.gis.Constants.FieldType;
import cn.com.sdgis.gis.Constants.GeometryType;
import cn.com.sdgis.gis.geometry.Envelope;
import cn.com.sdgis.gis.geometry.Geometry;
import cn.com.sdgis.gis.geometry.Point;
import cn.com.sdgis.gis.geometry.Polyline;


/**
 * TODO describe this class please.
 *
 * @author Zhang Xuehu
 * @version
 * @see
 *
 * Change log:
 */
public class FeatureClass {


    private GeometryType geoType;
    
    private Envelope envelope;

    private long featureCount;
    
    private RandomAccessFile shpFile;
    
    private RandomAccessFile dbfFile;
    
    private RandomAccessFile shxFile;
    
    /**
     * 
     *
     */
    private FeatureClass(){
        
    }
    
    /**
     * @return Returns the geoType.
     */
    public GeometryType getGeoType() {
        return geoType;
    }

    /**
     * @param geoType The geoType to set.
     */
    public void setGeoType(GeometryType geoType) {
        this.geoType = geoType;
    }

    /**
     * @return Returns the envelope.
     */
    public Envelope getEnvelope() {
        return envelope;
    }

    /**
     * @param envelope The envelope to set.
     */
    public void setEnvelope(Envelope envelope) {
        this.envelope = envelope;
    }

    /**
     * @return Returns the featureCount.
     */
    public long getFeatureCount() {
        return featureCount;
    }

    /**
     * @param featureCount The featureCount to set.
     */
    public void setFeatureCount(long featureCount) {
        this.featureCount = featureCount;
    }

    /**
     * @return Returns the dbfFile.
     */
    public RandomAccessFile getDbfFile() {
        return dbfFile;
    }

    /**
     * @param dbfFile The dbfFile to set.
     */
    public void setDbfFile(RandomAccessFile dbfFile) {
        this.dbfFile = dbfFile;
    }

    /**
     * @return Returns the shpFile.
     */
    public RandomAccessFile getShpFile() {
        return shpFile;
    }

    /**
     * @param shpFile The shpFile to set.
     */
    public void setShpFile(RandomAccessFile shpFile) {
        this.shpFile = shpFile;
    }

    /**
     * @return Returns the shxFile.
     */
    public RandomAccessFile getShxFile() {
        return shxFile;
    }

    /**
     * @param shxFile The shxFile to set.
     */
    public void setShxFile(RandomAccessFile shxFile) {
        this.shxFile = shxFile;
    }

    /**
     * 
     * @param index Feature的索引号
     * @return Feature
     * @throws BusinessException 
     */
    public Feature get(int index) {
        
        Feature feature = new Feature();
        feature.setFields(geteField(dbfFile, index));
        feature.setShape(getGeometry(shpFile, shxFile, geoType, index));
        
        return feature;
    }
    
    
    
    /**
     * @return string
     */
    public String toString(){
        
        if(geoType == null && envelope == null)
            return "错误的要素类";
        
        return "要素类型为: [" + this.geoType + "] 地理范围为:{[top-" +
                this.envelope.getMaxY() + "] [bottom-" + 
                this.envelope.getMinY() + "] [left-" + 
                this.envelope.getMinX() + "] [right-" + 
                this.envelope.getMaxX() + "]" ;
    }
    
    /**
     * 
     * @param feature 要素名称(包含绝对路径)
     * @return 要素类
     * @throws FileNotFoundException  要素名称无效
     * @throws BusinessException 
     */
    public static FeatureClass getFeatureClassFromFile(String feature) 
                            throws FileNotFoundException{
        
        FeatureClass fClass = new FeatureClass();
        
        //分别打开shp,dbf,shx文件
        RandomAccessFile shpFile = new RandomAccessFile(
                new File(feature + ".shp"), "r");
        RandomAccessFile dbfFile = new RandomAccessFile(
                new File(feature + ".dbf"), "r");
        RandomAccessFile shxFile = new RandomAccessFile(
                new File(feature + ".shx"), "r");
        if(shpFile != null && dbfFile != null && shxFile != null){
            
            fClass = new FeatureClass();
            fClass.setShpFile(shpFile);
            fClass.setDbfFile(dbfFile);
            fClass.setShxFile(shxFile);
            fClass.setGeoType(getGeometyType(shpFile));
            fClass.setEnvelope(getEnvelope(shpFile));
            fClass.setFeatureCount(getFeatureCount(dbfFile));
        }

        return fClass;
    }
    
    /*
     * 从shp文件中读出要素类型
     */
    private static GeometryType getGeometyType(RandomAccessFile shpFile) {
        
        try {
  
            shpFile.seek(32);
            byte[] buffer = new byte[4];
            shpFile.read(buffer);
          
            ByteBuffer bf = ByteBuffer.wrap(converse(buffer, 4));
            
            switch(bf.getInt()){
                case 1:
                    return GeometryType.POINT;
                case 3:
                    return GeometryType.POLYLINE;
                case 8:
                    return GeometryType.MULTIPOINT;
                default:
                    return null;
            }

        } catch (FileNotFoundException e) {
           e.printStackTrace();
           return null;
        } catch (IOException e) {
        	  e.printStackTrace();
        	  return null;
        }
    }
    
    /*
     * 从shp文件中读取数据集的坐标范围
     */
    private static Envelope getEnvelope(RandomAccessFile shpFile) {
        
        Envelope enve = new Envelope();
        
        try {
            
            //envelope在shape文件的36-68字节间
            shpFile.seek(36);
            byte[] buffer = new byte[8];
            shpFile.read(buffer);
            ByteBuffer bf = ByteBuffer.wrap(converse(buffer, 8));
            enve.setMinX(bf.getDouble());
            shpFile.read(buffer);
            bf = ByteBuffer.wrap(converse(buffer, 8));
            enve.setMinY(bf.getDouble());
            shpFile.read(buffer);
            bf = ByteBuffer.wrap(converse(buffer, 8));
            enve.setMaxX(bf.getDouble());
            shpFile.read(buffer);
            bf = ByteBuffer.wrap(converse(buffer, 8));
            enve.setMaxY(bf.getDouble());
          
        } catch (FileNotFoundException e) {
        	 e.printStackTrace();
             return null;
        } catch (IOException e) {
        	 e.printStackTrace();
             return null;
        }
        
        return enve;
    }
    
    /*
     * 获取记录数目
     */
    private static int getFeatureCount(RandomAccessFile dbfFile) 
                      {
        
        int recordCount = -1;
        
        try {
            dbfFile.seek(4);
            int[] tInt = new int[1];
            if(readInt(dbfFile, 4, tInt))
                recordCount = tInt[0];
            else
                return -1;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -