📄 spatialindex.java
字号:
// 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.seek(0); shp.seek(0); ESRIRecord result[] = new ESRIRecord[nRecords]; v.copyInto(result); return result; } /** * Determines if two rectangles intersect. Actually, this method * determines if two rectangles don't intersect, and then returns * a negation of that result. But the bottom line is the same. * * @param xmin1 the small x of rectangle 1 * @param ymin1 the small y of rectangle 1 * @param xmax1 the big x of rectangle 1 * @param ymax1 the big y of rectangle 1 * @param xmin2 the small x of rectangle 2 * @param ymin2 the small y of rectangle 2 * @param xmax2 the big x of rectangle 2 * @param ymax2 the big y of rectangle 2 * @return <code>true</code> if the rectangles intersect, * <code>false</code> if they do not */ protected static final boolean intersects(double xmin1, double ymin1, double xmax1, double ymax1, double xmin2, double ymin2, double xmax2, double ymax2) { return !((xmax1 <= xmin2) || (ymax1 <= ymin2) || (xmin1 >= xmax2) || (ymin1 >= ymax2)); } /** * Displays the contents of this index. * * @param showBounds true to show bounding box, false to skip it * @exception IOException if something goes wrong reading the file */ public void dumpIndex(boolean showBounds) throws IOException { byte ixRecord[] = new byte[SPATIAL_INDEX_RECORD_LENGTH]; int recNum = 0; 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) { // Debug.output("Processed " + recNum + " records"); break;//EOF } else { recNum++; int offset = readBEInt(ixRecord, 0); int length = readBEInt(ixRecord, 4); Debug.output("Record " + recNum + ": " + offset + ", " + length + (showBounds ? ("; " + readLEDouble(ixRecord, 8) + ", " + readLEDouble(ixRecord, 16) + ", " + readLEDouble(ixRecord, 24) + ", " + readLEDouble(ixRecord, 32)) : "")); } } } /** * Writes the spatial index for a polygon shape file. * * @param is the shape file input stream * @param ptr the current position in the file * @param os the spatial index file output stream */ protected static void indexPolygons(InputStream is, long ptr, OutputStream os) { boolean moreRecords = true; byte rHdr[] = new byte[SHAPE_RECORD_HEADER_LENGTH]; byte outBuf[] = new byte[SPATIAL_INDEX_RECORD_LENGTH]; int result; int nRecords = 0; int recLengthWords, recLengthBytes; long recOffset; int recBufSize = 100000; byte recBuf[] = new byte[recBufSize]; ESRIBoundingBox polyBounds; try { while (moreRecords) { result = is.read(rHdr, 0, SHAPE_RECORD_HEADER_LENGTH); if (result < 0) { moreRecords = false; Debug.output("Shapefile SpatialIndex Found " + nRecords + " records"); Debug.output("Shapefile SpatialIndex recBufSize = " + recBufSize); } else { nRecords++; recOffset = ptr; /*int recNumber = */readBEInt(rHdr, 0); recLengthWords = readBEInt(rHdr, 4); recLengthBytes = recLengthWords * 2; if (recLengthBytes > recBufSize) { Debug.output("Shapefile SpatialIndex increasing recBufSize to " + recLengthBytes); recBufSize = recLengthBytes; recBuf = new byte[recBufSize]; } result = is.read(recBuf, 0, recLengthBytes); polyBounds = readBox(recBuf, 4); ptr += recLengthBytes + 8; writeBEInt(outBuf, 0, (int) (recOffset / 2)); writeBEInt(outBuf, 4, recLengthWords); writeLEDouble(outBuf, 8, polyBounds.min.x); writeLEDouble(outBuf, 16, polyBounds.min.y); writeLEDouble(outBuf, 24, polyBounds.max.x); writeLEDouble(outBuf, 32, polyBounds.max.y); os.write(outBuf, 0, SPATIAL_INDEX_RECORD_LENGTH); } } } catch (java.io.IOException e) { e.printStackTrace(); } finally { try { is.close(); } catch (java.io.IOException e) { } } } /** * Writes the spatial index for a point shape file. * * @param is the shape file input stream * @param ptr the current position in the file * @param os the spatial index file output stream */ protected static void indexPoints(InputStream is, long ptr, OutputStream os) { boolean moreRecords = true; byte rHdr[] = new byte[SHAPE_RECORD_HEADER_LENGTH]; byte outBuf[] = new byte[SPATIAL_INDEX_RECORD_LENGTH]; int result; int nRecords = 0; int recLengthWords, recLengthBytes; long recOffset; int recBufSize = 20; byte recBuf[] = new byte[recBufSize]; double x; double y; try { while (moreRecords) { result = is.read(rHdr, 0, SHAPE_RECORD_HEADER_LENGTH); if (result < 0) { moreRecords = false; Debug.output("Found " + nRecords + " records"); Debug.output("recBufSize = " + recBufSize); } else { nRecords++; recOffset = ptr; /* int recNumber = */readBEInt(rHdr, 0); recLengthWords = readBEInt(rHdr, 4); recLengthBytes = recLengthWords * 2; if (recLengthBytes > recBufSize) { Debug.output("Shapefile SpatialIndex increasing recBufSize to " + recLengthBytes); recBufSize = recLengthBytes; recBuf = new byte[recBufSize]; } result = is.read(recBuf, 0, recLengthBytes); x = readLEDouble(recBuf, 4); y = readLEDouble(recBuf, 12); ptr += recLengthBytes + 8; writeBEInt(outBuf, 0, (int) (recOffset / 2)); writeBEInt(outBuf, 4, recLengthWords); writeLEDouble(outBuf, 8, x); writeLEDouble(outBuf, 16, y); writeLEDouble(outBuf, 24, x); writeLEDouble(outBuf, 32, y); os.write(outBuf, 0, SPATIAL_INDEX_RECORD_LENGTH); } } } catch (java.io.IOException e) { e.printStackTrace(); } finally { try { is.close(); } catch (java.io.IOException e) { }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -