Revision 10627 trunk/libraries/libFMap/src/com/iver/cit/gvsig/fmap/drivers/shp/IndexedShpDriver.java

View differences:

IndexedShpDriver.java
54 54
import org.apache.log4j.Logger;
55 55

  
56 56
import com.hardcode.gdbms.driver.DriverUtilities;
57
import com.hardcode.gdbms.driver.exceptions.CloseDriverException;
58
import com.hardcode.gdbms.driver.exceptions.FileNotFoundDriverException;
59
import com.hardcode.gdbms.driver.exceptions.InitializeDriverException;
60
import com.hardcode.gdbms.driver.exceptions.InitializeWriterException;
61
import com.hardcode.gdbms.driver.exceptions.OpenDriverException;
62
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
63
import com.hardcode.gdbms.driver.exceptions.ReloadDriverException;
64
import com.iver.cit.gvsig.exceptions.visitors.ProcessWriterVisitorException;
65
import com.iver.cit.gvsig.exceptions.visitors.StartWriterVisitorException;
66
import com.iver.cit.gvsig.exceptions.visitors.StopWriterVisitorException;
57 67
import com.iver.cit.gvsig.fmap.core.FShape;
58 68
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
59 69
import com.iver.cit.gvsig.fmap.core.IGeometry;
......
63 73
import com.iver.cit.gvsig.fmap.drivers.ExternalData;
64 74
import com.iver.cit.gvsig.fmap.drivers.ITableDefinition;
65 75
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver;
66
import com.iver.cit.gvsig.fmap.edition.EditionException;
67 76
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
68 77
import com.iver.cit.gvsig.fmap.edition.ISpatialWriter;
69 78
import com.iver.cit.gvsig.fmap.edition.IWriteable;
......
113 122
	 *
114 123
	 * @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#close()
115 124
	 */
116
	public void close() throws IOException {
117
		IOException ret = null;
125
	public void close() throws CloseDriverException {
126
		CloseDriverException ret = null;
118 127

  
119 128
		try {
120 129
			channel.close();
121 130
            channelShx.close();
122 131
		} catch (IOException e) {
123
			ret = e;
132
			ret = new CloseDriverException(getName(),e);
124 133
		} finally {
125 134
			try {
126 135
				fin.close();
127 136
			} catch (IOException e1) {
128
				ret = e1;
137
				ret = new CloseDriverException(getName(),e1);
129 138
			}
130 139
		}
131 140

  
132 141
		if (ret != null) {
133 142
			throw ret;
134 143
		}
135
		else // Si todo ha ido bien, preparamos para liberar memoria.
136
        {
137
		    bb = null;
138
            bbShx = null;
139
        }
144
		bb = null;
145
		bbShx = null;
140 146
	}
141 147

  
142 148
	/**
143 149
	 * @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#open(java.io.File)
144 150
	 */
145
	public void open(File f) throws IOException {
151
	public void open(File f) throws OpenDriverException {
146 152
		fileShp = f;
147 153

  
148
		fin = new FileInputStream(f);
154
		try {
155
			fin = new FileInputStream(f);
149 156

  
157

  
150 158
		// Open the file and then get a channel from the stream
151 159
		channel = fin.getChannel();
152 160

  
......
168 176
        // bbShx = channelShx.map(FileChannel.MapMode.READ_ONLY, 0, sizeShx);
169 177
        bbShx = new BigByteBuffer2(channelShx, FileChannel.MapMode.READ_ONLY);
170 178
        bbShx.order(ByteOrder.BIG_ENDIAN);
179
		} catch (FileNotFoundException e) {
180
			throw new FileNotFoundDriverException(getName(),e,f.getAbsolutePath());
181
		} catch (IOException e) {
182
			throw new OpenDriverException(getName(),e);
183
		}
171 184
	}
172 185

  
173 186
	/**
174
	 * DOCUMENT ME!
175
	 *
176
	 * @param index DOCUMENT ME!
177
	 *
178
	 * @return DOCUMENT ME!
179
	 *
180
	 * @throws IOException
181
	 *
182
	 * @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#getShape(int)
183
	 */
184

  
185
	/* public FShape getShapeByID(int ID) {
186
	   Point2D.Double p = new Point2D.Double();
187
	   Point2D.Double pAnt = null;
188
	   int numParts;
189
	   int numPoints;
190
	   int i;
191
	   int j;
192
	   int numReg;
193
	   int numeroPuntos;
194
	   int hasta;
195
	   int desde;
196
	   Rectangle2D.Double BoundingBox = new Rectangle2D.Double();
197

  
198
	           SHPShape shapeShp=null;
199
	   FShape resulShape = null;
200
	   try {
201
	       bb.position(m_posShapes[ID]);
202
	       bb.order(ByteOrder.LITTLE_ENDIAN);
203
	       int tipoShape = bb.getInt();
204
	       m_shapeType = tipoShape;
205
	       // retrieve that shape.
206
	       // tempRecord.setShape(readShape(tempShapeType, tempContentLength, in));
207
	       if (tipoShape == FConstant.SHAPE_TYPE_POINT) {
208
	           p = readPoint(bb);
209
	           resulShape = new FShape(new FPoint(p.getX(),p.getY()),FConstant.SHAPE_TYPE_POINT);
210
	           //Comprobaci?n punto.
211
	           //System.err.println("p.x = "+p.x);
212
	           //System.err.println("p.y = "+p.y);
213

  
214
	       } else if ((tipoShape == FConstant.SHAPE_TYPE_POLYLINE) ||
215
	               (tipoShape == FConstant.SHAPE_TYPE_POLYGON)) {
216
	           // BoundingBox
217
	           BoundingBox = readRectangle(bb);
218
	           numParts = bb.getInt();
219
	           numPoints = bb.getInt();
220
	           // part indexes.
221
	           // Geometry geom = GeometryFactory.toGeometryArray();
222
	           GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
223
	                   numPoints);
224
	           int[] tempParts = new int[numParts];
225
	           for (i = 0; i < numParts; i++) {
226
	               tempParts[i] = bb.getInt();
227
	           }
228
	           j = 0;
229
	           ///Line2D.Double line2D;
230
	           FPoint[] points=new FPoint[numPoints];
231
	           for (i = 0; i < numPoints; i++) {
232
	                   p=readPoint(bb);
233
	               points[i] = new FPoint(p.x,p.y);
234
	               // System.out.println("x= " + p.x + " y=" + p.y);
235
	               // System.out.println("x= " + (float) p.x + " y=" + (float) p.y);
236
	               if (i == tempParts[j]) {
237
	                   elShape.moveTo(p.x, p.y);
238
	                   if (j < (numParts - 1)) {
239
	                       j++;
240
	                   }
241
	               } else {
242
	                   elShape.lineTo(p.x, p.y);
243
	               }
244
	           }
245
	           //FGeometry pol=new FPolyLine(points,tempParts,BoundingBox);
246

  
247
	           resulShape = new FShape(tipoShape,elShape);
248
	       } else if (tipoShape == FConstant.SHAPE_TYPE_MULTIPOINT) {
249
	           // BoundingBox
250
	           BoundingBox = readRectangle(bb);
251
	           numPoints = bb.getInt();
252
	           FPoint[] tempPoints = new FPoint[numPoints];
253
	           for (i = 0; i < numPoints; i++) {
254
	                   Point2D p2=readPoint(bb);
255
	               tempPoints[i] = new FPoint(p2.getX(),p2.getY(),0);
256
	           }
257
	           FMultiPoint multipoint = new FMultiPoint(tempPoints,BoundingBox);
258
	           resulShape = new FShape(multipoint,tipoShape);
259
	       } else if (tipoShape == FConstant.SHAPE_TYPE_POINTZ) {
260
	           FPoint p3d = new FPoint();
261
	           p3d.read(bb);
262
	           resulShape = new FShape(p3d,tipoShape);
263
	       } else if ((tipoShape == FConstant.SHAPE_TYPE_POLYLINEZ) ||
264
	               (tipoShape == FConstant.SHAPE_TYPE_POLYGONZ)) {
265
	           // BoundingBox
266
	           BoundingBox = readRectangle(bb);
267
	           numParts = bb.getInt();
268
	           numPoints = bb.getInt();
269
	           // part indexes.
270
	           // Geometry geom = GeometryFactory.toGeometryArray();
271

  
272
	           GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
273
	                   numPoints);
274
	           int[] tempParts = new int[numParts];
275
	           for (i = 0; i < numParts; i++) {
276
	               tempParts[i] = bb.getInt();
277
	           }
278
	           j = 0;
279
	          //Line2D.Double line2D;
280
	           FPoint[] points=new FPoint[numPoints];
281
	           for (i = 0; i < numPoints; i++) {
282
	               p = readPoint(bb);
283
	               points[i]=new FPoint(p.x,p.y);
284

  
285
	               if (i == tempParts[j]) {
286
	                   elShape.moveTo(p.x, p.y);
287
	                   if (j < (numParts - 1)) {
288
	                       j++;
289
	                   }
290
	               } else {
291
	                   elShape.lineTo(p.x, p.y);
292
	               }
293

  
294
	           }
295

  
296
	           double[] boxZ = new double[2];
297
	           boxZ[0] = bb.getDouble();
298
	           boxZ[1] = bb.getDouble();
299
	           double[] pZ = new double[numPoints];
300
	           for (i = 0; i < numPoints; i++) {
301
	               pZ[i] = bb.getDouble();
302
	           }
303
	           //FGeometry pol=new FPolyLine(points,tempParts,BoundingBox);
304
	           resulShape = new FShape(tipoShape, elShape, pZ);
305
	       } else if (tipoShape == FConstant.SHAPE_TYPE_MULTIPOINTZ) {
306
	           // BoundingBox
307
	           BoundingBox = readRectangle(bb);
308
	           numPoints = bb.getInt();
309
	           FPoint[] tempPoints3D = new FPoint[numPoints];
310
	           for (i = 0; i < numPoints; i++) {
311
	               tempPoints3D[i] = new FPoint();
312
	               tempPoints3D[i].read(bb);
313
	           }
314
	           FMultiPoint multipoint3D = new FMultiPoint(tempPoints3D,BoundingBox);
315
	           resulShape = new FShape(multipoint3D,tipoShape);
316
	       }
317
	   } catch (Exception e) {
318
	       System.err.println("Fallo en getShapeByID. ID=" + ID +
319
	           " m_posShapes[ID]=" + m_posShapes[ID]);
320
	       System.err.println("getShapeByID: " + e.getMessage());
321
	       e.printStackTrace();
322
	   }
323
	   return resulShape;
324
	   }
325
	 */
326

  
327
	/**
328 187
	 * Devuelve la geometria a partir de un ?ndice.
329 188
	 *
330 189
	 * @param index DOCUMENT ME!
331 190
	 *
332 191
	 * @return DOCUMENT ME!
333
	 *
334
	 * @throws IOException DOCUMENT ME!
335 192
	 */
336
	public synchronized IGeometry getShape(int index) throws IOException {
193
	public synchronized IGeometry getShape(int index) throws ReadDriverException {
337 194
		Point2D.Double p = new Point2D.Double();
338 195
		int numParts;
339 196
		int numPoints;
......
573 430
	/**
574 431
	 * @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#getShapeCount()
575 432
	 */
576
	public int getShapeCount() {
433
	public int getShapeCount() throws ReadDriverException {
577 434
		return numReg;
578 435
	}
579 436

  
......
615 472
	/**
616 473
	 * @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#initialize()
617 474
	 */
618
	public void initialize() throws IOException {
475
	public void initialize() throws InitializeDriverException {
619 476
		// create a new header.
620 477
		ShapeFileHeader2 myHeader = new ShapeFileHeader2();
621 478

  
......
651 508

  
652 509
		DbaseFileNIO m_FichDbf = new DbaseFileNIO();
653 510

  
654
		m_FichDbf.open(new File(strFichDbf));
511
		try {
512
			m_FichDbf.open(new File(strFichDbf));
513
		} catch (IOException e) {
514
			throw new FileNotFoundDriverException(getName(),e,strFichDbf);
515
		}
655 516
		numReg = m_FichDbf.getRecordCount();
656 517

  
657 518
		// shpWriter.initialize(file);
......
717 578
	 *
718 579
	 * @throws IOException
719 580
	 */
720
	private synchronized Rectangle2D.Double readRectangle(BigByteBuffer2 in)
721
		throws IOException {
581
	private synchronized Rectangle2D.Double readRectangle(BigByteBuffer2 in){
722 582
		Rectangle2D.Double tempRect = new Rectangle2D.Double();
723 583
		in.order(ByteOrder.LITTLE_ENDIAN);
724 584
		tempRect.x = in.getDouble();
......
744 604
	/**
745 605
	 * @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#getFullExtent()
746 606
	 */
747
	public Rectangle2D getFullExtent() throws IOException {
607
	public Rectangle2D getFullExtent() throws ReadDriverException {
748 608
		return extent;
749 609
	}
750 610

  
......
755 615
	 *
756 616
	 * @return Rect?ngulo.
757 617
	 *
758
	 * @throws IOException
759
	 *
760 618
	 * @see com.iver.cit.gvsig.fmap.drivers.BoundedShapes#getShapeBounds()
761 619
	 */
762
	public synchronized Rectangle2D getShapeBounds(int index) throws IOException {
620
	public synchronized Rectangle2D getShapeBounds(int index) throws ReadDriverException {
763 621
		Point2D p = new Point2D.Double();
764 622
		Rectangle2D BoundingBox = new Rectangle2D.Double();
765 623
		bb.position(getPositionForRecord(index));
......
876 734
		return fileShp;
877 735
	}
878 736

  
879
	public void reload() throws IOException {
880
		open(fileShp);
881
		initialize();
737
	public void reload() throws ReloadDriverException {
738
		try {
739
			open(fileShp);
740
			initialize();
741
		} catch (OpenDriverException e) {
742
			throw new ReloadDriverException(getName(),e);
743
		} catch (InitializeDriverException e) {
744
			throw new ReloadDriverException(getName(),e);
745
		}
882 746
	}
883 747

  
884 748
	public boolean canWriteGeometry(int gvSIGgeometryType) {
......
888 752
	/* (non-Javadoc)
889 753
	 * @see com.iver.cit.gvsig.fmap.edition.IWriter#preProcess()
890 754
	 */
891
	public void preProcess() throws EditionException {
755
	public void preProcess() throws StartWriterVisitorException {
892 756
		// Necesitamos crear un fichero temporal porque a la vez que esic
893 757
		// La cabecera ya existe, as? que no la sobreescribimos.
894 758
		// shpWriter.setWriteHeaders(false);
......
896 760

  
897 761
	}
898 762

  
899
	public void process(IRowEdited row) throws EditionException {
763
	public void process(IRowEdited row) throws ProcessWriterVisitorException {
900 764
		shpWriter.process(row);
901 765

  
902 766
	}
903 767

  
904
	public void postProcess() throws EditionException {
768
	public void postProcess() throws StopWriterVisitorException {
905 769
		shpWriter.postProcess();
906 770

  
907 771
		try {
......
934 798

  
935 799

  
936 800
		} catch (FileNotFoundException e) {
937
			throw new EditionException(e);
801
			throw new StopWriterVisitorException(getName(),e);
938 802
		} catch (IOException e) {
939
			throw new EditionException(e);
803
			throw new StopWriterVisitorException(getName(),e);
804
		} catch (ReloadDriverException e) {
805
			throw new StopWriterVisitorException(getName(),e);
940 806
		}
941 807

  
942 808

  
......
963 829
		this.shpWriter = shpWriter;
964 830
	}
965 831

  
966
	public void initialize(ITableDefinition layerDef) throws EditionException {
832
	public void initialize(ITableDefinition layerDef) throws InitializeWriterException {
967 833
		int aux = (int)(Math.random() * 1000);
968 834
		fTemp = new File(tempDirectoryPath + "/tmpShp" + aux + ".shp");
969 835
		shpWriter.setFile(fTemp);
......
995 861
	public boolean canSaveEdits() {
996 862
		return shpWriter.canSaveEdits();
997 863
	}
998
	
864

  
999 865
	public boolean isWriteAll() {
1000 866
		return true;
1001 867
	}

Also available in: Unified diff