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 fr.ign.cogit.geoxygene.datatools.Geodatabase;
030: import fr.ign.cogit.geoxygene.datatools.ojb.GeodatabaseOjbFactory;
031: import fr.ign.cogit.geoxygene.feature.FT_Feature;
032: import fr.ign.cogit.geoxygene.feature.FT_FeatureCollection;
033: import fr.ign.cogit.geoxygene.spatial.coordgeom.GM_Envelope;
034: import fr.ign.cogit.geoxygene.spatial.coordgeom.GM_Polygon;
035: import fr.ign.cogit.geoxygene.util.index.Tiling;
036:
037: /**
038: * Exemple et test d'utilisation d'un index spatial (dallage) sur FT_FeatureCollection.
039: *
040: * @author Thierry Badard & Arnaud Braun
041: * @version 1.1
042: *
043: */
044:
045: public class TestIndex {
046:
047: private Geodatabase db;
048: private Class featureClass;
049: private String nomClasse = "geoxygene.geodata.Troncon_route";
050: private FT_FeatureCollection featureList;
051: private FT_FeatureCollection sublist;
052: private GM_Envelope emprise;
053: private long t1, t2;
054:
055: // ======= constructeur ===========================================
056: private TestIndex() {
057:
058: // Classe geographique a charger
059: try {
060: featureClass = Class.forName(nomClasse);
061: } catch (ClassNotFoundException e) {
062: System.out.println(nomClasse
063: + "classe geographique non trouvee");
064: System.exit(0);
065: }
066:
067: // Initialisation connexion a la BD
068: db = GeodatabaseOjbFactory.newInstance();
069: db.begin();
070:
071: // Recherche du nombre d'objets a traiter
072: int n = db.countObjects(featureClass);
073: System.out.println("nombre d'objets a traiter : " + n);
074:
075: // Chargement de tous les objets
076: System.out.print("chargement ... ");
077: t1 = System.currentTimeMillis();
078: featureList = db.loadAllFeatures(featureClass);
079: t2 = System.currentTimeMillis();
080: System.out.println((t2 - t1) / 1000.0 + " sec.");
081:
082: //Fermeture transaction
083: db.commit();
084:
085: }
086:
087: // ======= main ===================================================
088: public static void main(String args[]) {
089: TestIndex test = new TestIndex();
090: test.testPerf();
091: test.testMiseAJour();
092: }
093:
094: // ======= test des performances compare a Oracle =================
095: private void testPerf() {
096:
097: // Calcul de l'index
098: System.out.print("calcul index ... ");
099: t1 = System.currentTimeMillis();
100: featureList.initSpatialIndex(Tiling.class, false, 15);
101: t2 = System.currentTimeMillis();
102: System.out.println((t2 - t1) / 1000.0 + " sec.");
103:
104: // emprise de la couche
105: // GM_Envelope empriseIni = db.getMetadata(featureClass).getEnvelope(); // lecture de l'emprise dans le SGBD
106: GM_Envelope empriseIni = featureList.envelope(); // calcul direct
107:
108: // boucle pour regler la taille de l'emprise par homotheties successives
109: for (double h = 0.1; h < 0.5; h += 0.1) {
110: System.out
111: .println("### coefficient d'extension de l'enveloppe "
112: + h);
113: emprise = (GM_Envelope) empriseIni.clone();
114: emprise.expandBy(h);
115: testGM_Envelope();
116: testGM_Polygon();
117: testOracle();
118: }
119: }
120:
121: // ======= selection index memoire avec une GM_Envelope ========
122: private void testGM_Envelope() {
123: System.out.print("extraction GM_Envelope ... ");
124: t1 = System.currentTimeMillis();
125: sublist = featureList.select(emprise);
126: t2 = System.currentTimeMillis();
127: System.out.print("taille de la selection : " + sublist.size()
128: + "\t");
129: System.out.println("temps de calcul : " + ((t2 - t1) / 1000.0)
130: + " sec.");
131: sublist.clear();
132: }
133:
134: // ======= selection index memoire avec un GM_Polygon ==========
135: private void testGM_Polygon() {
136: System.out.print("extraction GM_Polygon ... ");
137: t1 = System.currentTimeMillis();
138: sublist = featureList.select(new GM_Polygon(emprise));
139: t2 = System.currentTimeMillis();
140: System.out.print("taille de la selection : " + sublist.size()
141: + "\t");
142: System.out.println("temps de calcul : " + ((t2 - t1) / 1000.0)
143: + " sec.");
144: sublist.clear();
145: }
146:
147: // ======= selection avec Oracle =============================
148: private void testOracle() {
149: System.out.print("extraction oracle ... ");
150: t1 = System.currentTimeMillis();
151: sublist = db.loadAllFeatures(featureClass, new GM_Polygon(
152: emprise));
153: t2 = System.currentTimeMillis();
154: System.out.print("taille de la selection : " + sublist.size()
155: + "\t");
156: System.out.println("temps de calcul : " + ((t2 - t1) / 1000.0)
157: + " sec.");
158: }
159:
160: // ======= test des fonctions de mise a jour =================
161: private void testMiseAJour() {
162:
163: featureList.initSpatialIndex(Tiling.class, true, 15);
164: Tiling dallage = (Tiling) featureList.getSpatialIndex();
165: GM_Envelope env;
166:
167: // avant mise a jour
168: FT_Feature feature = featureList.get(10);
169: System.out.println("ancienne taille : " + featureList.size());
170: env = dallage.getDallage(feature)[0];
171: System.out.println("dalle contenant le feature : "
172: + env.hashCode());
173: System.out.println("nombre d'objets dans cette dalle : "
174: + featureList.select(env).size());
175:
176: // ajout
177: FT_Feature newFeature = feature.cloneGeom();
178: newFeature
179: .setGeom((newFeature.getGeom().translate(1., 1., 0.)));
180: featureList.add(newFeature);
181: System.out.println("nouvelle taille : " + featureList.size());
182: env = dallage.getDallage(feature)[0];
183: System.out.println("dalle contenant le feature : "
184: + env.hashCode());
185: System.out.println("nombre d'objets dans cette dalle : "
186: + featureList.select(env).size());
187:
188: // suppression
189: featureList.remove(newFeature);
190: System.out.println("nouvelle taille : " + featureList.size());
191: env = dallage.getDallage(feature)[0];
192: System.out.println("dalle contenant le feature : "
193: + env.hashCode());
194: System.out.println("nombre d'objets dans cette dalle : "
195: + featureList.select(env).size());
196: }
197:
198: }
|