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.generalisation.Filtering;
034: import fr.ign.cogit.geoxygene.spatial.coordgeom.GM_LineString;
035: import fr.ign.cogit.geoxygene.spatial.coordgeom.GM_Polygon;
036: import fr.ign.cogit.geoxygene.util.viewer.ObjectViewer;
037:
038: /**
039: * Simple exemple de code pour l'utilisation de GeOxygene.
040: * On suppose qu'il existe une classe persistante "donnees.defaut.Troncon_route"
041: * (sinon changer le nom de la classe dans le code).
042: * Si la classe a charger contient beaucoup d'objet, lancer le programme avec l'option '-Xmx512M'
043: * (java -Xmx512M exemple.FirstExample) .
044: *
045: * @author Thierry Badard & Arnaud Braun
046: * @version 1.1
047: *
048: */
049:
050: public class FirstExample {
051:
052: ///////////////////////////////////////////////////////////////////////////////////////////////////////
053: ///////////////////////////////////////////////////////////////////////////////////////////////////////
054: /* Attributs */
055: private Geodatabase db; // connection a la base de donnees
056: private Class tronconClasse; // classe de troncons
057:
058: private String nomClasse = "geoxygene.geodata.Troncon_route"; // nom de la classe a charger
059:
060: ///////////////////////////////////////////////////////////////////////////////////////////////////////
061: ///////////////////////////////////////////////////////////////////////////////////////////////////////
062: /* Constructeur */
063: public FirstExample() {
064:
065: // initialisation de la connection et lecture des fichiers de mapping
066: System.out.println("Debut initialisation");
067: db = GeodatabaseOjbFactory.newInstance();
068: System.out.println("Initialisation OK");
069: try {
070: tronconClasse = Class.forName(nomClasse);
071: } catch (ClassNotFoundException e) {
072: System.out.println(nomClasse + " : non trouvee");
073: System.exit(0);
074: }
075: }
076:
077: ///////////////////////////////////////////////////////////////////////////////////////////////////////
078: ///////////////////////////////////////////////////////////////////////////////////////////////////////
079: /* méthode main */
080: public static void main(String args[]) {
081: FirstExample test = new FirstExample();
082:
083: test.exemple1();
084: test.exemple2();
085:
086: }
087:
088: ///////////////////////////////////////////////////////////////////////////////////////////////////////
089: ///////////////////////////////////////////////////////////////////////////////////////////////////////
090: /* méthode exemple1 : exemple de traitement sur un objet.*/
091: public void exemple1() {
092:
093: // Declaration des variables
094: FT_Feature troncon;
095: Resultat resultat;
096: Resultat buffer;
097: GM_LineString polyligne;
098: GM_Polygon polygone;
099: int rayon = 20; // rayon de buffer
100:
101: // Debut d'une transaction
102: System.out.println("Debut transaction");
103: db.begin();
104:
105: // Recherche du plus petit identifiant
106: Integer gid = new Integer(db.maxId(tronconClasse));
107:
108: // Chargement d'un objet par son identifiant
109: troncon = (FT_Feature) db.load(tronconClasse, gid);
110: System.out.println("identifiant de l'objet charge : "
111: + troncon.getId());
112:
113: // Sa géométrie
114: polyligne = (GM_LineString) troncon.getGeom();
115: System.out.println(polyligne);
116:
117: // Creation d'un objet Resultat auquel on affecte la geometrie du Troncon_route
118: resultat = new Resultat();
119: resultat.setGeom(polyligne);
120:
121: // Creation d'un objet Resultat auquel on affecte la geometrie d'un buffer autour du Troncon
122: System.out.println("Traitement");
123: polygone = (GM_Polygon) polyligne.buffer(rayon);
124: buffer = new Resultat();
125: buffer.setGeom(polygone);
126:
127: // Ecriture des résultats dans la base
128: db.makePersistent(resultat);
129: db.makePersistent(buffer);
130:
131: //Fermeture transaction et commit ( = validation des ecritures dans la base)
132: System.out.println("commit");
133: db.commit();
134:
135: // Visualisation dans le viewer
136: ObjectViewer viewer = new ObjectViewer(db);
137: FT_FeatureCollection iniColl = new FT_FeatureCollection();
138: iniColl.add(troncon);
139: FT_FeatureCollection bufferColl = new FT_FeatureCollection();
140: bufferColl.add(buffer);
141: viewer.addFeatureCollection(iniColl, "Tronçons");
142: viewer.addFeatureCollection(bufferColl, "Résultats");
143:
144: }
145:
146: ///////////////////////////////////////////////////////////////////////////////////////////////////////
147: ///////////////////////////////////////////////////////////////////////////////////////////////////////
148: /* méthode exemple2 : exemple de traitement sur une couche entiere.*/
149: public void exemple2() {
150:
151: // Declaration des variables
152: FT_Feature troncon;
153: FT_FeatureCollection tronconList;
154: Resultat resultat;
155: GM_LineString polyligne1, polyligne2;
156: double seuil = 80.0; // seuil pour le Douglas-Peucker
157: int compteur = 0; // pour voir l'avancement
158:
159: // Debut d'une transaction
160: System.out.println("Debut transaction");
161: db.begin();
162:
163: // Recherche du nombre d'objets a traiter
164: int n = db.countObjects(tronconClasse);
165: System.out.println("Nombre d'objets a traiter :" + n);
166:
167: // Chargement de tous les objets
168: tronconList = db.loadAllFeatures(tronconClasse);
169: System.out.println("chargement termine");
170:
171: // Création d'une nouvelle collection pour stocker les résultats
172: FT_FeatureCollection allResults = new FT_FeatureCollection();
173:
174: // Traitement des les objets
175: tronconList.initIterator();
176: while (tronconList.hasNext()) {
177: troncon = tronconList.next();
178: polyligne1 = (GM_LineString) troncon.getGeom();
179: polyligne2 = (GM_LineString) Filtering.DouglasPeucker(
180: polyligne1, seuil);
181: resultat = new Resultat();
182: resultat.setGeom(polyligne2);
183: db.makePersistent(resultat);
184: compteur++;
185: if (compteur % 100 == 0)
186: System.out.println(compteur);
187: allResults.add(resultat);
188: }
189:
190: //Fermeture transaction et commit ( = validation des ecritures dans la base)
191: System.out.println("commit");
192: db.commit();
193:
194: // Visualisation dans le viewer
195: ObjectViewer viewer = new ObjectViewer(db);
196: viewer.addFeatureCollection(tronconList, "Tronçons");
197: viewer.addFeatureCollection(allResults, "Résultats");
198: }
199:
200: }
|