/************************************************************************ * This layer is for the reading and display of any spatial data retrieved * from Oracle 8i (8.1.6) that is stored using the object-relational method. * * NOTE: This layer requires the Oracle API's to be available (sdoapi.zip) * * Copyright 2000 Wrenfeld Pty Ltd * PO Box 1089, Belconnen ACT Australia 2616 * Phone +61 (0417) 211 411 * Fax +61 (02) 6287 3428 * * Author name: Ian Batley ianbatley@mypostbox.com * Version 1.0 * ************************************************************************/ package com.bbn.openmap.layer; /* Java Core */ import java.awt.Color; import java.awt.Graphics; import java.sql.*; import java.util.*; import javax.swing.ImageIcon; /* OpenMap */ import com.bbn.openmap.layer.OMGraphicHandlerLayer; import com.bbn.openmap.omGraphics.*; import com.bbn.openmap.proj.Projection; import com.bbn.openmap.util.Debug; import com.bbn.openmap.util.PropUtils; /* Oracle */ import oracle.jdbc.driver.*; import oracle.sdoapi.OraSpatialManager; import oracle.sdoapi.adapter.*; import oracle.sdoapi.geom.*; import oracle.sql.STRUCT; /** * This layer is for the reading and display of any spatial data retrieved * from Oracle 8i (8.1.6) that is stored using the object-relational method. *

* NOTE: This layer requires the Oracle API's to be available (sdoapi.zip) *

* Copyright © 2000 Wrenfeld Pty Ltd
* PO Box 1089, Belconnen ACT Australia 2616
* Phone +61 (0417) 211 411
* Fax +61 (02) 6287 3428
*

* @author Author name: Ian Batley ianbatley@mypostbox.com
* @version 1.0
*

*

  * ############################
* # Required Properties for a oracle spatial layer
* osl.class=scats.gui.displays.map.openMapImpl.OracleSpatialLayer
* osl.prettyName=<Your Layer Name>
* osl.userName=<Database Username>
* osl.userPassword=<Database Password>
* osl.geomTable=<Database Tablename>
* osl.geomColumn=<Column name which contains the geometry>
* # Optional Properties - use as required * # NOTE: There are default for each of these * osl.pointSymbol=<Path (or URL) to filename for image to use for point objects>
* osl.lineColor=<Color for lines>Default is red
* osl.lineWidth=<Pixel width of lines>Default is 0
* osl.fillColor=<Color of fill>Default is red
* ############################
*
*/ public class OracleSpatialLayer extends OMGraphicHandlerLayer { /** * NOTE: * Under my implementation * Oracle HOST, PORT and Database ID are hardcoded */ private static String m_host = "IP address"; private static String m_port = "Port number"; private static String m_sid = "Database Name"; protected String sdoFilter1 = " WHERE SDO_FILTER(Geom, " + "mdsys.sdo_geometry(2003,NULL,NULL, " + "mdsys.sdo_elem_info_array(1,1003,3), " + "mdsys.sdo_ordinate_array("; protected String sdoFilter2 = ")), 'querytype=WINDOW') = 'TRUE'"; /** User name to be used for connecing to DB. */ protected String userName = null; /** Property to specify userName for connecting to Database */ public static final String userNameProperty = "userName"; /** User password to be used for connecing to DB. */ protected String userPassword = null; /** Property to specify userPassword for connecting to Database */ public static final String userPasswordProperty = "userPassword"; /** Table name which contains the geometry to be used. */ protected String geomTable = null; /** Property to specify geomTable in the Database */ public static final String geomTableProperty = "geomTable"; /** Column name which contains the geometry to be used. */ protected String geomColumn = null; /** Property to specify geomColumn in the Database */ public static final String geomColumnProperty = "geomColumn"; /** Default GIF (symbol) to use for Points. */ protected String pointSymbol = "com/bbn/openmap/gui/center.gif"; /** Property to specify GIF (symbol) to use for Points. */ public static final String pointSymbolProperty = "pointSymbol"; /** * ImageIcon containing image for points. If null (no image * specified) OMPoint objects will be used. */ protected ImageIcon actualPointSymbol = null; /** * DrawingAttributes object to use for rendering database items. * The full suite of parameters are supported for modification in * the properties file. */ protected DrawingAttributes drawingAttributes = DrawingAttributes.getDefaultClone(); /** * The default constructor for the Layer. All of the attributes * are set to their default values. */ public OracleSpatialLayer() { setProjectionChangePolicy(new com.bbn.openmap.layer.policy.ListResetPCPolicy(this)); } /** * The properties and prefix are managed and decoded here, for * the standard uses of the OracleSpatialLayer. * * @param prefix string prefix used in the properties file for this layer. * @param properties the properties set in the properties file. */ public void setProperties(String prefix, Properties properties) { super.setProperties(prefix, properties); drawingAttributes.setProperties(prefix, properties); prefix = PropUtils.getScopedPropertyPrefix(prefix); userName = properties.getProperty(prefix+userNameProperty); userPassword = properties.getProperty(prefix+userPasswordProperty); geomTable = properties.getProperty(prefix+geomTableProperty); geomColumn = properties.getProperty(prefix+geomColumnProperty); pointSymbol = properties.getProperty(prefix+pointSymbolProperty); if (pointSymbol != null) { URL pointSymbolURL = PropUtils.getResourceOrFileOrURL(pointSymbol); if (pointSymbolURL != null) { actualPointSymbol = new ImageIcon(pointSymbol); } } } public Properties getProperties(Properties props) { props = super.getProperties(props); String prefix = PropUtils.getScopedPropertyPrefix(this); properties.put(prefix+userNameProperty, userName); properties.put(prefix+userPasswordProperty, userPassword); properties.put(prefix+geomTableProperty, geomTable); properties.put(prefix+geomColumnProperty, geomColumn); properties.put(prefix+pointSymbolProperty, pointSymbol); drawingAttributes.getProperties(props); return props; } public Properties getPropertyInfo(Properties props) { props = super.getPropertyInfo(props); String internString = i18n.get(OracleSpatialLayer.class, userNameProperty, I18n.TOOLTIP, "User name for database account"); properties.put(userNameProperty, internString); internString = i18n.get(OracleSpatialLayer.class, userPasswordProperty, I18n.TOOLTIP, "Password for database account"); properties.put(userPasswordProperty, internString); internString = i18n.get(OracleSpatialLayer.class, geomTableProperty, I18n.TOOLTIP, "Geometry table name in database"); properties.put(geomTableProperty, internString); internString = i18n.get(OracleSpatialLayer.class, geomColumnProperty, I18n.TOOLTIP, "Geometry column in table"); properties.put(geomColumnProperty, internString); internString = i18n.get(OracleSpatialLayer.class, pointSymbolProperty, I18n.TOOLTIP, "Path to point symbol icon (path or URL)"); properties.put(pointSymbolProperty, internString); internString = i18n.get(OracleSpatialLayer.class, userNameProperty, "User name"); properties.put(userNameProperty + LabelEditorProperty, internString); internString = i18n.get(OracleSpatialLayer.class, userPasswordProperty, "Password"); properties.put(userPasswordProperty + LabelEditorProperty, internString); internString = i18n.get(OracleSpatialLayer.class, geomTableProperty, "Geometry table name"); properties.put(geomTableProperty + LabelEditorProperty, internString); internString = i18n.get(OracleSpatialLayer.class, geomColumnProperty, "Geometry column"); properties.put(geomColumnProperty + LabelEditorProperty, internString); internString = i18n.get(OracleSpatialLayer.class, pointSymbolProperty, "Point Symbol"); properties.put(pointSymbolProperty + LabelEditorProperty, internString); properties.put(pointSymbolProperty + ScopedEditorProperty, "com.bbn.openmap.util.propertyEditor.FUPropertyEditor"); drawingAttributes.getPropertyInfo(props); return props; } public OMGraphicList prepare() { return getGeom(); } /** * Actually connects to the database and retrieves the data. */ protected OMGraphicList getGeom() { OMGraphicList list = new OMGraphicList(); Projection proj = getProjection(); try { Geometry geom = null; String database = "jdbc:oracle:thin:@" + m_host + ":" + m_port + ":" + m_sid; DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); OracleConnection conn = (OracleConnection)DriverManager.getConnection(database, userName, userPassword); conn.setDefaultRowPrefetch(1); GeometryAdapter sdoAdapter = OraSpatialManager.getGeometryAdapter("SDO", "8.1.6", STRUCT.class, null, null, conn); String query = "select " + geomColumn + " from " + geomTable + sdoFilter1 + getProjection().getUpperLeft().getLongitude() + "," + getProjection().getUpperLeft().getLatitude() + "," + getProjection().getLowerRight().getLongitude() + "," + getProjection().getLowerRight().getLatitude() + sdoFilter2; Statement stmt = conn.createStatement(); OracleResultSet ors = (OracleResultSet) stmt.executeQuery(query); while (ors.next()) { // read the Geometry from column 1 of the next row // column 1 should the the only column returned STRUCT dbObject = (STRUCT) ors.getObject(1); // do the conversion try { geom = sdoAdapter.importGeometry(dbObject); // STRUCT -> Geometry } catch(GeometryInputTypeNotSupportedException e) { System.out.println("Input Type not supported"); } catch(InvalidGeometryException e) { System.out.println("Invalid geometry"); } if (geom != null) { OMGraphic omg = createGraphic(geom); if (omg != null) { omg.generate(proj); list.add(omg); } } }//end while ors.close(); conn.close(); } catch(SQLException sqlE) { Debug.error("OracleSpatialLayer " + getName() + ":SQL Exception: " + sqlE.getMessage()); sqlE.printStackTrace(); } return list; } /** * Create the OMGraphic from a geometry object. */ protected OMGraphic createGraphic(Geometry geom) { OMGraphic ret = null; if (geom == null) { return ret; } if (geom instanceof Point) { // point Point point = (Point) geom; if (actionPointSymbol != null) { ret = new OMScalingIcon((float)point.getY(), (float)point.getX(), actualPointSymbol); } else { ret = new OMPoint((float)point.getY(), (float) point.getX()); drawingAttributes.setTo(ret); } } else if (geom instanceof LineString) {// linestring // Create the linestring graphics LineString lineString = (LineString) geom; CoordPoint[] coordArray = lineString.getPointArray(); float[] llArray = new float[coordArray.length * 2]; // re-arrange coordinates from long,lat to lat,long for (int i = 0; i < coordArray.length; i++) { if (i == 0) { llArray[i] = (float) coordArray[i].getY(); llArray[i + 1] = (float) coordArray[i].getX(); } else { llArray[i * 2] = (float) coordArray[i].getY(); llArray[i * 2 + 1] = (float) coordArray[i].getX(); } } // need to now create the OMGraphics ret = new OMPoly(llArray, OMGraphic.DECIMAL_DEGREES, OMGraphic.LINETYPE_STRAIGHT); drawingAttributes.setTo(ret); ret.setFillPaint(OMColor.clear); } else if (geom instanceof Polygon) {// Polygon // Create a linear polygon by iterating through all its parts Polygon polygon = (Polygon) geom; for (Enumeration e = polygon.getRings(); e.hasMoreElements();) { // Create the linestrings that make up the boundary LineString lineString = (LineString)e.nextElement(); CoordPoint[] coordArray = lineString.getPointArray(); float[] llArray = new float[coordArray.length * 2]; // re-arrange coordinates from long,lat to lat,long for (int i = 0; i < coordArray.length; i++) { if (i == 0) { llArray[i] = (float) coordArray[i].getY(); llArray[i + 1] = (float) coordArray[i].getX(); } else { llArray[i * 2] = (float) coordArray[i].getY(); llArray[i * 2 + 1] = (float) coordArray[i].getX(); } } // need to now create the OMGraphics ret = new OMPoly2D(llArray, OMGraphic.DECIMAL_DEGREES, OMGraphic.LINETYPE_STRAIGHT); drawingAttributes.setTo(ret); } } else if (geom instanceof GeometryCollection) { // Collection // Print a geometry collection by printing all its member geometries GeometryCollection geomColl = (GeometryCollection) geom; ret = new OMGraphicList(); for (Enumeration e = geomColl.getGeometries(); e.hasMoreElements();) { OMGraphic omg = createGraphic((Geometry)e.nextElement()); if (omg != null) { ((OMGraphicList)ret).add(omg); } } } else { // Other types of geometry Debug.output("OracleSpatialLayer " + getName() + ": Geometry type not supported"); } return ret; } // end of createGraphic } // end of class OracleSpatialLayer