📄 shapelayer.java
字号:
// **********************************************************************// // <copyright>// // BBN Technologies// 10 Moulton Street// Cambridge, MA 02138// (617) 873-8000// // Copyright (C) BBNT Solutions LLC. All rights reserved.// // </copyright>// **********************************************************************// // $Source: /cvs/distapps/openmap/src/openmap/com/bbn/openmap/layer/shape/ShapeLayer.java,v $// $RCSfile: ShapeLayer.java,v $// $Revision: 1.12.2.10 $// $Date: 2007/06/21 21:41:39 $// $Author: dietrick $// // **********************************************************************package com.bbn.openmap.layer.shape;import java.awt.Component;import java.awt.Container;import java.awt.Graphics;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.IOException;import java.io.InterruptedIOException;import java.net.MalformedURLException;import java.net.URL;import java.util.Properties;import javax.swing.BoxLayout;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JPanel;import com.bbn.openmap.LatLonPoint;import com.bbn.openmap.PropertyConsumer;import com.bbn.openmap.dataAccess.shape.DbfHandler;import com.bbn.openmap.io.BinaryBufferedFile;import com.bbn.openmap.io.BinaryFile;import com.bbn.openmap.io.FormatException;import com.bbn.openmap.layer.OMGraphicHandlerLayer;import com.bbn.openmap.omGraphics.DrawingAttributes;import com.bbn.openmap.omGraphics.OMGraphic;import com.bbn.openmap.omGraphics.OMGraphicList;import com.bbn.openmap.proj.ProjMath;import com.bbn.openmap.proj.Projection;import com.bbn.openmap.proj.coords.GeoCoordTransformation;import com.bbn.openmap.util.ComponentFactory;import com.bbn.openmap.util.DataBounds;import com.bbn.openmap.util.DataBoundsProvider;import com.bbn.openmap.util.Debug;import com.bbn.openmap.util.PropUtils;/** * An OpenMap Layer that displays shape files. Note that the ESRIRecords have * been updated so that the OMGraphics that get created from them are loaded * with an Integer object that notes the number of the record as it was read * from the .shp file. This lets you align the object with the correct attribute * data in the .dbf file. * <p> * <code><pre> * * * * * ############################ * # Properties for a shape layer * shapeLayer.class=com.bbn.openmap.layer.shape.ShapeLayer * shapeLayer.prettyName=Name_for_Menu * shapeLayer.shapeFile=&ltpath to shapefile (.shp)&gt * shapeLayer.spatialIndex=&ltpath to generated spatial index file (.ssx)&gt * shapeLayer.lineColor=ff000000 * shapeLayer.fillColor=ff000000 * # plus any other properties used by the DrawingAttributes object. * shapeLayer.pointImageURL=&ltURL for image to use for point objects&gt * ############################ * * * * * </pre></code> * * @author Tom Mitchell <tmitchell@bbn.com> * @version $Revision: 1.12.2.10 $ $Date: 2007/06/21 21:41:39 $ * @see SpatialIndex */public class ShapeLayer extends OMGraphicHandlerLayer implements ActionListener, DataBoundsProvider { /** The name of the property that holds the name of the shape file. */ public final static String shapeFileProperty = "shapeFile"; /** * The name of the property that holds the name of the spatial index file. */ public final static String spatialIndexProperty = "spatialIndex"; /** The URL of an image to use for point objects. */ public final static String pointImageURLProperty = "pointImageURL"; // Note that shadows are really in the eye of the beholder // The X,Y shadow offset just pushes the resulting picture in the // direction of the offset and draws it there. By setting the // fill and line colors, you make it seem shadowy. By drawing // a layer as a shadow, and then again as a regular layer, you // get the proper effect. /** The name of the property that holds the offset of the shadow. */ public final static String shadowXProperty = "shadowX"; public final static String shadowYProperty = "shadowY"; /** * The name of the class providing translation services if needed. */ public final static String TransformProperty = "transform"; /** * The holders of the shadow offset. ** */ protected int shadowX = 0; protected int shadowY = 0; /** The spatial index of the shape file to be rendered. */ protected SpatialIndex spatialIndex; /** * The DrawingAttributes object to describe the rendering of graphics. */ protected DrawingAttributes drawingAttributes; /** * A translator for converting pre-projected coordinates from the file into * decimal degree lat/lon. */ protected GeoCoordTransformation coordTransform; // For writing out to properties file later. String shapeFileName = null; String spatialIndexFileName = null; String imageURLString = null; /** * Initializes an empty shape layer. */ public ShapeLayer() { setProjectionChangePolicy(new com.bbn.openmap.layer.policy.ListResetPCPolicy(this)); } public ShapeLayer(String shapeFileName) { this(); spatialIndex = SpatialIndex.locateAndSetShapeData(shapeFileName); } public void setSpatialIndex(SpatialIndex si) { spatialIndex = si; } public SpatialIndex getSpatialIndex() { return spatialIndex; } public GeoCoordTransformation getCoordTransform() { return coordTransform; } public void setCoordTransform(GeoCoordTransformation coordTranslator) { this.coordTransform = coordTranslator; } /** * This method gets called from setProperties. * * @param realPrefix This prefix has already been scoped, which means it is * an empty string if setProperties was called with a null prefix, or * it's a String ending with a period if it was defined with * characters. * @param props Properties containing information about files and the layer. */ protected void setFileProperties(String realPrefix, Properties props) { shapeFileName = props.getProperty(realPrefix + shapeFileProperty); spatialIndexFileName = props.getProperty(realPrefix + spatialIndexProperty); if (shapeFileName != null && !shapeFileName.equals("")) { SpatialIndex spatialIndex; if (spatialIndexFileName != null && !spatialIndexFileName.equals("")) { spatialIndex = SpatialIndex.locateAndSetShapeData(shapeFileName, spatialIndexFileName); } else { spatialIndex = SpatialIndex.locateAndSetShapeData(shapeFileName); } String dbfFileName = shapeFileName.substring(0, shapeFileName.indexOf(".shp")) + ".dbf"; try { if (BinaryFile.exists(dbfFileName)) { BinaryBufferedFile bbf = new BinaryBufferedFile(dbfFileName); DbfHandler dbfh = new DbfHandler(bbf); dbfh.setProperties(realPrefix, props); spatialIndex.setDbf(dbfh); } } catch (FormatException fe) { if (Debug.debugging("shape")) { Debug.error("ShapeLayer: Couldn't create DBF handler for " + dbfFileName + ", FormatException: " + fe.getMessage()); } } catch (IOException ioe) { if (Debug.debugging("shape")) { Debug.error("ShapeLayer: Couldn't create DBF handler for " + dbfFileName + ", IOException: " + ioe.getMessage()); } } imageURLString = props.getProperty(realPrefix + pointImageURLProperty); try { if (imageURLString != null && !imageURLString.equals("")) { URL imageURL = PropUtils.getResourceOrFileOrURL(this, imageURLString); ImageIcon imageIcon = new ImageIcon(imageURL); spatialIndex.setPointIcon(imageIcon); } } catch (MalformedURLException murle) { Debug.error("ShapeLayer.setFileProperties: point image URL not so good: \n\t" + imageURLString); } catch (NullPointerException npe) { // May happen if not connected to the internet. fireRequestMessage("Can't access icon image: \n " + imageURLString); } setSpatialIndex(spatialIndex); } else { Debug.error("One of the following properties was null or empty:"); Debug.error("\t" + realPrefix + shapeFileProperty); Debug.error("\t" + realPrefix + spatialIndexProperty); } } /** * Initializes this layer from the given properties. * * @param props the <code>Properties</code> holding settings for this * layer */ public void setProperties(String prefix, Properties props) { super.setProperties(prefix, props); String realPrefix = PropUtils.getScopedPropertyPrefix(this); setFileProperties(realPrefix, props); drawingAttributes = new DrawingAttributes(prefix, props); shadowX = PropUtils.intFromProperties(props, realPrefix + shadowXProperty, 0); shadowY = PropUtils.intFromProperties(props, realPrefix + shadowYProperty, 0); String transClassName = props.getProperty(realPrefix + TransformProperty); if (transClassName != null) { try { coordTransform = (GeoCoordTransformation) ComponentFactory.create(transClassName, realPrefix + TransformProperty, props); } catch (ClassCastException cce) { } } } /** * PropertyConsumer method. */ public Properties getProperties(Properties props) { props = super.getProperties(props); String prefix = PropUtils.getScopedPropertyPrefix(this); props.put(prefix + shapeFileProperty, (shapeFileName == null ? "" : shapeFileName)); props.put(prefix + spatialIndexProperty, (spatialIndexFileName == null ? "" : spatialIndexFileName)); props.put(prefix + pointImageURLProperty, (imageURLString == null ? "" : imageURLString)); props.put(prefix + shadowXProperty, Integer.toString(shadowX)); props.put(prefix + shadowYProperty, Integer.toString(shadowY)); if (drawingAttributes != null) { drawingAttributes.setPropertyPrefix(getPropertyPrefix()); drawingAttributes.getProperties(props); } else { DrawingAttributes da = (DrawingAttributes) DrawingAttributes.DEFAULT.clone(); da.setPropertyPrefix(getPropertyPrefix()); da.getProperties(props); } if (coordTransform != null && coordTransform instanceof PropertyConsumer) { ((PropertyConsumer) coordTransform).getProperties(props); } if (spatialIndex != null) { DbfHandler dbfh = spatialIndex.getDbf(); if (dbfh != null) { dbfh.getProperties(props); } } return props; } /** * Method to fill in a Properties object with values reflecting the
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -