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