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

📄 multilinehandler.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.
 */
package org.geotools.data.shapefile.shp;

import java.nio.ByteBuffer;

import org.geotools.geometry.jts.coordinatesequence.CSBuilder;
import org.geotools.geometry.jts.coordinatesequence.CSBuilderFactory;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.CoordinateSequence;
import com.vividsolutions.jts.geom.Envelope;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.LineString;
import com.vividsolutions.jts.geom.MultiLineString;

/*
 * $Id: MultiLineHandler.java 30011 2008-04-21 14:47:24Z groldan $ @author
 * aaime @author Ian Schneider
 */
/**
 * The default JTS handler for shapefile. Currently uses the default JTS
 * GeometryFactory, since it doesn't seem to matter.
 * 
 * @source $URL:
 *         http://svn.geotools.org/geotools/trunk/gt/modules/plugin/shapefile/src/main/java/org/geotools/data/shapefile/shp/MultiLineHandler.java $
 */
public class MultiLineHandler implements ShapeHandler {
    final ShapeType shapeType;

    GeometryFactory geometryFactory = new GeometryFactory();

    CSBuilder builder = CSBuilderFactory.getDefaultBuilder();

    double[] x;

    double[] y;

    double[] z;

    /** Create a MultiLineHandler for ShapeType.ARC */
    public MultiLineHandler() {
        shapeType = ShapeType.ARC;
    }

    /**
     * Create a MultiLineHandler for one of: <br>
     * ShapeType.ARC,ShapeType.ARCM,ShapeType.ARCZ
     * 
     * @param type
     *                The ShapeType to use.
     * @throws ShapefileException
     *                 If the ShapeType is not correct (see constructor).
     */
    public MultiLineHandler(ShapeType type) throws ShapefileException {
        if ((type != ShapeType.ARC) && (type != ShapeType.ARCM)
                && (type != ShapeType.ARCZ)) {
            throw new ShapefileException(
                    "MultiLineHandler constructor - expected type to be 3,13 or 23");
        }

        shapeType = type;
    }

    /**
     * Get the type of shape stored
     * (ShapeType.ARC,ShapeType.ARCM,ShapeType.ARCZ)
     */
    public ShapeType getShapeType() {
        return shapeType;
    }

    /** */
    public int getLength(Object geometry) {
        MultiLineString multi = (MultiLineString) geometry;

        int numlines;
        int numpoints;
        int length;

        numlines = multi.getNumGeometries();
        numpoints = multi.getNumPoints();

        if (shapeType == ShapeType.ARC) {
            length = 44 + (4 * numlines) + (numpoints * 16);
        } else if (shapeType == ShapeType.ARCM) {
            length = 44 + (4 * numlines) + (numpoints * 16) + 8 + 8
                    + (8 * numpoints);
        } else if (shapeType == ShapeType.ARCZ) {
            length = 44 + (4 * numlines) + (numpoints * 16) + 8 + 8
                    + (8 * numpoints) + 8 + 8 + (8 * numpoints);
        } else {
            throw new IllegalStateException("Expected ShapeType of Arc, got "
                    + shapeType);
        }

        return length;
    }

    private Object createNull() {
        return geometryFactory.createMultiLineString((LineString[]) null);
    }

    public Object readOld(ByteBuffer buffer, ShapeType type) {
        if (type == ShapeType.NULL) {
            return createNull();
        }
        int dimensions = shapeType == ShapeType.ARCZ ? 3 : 2;
        // read bounding box (not needed)
        buffer.position(buffer.position() + 4 * 8);

        int numParts = buffer.getInt();
        int numPoints = buffer.getInt(); // total number of points

        int[] partOffsets = new int[numParts];

        // points = new Coordinate[numPoints];
        for (int i = 0; i < numParts; i++) {
            partOffsets[i] = buffer.getInt();
        }

        LineString[] lines = new LineString[numParts];
        // Coordinate[] coords = new Coordinate[numPoints];
        prepareCoordinateArrays(numPoints);

        for (int t = 0; t < numPoints; t++) {
            x[t] = buffer.getDouble();
            y[t] = buffer.getDouble();
            // coords[t] = new Coordinate(buffer.getDouble(),
            // buffer.getDouble());
        }

        if (dimensions == 3) {
            // z min, max
            buffer.position(buffer.position() + 2 * 8);
            for (int t = 0; t < numPoints; t++) {
                // coords[t].z = buffer.getDouble(); //z value
                z[t] = buffer.getDouble();
            }
        }

        int offset = 0;
        int start = 0;
        int finish;
        int length;

        for (int part = 0; part < numParts; part++) {
            if (part == (numParts - 1)) {
                finish = numPoints;
            } else {
                finish = partOffsets[part + 1];
            }

            length = finish - start;
            start = finish;

            // Lines may be either composed of 0, 2 or more coordinates, but not
            // just one.
            // since some tools produce such files, we work around it by
            // duplicating the single
            // coordinate
            // Coordinate[] points = null;
            if (length == 1)
                // points = new Coordinate[2];
                builder.start(2, dimensions);
            else
                // points = new Coordinate[length];
                builder.start(length, dimensions);

            for (int i = 0; i < length; i++) {
                builder.setOrdinate(x[offset], 0, i);
                builder.setOrdinate(y[offset], 1, i);
                if (dimensions == 3)
                    builder.setOrdinate(z[offset], 2, i);
                // points[i] = coords[offset];
                offset++;
            }

            if (length == 1) {
                // points[1] = (Coordinate) points[0].clone();
                builder.setOrdinate(x[offset - 1], 0, 1);
                builder.setOrdinate(y[offset - 1], 1, 1);
                if (dimensions == 3)
                    builder.setOrdinate(z[offset - 1], 2, 1);
            }

            lines[part] = geometryFactory.createLineString(builder.end());
        }

        return geometryFactory.createMultiLineString(lines);
    }

    public Object read(ByteBuffer buffer, ShapeType type) {
        if (type == ShapeType.NULL) {
            return createNull();
        }
        int dimensions = (shapeType == ShapeType.ARCZ) ? 3 : 2;
        // read bounding box (not needed)
        buffer.position(buffer.position() + 4 * 8);

        int numParts = buffer.getInt();
        int numPoints = buffer.getInt(); // total number of points

        int[] partOffsets = new int[numParts];

        // points = new Coordinate[numPoints];
        for (int i = 0; i < numParts; i++) {
            partOffsets[i] = buffer.getInt();
        }
        // read the first two coordinates and start building the coordinate
        // sequences

⌨️ 快捷键说明

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