if (shpFileName == null) {
return null;
}
BinaryBufferedFile ssx = new BinaryBufferedFile(ssx(shpFileName));
if (shp == null) {
shp = new BinaryBufferedFile(shpFileName);
}
// Need to figure out what the shape type is...
ssx.seek(32);
// int shapeType = readLEInt(ssx);
// /
ssx.byteOrder(false);
int shapeType = ssx.readInteger();
// /
ssx.seek(100); // skip the file header
while (true) {
int result = ssx.read(ixRecord, 0, SPATIAL_INDEX_RECORD_LENGTH);
// if (result == -1) {
if (result <= 0) {
break;// EOF
} else {
recNum++;
double xmin2 = readLEDouble(ixRecord, 8);
double ymin2 = readLEDouble(ixRecord, 16);
double xmax2 = readLEDouble(ixRecord, 24);
double ymax2 = readLEDouble(ixRecord, 32);
if (Debug.debugging("spatialindexdetail")) {
Debug.output("Looking at rec num " + recNum);
Debug.output(" " + xmin2 + ", " + ymin2 + "\n " + xmax2
+ ", " + ymax2);
}
if (gatherBounds) {
bounds.addPoint(xmin2, ymin2);
bounds.addPoint(xmax2, ymax2);
}
if (intersects(xmin,
ymin,
xmax,
ymax,
xmin2,
ymin2,
xmax2,
ymax2)) {
int offset = readBEInt(ixRecord, 0);
int byteOffset = offset * 2;
int contentLength = readBEInt(ixRecord, 4);
int recordSize = (contentLength * 2) + 8;
// System.out.print(".");
// System.out.flush();
if (recordSize < 0) {
Debug.error("SpatialIndex: supposed to read record size of "
+ recordSize);
break;
}
if (recordSize > sRecordSize) {
sRecordSize = recordSize;
if (Debug.debugging("spatialindexdetail")) {
Debug.output("Shapefile SpatialIndex record size: "
+ sRecordSize);
}
sRecord = new byte[sRecordSize];
}
if (Debug.debugging("spatialindex")) {
Debug.output("going to shp byteOffset = " + byteOffset
+ " for record size = " + recordSize
+ ", offset = " + offset + ", shape type = "
+ shapeType);
}
try {
shp.seek(byteOffset);
int nBytes = shp.read(sRecord, 0, recordSize);
if (nBytes < recordSize) {
Debug.error("Shapefile SpatialIndex expected "
+ recordSize + " bytes, but got " + nBytes
+ " bytes instead.");
}
ESRIRecord record = makeESRIRecord(shapeType,
sRecord,
0);
v.addElement(record);
} catch (IOException ioe) {
Debug.error("SpatialIndex.locateRecords: IOException. ");
ioe.printStackTrace();
break;
}
}
}
}
if (Debug.debugging("spatialindex")) {
Debug.output("Processed " + recNum + " records");
Debug.output("Selected " + v.size() + " records");
}
int nRecords = v.size();
ssx.close();
shp.close();
shp = null;
ESRIRecord result[] = new ESRIRecord[nRecords];
v.copyInto(result);
return result;