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

📄 shapefile.java

📁 openmap java写的开源数字地图程序. 用applet实现,可以像google map 那样放大缩小地图.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    public ESRIRecord getNextRecord() throws IOException {        // Debug.output("getNextRecord: ptr = " +        // raf.getFilePointer());        int result = raf.read(recHdr,                0,                ShapeUtils.SHAPE_FILE_RECORD_HEADER_LENGTH);        if (result == -1) { // EOF            // Debug.output("getNextRecord: EOF");            return null;        }        int contentLength = ShapeUtils.readBEInt(recHdr, 4);        int bytesToRead = contentLength * 2;        int fullRecordSize = bytesToRead + 8;        if (recBuf.length < fullRecordSize) {            if (Debug.debugging("shape")) {                Debug.output("record size: " + fullRecordSize);            }            recBuf = new byte[fullRecordSize];        }        System.arraycopy(recHdr,                0,                recBuf,                0,                ShapeUtils.SHAPE_FILE_RECORD_HEADER_LENGTH);        raf.read(recBuf,                ShapeUtils.SHAPE_FILE_RECORD_HEADER_LENGTH,                bytesToRead);        switch (fileShapeType) {        case ShapeUtils.SHAPE_TYPE_NULL:            throw new IOException("Can't parse NULL shape type");        case ShapeUtils.SHAPE_TYPE_POINT:            return new ESRIPointRecord(recBuf, 0);        case ShapeUtils.SHAPE_TYPE_ARC:            // case ShapeUtils.SHAPE_TYPE_POLYLINE:            return new ESRIPolygonRecord(recBuf, 0);        case ShapeUtils.SHAPE_TYPE_POLYGON:            return new ESRIPolygonRecord(recBuf, 0);        case ShapeUtils.SHAPE_TYPE_MULTIPOINT:            throw new IOException("Multipoint shape not yet implemented");        default:            throw new IOException("Unknown shape type: " + fileShapeType);        }    }    /**     * Adds a record to the end of this file. The record is written to     * the file at the end of the last record.     *      * @param r the record to be added     * @exception IOException if something goes wrong writing to the     *            file     */    public void add(ESRIRecord r) throws IOException {        if (r.getShapeType() == fileShapeType) {            verifyRecordBuffer(r.getBinaryStoreSize());            int nBytes = r.write(recBuf, 0);            // long len = raf.length();            // Debug.output("seek to " + len);            raf.seek(raf.length());            raf.write(recBuf, 0, nBytes);        } else {            Debug.error("ShapeFile.add(): type=" + r.getShapeType()                    + " does not match file type=" + fileShapeType);        }    }    /**     * Closes the shape file and disposes of resources.     *      * @exception IOException if something goes wrong closing the file     */    public void close() throws IOException {        raf.close();        raf = null;    }    /**     * Verifies the contents of a shape file. The header is verified     * for file length, bounding box, and shape type. The records are     * verified for shape type and record number. The file is verified     * for proper termination (EOF at the end of a record).     *      * @param repair NOT CURRENTLY USED - would signal that the file     *        should be repaired if possible     * @param verbose NOT CURRENTLY USED - would cause the verifier to     *        display progress and status     * @exception IOException if something goes wrong reading or     *            writing the file     */    public void verify(boolean repair, boolean verbose) throws IOException {        // Is file length stored in header correctly?        // Is file bounding box correct?        // Does file have a valid shape type?        // Is each record the correct shape type?        // Does each record header have the correct record number?        // Do we reach EOF at the end of a record?        boolean headerChanged = false;        long fLen = raf.length();        if (verbose) {            Debug.output("Checking file length...");            System.out.flush();        }        if (fileLength == fLen) {            if (verbose) {                Debug.output("correct.");            }        } else {            if (verbose) {                Debug.output("incorrect (got " + fileLength + ", should be "                        + fLen + ")");            }            if (repair) {                fileLength = fLen;                writeBEInt(header, 24, ((int) fLen / 2));                headerChanged = true;                if (verbose) {                    Debug.output("...repaired.");                }            }        }        // loop through file to verify:        // record numbers        // Shape types        // bounding box        // correct EOF        raf.seek(100);        ESRIRecord r;        int nRecords = 0;        Vector v = new Vector();        ESRIBoundingBox bounds = new ESRIBoundingBox();        long recStart = raf.getFilePointer();        byte intBuf[] = new byte[4];        while ((r = getNextRecord()) != null) {            long recEnd = raf.getFilePointer();            // Debug.output("verify - start: " + recStart +            // "; end: " + recEnd);            nRecords++;            v.addElement(r);            if (r.getRecordNumber() != nRecords) {                // Debug.output("updating record number for record "                // + nRecords);                writeBEInt(intBuf, 0, nRecords);                raf.seek(recStart);                raf.write(intBuf, 0, 4);                raf.seek(recEnd);            }            if (fileShapeType == SHAPE_TYPE_NULL) {                Debug.output("updating shape type in header.");                fileShapeType = r.getShapeType();                writeLEInt(header, 32, fileShapeType);                headerChanged = true;            }            if (r.getShapeType() != fileShapeType) {                Debug.output("invalid shape type " + r.getShapeType()                        + ", expecting " + fileShapeType);            }            bounds.addBounds(r.getBoundingBox());            recStart = recEnd;        }        if (!fileBounds.equals(bounds)) {            Debug.output("adjusting bounds");            Debug.output("from min: " + fileBounds.min);            Debug.output("to min: " + bounds.min);            Debug.output("from max: " + fileBounds.max);            Debug.output("to max: " + bounds.max);            writeBox(header, 36, bounds);            headerChanged = true;            fileBounds = bounds;        }        if (headerChanged) {            Debug.output("writing changed header");            raf.seek(0);            raf.write(header, 0, 100);        }    }    /**     * Verifies that the record buffer is big enough to hold the given     * number of bytes. If it is not big enough a new buffer is     * created that can hold the given number of bytes.     *      * @param size the number of bytes the buffer needs to hold     */    protected void verifyRecordBuffer(int size) {        if (recBuf.length < size) {            recBuf = new byte[size];        }    }    /**     * The driver for the command line interface. Reads the command     * line arguments and executes appropriate calls.     * <p>     * See the file documentation for usage.     *      * @param args the command line arguments     * @exception IOException if something goes wrong reading or     *            writing the file     */    public static void main(String args[]) throws IOException {        Debug.init(System.getProperties());        int argc = args.length;        if (argc == 1) {            ShapeFile sf = new ShapeFile(args[0]);            Debug.output("Shape file: " + args[0]);            Debug.output("version: " + sf.getFileVersion());            Debug.output("length: " + sf.getFileLength());            Debug.output("bounds:");            Debug.output("\tmin: " + sf.getBoundingBox().min);            Debug.output("\tmax: " + sf.getBoundingBox().max);            int nRecords = 0;            while (sf.getNextRecord() != null) {                nRecords++;            }            Debug.output("records: " + nRecords);        } else if ("-a".equals(args[0])) {            // Append a shape file to another shape file            String destFile = args[1];            String srcFile = args[2];            ShapeFile in = new ShapeFile(srcFile);            ShapeFile out = new ShapeFile(destFile);            if (in.getShapeType() != out.getShapeType()) {                try {                    out.setShapeType(in.getShapeType());                } catch (IllegalArgumentException e) {                    Debug.error("Incompatible shape types.");                    System.exit(1);                }            }            ESRIRecord r;            while ((r = in.getNextRecord()) != null) {                out.add(r);            }            out.verify(true, true);        } else if ("-v".equals(args[0])) {            // Verify a shape file            String shpFile = args[1];            ShapeFile s = new ShapeFile(shpFile);            s.verify(true, true);        } else {            Debug.output("Usage:");            Debug.output("ShapeFile file.shp  -- displays information about file.shp");            Debug.output("ShapeFile -a dest.shp src.shp  -- appends records from src.shp to dest.shp");            Debug.output("ShapeFile -v file.shp  -- verifies file.shp");        }    }}

⌨️ 快捷键说明

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