spatialindex.java
来自「OpenMap是一个基于JavaBeansTM的开发工具包。利用OpenMap你」· Java 代码 · 共 1,278 行 · 第 1/3 页
JAVA
1,278 行
if (shapeType != SHAPE_TYPE_NULL) { 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) { } } } /** * Writes the spatial index for a null 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 indexNulls(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/* , recNumber */; 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; /* 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 = 0; y = 0; 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) { } } } /** * Creates a spatial index for a shape file. Reads the records from the * shape file, writing appropriate index records to the spatial index file. * * @param inFile the shape file * @param outFile the spatial index file */ public static void createIndex(String inFile, String outFile) { byte fileHeader[] = new byte[SHAPE_FILE_HEADER_LENGTH]; FileInputStream shp = null; FileOutputStream ssx = null; int shapeType; try { shp = new FileInputStream(inFile); ssx = new FileOutputStream(outFile); shp.read(fileHeader, 0, SHAPE_FILE_HEADER_LENGTH); ssx.write(fileHeader, 0, SHAPE_FILE_HEADER_LENGTH); shapeType = readLEInt(fileHeader, 32); switch (shapeType) { case SHAPE_TYPE_NULL: indexNulls(shp, SHAPE_FILE_HEADER_LENGTH, ssx); break; case SHAPE_TYPE_POINT: case SHAPE_TYPE_POINTZ: case SHAPE_TYPE_POINTM: indexPoints(shp, SHAPE_FILE_HEADER_LENGTH, ssx); break; case SHAPE_TYPE_MULTIPOINT: case SHAPE_TYPE_MULTIPOINTZ: case SHAPE_TYPE_MULTIPOINTM: // case SHAPE_TYPE_ARC: case SHAPE_TYPE_POLYLINE: case SHAPE_TYPE_POLYLINEZ: case SHAPE_TYPE_POLYLINEM: case SHAPE_TYPE_POLYGON: case SHAPE_TYPE_POLYGONZ: case SHAPE_TYPE_POLYGONM: indexPolygons(shp, SHAPE_FILE_HEADER_LENGTH, ssx); break; default: Debug.error("Shapefile SpatialIndex.createIndex: Unknown shape type: " + shapeType); } } catch (java.io.IOException e) { e.printStackTrace(); } finally { try { shp.close(); ssx.close(); } catch (java.io.IOException e) { } } } /** * Prints a usage statement describing how to use this class from the * command line. * * @param out The output stream to use for output */ public static void printUsage(PrintStream out) { String className = SpatialIndex.class.getName(); out.println("Usage:"); out.println(); out.println("java " + className + " -c file.ssx file.shp"); out.println("Creates spatial index <file.ssx> from " + "shape file <file.shp>."); out.println(); out.println("java " + className + " -d file.ssx"); out.println("Dumps spatial index information, excluding " + "bounding boxes to stdout. Useful for " + "comparing to a shape index."); out.println(); out.println("java " + className + " -d -b file.ssx"); out.println("Dumps spatial index information including " + "bounding boxes to stdout."); out.println(); } /** * Locate file 'fileName' in classpath, if it is not an absolute file name. * * @return absolute name of the file as a string if found, null otherwise. */ public static String locateFile(String name) { File file = new File(name); if (file.exists()) { return name; } else { java.net.URL url = ClassLoader.getSystemResource(name); // OK, now we want to look around for the file, in the // classpaths, and as a resource. It may be a file in // a classpath, available for direct access. if (url != null) { String newname = url.getFile(); file = new File(newname); if (file.exists()) { return newname; } } } return null; } /** * Create a SpatialIndex object with just a shape file name. If the shape * file is local, this method will attempt to build the spatial index file * and place it next to the shape file. */ public static SpatialIndex locateAndSetShapeData(String shapeFileName) { SpatialIndex spi = null; int appendixIndex = shapeFileName.indexOf(".shp"); String spatialIndexFileName, newShapeFileName, newSpatialIndexFileName; if (Debug.debugging("shape")) { Debug.output("SpatialIndex: created with just the shape file " + shapeFileName); } if (appendixIndex != -1) { if (BinaryFile.exists(shapeFileName)) { // OK, the shape files exists - now look for spatial // index file next to it. spatialIndexFileName = shapeFileName.substring(0, appendixIndex) + ".ssx"; // Now, see if the spatialIndexFileName exists, and if // not, create it. if (Debug.debugging("shape")) { Debug.output("Trying to locate spatial index file " + spatialIndexFileName); } if (!BinaryFile.exists(spatialIndexFileName)) { // OK, the spatial index doesn't exist, but if the // shape file is local, we have a shot at creating // it. newShapeFileName = locateFile(shapeFileName); if (newShapeFileName != null) { // It's Local!! Debug.output("Creating spatial index file: " + spatialIndexFileName); appendixIndex = newShapeFileName.indexOf(".shp"); newSpatialIndexFileName = newShapeFileName.substring(0, appendixIndex) + ".ssx"; SpatialIndex.createIndex(newShapeFileName, newSpatialIndexFileName); } else { Debug.error("Can't create SpatialIndex for URL/JAR shapefile: " + shapeFileName); } } try { spi = new SpatialIndex(spatialIndexFileName, shapeFileName); } catch (java.io.IOException ioe) { Debug.error(ioe.getMessage()); ioe.printStackTrace(Debug.getErrorStream()); spi = null; } } else { Debug.error("SpatialIndex: Couldn't locate shape file " + shapeFileName); } } else { if (Debug.debugging("shape")) { Debug.output("SpatialIndex: file " + shapeFileName + " doesn't look like a shape file"); } } return spi; } public static SpatialIndex locateAndSetShapeData(String shapeFileName, String spatialIndexFileName) { SpatialIndex spi = null; String message = "ShapeLayer SpatialIndex: problem setting up the shape files:\n shape file: " + shapeFileName + "\n spatial index file: " + spatialIndexFileName; try { if (BinaryFile.exists(shapeFileName) && BinaryFile.exists(spatialIndexFileName)) { spi = new SpatialIndex(spatialIndexFileName, shapeFileName); } else { Debug.error(message); } } catch (java.io.IOException ioe) { Debug.error(message + "\n" + ioe.getMessage()); ioe.printStackTrace(Debug.getErrorStream()); } return spi; } /** * The driver for the command line interface. Reads the command line * arguments and executes appropriate calls. * <p> * See the file documentation for usage. * * @param argv the command line arguments * @exception IOException if something goes wrong reading or writing the * file */ public static void main(String argv[]) throws IOException { int argc = argv.length; if (argc == 0) { // No arguments, give the user some help printUsage(System.out); System.exit(0); } if (argv[0].equals("-d")) { if (argc == 2) { String name = argv[1]; SpatialIndex si = new SpatialIndex(name); si.dumpIndex(false); } else if ((argc == 3) && (argv[1].equals("-b"))) { String name = argv[2]; SpatialIndex si = new SpatialIndex(name); si.dumpIndex(true); } else { printUsage(System.err); System.exit(1); } } else if ((argc == 3) && argv[0].equals("-c")) { String indexFile = argv[1]; String shapeFile = argv[2]; SpatialIndex.createIndex(shapeFile, indexFile); } else { printUsage(System.err); System.exit(1); } } /** * Set the icon to use for point objects, in general. * * @param ii ImageIcon to use for icon. */ public synchronized void setPointIcon(ImageIcon ii) { pointIcon = ii; } /** * Get the icon used for general point objects. * * @return ImageIcon, null if not set. */ public synchronized ImageIcon getPointIcon() { return pointIcon; } /* * (non-Javadoc) * * @see com.bbn.openmap.io.Closable#close(boolean) */ public boolean close(boolean done) { try { if (shp != null) { shp.close(); } if (ssx != null) { ssx.close(); } return true; } catch (IOException ioe) { } return false; } public static class Entry { double xMin; double yMin; double xMax; double yMax; int byteOffset; public Entry(double xMin, double yMin, double xMax, double yMax, int byteOffset) { this.xMin = xMin; this.yMin = yMin; this.xMax = xMax; this.yMax = yMax; this.byteOffset = byteOffset; } public boolean intersects(double xmin, double ymin, double xmax, double ymax) { return SpatialIndex.intersects(xmin, ymin, xmax, ymax, xMin, yMin, xMax, yMax); } public int getByteOffset() { return byteOffset; } public void addToBounds(ESRIBoundingBox bounds) { bounds.addPoint(xMin, yMin); bounds.addPoint(xMax, yMax); } } public DbfHandler getDbf() { return dbf; } public void setDbf(DbfHandler dbf) { this.dbf = dbf; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?