// This program is modified from The Virtual Slaithwaite // http://www.ccg.leeds.ac.uk/geotools/demos/village/ // This code largely culled from James MacGill's 'A simple shaded shapefile viewer' import java.awt.*; import java.applet.*; import java.net.*; import java.awt.event.*; import java.io.*; import uk.ac.leeds.ccg.geotools.*; import uk.ac.leeds.ccg.widgets.*; /** * This applet displays a set of shape format files, allows you to * pan and zoom the image, and pulls attribute information from dbf files * of attributes associated with the polygons in the shape files (the * attributes and polygons must be in the same order in both files).
* * The applet is largely culled from James MacGill's * A simple shaded shapefile viewer
* * The files and coloring are determined by PARAM tags in the applets HTML file.
*
* You need to tell the program a few general details:
* 1) The number of layers you want (starting counting at one).
* 2) The target script to throw the attribute data to (defaults to not sending the info on).
* 3) The name of the frame associated with the script (defaults to _top).
* 4) Whether you want to pass on default information from areas with no associated information (defaults to "true")
* 5) The default info, with spaces replaced by plus symbols (defaults to nothing, NOT an empty space)
*
* PARAM Name="NumOfLayers" Value="6"
* PARAM Name="targetScript" Value="cvtheader.pl"
* PARAM Name="targetFrame" Value="header"
* PARAM Name="processDefaults" Value="true" (or "false" - note case)
* PARAM Name="defaultString" Value="Open+space"
* * The you can give the details for as many files as you like, by * repeating the following PARAM tags, but replacing the 'X' with * the layer number, starting counting at zero (numbers must be * consecutive).
*
* PARAM Name="shapefileX" Value="buildings"
* PARAM Name="shadeByX" Value="use"
* PARAM Name="labelByX" Value="name"
* PARAM Name="lutX" Value="buildings.key"
* PARAM Name="lightX" Value="false" (or "true" - note case)
* * The shapefileX param is the name of the .shp and .dbf file * (the program adds the extensions). The shadeByX value is the * first value (the 'title' if you like) in the column of attributes * you want to shade by, and similarly for the labelByX and attribute * data column. The lightX value lets you turn off the highlight and * search routines (defaults to "true"/on). The lutX value is the * filename for a .key text file of the format...
* * integer,'#XXXXXX','discription'
* * where the quote marks are necessary, the integer represents the value * looked for in the shadeByX column of the .dbf file, the discription * will at some point be used for a key, and #XXXXXX is a hexidecimal * color code.
* * To use this applet you also need a copy of GeoTools * @author: Leeds University Centre for Computational Geography. Email geoaje@leeds.ac.uk (Andy Evans)
*/ public class cu_map extends Applet { // Geotool objects Viewer view = new Viewer(); Viewer nav = new Viewer(); Frame navFrame; ShapefileReader loader; Theme theme[]; HighlightManager high[]; GeoLabel label; boolean visible = true; // PARAM objects int NumOfLayers; String targetScript; String targetFrame; String processDefaults; String defaultString; // Progress box Progress info; /** * Sets up the GeoTools viewer and puts up the maps based on the parameters
*/ public void init() { // PARAM objects String shadeBy; String labelBy; String file; String lut; String none; String missing; // Geotool objects Shader shader; repaint(); URL base = getCodeBase(); // Set up progress box. Frame f = new Frame (); info = new Progress (f, false); info.setTitle("Progress"); Label status = new Label (" Starting "); info.add(status, "West"); info.show(); status.setForeground(Color.blue); // Setup look. setBackground(Color.white); setLayout(new BorderLayout()); view.setBackground(Color.white); // Get some PARAMs. NumOfLayers = Integer.parseInt(getParameter("NumOfLayers")); targetScript = getParameter("targetScript"); targetFrame = getParameter("targetFrame"); processDefaults = getParameter("processDefaults"); defaultString = getParameter("defaultString"); // If target unscripted, don't bother doing anything with clicks. if (targetFrame == null) targetFrame = "_top"; // Setup layout of controls. Panel main = new Panel(); main.setLayout(new BorderLayout()); add(main,"Center"); //view.setSize(500,500); main.add(view,"Center"); Panel controls = new Panel(); ToolBar toolbar = new ToolBar(view); controls.add(toolbar); //add(controls,"South"); // Setup layout of keys panel/ Panel tools = new Panel(); Panel keys = new Panel(); tools.setLayout(new BorderLayout()); keys.setLayout(new GridBagLayout()); //keys.setBackground(Color.blue); ScrollPane sp = new ScrollPane(); sp.add(keys); tools.add(sp,"Center"); nav.setSize(200,100); tools.add(nav,"North"); add(tools,"East"); tools.add(controls,"South"); add(view.getThemePanel(),"West"); GridBagConstraints c = new GridBagConstraints(); c.fill = c.BOTH; c.insets = new Insets(0,0,0,0); c.gridx = 0; c.weightx = 0; c.weighty = 0; c.fill = c.BOTH; nav.setToolMode(nav.NAVIGATE); nav.setNavigationTarget(view); // Setup GeoLabel. label = new GeoLabel(); label.setForeground(Color.red); // Hide it if we don't need to see it. if (!((getParameter("showLabel")).equals("false"))) main.add(label,"North"); main.doLayout(); // Setup holder for themes and max possible HighLightManagers. theme = new Theme[NumOfLayers]; high = new HighlightManager[NumOfLayers]; // Loop through number of layers. for (int i = 0; i < NumOfLayers; i++) { // Get PARAMs for layer. shadeBy = getParameter("shadeBy" + i); labelBy = getParameter("labelBy" + i); file = getParameter("shapefile" + i); lut = getParameter("lut" + i); none = getParameter("missingColor" + i); missing = getParameter("missingValue" + i); // Update progress box. status.setText(" Loading " + file + " data"); status.validate(); Color missingColor = (none == null) ? null : Color.decode(none); int missingValue = (missing == null) ? -9999 : (new Integer(missing)).intValue(); try { loader = new ShapefileReader(new URL(base,file));//jarFileLoader(file); } catch (Exception e) {System.out.println(base + file + ".shp");} shader = null; try{ shader = new DiscreteShader(new URL(base,lut)); } catch(IOException e){ System.err.println("Unable to load lutfile "+ lut +": switching to mono shade of grey"); shader = new MonoShader(Color.gray); } shader.setMissingValueCode(missingValue); if(missingColor != null){shader.setMissingValueColor(missingColor);} //above defaults to white if not specifed. //add key to key panel Key key = shader.getKey(); keys.add(key,c); tools.validate(); System.out.println("LUT SIZE = "+key.getPreferredSize()); // Add HighLightManager if we want to layer searchable (Could ideally // do with keeping the HighLightManager and turning off the visual effect) if (!(getParameter("light" + i).equals("false"))) { high[i] = new HighlightManager(); theme[i] = loader.getTheme(shader,shadeBy); theme[i].setHighlightManager(high[i]); theme[i].setTipData(loader.readData(labelBy)); label.addHighlightManager(high[i],loader.readData(labelBy)); } else { theme[i] = loader.getTheme(shader,shadeBy); } /*Key key = shader.getKey(); keys.add(key,c);*/ } // Get rid of the progress box. info.setVisible(false); } /** * For some reason, Netscape refuses to put up GeoTool Themes in the init() method, * but will do it in start(), so this is where we do it. Weird. * Seems to do it sometimes, and not for others.
*/ public void start(){ for (int j = 0; j < NumOfLayers; j++) { view.addTheme(theme[j]); nav.addTheme(theme[j]); } } /** * This class resizes and positions the little progress box * that is thrown up while files are loading.
*/ class Progress extends Dialog { Progress (Frame parent, boolean bool) { super(parent, bool); setSize (300, 100); setBounds(300,300,300,100); } } }