001: /*
002: * Geotools2 - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002, Geotools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: */
017: package org.geotools.data.shapefile;
018:
019: import java.awt.Rectangle;
020: import java.io.IOException;
021: import java.net.MalformedURLException;
022: import java.net.URL;
023: import java.nio.channels.ReadableByteChannel;
024:
025: import org.geotools.data.FIDReader;
026: import org.geotools.data.shapefile.dbf.IndexedDbaseFileReader;
027: import org.geotools.data.shapefile.indexed.IndexedFidReader;
028: import org.geotools.data.shapefile.indexed.IndexedShapefileDataStore;
029: import org.geotools.data.shapefile.indexed.RecordNumberTracker;
030: import org.geotools.data.shapefile.shp.ShapeType;
031: import org.geotools.data.shapefile.shp.ShapefileReader;
032: import org.geotools.renderer.shape.shapehandler.simple.MultiLineHandler;
033: import org.geotools.renderer.shape.shapehandler.simple.MultiPointHandler;
034: import org.geotools.renderer.shape.shapehandler.simple.PointHandler;
035: import org.geotools.renderer.shape.shapehandler.simple.PolygonHandler;
036: import org.opengis.referencing.operation.MathTransform;
037: import org.opengis.referencing.operation.TransformException;
038:
039: import com.vividsolutions.jts.geom.Envelope;
040:
041: /**
042: * Allows access the the ShapefileReaders.
043: *
044: * @author jeichar
045: * @since 2.1.x
046: * @source $URL:
047: * http://svn.geotools.org/geotools/branches/2.2.x/ext/shaperenderer/src/org/geotools/data/shapefile/ShapefileRendererUtil.java $
048: */
049: public class ShapefileRendererUtil {
050: /**
051: * gets a shapefile reader with the custom shaperenderer shape handler.
052: *
053: * @param ds the datastore used to obtain the reader
054: * @param bbox the area, in data coordinates, of the viewed data.
055: * @param mt The transform used to transform from data->world coordinates->screen coordinates
056: * @param hasOpacity the transform from screen coordinates to world coordinates. Used for
057: * decimation.
058: * @return
059: * @throws IOException
060: * @throws TransformException
061: */
062: public static ShapefileReader getShpReader(ShapefileDataStore ds,
063: Envelope bbox, Rectangle screenSize, MathTransform mt,
064: boolean hasOpacity, boolean returnJTS) throws IOException,
065: TransformException {
066: ShapefileReader reader = ds.openShapeReader();
067: ShapeType type = reader.getHeader().getShapeType();
068:
069: if ((type == ShapeType.ARC) || (type == ShapeType.ARCM)
070: || (type == ShapeType.ARCZ)) {
071: if (returnJTS)
072: reader
073: .setHandler(new org.geotools.renderer.shape.shapehandler.jts.MultiLineHandler(
074: type, bbox, mt, hasOpacity, screenSize));
075: else
076: reader.setHandler(new MultiLineHandler(type, bbox, mt,
077: hasOpacity, screenSize));
078: }
079:
080: if ((type == ShapeType.POLYGON) || (type == ShapeType.POLYGONM)
081: || (type == ShapeType.POLYGONZ)) {
082: if (returnJTS)
083: reader
084: .setHandler(new org.geotools.renderer.shape.shapehandler.jts.PolygonHandler(
085: type, bbox, mt, hasOpacity));
086: else
087: reader.setHandler(new PolygonHandler(type, bbox, mt,
088: hasOpacity));
089: }
090:
091: if ((type == ShapeType.POINT) || (type == ShapeType.POINTM)
092: || (type == ShapeType.POINTZ)) {
093: if (returnJTS)
094: reader
095: .setHandler(new org.geotools.renderer.shape.shapehandler.jts.PointHandler(
096: type, bbox, screenSize, mt, hasOpacity));
097: else
098: reader.setHandler(new PointHandler(type, bbox,
099: screenSize, mt, hasOpacity));
100: }
101:
102: if ((type == ShapeType.MULTIPOINT)
103: || (type == ShapeType.MULTIPOINTM)
104: || (type == ShapeType.MULTIPOINTZ)) {
105: if (returnJTS)
106: reader
107: .setHandler(new org.geotools.renderer.shape.shapehandler.jts.MultiPointHandler(
108: type, bbox, screenSize, mt, hasOpacity));
109: else
110: reader.setHandler(new MultiPointHandler(type, bbox,
111: screenSize, mt, hasOpacity));
112: }
113:
114: return reader;
115: }
116:
117: public static IndexedDbaseFileReader getDBFReader(
118: ShapefileDataStore ds) throws IOException {
119: return new IndexedDbaseFileReader(ds.getReadChannel(ds.dbfURL),
120: ds.useMemoryMappedBuffer, ds.getStringCharset());
121: }
122:
123: public static ReadableByteChannel getShpReadChannel(
124: ShapefileDataStore ds) throws IOException {
125: return ds.getReadChannel(ds.shpURL);
126: }
127:
128: public static URL getshpURL(ShapefileDataStore ds) {
129: return ds.shpURL;
130: }
131:
132: public static FIDReader getFidReader(ShapefileDataStore datastore,
133: RecordNumberTracker tracker) throws MalformedURLException {
134: if (datastore instanceof IndexedShapefileDataStore) {
135: IndexedShapefileDataStore ids = (IndexedShapefileDataStore) datastore;
136: if (ids.fixURL == null)
137: return createBasicFidReader(datastore, tracker);
138: try {
139: return new IndexedFidReader(datastore
140: .getCurrentTypeName(), tracker, datastore
141: .getReadChannel(ids.fixURL));
142: } catch (Exception e) {
143: return createBasicFidReader(datastore, tracker);
144: }
145: } else {
146: return createBasicFidReader(datastore, tracker);
147: }
148: }
149:
150: private static FIDReader createBasicFidReader(
151: ShapefileDataStore datastore,
152: final RecordNumberTracker tracker) {
153: final String typename = datastore.getCurrentTypeName() + ".";
154: return new FIDReader() {
155: int i = 0;
156: boolean closed = false;
157:
158: public void close() throws IOException {
159: closed = true;
160: }
161:
162: public boolean hasNext() throws IOException {
163: if (closed)
164: return false;
165: return true;
166: }
167:
168: public String next() throws IOException {
169: if (closed)
170: throw new IllegalStateException("Reader is closed"); //$NON-NLS-1$
171: i++;
172: return typename + tracker.getRecordNumber();
173: }
174:
175: };
176: }
177: }
|