Source Code Cross Referenced for MainFile.java in  » GIS » deegree » org » deegree » io » shpapi » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » GIS » deegree » org.deegree.io.shpapi 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/trunk/src/org/deegree/io/shpapi/MainFile.java $
002:        /*----------------    FILE HEADER  ------------------------------------------
003:
004:         This file is part of deegree.
005:         Copyright (C) 2001-2008 by:
006:         EXSE, Department of Geography, University of Bonn
007:         http://www.giub.uni-bonn.de/deegree/
008:         lat/lon GmbH
009:         http://www.lat-lon.de
010:
011:         This library is free software; you can redistribute it and/or
012:         modify it under the terms of the GNU Lesser General Public
013:         License as published by the Free Software Foundation; either
014:         version 2.1 of the License, or (at your option) any later version.
015:
016:         This library is distributed in the hope that it will be useful,
017:         but WITHOUT ANY WARRANTY; without even the implied warranty of
018:         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
019:         Lesser General Public License for more details.
020:
021:         You should have received a copy of the GNU Lesser General Public
022:         License along with this library; if not, write to the Free Software
023:         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
024:
025:         Contact:
026:
027:         Andreas Poth
028:         lat/lon GmbH
029:         Aennchenstr. 19
030:         53177 Bonn
031:         Germany
032:         E-Mail: poth@lat-lon.de
033:
034:         Prof. Dr. Klaus Greve
035:         Department of Geography
036:         University of Bonn
037:         Meckenheimer Allee 166
038:         53115 Bonn
039:         Germany
040:         E-Mail: greve@giub.uni-bonn.de
041:
042:        
043:         ---------------------------------------------------------------------------*/
044:
045:        package org.deegree.io.shpapi;
046:
047:        import java.io.File;
048:        import java.io.IOException;
049:        import java.io.RandomAccessFile;
050:
051:        import org.deegree.model.spatialschema.ByteUtils;
052:
053:        /**
054:         * Class representing an ESRI Shape File. 
055:         * <p>
056:         * Uses class ByteUtils modified 
057:         * from the original package com.bbn.openmap.layer.shape
058:         * <br>
059:         * Copyright (C) 1998 BBN Corporation 10 Moulton St. Cambridge, MA 02138
060:         * <br>
061:         * 
062:         * @version 16.08.2000
063:         * @author Andreas Poth
064:         *
065:         */
066:        public class MainFile {
067:
068:            /* 
069:             * A buffer for current record's header. 
070:             */
071:            protected byte[] recHdr = new byte[ShapeConst.SHAPE_FILE_RECORD_HEADER_LENGTH];
072:
073:            /*
074:             * instance variables
075:             */
076:            private FileHeader fh;
077:            private IndexFile shx;
078:
079:            /* 
080:             * file suffixes for shp
081:             */
082:            private static final String _shp = ".shp";
083:
084:            /*
085:             * references to the main file 
086:             */
087:            private RandomAccessFile raf;
088:
089:            /**
090:             * Construct a MainFile from a file name.
091:             */
092:            public MainFile(String url) throws IOException {
093:
094:                /* 
095:                 * creates raf 
096:                 */
097:                raf = new RandomAccessFile(url + _shp, "r");
098:
099:                fh = new FileHeader(raf);
100:
101:                shx = new IndexFile(url);
102:
103:            }
104:
105:            /**
106:             * Construct a MainFile from a file name.
107:             */
108:            public MainFile(String url, String rwflag) throws IOException {
109:
110:                // delet file if it exists
111:                File file = new File(url + _shp);
112:
113:                if (rwflag.indexOf('w') > -1 && file.exists())
114:                    file.delete();
115:                file = null;
116:
117:                /* 
118:                 * creates raf 
119:                 */
120:                raf = new RandomAccessFile(url + _shp, rwflag);
121:
122:                fh = new FileHeader(raf, rwflag.indexOf('w') > -1);
123:
124:                shx = new IndexFile(url, rwflag);
125:
126:            }
127:
128:            public void close() {
129:                try {
130:                    raf.close();
131:                } catch (Exception ex) {
132:                    ex.printStackTrace();
133:                }
134:                try {
135:                    shx.close();
136:                } catch (Exception ex) {
137:                    ex.printStackTrace();
138:                }
139:            }
140:
141:            /**
142:             * method: getFileMBR()<BR>
143:             * returns the minimum bounding rectangle of geometries<BR>
144:             * within the shape-file
145:             */
146:            public SHPEnvelope getFileMBR() {
147:
148:                return fh.getFileMBR();
149:
150:            }
151:
152:            /**
153:             * method: getRecordNum()<BR>
154:             * returns the number of record with in a shape-file<BR>
155:             */
156:            public int getRecordNum() {
157:
158:                return shx.getRecordNum();
159:
160:            }
161:
162:            /**
163:             * method: getRecordMBR(int RecNo)<BR>
164:             * returns the minimum bound rectangle of RecNo's Geometrie 
165:             * of the shape-file<BR>
166:             */
167:            public SHPEnvelope getRecordMBR(int RecNo) throws IOException {
168:
169:                SHPEnvelope recordMBR = null;
170:                byte[] recBuf = null;
171:
172:                // index in IndexArray (see IndexFile) 
173:                int iaIndex = RecNo - 1;
174:
175:                int off = shx.getRecordOffset(iaIndex);
176:
177:                // calculate length from 16-bit words (= 2 bytes) to lenght in bytes
178:                int len = shx.getRecordLength(iaIndex) * 2;
179:
180:                // off holds the offset of the shape-record in 16-bit words (= 2 byte)
181:                // multiply with 2 gets number of bytes to seek
182:                long rafPos = off * 2;
183:
184:                // fetch shape record 
185:                raf.seek(rafPos + ShapeConst.SHAPE_FILE_RECORD_HEADER_LENGTH);
186:
187:                recBuf = null;
188:                recBuf = new byte[len];
189:
190:                if (raf.read(recBuf, 0, len) != -1) {
191:
192:                    int shpType = ByteUtils.readLEInt(recBuf, 0);
193:
194:                    /*
195:                     * only for PolyLines, Polygons and MultiPoints 
196:                     * minimum bounding rectangles are defined
197:                     */
198:                    if ((shpType == ShapeConst.SHAPE_TYPE_POLYLINE)
199:                            || (shpType == ShapeConst.SHAPE_TYPE_POLYGON)
200:                            || (shpType == ShapeConst.SHAPE_TYPE_MULTIPOINT)) {
201:
202:                        recordMBR = new SHPEnvelope(recBuf);
203:
204:                    } // end if shpType
205:
206:                } // end if result
207:
208:                return recordMBR;
209:            }
210:
211:            /**
212:             * method: getByRecNo (int RecNo)<BR>
213:             * retruns a ShapeRecord-Geometry by RecorcNumber<BR>
214:             */
215:            public SHPGeometry getByRecNo(int RecNo) throws IOException {
216:
217:                SHPGeometry shpGeom = null;
218:                byte[] recBuf = null;
219:
220:                // index in IndexArray (see IndexFile) 
221:                int iaIndex = RecNo - 1;
222:
223:                int off = shx.getRecordOffset(iaIndex);
224:
225:                // calculate length from 16-bit words (= 2 bytes) to lenght in bytes
226:                int len = shx.getRecordLength(iaIndex) * 2;
227:
228:                // off holds the offset of the shape-record in 16-bit words (= 2 byte)
229:                // multiply with 2 gets number of bytes to seek
230:                long rafPos = off * 2;
231:
232:                //fetch record header
233:                raf.seek(rafPos);
234:
235:                recBuf = null;
236:                recBuf = new byte[ShapeConst.SHAPE_FILE_RECORD_HEADER_LENGTH];
237:
238:                // fetch shape record 
239:                raf.seek(rafPos + ShapeConst.SHAPE_FILE_RECORD_HEADER_LENGTH);
240:
241:                recBuf = null;
242:                recBuf = new byte[len];
243:
244:                if (raf.read(recBuf, 0, len) != -1) {
245:
246:                    int shpType = ByteUtils.readLEInt(recBuf, 0);
247:
248:                    // create a geometry out of record buffer with shapetype
249:                    if (shpType == ShapeConst.SHAPE_TYPE_POINT) {
250:                        shpGeom = new SHPPoint(recBuf, 4);
251:                    } else if (shpType == ShapeConst.SHAPE_TYPE_MULTIPOINT) {
252:                        shpGeom = new SHPMultiPoint(recBuf);
253:                    } else if (shpType == ShapeConst.SHAPE_TYPE_POLYLINE) {
254:                        shpGeom = new SHPPolyLine(recBuf);
255:                    } else if (shpType == ShapeConst.SHAPE_TYPE_POLYGON) {
256:                        shpGeom = new SHPPolygon(recBuf);
257:                    } else if (shpType == ShapeConst.SHAPE_TYPE_POLYGONZ) {
258:                        shpGeom = new SHPPolygon3D(recBuf);
259:                    }
260:
261:                } // end if result
262:
263:                return shpGeom;
264:
265:            }
266:
267:            /**
268:             * method: getShapeType(int RecNo)<BR>
269:             * returns the minimum bound rectangle of RecNo's Geometrie 
270:             * of the shape-file<BR>
271:             */
272:            public int getShapeTypeByRecNo(int RecNo) throws IOException {
273:
274:                byte[] recBuf = null;
275:                int shpType = -1;
276:
277:                // index in IndexArray (see IndexFile) 
278:                int iaIndex = RecNo - 1;
279:
280:                int off = shx.getRecordOffset(iaIndex);
281:
282:                // calculate length from 16-bit words (= 2 bytes) to lenght in bytes
283:                int len = shx.getRecordLength(iaIndex) * 2;
284:
285:                // off holds the offset of the shape-record in 16-bit words (= 2 byte)
286:                // multiply with 2 gets number of bytes to seek
287:                long rafPos = off * 2;
288:
289:                // fetch shape record 
290:                raf.seek(rafPos + ShapeConst.SHAPE_FILE_RECORD_HEADER_LENGTH);
291:
292:                recBuf = null;
293:                recBuf = new byte[len];
294:
295:                if (raf.read(recBuf, 0, len) != -1) {
296:
297:                    shpType = ByteUtils.readLEInt(recBuf, 0);
298:
299:                } // end if result
300:
301:                return shpType;
302:            }
303:
304:            /**
305:             * method: public void write(byte[] bytearray)<BR>
306:             * appends a bytearray to the shape file<BR>
307:             */
308:            public void write(byte[] bytearray, IndexRecord record,
309:                    SHPEnvelope mbr) throws IOException {
310:                raf.seek(record.offset * 2);
311:                raf.write(bytearray);
312:                shx.appendRecord(record, mbr);
313:            }
314:
315:            /**
316:             * method: public void writeHeader(int filelength, byte shptype, SHPEnvelope mbr)<BR>
317:             * writes a header to the shape and index file<BR>
318:             */
319:            public void writeHeader(int filelength, byte shptype,
320:                    SHPEnvelope mbr) throws IOException {
321:                fh.writeHeader(filelength, shptype, mbr);
322:                shx.writeHeader(shptype, mbr);
323:            }
324:
325:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.