001: /*
002: * This file is part of the GeOxygene project source files.
003: *
004: * GeOxygene aims at providing an open framework which implements OGC/ISO specifications for
005: * the development and deployment of geographic (GIS) applications. It is a open source
006: * contribution of the COGIT laboratory at the Institut Géographique National (the French
007: * National Mapping Agency).
008: *
009: * See: http://oxygene-project.sourceforge.net
010: *
011: * Copyright (C) 2005 Institut Géographique National
012: *
013: * This library is free software; you can redistribute it and/or modify it under the terms
014: * of the GNU Lesser General Public License as published by the Free Software Foundation;
015: * either version 2.1 of the License, or any later version.
016: *
017: * This library is distributed in the hope that it will be useful, but WITHOUT ANY
018: * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
019: * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
020: *
021: * You should have received a copy of the GNU Lesser General Public License along with
022: * this library (see file LICENSE if present); if not, write to the Free Software
023: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024: *
025: */
026:
027: package fr.ign.cogit.geoxygene.example;
028:
029: import java.math.BigDecimal;
030: import java.util.Iterator;
031: import java.util.List;
032:
033: import fr.ign.cogit.geoxygene.datatools.Geodatabase;
034: import fr.ign.cogit.geoxygene.datatools.Metadata;
035: import fr.ign.cogit.geoxygene.datatools.ojb.GeodatabaseOjbFactory;
036: import fr.ign.cogit.geoxygene.feature.FT_Feature;
037: import fr.ign.cogit.geoxygene.feature.FT_FeatureCollection;
038: import fr.ign.cogit.geoxygene.spatial.geomroot.GM_Object;
039:
040: /**
041: * Exemple et test d'utilisation de l'interface Geodatabase.
042: * On suppose qu'il existe une classe persistante "donnees.defaut.Troncon_route".
043: * (sinon changer le nom de la classe dans le code).
044: * Si la classe a charger contient beaucoup d'objet, lancer le programme avec l'option '-Xmx512M'
045: * (java -Xmx512M exemple.FirstExample) .
046: *
047: * @author Thierry Badard & Arnaud Braun
048: * @version 1.1
049: *
050: */
051:
052: public class TestGeodatabase {
053:
054: // #########################################################
055: // #########################################################
056: /* Attribut */
057: private Geodatabase db;
058: private Class tronconClass; // classe de troncons
059: private String nomClasse = "donnees.defaut.Bdc38_troncon_route"; // nom de la classe a charger
060:
061: // #########################################################
062: // #########################################################
063: /* Constructeur */
064: public TestGeodatabase() {
065:
066: // iniatilsation de la Geodatabase
067: db = GeodatabaseOjbFactory.newInstance();
068:
069: try {
070: tronconClass = Class.forName(nomClasse);
071: } catch (ClassNotFoundException e) {
072: System.out.println(nomClasse + " : non trouvee");
073: System.exit(0);
074: }
075:
076: }
077:
078: // #########################################################
079: // #########################################################
080: /* Methode main */
081: public static void main(String args[]) {
082: TestGeodatabase test = new TestGeodatabase();
083: test.testJDO();
084: test.testMetadata();
085: test.testSpatial();
086: test.testSQL();
087: }
088:
089: // #########################################################
090: // #########################################################
091: /* Teste les methodes JDO (ou ODMG) */
092: public void testJDO() {
093:
094: // Declaration des variables
095: FT_Feature feature;
096: FT_FeatureCollection featList;
097: Resultat result;
098: GM_Object geom;
099: List list;
100:
101: int gid = 43897; // identifiant feature (trouver un COGITID existant par lecture de la table)
102: int seuil = 100; // distance buffer
103:
104: // ouvre une transaction
105: db.begin();
106: System.out.println("debut transaction");
107:
108: // teste l'ouverture de la transaction
109: System.out.println("transaction ouverte ? : " + db.isOpen());
110:
111: // charge un objet par son identifiant
112: feature = (FT_Feature) db.load(tronconClass, new Integer(gid));
113: if (feature != null)
114: System.out.println("objet charge : " + feature.getClass()
115: + " - id : " + feature.getId());
116:
117: // chargement de tous les FT_Feature d'une classe
118: featList = db.loadAllFeatures(tronconClass);
119: System.out.println("nombre de feature charges : "
120: + featList.size());
121:
122: // rend un objet persistent
123: featList.initIterator();
124: feature = featList.next();
125: geom = feature.getGeom();
126: System.out.println(geom);
127: result = new Resultat();
128: result.setGeom(geom);
129: db.makePersistent(result);
130: System.out
131: .println("objet result cree - id : " + result.getId());
132:
133: // commit intermediaire
134: db.checkpoint();
135: System.out.println("checkpoint");
136:
137: // re-chargement de tous les FT_Feature d'une classe
138: // ils sont en fait deja charges, donc c'est instantane !
139: featList = db.loadAllFeatures(tronconClass);
140: System.out.println("nombre de feature charges : "
141: + featList.size());
142:
143: // chargement de tous les troncons intersectant la geometrie "geom"
144: featList = db.loadAllFeatures(tronconClass, geom);
145: System.out.println("nombre de feature charges : "
146: + featList.size());
147:
148: // chargement de tous les troncons dans un buffer autour de "geom"
149: featList = db.loadAllFeatures(tronconClass, geom, seuil);
150: System.out.println("nombre de feature charges : "
151: + featList.size());
152:
153: // chargement d'objets par une requete OQL simple
154: String query = "select x from " + Resultat.class.getName()
155: + " where int1 = $0";
156: System.out.println(query);
157: list = db.loadOQL(query, new Integer(0));
158: System.out.println("nombre d'objets trouves par la requete : "
159: + list.size());
160:
161: // destruction d'un objet
162: db.deletePersistent(result);
163: System.out.println("objet result detruit - id : "
164: + result.getId());
165:
166: // Commite et ferme la transaction. */
167: db.commit();
168: System.out.println("fin transaction");
169:
170: // teste l'ouverture de la transaction
171: System.out.println("transaction ouverte ? : " + db.isOpen());
172:
173: }
174:
175: // #########################################################
176: // #########################################################
177: /* Teste les metadonnees */
178: public void testMetadata() {
179:
180: // liste des metadonnees issues du mapping
181: List metadataList = db.getMetadata();
182: Iterator it = metadataList.iterator();
183: while (it.hasNext()) {
184: Metadata metadata = (Metadata) it.next();
185: if (metadata.getClassName() != null)
186: System.out.println(metadata.getClassName());
187: if (metadata.getJavaClass() != null)
188: System.out.println(metadata.getJavaClass());
189: if (metadata.getTableName() != null)
190: System.out.println(metadata.getTableName());
191: if (metadata.getGeomColumnName() != null)
192: System.out.println(metadata.getGeomColumnName());
193: if (metadata.getIdColumnName() != null)
194: System.out.println(metadata.getIdColumnName());
195: if (metadata.getSRID() != 0)
196: System.out.println(metadata.getSRID());
197: if (metadata.getEnvelope() != null)
198: System.out.println(metadata.getEnvelope());
199: if (metadata.getTolerance() != null)
200: System.out.println(metadata.getTolerance(0));
201: if (metadata.getDimension() != 0)
202: System.out.println(metadata.getDimension());
203: System.out.println("");
204: }
205:
206: // acces direct aux metadonnees
207: String tableName = db.getMetadata(tronconClass).getTableName();
208: System.out.println(tableName);
209: System.out.println("");
210: System.out.println(db.getMetadata(tableName).getJavaClass()
211: .getName());
212: System.out.println("");
213: }
214:
215: // #########################################################
216: // #########################################################
217: /* Teste les fonctionnalites spatiales */
218: public void testSpatial() {
219:
220: // affectation d'une emprise
221: db.mbr(tronconClass);
222: System.out.println("emprise ok");
223:
224: // calcul d'un index spatial
225: db.spatialIndex(tronconClass);
226: System.out.println("index spatial ok");
227: }
228:
229: // #########################################################
230: // #########################################################
231: /* Teste les fonctionnalites SQL */
232: public void testSQL() {
233:
234: // execution directe d'une requete SQL
235: List list = db.exeSQLQuery("SELECT COGITID FROM RESULTAT");
236: Iterator it = list.iterator();
237: while (it.hasNext()) {
238: Object[] resultat = (Object[]) it.next();
239: int featureId = ((BigDecimal) resultat[0]).intValue();
240: //System.out.println("feature : "+featureId+" - topo : "+topoId);
241: }
242: System.out.println("Requete directe SQL ok");
243:
244: // execution d'une commande SQL
245: db.exeSQL("DELETE FROM RESULTAT");
246: System.out.println("delete ok");
247:
248: // nombre d'objets d'une classe
249: System.out.println("nombre d'objets : "
250: + db.countObjects(tronconClass));
251:
252: // identifiant minimum
253: System.out.println("min id : " + db.minId(tronconClass));
254:
255: // identifiant maximum
256: System.out.println("max id : " + db.maxId(tronconClass));
257:
258: }
259:
260: }
|