svn-gvsig-desktop / trunk / libraries / libDataSourceBaseDrivers / src / org / gvsig / data / datastores / vectorial / file / shp / SHPStore.java @ 20414
History | View | Annotate | Download (20 KB)
1 |
package org.gvsig.data.datastores.vectorial.file.shp; |
---|---|
2 |
|
3 |
import java.awt.geom.Point2D; |
4 |
import java.io.File; |
5 |
import java.io.FileInputStream; |
6 |
import java.io.FileNotFoundException; |
7 |
import java.io.IOException; |
8 |
import java.lang.ref.WeakReference; |
9 |
import java.nio.ByteOrder; |
10 |
import java.nio.channels.FileChannel; |
11 |
import java.text.DateFormat; |
12 |
import java.text.ParseException; |
13 |
import java.util.Collection; |
14 |
import java.util.Date; |
15 |
import java.util.Iterator; |
16 |
import java.util.List; |
17 |
|
18 |
import org.gvsig.data.IDataCollection; |
19 |
import org.gvsig.data.IDataStoreParameters; |
20 |
import org.gvsig.data.datastores.vectorial.IFeaturesWriter; |
21 |
import org.gvsig.data.datastores.vectorial.file.dbf.DBFStore; |
22 |
import org.gvsig.data.datastores.vectorial.file.shp.utils.SHP; |
23 |
import org.gvsig.data.datastores.vectorial.file.shp.utils.ShapeFileHeader2; |
24 |
import org.gvsig.data.exception.CloseException; |
25 |
import org.gvsig.data.exception.InitializeException; |
26 |
import org.gvsig.data.exception.OpenException; |
27 |
import org.gvsig.data.exception.ReadException; |
28 |
import org.gvsig.data.exception.WriteException; |
29 |
import org.gvsig.data.spatialprovisional.Extent; |
30 |
import org.gvsig.data.spatialprovisional.IExtent; |
31 |
import org.gvsig.data.vectorial.DefaultAttributeDescriptor; |
32 |
import org.gvsig.data.vectorial.FeatureStoreNotification; |
33 |
import org.gvsig.data.vectorial.IFeature; |
34 |
import org.gvsig.data.vectorial.IFeatureAttributeDescriptor; |
35 |
import org.gvsig.data.vectorial.IFeatureCollection; |
36 |
import org.gvsig.data.vectorial.IFeatureID; |
37 |
import org.gvsig.data.vectorial.IFeatureType; |
38 |
import org.gvsig.data.vectorial.IsNotFeatureSettingException; |
39 |
import org.gvsig.exceptions.BaseException; |
40 |
import org.gvsig.metadata.IMetadata; |
41 |
import org.gvsig.metadata.IMetadataManager; |
42 |
import org.gvsig.metadata.MetadataManager; |
43 |
|
44 |
import com.iver.cit.gvsig.fmap.core.FNullGeometry; |
45 |
import com.iver.cit.gvsig.fmap.core.FShape; |
46 |
import com.iver.cit.gvsig.fmap.core.GeneralPathX; |
47 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
48 |
import com.iver.cit.gvsig.fmap.core.ShapeFactory; |
49 |
import com.iver.utiles.bigfile.BigByteBuffer2; |
50 |
|
51 |
public class SHPStore extends DBFStore{ |
52 |
public static String DATASTORE_NAME = "SHPStore"; |
53 |
private SHPStoreParameters shpParameters=null; |
54 |
private File fileShp; |
55 |
private FileInputStream fin; |
56 |
private FileChannel channel; |
57 |
private BigByteBuffer2 bb;
|
58 |
private File fileShx; |
59 |
private FileInputStream finShx; |
60 |
private FileChannel channelShx; |
61 |
private BigByteBuffer2 bbShx;
|
62 |
private Extent extent;
|
63 |
private int type; |
64 |
|
65 |
|
66 |
public void init(IDataStoreParameters parameters) throws InitializeException { |
67 |
super.init(parameters);
|
68 |
featureType=super.getDefaultFeatureType();
|
69 |
|
70 |
DefaultAttributeDescriptor dad=new DefaultAttributeDescriptor();
|
71 |
dad.setName("GEOMETRY");
|
72 |
dad.setType(IFeatureAttributeDescriptor.TYPE_GEOMETRY); |
73 |
featureType.add(dad); |
74 |
featureType.setDefaultGeometry("GEOMETRY");
|
75 |
|
76 |
SHPStoreParameters shpParameters=(SHPStoreParameters)parameters; |
77 |
|
78 |
fileShp = shpParameters.getSHPFile(); |
79 |
|
80 |
try {
|
81 |
fin = new FileInputStream(fileShp); |
82 |
|
83 |
|
84 |
// Open the file and then get a channel from the stream
|
85 |
channel = fin.getChannel(); |
86 |
|
87 |
// long size = channel.size();
|
88 |
|
89 |
// Get the file's size and then map it into memory
|
90 |
// bb = channel.map(FileChannel.MapMode.READ_ONLY, 0, size);
|
91 |
bb = new BigByteBuffer2(channel, FileChannel.MapMode.READ_ONLY); |
92 |
fileShx=shpParameters.getSHXFile(); |
93 |
finShx = new FileInputStream(fileShx); |
94 |
|
95 |
// Open the file and then get a channel from the stream
|
96 |
channelShx = finShx.getChannel(); |
97 |
|
98 |
// long sizeShx = channelShx.size();
|
99 |
|
100 |
// Get the file's size and then map it into memory
|
101 |
// bb = channel.map(FileChannel.MapMode.READ_ONLY, 0, size);
|
102 |
// bbShx = channelShx.map(FileChannel.MapMode.READ_ONLY, 0, sizeShx);
|
103 |
bbShx = new BigByteBuffer2(channelShx, FileChannel.MapMode.READ_ONLY); |
104 |
bbShx.order(ByteOrder.BIG_ENDIAN);
|
105 |
} catch (FileNotFoundException e) { |
106 |
throw new OpenException(getName(),e); |
107 |
} catch (IOException e) { |
108 |
throw new OpenException(getName(),e); |
109 |
} |
110 |
|
111 |
} |
112 |
|
113 |
|
114 |
// private IFeatureAttributeDescriptor createFeatureAttribute(int i) {
|
115 |
// char fieldType = dbf.getFieldType(i);
|
116 |
// DefaultAttributeDescriptor dad=new DefaultAttributeDescriptor();
|
117 |
// dad.setOrdinal(i);
|
118 |
// dad.setName(dbf.getFieldName(i));
|
119 |
// dad.setSize(dbf.getFieldLength(i));
|
120 |
// if (fieldType == 'L') {
|
121 |
// dad.setType(IFeatureAttributeDescriptor.TYPE_BOOLEAN);
|
122 |
//
|
123 |
// } else if ((fieldType == 'F') || (fieldType == 'N')) {
|
124 |
// int precision = dbf.getFieldDecimalLength(i);
|
125 |
// if (precision > 0){
|
126 |
// dad.setType(IFeatureAttributeDescriptor.TYPE_DOUBLE);
|
127 |
// dad.setPrecision(precision);
|
128 |
// } else{
|
129 |
// dad.setType(IFeatureAttributeDescriptor.TYPE_INT);
|
130 |
// }
|
131 |
// } else if (fieldType == 'C') {
|
132 |
// dad.setType(IFeatureAttributeDescriptor.TYPE_STRING);
|
133 |
// } else if (fieldType == 'D') {
|
134 |
// dad.setType(IFeatureAttributeDescriptor.TYPE_DATE);
|
135 |
// } else {
|
136 |
//// throw new BadFieldDriverException(getName(),null,String.valueOf(fieldType));
|
137 |
// }
|
138 |
// return dad;
|
139 |
//
|
140 |
// }
|
141 |
|
142 |
protected void doFinishEdition() throws WriteException, ReadException, IsNotFeatureSettingException { |
143 |
IFeaturesWriter writer = getFeaturesWriter(); |
144 |
writer.init(this);
|
145 |
writer.updateFeatureType(featureType); |
146 |
writer.preProcess(); |
147 |
Collection collection=getDataCollection();
|
148 |
Iterator iterator=collection.iterator();
|
149 |
IFeature feature; |
150 |
while (iterator.hasNext()) {
|
151 |
feature= (IFeature) iterator.next(); |
152 |
writer.insertFeature(feature); |
153 |
} |
154 |
writer.postProcess(); |
155 |
|
156 |
} |
157 |
|
158 |
public IDataCollection getDataCollection(IFeatureType type, String filter, String order) throws ReadException { |
159 |
if (type==null){ |
160 |
type=getDefaultFeatureType(); |
161 |
} |
162 |
IFeatureCollection coll; |
163 |
if (order == null){ |
164 |
coll=new ShpFeatureCollection(featureManager,this,type,filter); |
165 |
}else{
|
166 |
coll=new ShpFeatureCollectionWithFeatureID(featureManager,this,type,filter,order); |
167 |
} |
168 |
this.addObserver(new WeakReference(coll)); |
169 |
return coll;
|
170 |
} |
171 |
|
172 |
public IFeature getFeatureByID(IFeatureID id) throws ReadException, IsNotFeatureSettingException { |
173 |
if (this.alterMode){ |
174 |
if (featureManager.contains(id)) {
|
175 |
return featureManager.getFeature(id);
|
176 |
} |
177 |
} |
178 |
return getFeatureByPosition(featureType,((ShpFeatureID)id).getIndex());
|
179 |
} |
180 |
|
181 |
public List getFeatureTypes() { |
182 |
featureTypes.set(0,getDefaultFeatureType());
|
183 |
return featureTypes;
|
184 |
} |
185 |
|
186 |
public IFeatureType getDefaultFeatureType() {
|
187 |
IFeatureType ft = featureType; |
188 |
if (isEditing()){
|
189 |
// Aqu? hay que construir un FeatureType con los cambios que se hayan hecho en la edici?n.
|
190 |
return attributeManager.getFeatureType(ft);
|
191 |
} |
192 |
return ft;
|
193 |
} |
194 |
|
195 |
public boolean isWithDefaultLegend() { |
196 |
return false; |
197 |
} |
198 |
|
199 |
public Object getDefaultLegend() { |
200 |
return null; |
201 |
} |
202 |
|
203 |
public Object getDefaultLabelingStrategy() { |
204 |
return null; |
205 |
} |
206 |
|
207 |
public boolean canAlterFeatureType() { |
208 |
return true; |
209 |
} |
210 |
|
211 |
public String getName() { |
212 |
return DATASTORE_NAME;
|
213 |
} |
214 |
|
215 |
public void open() throws OpenException { |
216 |
this.observable.notifyObservers(
|
217 |
new FeatureStoreNotification(this,FeatureStoreNotification.BEFORE_OPEN) |
218 |
); |
219 |
super.open();
|
220 |
// create a new header.
|
221 |
ShapeFileHeader2 myHeader = new ShapeFileHeader2();
|
222 |
|
223 |
bb.position(0);
|
224 |
|
225 |
// read the header
|
226 |
myHeader.readHeader(bb); |
227 |
|
228 |
extent = new Extent(myHeader.myXmin, myHeader.myYmin,myHeader.myXmax,myHeader.myYmax);
|
229 |
// extent = new Rectangle2D.Double(myHeader.myXmin, myHeader.myYmin,
|
230 |
// myHeader.myXmax - myHeader.myXmin,
|
231 |
// myHeader.myYmax - myHeader.myYmin);
|
232 |
|
233 |
type = myHeader.myShapeType; |
234 |
|
235 |
double x = myHeader.myXmin;
|
236 |
double y = myHeader.myYmin;
|
237 |
double w = myHeader.myXmax - myHeader.myXmin;
|
238 |
double h = myHeader.myYmax - myHeader.myYmin;
|
239 |
|
240 |
if (w == 0) { |
241 |
x -= 0.1;
|
242 |
w = 0.2;
|
243 |
} |
244 |
|
245 |
if (h == 0) { |
246 |
y -= 0.1;
|
247 |
h = 0.2;
|
248 |
} |
249 |
featureType.setGeometryTypes(new int[]{getGeometryType()}); |
250 |
// String strFichDbf = m_Path.toLowerCase().replaceAll("\\.shp", ".dbf");
|
251 |
// String strFichDbf = fileShp.getAbsolutePath().replaceAll("\\.shp", ".dbf");
|
252 |
// strFichDbf = strFichDbf.replaceAll("\\.SHP", ".DBF");
|
253 |
//
|
254 |
// DbaseFileNIO m_FichDbf = new DbaseFileNIO();
|
255 |
//
|
256 |
// try {
|
257 |
// m_FichDbf.open(new File(strFichDbf));
|
258 |
// } catch (IOException e) {
|
259 |
//// throw new FileNotFoundDriverException(getName(),e,strFichDbf);
|
260 |
// }
|
261 |
// numReg = m_FichDbf.getRecordCount();
|
262 |
this.observable.notifyObservers(
|
263 |
new FeatureStoreNotification(this,FeatureStoreNotification.AFTER_OPEN) |
264 |
); |
265 |
|
266 |
} |
267 |
|
268 |
public void close() throws CloseException { |
269 |
this.observable.notifyObservers(
|
270 |
new FeatureStoreNotification(this,FeatureStoreNotification.BEFORE_CLOSE) |
271 |
); |
272 |
super.close();
|
273 |
CloseException ret = null;
|
274 |
|
275 |
try {
|
276 |
channel.close(); |
277 |
channelShx.close(); |
278 |
} catch (IOException e) { |
279 |
ret = new CloseException(getName(),e);
|
280 |
} finally {
|
281 |
try {
|
282 |
fin.close(); |
283 |
} catch (IOException e1) { |
284 |
ret = new CloseException(getName(),e1);
|
285 |
} |
286 |
} |
287 |
|
288 |
// if (ret != null) {
|
289 |
// throw ret;
|
290 |
// }
|
291 |
bb = null;
|
292 |
bbShx = null;
|
293 |
this.observable.notifyObservers(
|
294 |
new FeatureStoreNotification(this,FeatureStoreNotification.AFTER_CLOSE) |
295 |
); |
296 |
|
297 |
} |
298 |
|
299 |
public void dispose() { |
300 |
this.observable.notifyObservers(
|
301 |
new FeatureStoreNotification(this,FeatureStoreNotification.BEFORE_DISPOSE) |
302 |
); |
303 |
super.dispose();
|
304 |
this.observable.notifyObservers(
|
305 |
new FeatureStoreNotification(this,FeatureStoreNotification.AFTER_DISPOSE) |
306 |
); |
307 |
} |
308 |
|
309 |
public boolean isEditable() { |
310 |
if (super.isEditable() && fileShp.canWrite() && fileShx.canWrite()) return true; |
311 |
return false; |
312 |
} |
313 |
|
314 |
public IMetadata getMetadata() throws BaseException { |
315 |
if (metadata==null){ |
316 |
IMetadataManager manager=MetadataManager.getManager(); |
317 |
metadata=manager.create(DATASTORE_NAME); |
318 |
IExtent extent=getFullExtent(); |
319 |
metadata.set("extent",extent);
|
320 |
String srs=getSRS();
|
321 |
metadata.set("srs",srs);
|
322 |
} |
323 |
if (this.alterMode){ |
324 |
IExtent extent=(IExtent)metadata.get("extent");
|
325 |
IFeatureCollection featureCollection=(IFeatureCollection)getDataCollection(); |
326 |
if (spatialManager.isFullExtentDirty()){
|
327 |
if (!featureCollection.isEmpty()){
|
328 |
Iterator featureIterator=featureCollection.iterator();
|
329 |
extent = ((IFeature)featureIterator.next()).getExtent(); |
330 |
while(featureIterator.hasNext()){
|
331 |
IFeature feature=(IFeature)featureIterator.next(); |
332 |
IExtent boundExtent=feature.getExtent(); |
333 |
if (boundExtent!=null) |
334 |
extent.add(boundExtent); |
335 |
} |
336 |
} |
337 |
} |
338 |
metadata.set("extent",extent);
|
339 |
} |
340 |
return metadata;
|
341 |
} |
342 |
protected IFeature getFeatureByPosition(IFeatureType featureType,long position) throws ReadException, IsNotFeatureSettingException { |
343 |
ShpFeature feature=new ShpFeature(featureType,this,position); |
344 |
feature.load(dbf, this.getGeometry(position));
|
345 |
return feature;
|
346 |
} |
347 |
|
348 |
protected IFeaturesWriter getFeaturesWriter() {
|
349 |
IFeaturesWriter writer = new ShpFeaturesWriter();
|
350 |
// writer.init(this);
|
351 |
return writer;
|
352 |
} |
353 |
protected long getFeatureCount(){ |
354 |
return dbf.getRecordCount();
|
355 |
} |
356 |
|
357 |
|
358 |
public IDataStoreParameters getParameters() {
|
359 |
return parameters;
|
360 |
} |
361 |
private synchronized IGeometry getGeometry(long position) { |
362 |
Point2D p = new Point2D.Double(); |
363 |
int numParts;
|
364 |
int numPoints;
|
365 |
int i;
|
366 |
int j;
|
367 |
int shapeType;
|
368 |
|
369 |
bb.position(getPositionForRecord(position)); |
370 |
bb.order(ByteOrder.LITTLE_ENDIAN);
|
371 |
|
372 |
shapeType = bb.getInt(); |
373 |
//el shape tal con tema tal y n?mro tal es null
|
374 |
if (shapeType==SHP.NULL){
|
375 |
return new FNullGeometry(); |
376 |
} |
377 |
|
378 |
// retrieve that shape.
|
379 |
// tempRecord.setShape(readShape(tempShapeType, tempContentLength, in));
|
380 |
switch (type) {
|
381 |
case (SHP.POINT2D):
|
382 |
p = readPoint(bb); |
383 |
|
384 |
return ShapeFactory.createPoint2D(p.getX(), p.getY());
|
385 |
|
386 |
case (SHP.POLYLINE2D):
|
387 |
|
388 |
bb.position(bb.position() + 32);
|
389 |
numParts = bb.getInt(); |
390 |
numPoints = bb.getInt(); |
391 |
|
392 |
// part indexes.
|
393 |
// Geometry geom = GeometryFactory.toGeometryArray();
|
394 |
GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
|
395 |
numPoints); |
396 |
|
397 |
int[] tempParts = new int[numParts]; |
398 |
|
399 |
for (i = 0; i < numParts; i++) { |
400 |
tempParts[i] = bb.getInt(); |
401 |
} |
402 |
|
403 |
j = 0;
|
404 |
|
405 |
for (i = 0; i < numPoints; i++) { |
406 |
p = readPoint(bb); |
407 |
|
408 |
if (i == tempParts[j]) {
|
409 |
elShape.moveTo(p.getX(), p.getY()); |
410 |
|
411 |
if (j < (numParts - 1)) { |
412 |
j++; |
413 |
} |
414 |
} else {
|
415 |
elShape.lineTo(p.getX(), p.getY()); |
416 |
} |
417 |
} |
418 |
|
419 |
return ShapeFactory.createPolyline2D(elShape);
|
420 |
|
421 |
case (SHP.POLYGON2D):
|
422 |
|
423 |
// BoundingBox = readRectangle(bb);
|
424 |
// bb.getDouble();
|
425 |
// bb.getDouble();
|
426 |
// bb.getDouble();
|
427 |
// bb.getDouble();
|
428 |
bb.position(bb.position() + 32);
|
429 |
numParts = bb.getInt(); |
430 |
|
431 |
numPoints = bb.getInt(); |
432 |
|
433 |
// part indexes.
|
434 |
// Geometry geom = GeometryFactory.toGeometryArray();
|
435 |
elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, numPoints);
|
436 |
|
437 |
tempParts = new int[numParts]; |
438 |
|
439 |
for (i = 0; i < numParts; i++) { |
440 |
tempParts[i] = bb.getInt(); |
441 |
} |
442 |
|
443 |
j = 0;
|
444 |
|
445 |
for (i = 0; i < numPoints; i++) { |
446 |
p = readPoint(bb); |
447 |
|
448 |
if (i == tempParts[j]) {
|
449 |
elShape.moveTo(p.getX(), p.getY()); |
450 |
|
451 |
if (j < (numParts - 1)) { |
452 |
j++; |
453 |
} |
454 |
} else {
|
455 |
if (i==numPoints-1){ |
456 |
elShape.closePath(); |
457 |
}else{
|
458 |
elShape.lineTo(p.getX(), p.getY()); |
459 |
} |
460 |
} |
461 |
} |
462 |
|
463 |
return ShapeFactory.createPolygon2D(elShape);
|
464 |
|
465 |
case (SHP.POINT3D):
|
466 |
|
467 |
double x = bb.getDouble();
|
468 |
double y = bb.getDouble();
|
469 |
double z = bb.getDouble();
|
470 |
|
471 |
return ShapeFactory.createPoint3D(x, y, z);
|
472 |
|
473 |
case (SHP.POLYLINE3D):
|
474 |
bb.position(bb.position() + 32);
|
475 |
numParts = bb.getInt(); |
476 |
numPoints = bb.getInt(); |
477 |
elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, numPoints);
|
478 |
tempParts = new int[numParts]; |
479 |
|
480 |
for (i = 0; i < numParts; i++) { |
481 |
tempParts[i] = bb.getInt(); |
482 |
} |
483 |
|
484 |
j = 0;
|
485 |
|
486 |
for (i = 0; i < numPoints; i++) { |
487 |
p = readPoint(bb); |
488 |
|
489 |
if (i == tempParts[j]) {
|
490 |
elShape.moveTo(p.getX(), p.getY()); |
491 |
|
492 |
if (j < (numParts - 1)) { |
493 |
j++; |
494 |
} |
495 |
} else {
|
496 |
elShape.lineTo(p.getX(), p.getY()); |
497 |
} |
498 |
} |
499 |
|
500 |
double[] boxZ = new double[2]; |
501 |
boxZ[0] = bb.getDouble();
|
502 |
boxZ[1] = bb.getDouble();
|
503 |
|
504 |
double[] pZ = new double[numPoints]; |
505 |
|
506 |
for (i = 0; i < numPoints; i++) { |
507 |
pZ[i] = bb.getDouble(); |
508 |
} |
509 |
|
510 |
return ShapeFactory.createPolyline3D(elShape, pZ);
|
511 |
case (SHP.POLYGON3D):
|
512 |
bb.position(bb.position() + 32);
|
513 |
numParts = bb.getInt(); |
514 |
numPoints = bb.getInt(); |
515 |
elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, numPoints);
|
516 |
tempParts = new int[numParts]; |
517 |
|
518 |
for (i = 0; i < numParts; i++) { |
519 |
tempParts[i] = bb.getInt(); |
520 |
} |
521 |
|
522 |
j = 0;
|
523 |
|
524 |
for (i = 0; i < numPoints; i++) { |
525 |
p = readPoint(bb); |
526 |
|
527 |
if (i == tempParts[j]) {
|
528 |
elShape.moveTo(p.getX(), p.getY()); |
529 |
|
530 |
if (j < (numParts - 1)) { |
531 |
j++; |
532 |
} |
533 |
} else {
|
534 |
if (i==numPoints-1){ |
535 |
elShape.closePath(); |
536 |
}else{
|
537 |
elShape.lineTo(p.getX(), p.getY()); |
538 |
} |
539 |
} |
540 |
} |
541 |
|
542 |
double[] boxpoZ = new double[2]; |
543 |
boxpoZ[0] = bb.getDouble();
|
544 |
boxpoZ[1] = bb.getDouble();
|
545 |
|
546 |
double[] poZ = new double[numPoints]; |
547 |
|
548 |
for (i = 0; i < numPoints; i++) { |
549 |
poZ[i] = bb.getDouble(); |
550 |
} |
551 |
|
552 |
return ShapeFactory.createPolygon3D(elShape, poZ);
|
553 |
|
554 |
case (SHP.MULTIPOINT2D):
|
555 |
bb.position(bb.position() + 32);
|
556 |
numPoints = bb.getInt(); |
557 |
|
558 |
double[] tempX = new double[numPoints]; |
559 |
double[] tempY = new double[numPoints]; |
560 |
|
561 |
for (i = 0; i < numPoints; i++) { |
562 |
tempX[i] = bb.getDouble(); |
563 |
tempY[i] = bb.getDouble(); |
564 |
} |
565 |
|
566 |
return ShapeFactory.createMultipoint2D(tempX, tempY);
|
567 |
|
568 |
case (SHP.MULTIPOINT3D):
|
569 |
bb.position(bb.position() + 32);
|
570 |
numPoints = bb.getInt(); |
571 |
|
572 |
double[] temX = new double[numPoints]; |
573 |
double[] temY = new double[numPoints]; |
574 |
double[] temZ = new double[numPoints]; |
575 |
|
576 |
for (i = 0; i < numPoints; i++) { |
577 |
temX[i] = bb.getDouble(); |
578 |
temY[i] = bb.getDouble(); |
579 |
//temZ[i] = bb.getDouble();
|
580 |
} |
581 |
|
582 |
for (i = 0; i < numPoints; i++) { |
583 |
temZ[i] = bb.getDouble(); |
584 |
} |
585 |
return ShapeFactory.createMultipoint3D(temX, temY, temZ);
|
586 |
} |
587 |
|
588 |
return null; |
589 |
|
590 |
|
591 |
} |
592 |
private long getPositionForRecord(long numRec) |
593 |
{ |
594 |
// shx file has a 100 bytes header, then, records
|
595 |
// 8 bytes length, one for each entity.
|
596 |
// first 4 bytes are the offset
|
597 |
// next 4 bytes are length
|
598 |
|
599 |
int posIndex = 100 + ((int)numRec * 8); |
600 |
// bbShx.position(posIndex);
|
601 |
long pos = 8 + 2* bbShx.getInt(posIndex); |
602 |
|
603 |
return pos;
|
604 |
} |
605 |
/**
|
606 |
* Reads the Point from the shape file.
|
607 |
*
|
608 |
* @param in ByteBuffer.
|
609 |
*
|
610 |
* @return Point2D.
|
611 |
*/
|
612 |
private synchronized Point2D readPoint(BigByteBuffer2 in) { |
613 |
// create a new point
|
614 |
Point2D.Double tempPoint = new Point2D.Double(); |
615 |
|
616 |
// bytes 1 to 4 are the type and have already been read.
|
617 |
// bytes 4 to 12 are the X coordinate
|
618 |
in.order(ByteOrder.LITTLE_ENDIAN);
|
619 |
tempPoint.setLocation(in.getDouble(), in.getDouble()); |
620 |
|
621 |
return tempPoint;
|
622 |
} |
623 |
/**
|
624 |
* Lee un rect?ngulo del fichero.
|
625 |
*
|
626 |
* @param in ByteBuffer.
|
627 |
*
|
628 |
* @return Rect?ngulo.
|
629 |
*
|
630 |
* @throws IOException
|
631 |
*/
|
632 |
private synchronized IExtent readRectangle(BigByteBuffer2 in){ |
633 |
|
634 |
in.order(ByteOrder.LITTLE_ENDIAN);
|
635 |
double x = in.getDouble();
|
636 |
double y = in.getDouble();
|
637 |
|
638 |
double x2 = in.getDouble();
|
639 |
|
640 |
if (x2-x == 0) { |
641 |
x2 = 0.2;
|
642 |
x -= 0.1;
|
643 |
} |
644 |
|
645 |
double y2 = in.getDouble();
|
646 |
|
647 |
if (y2-y == 0) { |
648 |
y2 = 0.2;
|
649 |
y -= 0.1;
|
650 |
} |
651 |
IExtent tempRect = new Extent(x,y,x2,y2);
|
652 |
return tempRect;
|
653 |
} |
654 |
public IExtent getBoundingBox(long featureIndex) { |
655 |
Point2D p = new Point2D.Double(); |
656 |
IExtent BoundingBox = null;
|
657 |
try {
|
658 |
bb.position(getPositionForRecord(featureIndex)); |
659 |
}catch (Exception e) { |
660 |
e.printStackTrace(); |
661 |
// logger.error(" Shapefile is corrupted. Drawing aborted. ="+e+ " "+"index = "+index);
|
662 |
} |
663 |
bb.order(ByteOrder.LITTLE_ENDIAN);
|
664 |
|
665 |
int tipoShape = bb.getInt();
|
666 |
|
667 |
//AZABALA: si tipoShape viene con valores erroneos deja de funcionar
|
668 |
//el metodo getShape(i)
|
669 |
// if (tipoShape != SHP.NULL) {
|
670 |
// type = tipoShape;
|
671 |
//
|
672 |
// }
|
673 |
|
674 |
// retrieve that shape.
|
675 |
// tempRecord.setShape(readShape(tempShapeType, tempContentLength, in));
|
676 |
switch (tipoShape) {
|
677 |
case (SHP.POINT2D):
|
678 |
case (SHP.POINT3D):
|
679 |
p = readPoint(bb); |
680 |
BoundingBox = new Extent(p.getX() - 0.1, p.getY() - 0.1,p.getX()+0.2,p.getY()+0.2); |
681 |
// new Rectangle2D.Double(p.getX() - 0.1,
|
682 |
// p.getY() - 0.1, 0.2, 0.2);
|
683 |
|
684 |
break;
|
685 |
|
686 |
case (SHP.POLYLINE2D):
|
687 |
case (SHP.POLYGON2D):
|
688 |
case (SHP.MULTIPOINT2D):
|
689 |
case (SHP.POLYLINE3D):
|
690 |
case (SHP.POLYGON3D):
|
691 |
case (SHP.MULTIPOINT3D):
|
692 |
|
693 |
// BoundingBox
|
694 |
BoundingBox = readRectangle(bb); |
695 |
|
696 |
break;
|
697 |
} |
698 |
|
699 |
return BoundingBox;
|
700 |
} |
701 |
private int getGeometryType() { |
702 |
int auxType = 0; |
703 |
|
704 |
switch (type) {
|
705 |
case (SHP.POINT2D):
|
706 |
case (SHP.POINT3D):
|
707 |
auxType = auxType | FShape.POINT; |
708 |
|
709 |
break;
|
710 |
|
711 |
case (SHP.POLYLINE2D):
|
712 |
case (SHP.POLYLINE3D):
|
713 |
auxType = auxType | FShape.LINE; |
714 |
|
715 |
break;
|
716 |
|
717 |
case (SHP.POLYGON2D):
|
718 |
case (SHP.POLYGON3D):
|
719 |
auxType = auxType | FShape.POLYGON; |
720 |
|
721 |
break;
|
722 |
case (SHP.MULTIPOINT2D):
|
723 |
case (SHP.MULTIPOINT3D):
|
724 |
auxType = auxType | FShape.MULTIPOINT; |
725 |
|
726 |
break;
|
727 |
} |
728 |
|
729 |
return auxType;
|
730 |
} |
731 |
/**
|
732 |
* @deprecated
|
733 |
* @return
|
734 |
*/
|
735 |
private IExtent getFullExtent() {
|
736 |
return extent;
|
737 |
} |
738 |
/**
|
739 |
* @deprecated
|
740 |
* @return
|
741 |
*/
|
742 |
private String getSRS() { |
743 |
// TODO Auto-generated method stub
|
744 |
return null; |
745 |
} |
746 |
} |