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

📄 dbasefileheader.java

📁 shape file read and write
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 *    GeoTools - OpenSource mapping toolkit
 *    http://geotools.org
 *    (C) 2002-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; either
 *    version 2.1 of the License, or (at your option) any later version.
 *
 *    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.
 *
 *    This file is based on an origional contained in the GISToolkit project:
 *    http://gistoolkit.sourceforge.net/
 */
package org.geotools.data.shapefile.dbf;

import java.io.EOFException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.geotools.resources.NIOUtilities;

/**
 * Class to represent the header of a Dbase III file. Creation date: (5/15/2001
 * 5:15:30 PM)
 * 
 * @source $URL:
 *         http://svn.geotools.org/geotools/trunk/gt/modules/plugin/shapefile/src/main/java/org/geotools/data/shapefile/dbf/DbaseFileHeader.java $
 */
public class DbaseFileHeader {
    // Constant for the size of a record
    private static final int FILE_DESCRIPTOR_SIZE = 32;

    // type of the file, must be 03h
    private static final byte MAGIC = 0x03;

    private static final int MINIMUM_HEADER = 33;

    // Date the file was last updated.
    private Date date = new Date();

    private int recordCnt = 0;

    private int fieldCnt = 0;

    // set this to a default length of 1, which is enough for one "space"
    // character which signifies an empty record
    private int recordLength = 1;

    // set this to a flagged value so if no fields are added before the write,
    // we know to adjust the headerLength to MINIMUM_HEADER
    private int headerLength = -1;

    private int largestFieldSize = 0;

    private Logger logger = org.geotools.util.logging.Logging
            .getLogger("org.geotools.data.shapefile");

    /**
     * Class for holding the information assicated with a record.
     */
    class DbaseField {

        // Field Name
        String fieldName;

        // Field Type (C N L D or M)
        char fieldType;

        // Field Data Address offset from the start of the record.
        int fieldDataAddress;

        // Length of the data in bytes
        int fieldLength;

        // Field decimal count in Binary, indicating where the decimal is
        int decimalCount;

    }

    // collection of header records.
    // lets start out with a zero-length array, just in case
    private DbaseField[] fields = new DbaseField[0];

    private void read(ByteBuffer buffer, ReadableByteChannel channel)
            throws IOException {
        while (buffer.remaining() > 0) {
            if (channel.read(buffer) == -1) {
                throw new EOFException("Premature end of file");
            }
        }
    }

    /**
     * Determine the most appropriate Java Class for representing the data in
     * the field.
     * 
     * <PRE>
     * All packages are java.lang unless otherwise specified.
     * C (Character) -&gt; String
     * N (Numeric)   -&gt; Integer or Double (depends on field's decimal count)
     * F (Floating)  -&gt; Double
     * L (Logical)   -&gt; Boolean
     * D (Date)      -&gt; java.util.Date
     * Unknown       -&gt; String
     * </PRE>
     * 
     * @param i
     *                The index of the field, from 0 to
     *                <CODE>getNumFields() - 1</CODE> .
     * @return A Class which closely represents the dbase field type.
     */
    public Class getFieldClass(int i) {
        Class typeClass = null;

        switch (fields[i].fieldType) {
        case 'C':
            typeClass = String.class;
            break;

        case 'N':
            if (fields[i].decimalCount == 0) {
                if (fields[i].fieldLength < 10) {
                    typeClass = Integer.class;
                } else {
                    typeClass = Long.class;
                }
            } else {
                typeClass = Double.class;
            }
            break;

        case 'F':
            typeClass = Double.class;
            break;

        case 'L':
            typeClass = Boolean.class;
            break;

        case 'D':
            typeClass = Date.class;
            break;

        default:
            typeClass = String.class;
            break;
        }

        return typeClass;
    }

    /**
     * Add a column to this DbaseFileHeader. The type is one of (C N L or D)
     * character, number, logical(true/false), or date. The Field length is the
     * total length in bytes reserved for this column. The decimal count only
     * applies to numbers(N), and floating point values (F), and refers to the
     * number of characters to reserve after the decimal point. <B>Don't expect
     * miracles from this...</B>
     * 
     * <PRE>
     * Field Type MaxLength
     * ---------- ---------
     * C          254
     * D          8
     * F          20
     * N          18
     * </PRE>
     * 
     * @param inFieldName
     *                The name of the new field, must be less than 10 characters
     *                or it gets truncated.
     * @param inFieldType
     *                A character representing the dBase field, ( see above ).
     *                Case insensitive.
     * @param inFieldLength
     *                The length of the field, in bytes ( see above )
     * @param inDecimalCount
     *                For numeric fields, the number of decimal places to track.
     * @throws DbaseFileException
     *                 If the type is not recognized.
     */
    public void addColumn(String inFieldName, char inFieldType,
            int inFieldLength, int inDecimalCount) throws DbaseFileException {
        if (inFieldLength <= 0) {
            throw new DbaseFileException("field length <= 0");
        }
        if (fields == null) {
            fields = new DbaseField[0];
        }
        int tempLength = 1; // the length is used for the offset, and there is a
                            // * for deleted as the first byte
        DbaseField[] tempFieldDescriptors = new DbaseField[fields.length + 1];
        for (int i = 0; i < fields.length; i++) {
            fields[i].fieldDataAddress = tempLength;
            tempLength = tempLength + fields[i].fieldLength;
            tempFieldDescriptors[i] = fields[i];
        }
        tempFieldDescriptors[fields.length] = new DbaseField();
        tempFieldDescriptors[fields.length].fieldLength = inFieldLength;
        tempFieldDescriptors[fields.length].decimalCount = inDecimalCount;
        tempFieldDescriptors[fields.length].fieldDataAddress = tempLength;

        // set the field name
        String tempFieldName = inFieldName;
        if (tempFieldName == null) {
            tempFieldName = "NoName";
        }
        // Fix for GEOT-42, ArcExplorer will not handle field names > 10 chars
        // Sorry folks.
        if (tempFieldName.length() > 10) {
            tempFieldName = tempFieldName.substring(0, 10);
            if (logger.isLoggable(Level.WARNING)) {
                logger.warning("FieldName " + inFieldName
                        + " is longer than 10 characters, truncating to "
                        + tempFieldName);
            }
        }
        tempFieldDescriptors[fields.length].fieldName = tempFieldName;

        // the field type
        if ((inFieldType == 'C') || (inFieldType == 'c')) {
            tempFieldDescriptors[fields.length].fieldType = 'C';
            if (inFieldLength > 254) {
                if (logger.isLoggable(Level.FINE)) {
                    logger
                            .fine("Field Length for "
                                    + inFieldName
                                    + " set to "
                                    + inFieldLength
                                    + " Which is longer than 254, not consistent with dbase III");
                }
            }
        } else if ((inFieldType == 'S') || (inFieldType == 's')) {
            tempFieldDescriptors[fields.length].fieldType = 'C';
            if (logger.isLoggable(Level.WARNING)) {
                logger
                        .warning("Field type for "
                                + inFieldName
                                + " set to S which is flat out wrong people!, I am setting this to C, in the hopes you meant character.");
            }
            if (inFieldLength > 254) {
                if (logger.isLoggable(Level.FINE)) {
                    logger
                            .fine("Field Length for "
                                    + inFieldName
                                    + " set to "
                                    + inFieldLength
                                    + " Which is longer than 254, not consistent with dbase III");
                }
            }
            tempFieldDescriptors[fields.length].fieldLength = 8;
        } else if ((inFieldType == 'D') || (inFieldType == 'd')) {
            tempFieldDescriptors[fields.length].fieldType = 'D';
            if (inFieldLength != 8) {
                if (logger.isLoggable(Level.FINE)) {
                    logger.fine("Field Length for " + inFieldName + " set to "
                            + inFieldLength + " Setting to 8 digets YYYYMMDD");
                }
            }
            tempFieldDescriptors[fields.length].fieldLength = 8;
        } else if ((inFieldType == 'F') || (inFieldType == 'f')) {
            tempFieldDescriptors[fields.length].fieldType = 'F';
            if (inFieldLength > 20) {
                if (logger.isLoggable(Level.FINE)) {
                    logger
                            .fine("Field Length for "
                                    + inFieldName
                                    + " set to "
                                    + inFieldLength
                                    + " Preserving length, but should be set to Max of 20 not valid for dbase IV, and UP specification, not present in dbaseIII.");
                }
            }
        } else if ((inFieldType == 'N') || (inFieldType == 'n')) {
            tempFieldDescriptors[fields.length].fieldType = 'N';
            if (inFieldLength > 18) {
                if (logger.isLoggable(Level.FINE)) {
                    logger
                            .fine("Field Length for "
                                    + inFieldName
                                    + " set to "
                                    + inFieldLength
                                    + " Preserving length, but should be set to Max of 18 for dbase III specification.");
                }
            }
            if (inDecimalCount < 0) {
                if (logger.isLoggable(Level.FINE)) {
                    logger.fine("Field Decimal Position for " + inFieldName
                            + " set to " + inDecimalCount
                            + " Setting to 0 no decimal data will be saved.");
                }
                tempFieldDescriptors[fields.length].decimalCount = 0;
            }
            if (inDecimalCount > inFieldLength - 1) {
                if (logger.isLoggable(Level.WARNING)) {
                    logger.warning("Field Decimal Position for " + inFieldName
                            + " set to " + inDecimalCount + " Setting to "
                            + (inFieldLength - 1)
                            + " no non decimal data will be saved.");
                }
                tempFieldDescriptors[fields.length].decimalCount = inFieldLength - 1;
            }
        } else if ((inFieldType == 'L') || (inFieldType == 'l')) {
            tempFieldDescriptors[fields.length].fieldType = 'L';
            if (inFieldLength != 1) {
                if (logger.isLoggable(Level.FINE)) {
                    logger.fine("Field Length for " + inFieldName + " set to "
                            + inFieldLength
                            + " Setting to length of 1 for logical fields.");
                }
            }
            tempFieldDescriptors[fields.length].fieldLength = 1;
        } else {
            throw new DbaseFileException("Undefined field type " + inFieldType
                    + " For column " + inFieldName);
        }
        // the length of a record
        tempLength = tempLength
                + tempFieldDescriptors[fields.length].fieldLength;

        // set the new fields.
        fields = tempFieldDescriptors;
        fieldCnt = fields.length;
        headerLength = MINIMUM_HEADER + 32 * fields.length;
        recordLength = tempLength;
    }

    /**
     * Remove a column from this DbaseFileHeader.
     * 
     * @todo This is really ugly, don't know who wrote it, but it needs fixin...
     * @param inFieldName
     *                The name of the field, will ignore case and trim.
     * @return index of the removed column, -1 if no found
     */
    public int removeColumn(String inFieldName) {

        int retCol = -1;
        int tempLength = 1;
        DbaseField[] tempFieldDescriptors = new DbaseField[fields.length - 1];
        for (int i = 0, j = 0; i < fields.length; i++) {
            if (!inFieldName.equalsIgnoreCase(fields[i].fieldName.trim())) {
                // if this is the last field and we still haven't found the
                // named field
                if (i == j && i == fields.length - 1) {
                    System.err.println("Could not find a field named '"
                            + inFieldName + "' for removal");
                    return retCol;
                }
                tempFieldDescriptors[j] = fields[i];
                tempFieldDescriptors[j].fieldDataAddress = tempLength;

⌨️ 快捷键说明

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