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