source: ether_eccad/trunk/ECCAD_INTERFACE/WEB-INF/src/org/medias/eccad/metier/dessinCarte/CarteMapScript.java @ 68

Last change on this file since 68 was 68, checked in by cbipsl, 14 years ago

commit v1 eccad

  • Property svn:executable set to *
File size: 15.6 KB
Line 
1package org.medias.eccad.metier.dessinCarte;
2
3import java.awt.Point;
4import java.beans.FeatureDescriptor;
5import java.io.File;
6import java.io.FileNotFoundException;
7import java.io.FileOutputStream;
8import java.io.IOException;
9import java.io.OutputStream;
10import java.io.PrintStream;
11import java.util.Iterator;
12import java.util.List;
13
14
15import org.medias.eccad.helpers.LoggerPerso;
16import org.medias.eccad.modele.Zone;
17
18import edu.umn.gis.mapscript.DBFInfo;
19import edu.umn.gis.mapscript.MS_FONT_TYPE;
20//import edu.umn.gis.mapscript.MS_LABEL_POSITIONS;
21import edu.umn.gis.mapscript.MS_LAYER_TYPE;
22import edu.umn.gis.mapscript.MS_SHAPE_TYPE;
23import edu.umn.gis.mapscript.MS_UNITS;
24import edu.umn.gis.mapscript.SWIGTYPE_p_FILE;
25import edu.umn.gis.mapscript.SWIGTYPE_p_int;
26import edu.umn.gis.mapscript.classObj;
27import edu.umn.gis.mapscript.colorObj;
28import edu.umn.gis.mapscript.imageObj;
29import edu.umn.gis.mapscript.labelObj;
30import edu.umn.gis.mapscript.layerObj;
31import edu.umn.gis.mapscript.lineObj;
32import edu.umn.gis.mapscript.mapObj;
33import edu.umn.gis.mapscript.pointObj;
34import edu.umn.gis.mapscript.projectionObj;
35import edu.umn.gis.mapscript.rectObj;
36import edu.umn.gis.mapscript.referenceMapObj;
37import edu.umn.gis.mapscript.shapeObj;
38import edu.umn.gis.mapscript.styleObj;
39// faire la fonction dans spm qui retourne une Zone
40/**
41 * Classe permettant la manipulation des objets issus de MapScript. Son but est de rendre possible la crᅵation de l'image d'une carte suivant des coordonnᅵes prᅵcises et en fonction des couches ᅵ afficher.
42 * REMARQUE : CLASSE DEPRECIE, CODE A MODIFIER CAR FONCTIONNE SEULEMENT POUR DEGRES DECIMAUX
43 *          : S'INSPIRER DE LA CLASSE CARTEMAPSERVER
44 * @author JP
45 *
46 */
47public class CarteMapScript {
48        private mapObj map;
49        private String nom;
50       
51        private referenceMapObj refmap; 
52        private double coefLatitude;
53        private double coefLongitude;
54        // ici ajouter les notions de décalage horizontal et vertical
55        private String webPath = "../tmp/";
56       
57        /**
58         * Constructeur par dᅵfaut de la CarteMapScript
59         * @param nomCarte nom du fichier .map dᅵcrivant les informations spatiales
60         * @param path chemin d'accᅵs au fichier .map ex: c:\mapFile\ ou /mapFile/
61         */
62        public CarteMapScript(String nomCarte, String path) {
63                map = new mapObj(path + nomCarte);
64
65                if (map == null)
66                        throw new RuntimeException("Erreur initialisation map: Class CarteMapScript::CarteMapScript()");
67                nom = nomCarte;
68                //map.setExtent(-180, -90, 180, 90);
69                //map.setSize(720, 360);
70
71
72        } // end CarteMapScript()
73
74
75       
76       
77       
78        /**
79         * Getter Nom
80         * @return le nom de la carte
81         */
82        public String getNom() {
83                return nom;
84        }
85       
86        /**
87         * Setter Nom
88         * @param nom le nom de la carte
89         */
90        public void setNom(String nom) {
91                this.nom = nom;
92        }
93       
94        /**
95         * Positionne les couches ᅵ afficher sur la carte
96         * @param couches liste des couches qui devront apparaᅵtre
97         */
98        public void choixCouches(List couches) {
99                /*** variables ***/
100                int nbr_layer = map.getNumlayers();
101                layerObj layer;
102
103                /*** parcourt des couches et mise ᅵ OFF de leur statut ***/
104                for (int i=0; i<nbr_layer; i++)
105                {
106                        layer = map.getLayer(i);
107                        if (layer==null)
108                                throw new RuntimeException("Erreur capture layer: CarteMapScript::choixCouche");
109                        layer.setStatus(0);
110                } // end for
111               
112               
113                Iterator i_couches = couches.iterator();
114                /*** parcourt des couches ᅵ afficher ***/
115                while (i_couches.hasNext()) {
116                        layer = map.getLayerByName((String) i_couches.next());
117                        if (layer!=null)
118                                layer.setStatus(1);
119                        else
120                                System.err.println("couche non detectᅵe dans la map\nCarte.java:choixCouches:");
121                } // end while
122        }
123       
124        /**
125         * Cree l'image de la carte dans le dossier indiquᅵ
126         * @param path dossier oᅵ sera crᅵᅵ l'image
127         * @param session session de l'utilisateur sans lui nom = 'img[map]defaut.png'
128         * @return le nom de l'image
129         */
130        public String dessinerCarte(String path, String session) {
131                LoggerPerso.log(CarteMapScript.class, LoggerPerso.DEBUG, "dessin cartera");
132                /**** variables ****/
133                imageObj img = map.draw();
134                String imageNom;
135                LoggerPerso.log(CarteMapScript.class, LoggerPerso.DEBUG, "dessin carte1");
136                /**** gestion erreurs ****/
137                if (img == null)
138                        throw new RuntimeException("Erreur création image: CarteMapScript::dessinerCarte");
139               
140                LoggerPerso.log(CarteMapScript.class, LoggerPerso.DEBUG, "dessin carte2");
141                /**** sauvegarde ****/
142                if (session==null)
143                        imageNom = "defaut" + "img" + nom + ".png";
144                else
145                        imageNom =  session + ".png";
146                img.save(path + imageNom, map);
147                LoggerPerso.log(CarteMapScript.class, LoggerPerso.DEBUG, "fin dessin carte");
148                return imageNom;
149        } // end dessinerCarte()
150       
151        public String dessinerCarte(String path, int identifiant) {
152                /**** variables ****/
153                imageObj img = map.draw();
154                String imageNom;
155               
156                /**** gestion erreurs ****/
157                if (img == null)
158                        throw new RuntimeException("Erreur création image: CarteMapScript::dessinerCarte");
159                /**** sauvegarde ****/
160                if (identifiant==0)
161                        imageNom = "defaut" + "img" + nom + ".png";
162                else
163                        imageNom =  "img_" + identifiant + ".png";
164                img.save(path + imageNom, map);
165               
166                return imageNom;
167        } // end dessinerCarte()
168       
169        public void dessinerCarte(OutputStream os) {
170                imageObj img = map.draw();
171               
172                /**** gestion erreurs ****/
173                if (img == null)
174                        throw new RuntimeException("Erreur création image: CarteMapScript::dessinerCarte");
175               
176                byte[] b = img.getBytes();
177               
178                try {
179                        os.write(b);
180                } catch (IOException e) {
181                        e.printStackTrace();
182                }
183
184        }
185        /**
186         * Permet de recentrer la carte sur un point donnᅵ avec un coefficient donnᅵ
187         * @param coef coefficient du zoom
188         * @param clic point qui servira de nouveau centre ᅵ l'image
189         */
190        public void definirZoom(double coef, Point clic, double resol) {
191                // TODO: tester ici
192                // passage du coef ᅵ 1 pour ᅵviter une coef nul lors du dessin de la carte
193                int icoef;
194                if (coef==0) 
195                        icoef = 1;
196                if (coef<0)
197                        icoef = (int) -(1/coef);
198                else
199                        icoef = (int) coef;
200               
201                pointObj my_clic;
202               
203                // si clic en dehors de l'image on centre le clic
204                if (clic.x !=0 && clic.y != 0)
205                        my_clic = new pointObj(clic.x, clic.y, 0);
206                else
207                        my_clic = new pointObj(map.getWidth()/2, map.getHeight()/2, 0);
208               
209               
210                Zone zonCouverte = getZoneCouverte();
211               
212                int coeff = (int) ((360 / ( zonCouverte.getEastbc() - zonCouverte.getWestbc() ) )*(1/resol));
213               
214                //blocage du zoom a 8
215                if ( coeff<16 )
216                {
217                        double maxX = map.getExtent().getMaxx();
218                        int diffX = (int)(map.getExtent().getMaxx() -map.getExtent().getMinx());
219                        double minX = map.getExtent().getMaxx() - diffX;
220               
221               
222                        double maxY = map.getExtent().getMaxy();
223                        int diffY = (int)(map.getExtent().getMaxy() -map.getExtent().getMiny());
224                        double minY = map.getExtent().getMaxy() - diffY;
225               
226                        map.getExtent().setMaxx(maxX);
227                        map.getExtent().setMinx(minX);
228                        map.getExtent().setMaxy(maxY);
229                        map.getExtent().setMiny(minY);
230               
231                        LoggerPerso.log(CarteMapScript.class, LoggerPerso.DEBUG, "++++++++++++++++++++++++++++++++++++++++___________coeff "+ coeff);
232
233                        LoggerPerso.log(CarteMapScript.class, LoggerPerso.DEBUG, "++++++++++++++++++++++++++++++++++++++++getMaxx "+ map.getExtent().getMaxx());
234                        LoggerPerso.log(CarteMapScript.class, LoggerPerso.DEBUG, "++++++++++++++++++++++++++++++++++++++++getMinx "+ map.getExtent().getMinx());
235                        LoggerPerso.log(CarteMapScript.class, LoggerPerso.DEBUG, "++++++++++++++++++++++++++++++++++++++++getMaxy "+ map.getExtent().getMaxy());
236                        LoggerPerso.log(CarteMapScript.class, LoggerPerso.DEBUG, "++++++++++++++++++++++++++++++++++++++++getMiny "+ map.getExtent().getMiny());
237
238                        LoggerPerso.log(CarteMapScript.class, LoggerPerso.DEBUG, "++++++++++++++++++++++++++++++++++++++++ getWidth"+ ((int)(map.getWidth() / coeff))*coeff);
239                        LoggerPerso.log(CarteMapScript.class, LoggerPerso.DEBUG, "++++++++++++++++++++++++++++++++++++++++ getHeight"+ ((int)(map.getHeight() / coeff))*coeff);
240               
241                        // mise ᅵ jour du zoom ᅵ null c'est le maxExtend mais son rᅵle :/ ?
242       
243                        map.zoomPoint(icoef, my_clic, ((int)(map.getWidth() / coeff))*coeff , ((int)(map.getHeight() / coeff))*coeff, map.getExtent(), null);
244                }
245                //map.zoomPoint(icoef, my_clic, 80 , 40, map.getExtent(), null);
246               
247        } // end definirZoom()
248       
249        public void modifDataLayer(String nom, String data) {
250                layerObj layer = map.getLayerByName(nom);
251                layer.setData(data);
252        }
253       
254        /**
255         * @deprecated
256         * fait planter la machine ^^
257         */
258        public void deleteAllLayer() {
259                LoggerPerso.log(CarteMapScript.class, LoggerPerso.DEBUG, "delete all layer");
260                int nbr_layer = map.getNumlayers();
261                /*** parcourt des couches et mise ᅵ OFF de leur statut ***/
262                for (int i=0; i<nbr_layer; i++)
263                {
264                        map.removeLayer(nbr_layer-1-i);
265                } // end for
266        }
267       
268        /**
269         * Equivalent ᅵ choixCouche
270         */
271        public void doLayerChoice(List layer) {
272                this.choixCouches(layer);
273        }
274       
275        private rectObj getExtent() {
276                return map.getExtent();
277               
278        }
279       
280       
281        public Zone getZoneCouverte() {
282                rectObj rect = getExtent();
283               
284                /*double maxX = map.getExtent().getMaxx();
285                int diffX = (int)(map.getExtent().getMaxx() -map.getExtent().getMinx());
286                double minX = map.getExtent().getMaxx() - diffX;
287               
288               
289                double maxY = map.getExtent().getMaxy();
290                int diffY = (int)(map.getExtent().getMaxy() -map.getExtent().getMiny());
291                double minY = map.getExtent().getMaxy() - diffY;
292               
293                rect.setMaxx(maxX);
294                rect.setMinx(minX);
295                rect.setMaxy(maxY);
296                rect.setMiny(minY);
297               
298                LoggerPerso.log(CarteMapScript.class, LoggerPerso.DEBUG, "++++++++++++++++++++++++++++++++++++++++getMaxx "+ rect.getMaxx());
299                LoggerPerso.log(CarteMapScript.class, LoggerPerso.DEBUG, "++++++++++++++++++++++++++++++++++++++++getMinx "+ rect.getMinx());
300                LoggerPerso.log(CarteMapScript.class, LoggerPerso.DEBUG, "++++++++++++++++++++++++++++++++++++++++getMaxy "+ rect.getMaxy());
301                LoggerPerso.log(CarteMapScript.class, LoggerPerso.DEBUG, "++++++++++++++++++++++++++++++++++++++++getMiny "+ rect.getMiny());*/
302
303
304               
305                Zone zone = new Zone(rect.getMaxy(), rect.getMiny(), rect.getMaxx(), rect.getMinx());
306                return zone;
307        }
308       
309        /**
310         * Equivalent ᅵ dessinerCarte
311         */
312        public String getMapDraw(String path, String session) {
313                return this.dessinerCarte(path, session);
314        }
315       
316        /**
317         * Equivalent ᅵ definirZoom
318         */
319        public void doZoomDefine(double coef, Point clic) {
320                this.definirZoom(coef, clic, 0.5);
321        }
322       
323       
324        public void zoneAffiche(double latmax, double latmin, double lonmax, double lonmin) {
325                rectObj rectangle = map.getExtent();
326                rectangle.setMaxx(lonmax);
327                rectangle.setMinx(lonmin);
328                rectangle.setMiny(latmin);
329                rectangle.setMaxy(latmax);
330                map.setExtent(rectangle);
331        }
332       
333        /**
334         * Définie la zone à afficher : zoom par sélection
335         * @param clicMax clic inferieur droit
336         * @param clicMin clic superieur gauche
337         */
338        public void doSelectZone(Point clicMin, Point clicMax) {
339                rectObj rectangle = map.getExtent();
340                double longExtX, longExtY; // longueur du rectangle extent
341                double facteurX, facteurY; // facteur multiplicatif (extent/taille image)
342                double newMinX, newMaxX, newMinY, newMaxY; // nouvelles valeurs extent
343               
344                // obtention de la longueur de l'extent
345                longExtX = Math.abs(rectangle.getMinx() - rectangle.getMaxx());
346                longExtY = Math.abs(rectangle.getMiny() - rectangle.getMaxy());
347               
348                // calcul des facteurs
349                facteurX = (double) longExtX/map.getWidth();
350                facteurY = (double) longExtY/map.getHeight();
351               
352                newMinX = (clicMin.x * facteurX) + rectangle.getMinx();
353                newMaxX = (clicMax.x * facteurX) + rectangle.getMinx();
354                newMinY = (clicMin.y * facteurY) + rectangle.getMiny();
355                newMaxY = (clicMin.y * facteurY) + rectangle.getMiny();
356               
357                rectangle.setMinx(newMinX);
358                rectangle.setMaxx(newMaxX);
359                rectangle.setMiny(newMinY);
360                rectangle.setMaxy(newMaxY);
361               
362                map.setExtent(rectangle);
363        }
364       
365
366        /**
367         * Dessine la carte de rᅵfᅵrence dont le fond de carte est situᅵ dans le path passᅵ en parametre et porte le nom donnᅵ.
368         * La fonction retourne le nom de l'image
369         * @deprecated utiliser getMapRefDraw(String path, String idImage)
370         * @param path chemin d'acces ᅵ l'image
371         * @param idImage numᅵro de l'image ᅵ crᅵer (utilisᅵ pour crᅵer le nom de l'image)
372         * @param IMGREF nom de l'image de rᅵfᅵrence
373         * @return le nom dᅵfinitif de l'image
374         */
375        public String getMapRefDraw(String path, String idImage, String IMGREF) {
376                referenceMapObj refemap = map.getReference();
377                String imageNom;
378               
379                if (refemap == null)
380                        refemap = new referenceMapObj();
381
382                /**** sauvegarde ****/
383                if (idImage==null)
384                        imageNom = "refimg" + nom + "defaut" + ".png";
385                else
386                        imageNom = idImage + "refimg" + nom + ".png";
387
388                rectObj rectangle = new rectObj(-180, -90, 180, 90, 0);
389               
390                //refemap.setMap(map);
391                refemap.setColor(new colorObj(-1, -1, -1, 1));
392                refemap.setImage(IMGREF);
393                refemap.setExtent(rectangle);
394                refemap.setOutlinecolor(new colorObj(255, 255, 255,1));
395                refemap.setWidth(200);
396                refemap.setHeight(99);
397
398                //map.setReference(refemap);
399               
400                imageObj img = map.drawReferenceMap();
401               
402                /**** gestion erreurs ****/
403                if (img == null)
404                        throw new RuntimeException("Erreur crᅵation ref image: CarteMapScript::dessinerCarte");
405                img.save(path + imageNom, map);
406               
407                return webPath + imageNom;
408        }
409
410        /**
411         * Dessine la carte de rᅵfᅵrence dᅵfinie pour la carte.
412         * La fonction retourne le nom de l'image
413         * @param path chemin d'acces ᅵ l'image
414         * @param idImage numᅵro de l'image ᅵ crᅵer (utilisᅵ pour crᅵer le nom de l'image)
415         * @return le nom dᅵfinitif de l'image
416         */
417                public String getMapRefDraw(String path, String idImage) {
418                String imageNom;
419               
420                /**** sauvegarde ****/
421                if (refmap == null)
422                        System.err.println("MapServer::fichier de reference ᅵ la carte non initialise ");
423               
424                if (idImage==null || refmap==null) {
425                        imageNom = "refimg" + nom + "defaut" + ".png";
426                        return imageNom;
427                }
428                else
429                        imageNom = idImage + "refimg" + nom + ".png";
430
431                imageObj img = map.drawReferenceMap();
432               
433                /**** gestion erreurs ****/
434                if (img == null)
435                        throw new RuntimeException("Erreur crᅵation ref image: CarteMapScript::dessinerCarte");
436               
437                img.save(path + imageNom, map);
438               
439                return webPath + imageNom;
440        }
441
442        public double getCoefLatitude() {
443                return coefLatitude;
444        }
445
446        public void setCoefLatitude(double coefLatitude) {
447                this.coefLatitude = coefLatitude;
448        }
449
450        public double getCoefLongitude() {
451                return coefLongitude;
452        }
453
454        public void setCoefLongitude(double coefLongitude) {
455                this.coefLongitude = coefLongitude;
456        }
457       
458       
459        public void setOTFLineLayer(String name, String data, int r, int g, int b) {
460                layerObj couche = new layerObj(map);
461                couche.setName(name);
462                couche.setData(data);
463                couche.setType(MS_LAYER_TYPE.MS_LAYER_LINE);
464                couche.setStatus(1);
465               
466                classObj classe = new classObj(couche);
467                classe.setName(name + "_classe");
468               
469                styleObj style = new styleObj(classe);
470               
471                colorObj couleur = new colorObj(r, g, b, 1);
472                style.setColor(couleur);
473        }
474       
475        public void setOTFRaster(String name, String data) {
476                layerObj couche = new layerObj(map);
477                couche.setName(name);
478                couche.setData(data);
479                couche.setType(MS_LAYER_TYPE.MS_LAYER_RASTER);
480                couche.setStatus(1);
481        }
482       
483        public void setOTFRasterCopyright(String name, String data) {
484                layerObj couche = new layerObj(map);
485                couche.setName(name);
486                couche.setData(data);
487                couche.setType(MS_LAYER_TYPE.MS_LAYER_RASTER);
488                couche.setStatus(1);
489                couche.setTransform(0);
490                /*pointObj point = new pointObj(5, 5, 0);
491                lineObj line = new lineObj();
492                line.add(point);
493                shapeObj shape = new shapeObj(MS_SHAPE_TYPE.MS_SHAPE_POINT.swigValue());
494                shape.add(line);
495                couche.addFeature(shape);*/
496        }
497       
498        public void changeProjection(String name) {
499        /*      map.setUnits(MS_UNITS.MS_METERS);
500                map.setExtent(new rectObj(-10000000, -90, 10000000, 90, 0));
501                map.setProjection("proj=ortho, ellps=WGS84, lat_0=51.0000, lon_0=0.0000, x_0=0.00, y_0=0.00");*/
502               
503        }
504       
505} // end class CarteMapScript
Note: See TracBrowser for help on using the repository browser.