📄 areahandler.java
字号:
* want to key in on a specific attribute that is a number, like the country * coloring code that ESRI has in the country file. We're going to assume * that if the number has an integer value, it shouldn't have decimal * places. That is, a 1.0 will be truncated to 1, because that makes more * sense in a data file where you are using a key as a factor. If the double * value doesn't match the integer value, though, we'll assume that's what * was meant and leave it alone. * <p> */ protected String createStringFromKeyObject(Object keyObj) { String key = null; if (keyObj instanceof String) { key = ((String) keyObj).toUpperCase().intern(); } else if (keyObj instanceof Number) { Number keyNum = (Number) keyObj; if (keyNum.doubleValue() == (double) keyNum.intValue()) { // Strips off empty decimal places, for sure key = Integer.toString(keyNum.intValue()).intern(); } else { key = Double.toString(keyNum.doubleValue()).intern(); } } else { try { key = keyObj.toString().toUpperCase().intern(); } catch (Exception e) { Debug.error("AreaHandler.createStringFromKeyObject: bad key object:" + keyObj); } } return key; } /** * Given the shapefile record number, find the drawing parameters that * should be used for the shape. Returns the default coloring if the key for * the drawing parameters isn't found. */ public DrawingAttributes getDrawParams(int recordNumber) { if (dbfModel != null) return getDrawParamsFromDBF(recordNumber); else return getDrawParamsFromCSV(recordNumber); } /** * This function takes an OMGraphicList and loads each one with the array * representing the records in the dbf file. Each graphics stores the * graphic in its object slot. */ public void loadDbfModelIntoGraphics(OMGraphicList list) { if (list != null && dbfModel.getRowCount() > 0) { int numgraphics = list.size(); for (int i = 0; i < numgraphics; i++) { try { OMGraphic omg = list.getOMGraphicAt(i); Integer recnum = (Integer) (omg.getAttribute(ShapeConstants.SHAPE_INDEX_ATTRIBUTE)); // OFF BY ONE!!! The shape record numbers // assigned to the records start with 1, while // everything else we do starts with 0... Object inforec = dbfModel.getRecord(recnum.intValue() - 1); omg.putAttribute(ShapeConstants.SHAPE_DBF_INFO_ATTRIBUTE, inforec); } catch (ClassCastException cce) { if (Debug.debugging("shape")) { cce.printStackTrace(); } } catch (NullPointerException npe) { npe.printStackTrace(); } } } } /** * Find a PoliticalArea named by the search key. If the shapefile is large, * the first query will take a little extra time on the first query to read * in the files. * * @param area_key the lookup key, of which the index for the column was * designated in the properties file. */ public PoliticalArea findPoliticalArea(String area_key) { // Right now, this method gathers all the graphics in the // shape file, groups them, and then returns the PoliticalArea // for the key. In the future, it would be nice to have the // option to actually search through the data file, find the // indexes of the graphics that go to the area, and assemble a // temporary list to pass back. if (politicalAreas == null) { Debug.message("areas", "AreaHandler: initializing graphic attributes"); initialize(originalPrefix, originalProperties); if (omgraphics == null) { omgraphics = getGraphics(); if (dbfModel != null) loadDbfModelIntoGraphics(omgraphics); else infoFile.loadIntoGraphics(omgraphics); } politicalAreas = determinePoliticalAreas(omgraphics); Debug.message("areas", "AreaHandler: completed initialization"); } if (politicalAreas != null) { String key = area_key.toUpperCase().intern(); // Just to // be sure. return (PoliticalArea) politicalAreas.get(key); } else { Debug.error("AreaHandler: initialization failed for " + originalPrefix + "\n\tNo data will be displayed"); return null; } } /** * Find the graphics that are represented by an search key. If the shapefile * is large, the first query will take a little extra time on the first * query to read in the files. * * @param area_key the lookup key, of which the index for the column was * designated in the properties file. */ public OMGeometryList findGraphics(String area_key) { PoliticalArea area = findPoliticalArea(area_key); if (area == null) { return null; } else { return area.getGeometry(); } } /** * DeterminePoliticalAreas goes over a list of omgraphics, and spits out a * hashtable that holds PoliticalArea objects for every area key. * * @param graphicList the list of graphics. The top level graphic entries on * the list represent areas. */ public Hashtable determinePoliticalAreas(OMGraphicList graphicList) { if (Debug.debugging("areas")) { Debug.output("AreaHandler: Determining political areas from OMGraphicList"); } Hashtable poli_areas = new Hashtable(); return determinePoliticalAreas(graphicList, poli_areas); } /** * DeterminePoliticalAreas goes over a list of omgraphics, and spits out a * hashtable that holds PoliticalArea objects for every area key. When an ID * is found in the graphics, it is checked in the hashtable for like * graphics, and added to that PoliticalArea if found. If not found, a new * PoliticalArea is created and placed in the Hashtable. This will duplicate * graphics if you call it more than once for the same graphic list. * * @param graphicList the list of graphics. The top level graphic entries on * the list represent areas. */ public Hashtable determinePoliticalAreas(OMGraphicList graphicList, Hashtable poli_areas) { // Simple case. No graphics means an empty list of regions. String name = null; String key = null; if (graphicList != null) { int size = graphicList.size(); for (int i = 0; i < size; i++) { OMGraphic graphic = graphicList.getOMGraphicAt(i); // below should be a vector like [ "Massachusetts", // "MA" ]; Object obj = graphic.getAttribute(ShapeConstants.SHAPE_DBF_INFO_ATTRIBUTE); if (obj == null) { if (Debug.debugging("verbose")) { Debug.error("AreaHandler: No attributes for graphic #" + i); } continue; } if (obj instanceof Vector) { Vector pair = (Vector) obj; name = (String) pair.elementAt(nameIndex); key = ((String) pair.elementAt(keyIndex)).toUpperCase() .intern(); if (Debug.debugging("areas")) { Debug.output("AreaHandler: looking at " + name + ", " + key); } } else if (obj instanceof String) { // Assume that the key is stored here, I guess. key = (String) obj; if (Debug.debugging("areas")) { Debug.output("AreaHandler: String app object, looking at " + key); } } else { if (Debug.debugging("verbose")) { Debug.output("AreaHandler: Unidentified app object type " + obj); } } // Get the political area object for this keyiation. PoliticalArea area = (PoliticalArea) poli_areas.get(key); if (area == null) { // key is not in table area = new PoliticalArea(name, key); poli_areas.put(key, area); // add it to the table // AreaDrawParams adp = // (AreaDrawParams)drawingParams.get(key); // if (adp != null) { // area.setDrawingAttributes(adp.drawingAttributes); // } } // Add the graphic to the list for this political // area. area.addGraphic(graphic); } if (Debug.debugging("areas")) { Debug.output("AreaHandler: Finished determinePoliticalAreas: " + poli_areas.size() + " areas defined."); } } return poli_areas; } protected Color getColor(String colorString) { Color result = Color.black; try { result = PropUtils.parseColor(colorString); } catch (NumberFormatException nfe) { result = GetColorFromString(colorString); } return result; } /** * This function would return a Color object for string such as red, * green,.. (all that are available from java.awt.color class). It can also * return a specific color represented by HEX or Octal number like * 0xffeeffee */ protected Color GetColorFromString(String token) { String tokstring = (String) token; Color result = Color.black; if (Debug.debugging("areas")) { Debug.output("AreaHandler: GetColorFromString(" + tokstring + ")"); } // Thank the heavens for Emacs macros! if (tokstring.equals("black")) result = Color.black; else if (tokstring.equals("blue")) result = Color.blue; else if (tokstring.equals("cyan")) result = Color.cyan; else if (tokstring.equals("darkGray")) result = Color.darkGray; else if (tokstring.equals("gray")) result = Color.gray; else if (tokstring.equals("green")) result = Color.green; else if (tokstring.equals("lightGray")) result = Color.lightGray; else if (tokstring.equals("magenta")) result = Color.magenta; else if (tokstring.equals("orange")) result = Color.orange; else if (tokstring.equals("pink")) result = Color.pink; else if (tokstring.equals("red")) result = Color.red; else if (tokstring.equals("white")) result = Color.white; else if (tokstring.equals("yellow")) result = Color.yellow; else // decode a hex color string. result = Color.decode(tokstring); if (Debug.debugging("areas")) { Debug.output("AreaHandler.GetColorFromToken returns (" + result + ")"); } return result; } public GeoCoordTransformation getCoordTransform() { return coordTransform; } public void setCoordTransform(GeoCoordTransformation dataTransform) { this.coordTransform = dataTransform; } /** * This main function basically reads in the data sources (the shape file * and the csv information file, and creates a serialized graphics file that * will act like a cache later. */ public static void main(String[] argv) { String propertiesFile = null; String prefix = null; String outputFile = null; Debug.init(); if (argv.length < 6) printUsage(); for (int i = 0; i < argv.length; i++) { if (argv[i].equalsIgnoreCase("-props")) { propertiesFile = argv[++i]; } else if (argv[i].equalsIgnoreCase("-prefix")) { prefix = argv[++i]; } else if (argv[i].equalsIgnoreCase("-file")) { outputFile = argv[++i]; } } if (propertiesFile == null || prefix == null || outputFile == null) { printUsage(); } try { Properties properties = new Properties(); // Read in the properties. URL propertiesURL = new URL(propertiesFile); InputStream is = propertiesURL.openStream(); properties.load(is); // Let's make a file ShapeLayer sl = new ShapeLayer(); sl.setProperties(prefix, properties); AreaHandler ah = new AreaHandler(sl.getSpatialIndex(), sl.getDrawingAttributes()); // Set the properties in the handler. ah.setProperties(prefix, properties); // Write the saved graphics. ah.getGraphics().writeGraphics(outputFile); } catch (java.net.MalformedURLException murle) { Debug.error("Bad URL for properties file : " + propertiesFile); printUsage(); } catch (java.io.IOException ioe) { Debug.error("IOException creating cached graphics file: " + outputFile); printUsage(); } } public static void printUsage() { Debug.output("Usage: java com.bbn.openmap.layer.shape.areas.AreaHandler -props <URL to properties file> -prefix <handler property prefix> -file <path to output file>"); System.exit(-1); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -