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

📄 shapefile.java

📁 geotools的源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    public double[] getBounds(){
        return mainHeader.getBounds();
    }
    
    /**
     * Gets the type of shape stored in this shapefile.
     * @return An int indicating the type
     * @see #getShapeTypeDescription()
     * @see #getShapeTypeDescription(int type)
     */
    public int getShapeType(){
        return mainHeader.getShapeType();
    }  
    
    /**
     * Returns a string for the shape type of index.
     * @param index An int coresponding to the shape type to be described
     * @return A string descibing the shape type
     */
    public static String getShapeTypeDescription(int index){
        switch(index){
            case(NULL):return ("Null");
            case(POINT):return ("Points");
            case(ARC):return ("Arcs");
            case(ARC_M):return ("ArcsM");
            case(POLYGON):return ("Polygons");
            case(MULTIPOINT):return ("Multipoints");
            default:return ("Undefined");
        }
    }
    
    /**
     * Returns a description of the shape type stored in this shape file.
     * @return String containing description
     */
    public String getShapeTypeDescription(){
        return getShapeTypeDescription(mainHeader.getShapeType());
    }
    
   public synchronized void readIndex(InputStream is) throws IOException {
        LEDataInputStream file = null;
        try{
            BufferedInputStream in = new BufferedInputStream(is);
            file = new LEDataInputStream(in);
        }catch(Exception e){System.err.println(e);}
         if(DEBUG)System.out.println("Reading index file");
         ShapefileHeader head = new ShapefileHeader(file);
         
				 int pos=0,len=0;
         file.setLittleEndianMode(false);
         //for(int i=0;i<records.size();i++){
            //if(DEBUG)System.out.println("Sf-->Reading index Record");
            //ShapeRecord item = (ShapeRecord)records.elementAt(i);
            //if(DEBUG)System.out.println("Sf-->Offset"+file.readShort());
            //len = item.header.getContentLength();
            //if(DEBUG)System.out.println("Sf-->Length"+file.readShort());
            //if(DEBUG)System.out.println("Sf-->"+pos+" "+len);
            //pos+=len;    
            
          //}
          file.close();
    }
}




class ShapefileHeader implements Serializable{
	private final static boolean DEBUG=false;
	private int fileCode = -1;
	private int fileLength = -1;
	private int indexLength = -1;
	private int version = -1;
	private int shapeType = -1;
	private double[] bounds = new double[4];

	public ShapefileHeader(LEDataInputStream file) throws IOException {
	    file.setLittleEndianMode(false);
		fileCode = file.readInt();
		if(DEBUG)System.out.println("Sfh->Filecode "+fileCode);
		if ( fileCode != Shapefile.SHAPEFILE_ID )
			System.err.println("Sfh->WARNING filecode "+fileCode+" not a match for documented shapefile code "+Shapefile.SHAPEFILE_ID);

			//throw new IOException("File ID in header not that of a Shapefile  (Found "+ fileCode+" : Expected "+Shapefile.SHAPEFILE_ID+")");

	    //file.skipBytes(20);//Skip unused part of header
	    for(int i=0;i<5;i++){
			int tmp = file.readInt();
			if(DEBUG)System.out.println("Sfh->blank "+tmp);
	    }
        fileLength = file.readInt();
        if(DEBUG)System.out.println("Sfh->***************IN FileLength" +fileLength);

        file.setLittleEndianMode(true);
        version=file.readInt();
        shapeType=file.readInt();
        if(DEBUG)System.out.println("Sfh->IN Type" +shapeType);

        //read in the bounding box
        for(int i = 0;i<4;i++){
            bounds[i]=file.readDouble();
        }

        //skip remaining unused bytes
        file.setLittleEndianMode(false);//well they may not be unused forever...
        file.skipBytes(32);
	}

	public ShapefileHeader(int shapeType,double[] bbox,ShapefileShape[] shapes){
	    if(DEBUG)System.out.println("Sfh->ShapefileHeader constructed with type "+shapeType);
	    this.shapeType = shapeType;
	    version = Shapefile.VERSION;
	    fileCode = Shapefile.SHAPEFILE_ID;
	    bounds = bbox;
	    fileLength = 0;
	    for(int i=0;i<shapes.length;i++){
	        fileLength+=shapes[i].getLength();
	        fileLength+=4;//for each header
	    }
	    fileLength+=50;//space used by this, the main header
	    indexLength = 50+(4*shapes.length);
	    if(DEBUG)System.out.println("Sfh->********************OUT fileLength = "+fileLength);
	}

	public void setFileLength(int fileLength){
	    this.fileLength = fileLength;
	}

	public void setBounds(double[] bbox){
	    bounds = bbox;
	}

	public void write(LEDataOutputStream file)throws IOException {
	    int pos = 0;
	    file.setLittleEndianMode(false);
		file.writeInt(fileCode);
		pos+=4;
		for(int i=0;i<5;i++){
	        file.writeInt(0);//Skip unused part of header
	        pos+=4;
	    }
        file.writeInt(fileLength);
        pos+=4;
        file.setLittleEndianMode(true);
        file.writeInt(version);
        pos+=4;
        file.writeInt(shapeType);
        pos+=4;
        //read in the bounding box
        for(int i = 0;i<4;i++){
            pos+=8;
            file.writeDouble(bounds[i]);
        }

        //skip remaining unused bytes
        //file.setLittleEndianMode(false);//well they may not be unused forever...
        for(int i=0;i<4;i++){
	        file.writeDouble(0.0);//Skip unused part of header
	        pos+=8;
	    }
	    if(DEBUG)System.out.println("Sfh->Position "+pos);
	}

	public void writeToIndex(LEDataOutputStream file)throws IOException {
	    int pos = 0;
	    file.setLittleEndianMode(false);
		file.writeInt(fileCode);
		pos+=4;
		for(int i=0;i<5;i++){
	        file.writeInt(0);//Skip unused part of header
	        pos+=4;
	    }
        file.writeInt(indexLength);
        pos+=4;
        file.setLittleEndianMode(true);
        file.writeInt(version);
        pos+=4;
        file.writeInt(shapeType);
        pos+=4;
        //write the bounding box
        for(int i = 0;i<4;i++){
            pos+=8;
            file.writeDouble(bounds[i]);
        }

        //skip remaining unused bytes
        //file.setLittleEndianMode(false);//well they may not be unused forever...
        for(int i=0;i<4;i++){
	        file.writeDouble(0.0);//Skip unused part of header
	        pos+=8;
	    }
	    if(DEBUG)System.out.println("Sfh->Index Position "+pos);
	}

	public int getShapeType(){
	    return shapeType;
	}

	public int getVersion(){
	    return version;
	}

	public double[] getBounds(){
	    return bounds;
	}

	public String toString()  {
		String res = new String("Sf-->type "+fileCode+" size "+fileLength+" version "+ version + " Shape Type "+shapeType);
		return res;
	}
}

class ShapeRecord implements Serializable {
    RecordHeader header;
    ShapefileShape shape;
		int mainindex = -1;

    public ShapeRecord(RecordHeader header,ShapefileShape shape){
        this.header=header;
        this.shape=shape;
    }

    public ShapeRecord(int index,ShapefileShape shape){
        this.header = new RecordHeader(index,shape);
        this.shape = shape;
    }

    public int getShapeType(){
        return shape.getShapeType();
    }

    public ShapefileShape getShape(){
        return shape;
    }
}

class RecordHeader implements Serializable{
    private int recordNumber = -1;
    private int contentLength = -1;
    public RecordHeader(LEDataInputStream file)throws IOException {
        file.setLittleEndianMode(false);
        recordNumber=file.readInt();
        contentLength=file.readInt();
        //System.out.println("In contentLength = "+recordNumber+"   "+contentLength);
    }

    public RecordHeader(int count,ShapefileShape shape){
        recordNumber = count;
        contentLength = shape.getLength();
        //System.out.println("OUT contentLength = "+recordNumber+"   "+contentLength);

    }

    public void write(LEDataOutputStream file)throws IOException {
        file.setLittleEndianMode(false);
        file.writeInt(recordNumber);
        file.writeInt(contentLength);
    }

    public int getRecordNumber(){
        return recordNumber;
    }

    public int getContentLength(){
        return contentLength;
    }
}




⌨️ 快捷键说明

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