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

📄 filesystempagestore.java

📁 shape file read and write
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 *    GeoTools - OpenSource mapping toolkit
 *    http://geotools.org
 *    (C) 2003-2006, GeoTools Project Managment Committee (PMC)
 *    (C) 2002, Centre for Computational Geography
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation;
 *    version 2.1 of the License.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 */
package org.geotools.index.rtree.cachefs;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.geotools.index.DataDefinition;
import org.geotools.index.TreeException;
import org.geotools.index.DataDefinition.Field;
import org.geotools.index.rtree.Entry;
import org.geotools.index.rtree.Node;
import org.geotools.index.rtree.PageStore;

import com.vividsolutions.jts.geom.Envelope;

/**
 * DOCUMENT ME!
 * 
 * @author Tommaso Nolli
 * @source $URL:
 *         http://svn.geotools.org/geotools/trunk/gt/modules/plugin/shapefile/src/main/java/org/geotools/index/rtree/cachefs/FileSystemPageStore.java $
 */
public class FileSystemPageStore extends PageStore {
    private static final Logger LOGGER = org.geotools.util.logging.Logging
            .getLogger("org.geotools.index.rtree");
    protected static final byte B_SHORT = (byte) 1;
    protected static final byte B_INTEGER = (byte) 2;
    protected static final byte B_LONG = (byte) 3;
    protected static final byte B_FLOAT = (byte) 4;
    protected static final byte B_DOUBLE = (byte) 5;
    protected static final byte B_STRING = (byte) 6;
    private static final int DEF_MAX = 50;
    private static final int DEF_MIN = 25;
    private static final short DEF_SPLIT = SPLIT_QUADRATIC;
    private static final int FILE_VERSION = 19730103;
    private Parameters params = new Parameters();
    private RandomAccessFile raFile;
    private FileChannel channel;
    private ByteBuffer header;
    private long rootOffset;

    /**
     * Loads an index from the specified <code>File</code>
     * 
     * @param file
     *                The file that stores the index
     * 
     * @throws TreeException
     */
    public FileSystemPageStore(File file) throws TreeException {
        super();

        if (file.isDirectory()) {
            throw new TreeException("Cannot use a directory as index!");
        }

        this.params.setMaxNodeEntries(DEF_MAX);
        this.params.setMinNodeEntries(DEF_MIN);
        this.params.setSplitAlg(DEF_SPLIT);

        try {
            this.init(file);
        } catch (IOException e) {
            throw new TreeException(e);
        }
    }

    /**
     * Create and index with default values, if the file exists then a
     * <code>TreeException</code> will be thrown.
     * 
     * @param file
     * @param def
     * 
     * @throws TreeException
     */
    public FileSystemPageStore(File file, DataDefinition def)
            throws TreeException {
        this(file, def, DEF_MAX, DEF_MIN, DEF_SPLIT, -1);
    }

    /**
     * Create and index with default values, if the file exists then a
     * <code>TreeException</code> will be thrown.
     * 
     * @param file
     * @param def
     * @param cacheSize
     *                the size of the cache
     * 
     * @throws TreeException
     */
    public FileSystemPageStore(File file, DataDefinition def, int cacheSize)
            throws TreeException {
        this(file, def, DEF_MAX, DEF_MIN, DEF_SPLIT, cacheSize);
    }

    /**
     * Create and index with the specified values, if the file exists then a
     * <code>TreeException</code> will be thrown.
     * 
     * @param file
     *                The file to store the index
     * @param def
     *                DOCUMENT ME!
     * @param maxNodeEntries
     * @param minNodeEntries
     * @param splitAlg
     * 
     * @throws TreeException
     */
    public FileSystemPageStore(File file, DataDefinition def,
            int maxNodeEntries, int minNodeEntries, short splitAlg)
            throws TreeException {
        this(file, def, maxNodeEntries, minNodeEntries, splitAlg, -1);
    }

    /**
     * Create and index with the specified values, if the file exists then a
     * <code>TreeException</code> will be thrown.
     * 
     * @param file
     *                The file to store the index
     * @param def
     *                DOCUMENT ME!
     * @param maxNodeEntries
     * @param minNodeEntries
     * @param splitAlg
     * @param cacheSize
     *                the size of the cache
     * 
     * @throws TreeException
     */
    public FileSystemPageStore(File file, DataDefinition def,
            int maxNodeEntries, int minNodeEntries, short splitAlg,
            int cacheSize) throws TreeException {
        super(def, maxNodeEntries, minNodeEntries, splitAlg);

        if (file.exists() && (file.length() != 0)) {
            throw new TreeException("Cannot set dataDefinition, "
                    + "maxNodesEntries and "
                    + "minNodeEntries to an existing index " + file);
        }

        if (file.isDirectory()) {
            throw new TreeException("Cannot use a directory as index!");
        }

        this.params.setDataDef(def);
        this.params.setMaxNodeEntries(maxNodeEntries);
        this.params.setMinNodeEntries(minNodeEntries);
        this.params.setSplitAlg(splitAlg);

        if (cacheSize > -1) {
            this.params.setNodeCacheSize(cacheSize);
        }

        try {
            this.init(file);
        } catch (IOException e) {
            throw new TreeException(e);
        }
    }

    /**
     * DOCUMENT ME!
     * 
     * @param file
     * 
     * @throws IOException
     * @throws TreeException
     */
    private void init(File file) throws IOException, TreeException {
        this.raFile = new RandomAccessFile(file, "rw");
        this.channel = raFile.getChannel();

        this.params.setChannel(this.channel);

        try {
            if (file.length() > 0) {
                this.loadIndex();
            } else {
                this.prepareIndex();
            }
        } catch (TreeException e) {
            // close the channel, or we won't be able to delete the grx file
            channel.close();
            throw new TreeException(e);
        }
    }

    /**
     * DOCUMENT ME!
     * 
     * @throws IOException
     * @throws TreeException
     */
    private void loadIndex() throws IOException, TreeException {
        Charset charset = Charset.forName("US-ASCII");

        ByteBuffer buf = ByteBuffer.allocate(8);
        this.channel.read(buf);
        buf.position(0);

        if (buf.getInt() != FILE_VERSION) {
            throw new TreeException("Wrong file version, shoud be "
                    + FILE_VERSION);
        }

        // Get the header size
        int headerSize = buf.getInt();
        buf.position(0);

        this.header = ByteBuffer.allocate(headerSize);
        this.header.put(buf);
        this.header.mark();
        this.channel.read(this.header);
        this.header.reset();

        this.params.setMaxNodeEntries(this.header.getInt());
        this.params.setMinNodeEntries(this.header.getInt());
        this.params.setSplitAlg(this.header.getShort());

        // Get the charset used for string encoding
        int chLen = this.header.getInt();
        byte[] bytes = new byte[chLen];
        this.header.get(bytes);

        CharBuffer dummy = charset.decode(ByteBuffer.wrap(bytes));
        dummy.position(0);

        String defCharset = dummy.toString();

        DataDefinition def = new DataDefinition(defCharset);
        byte type = 0;
        int remaining = this.header.remaining();

        for (int i = 0; i < (remaining - 8); i += 5) {
            type = this.header.get();

            if (type == B_STRING) {
                def.addField(this.header.getInt());
            } else {

⌨️ 快捷键说明

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