📄 spatialindex.java
字号:
} } /** * 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: Debug.error("Unable to index shape type NULL"); break; case SHAPE_TYPE_POINT: indexPoints(shp, SHAPE_FILE_HEADER_LENGTH, ssx); break; case SHAPE_TYPE_ARC: // case SHAPE_TYPE_POLYLINE: indexPolygons(shp, SHAPE_FILE_HEADER_LENGTH, ssx); break; case SHAPE_TYPE_POLYGON: indexPolygons(shp, SHAPE_FILE_HEADER_LENGTH, ssx); break; case SHAPE_TYPE_MULTIPOINT: Debug.error("Shapefile SpatialIndex: Unable to index shape type MULTIPOINT"); 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; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -