/* * Title: CoordRefCreate.java 1.00 2002/05/14 * * Licensed Materials - Property of IBM and/or HCL * IBM Informix Spatial DataBlade * (c) Copyright IBM Corporation 2002 All rights reserved. * (c) Copyright HCL Technologies Ltd. 2017. All Rights Reserved. * * US Government Users Restricted Rights - Use, duplication or * disclosure restricted by GSA ADP Schedule Contract with * IBM Corp. */ import java.sql.*; import java.util.*; import com.ibm.spatial.geom.*; import com.ibm.spatial.srs.*; import com.ibm.spatial.*; public class CoordRefCreate { static String url = null; Connection conn = null; CoordRefManager crm = null; public CoordRefCreate() throws ClassNotFoundException { // Loading JDBC driver System.out.print("Loading JDBC driver..."); Class.forName("com.informix.jdbc.IfxDriver"); System.out.println("ok"); } public static void main(String[] args) { try { CoordRefCreate app = new CoordRefCreate(); app.init(args); app.doRun(); app.cleanup(); } catch (Exception e) { System.out.println("ERROR: " + e.getMessage() + "\n"); e.printStackTrace(); } } void doRun() throws SpatialException, ClassNotFoundException { /** * Set up a CoordRefManager database connection. This connection * will be used to retrieve spatial reference data associated * with the geometries we're going to read. */ crm = CoordRefManager.getInstance(); crm.setConnection(conn); /** * Search the database for a coordinate reference with * the authoritative name/id: "EPSG:4326" */ System.out.println("Search database for all the spatial references " + "identified by 'EPSG:4326'"); CoordRef epsg4326 = null; for (Enumeration e = crm.findByAuth("EPSG", new Integer(4326)); e.hasMoreElements(); ) { epsg4326 = (CoordRef) e.nextElement(); System.out.println(epsg4326); } /** * Note that we should not use epsg4326 and associate it to a * geometry object, as this is not stored in the CoordRefManager * "in-use" cache. Instead, we should use the object returned by * the get() method. */ CoordRef cr4326 = (epsg4326 != null) ? crm.get(epsg4326.getSrid()) : null; /** * This object is read-only: calling any of the set methods will * result in an exception. */ if (cr4326 != null) { try { cr4326.setAuthName("Dummy"); } catch (UnsupportedOperationException e) { System.out.println("Can't modify this coordinate reference"); } } // Search for all the coordinate reference objects in the database System.out.println("\nGetting all the CoordRefs in the database ..."); for (Enumeration allCrs = crm.find(new CoordRef(), 0); allCrs.hasMoreElements() ;) { CoordRef cr = (CoordRef) allCrs.nextElement(); System.out.println("Srid = " + cr.getSrid() + " Description = " + cr.getDescription() + "\nSrtext = " + cr.getSrtext() + "\n"); } /** * In order to create and store a new coordinate reference, we first * construct a new CoordRef object. This object is only a placeholder * for all the data that needs to be passed to the coordinate * reference manager. Do not associate it with geometry objects. */ System.out.println("Creating a new CoordRef object ..."); CoordRef cr = new CoordRef(); cr.setXY(-100, -100, 100); cr.setAuthName("TEST"); cr.setAuthSrid(new Integer(100)); cr.setDescription("Spatial Java API Sample"); cr.setSrtext("UNKNOWN"); System.out.println("Source CoordRef:"); System.out.println("Srid=" + cr.getSrid() + " Description= " + cr.getDescription() + "\nProjection= " + cr.getSrtext() + "\n"); /** * We put the coordinate reference into the CoordRefManager object, * which will serialize it to the database. * The object returned is an immutable coordinate reference, which * may be associated with geometry objects (it is already stored in * the cache). */ System.out.println("Storing the new CoordRef ..."); CoordRef crNew = crm.put(cr); System.out.println("The srid of the new object is: " + crNew.getSrid()); /** * The object returned by the previous operation is immutable: * calling any of the set methods will result in an exception. */ try { crNew.setAuthName("Dummy"); } catch (UnsupportedOperationException e) { System.out.println("Can't modify the returned coordinate reference"); } /** * Remove the coordinate reference from the cache and database. */ System.out.println("Deleting the coordinate reference with srid = " + crNew.getSrid()); crm.remove(crNew.getSrid()); } void init(String args[]) throws SQLException, SpatialException { if (args.length == 0) { if (url == null || url.length() == 0) { System.out.println("ERROR: connection URL must be provided in " + "order to run this sample program!"); return; } } else { url = args[0]; } // Getting connection System.out.print("Getting connection..."); conn = DriverManager.getConnection(url); System.out.println("Ok\n"); } void cleanup() throws SQLException { crm.setConnection(null); // Closing connection System.out.print("Closing connection..."); conn.close(); System.out.println("ok"); } }