1 | package org.medias.eccad.metier.dessinCarte; |
---|
2 | |
---|
3 | import java.awt.Point; |
---|
4 | import java.io.File; |
---|
5 | import java.util.Map; |
---|
6 | |
---|
7 | |
---|
8 | import org.medias.eccad.helpers.Donnees; |
---|
9 | import org.medias.eccad.helpers.LoggerPerso; |
---|
10 | import org.medias.eccad.helpers.SpringBeanFactory; |
---|
11 | import org.medias.eccad.metier.generationRaster.GenerateurWorldFile; |
---|
12 | import org.medias.eccad.metier.generationRaster.ServiceRaster; |
---|
13 | import org.medias.eccad.metier.statistique.ServiceStatistique; |
---|
14 | import org.medias.eccad.modele.StatReponse; |
---|
15 | import org.medias.eccad.modele.Zone; |
---|
16 | import org.medias.eccad.persistance.GestionFichier; |
---|
17 | import org.medias.eccad.persistance.dao.GrilleDAO; |
---|
18 | import org.medias.eccad.persistance.exception.PersistanceException; |
---|
19 | import org.springframework.beans.factory.xml.XmlBeanFactory; |
---|
20 | |
---|
21 | |
---|
22 | public class ServiceCarte { |
---|
23 | private CarteMapScript carte; |
---|
24 | private String emplacement; |
---|
25 | private XmlBeanFactory xmlbean; |
---|
26 | private ServiceStatistique serviceStat; |
---|
27 | /** |
---|
28 | * Constructeur par défaut. Initialise le chemin d'accÚs à l'application et la carte. |
---|
29 | * |
---|
30 | */ |
---|
31 | public ServiceCarte() { |
---|
32 | xmlbean = SpringBeanFactory.getXmlFactory(); |
---|
33 | initEmplacement(); |
---|
34 | initMap(); |
---|
35 | serviceStat = new ServiceStatistique(); |
---|
36 | } |
---|
37 | |
---|
38 | /** |
---|
39 | * Initialisation de l'emplacement de l'application |
---|
40 | * |
---|
41 | */ |
---|
42 | private void initEmplacement() { |
---|
43 | emplacement = Donnees.getHome(); |
---|
44 | } |
---|
45 | |
---|
46 | /** |
---|
47 | * Initialisation de la carte |
---|
48 | * Chargement d'un descripteur de carte vide et initialisation des couches à la volée |
---|
49 | * |
---|
50 | */ |
---|
51 | private void initMap() { |
---|
52 | LoggerPerso.log(ServiceCarte.class, LoggerPerso.DEBUG, "chargement neutre.map"); |
---|
53 | carte = new CarteMapScript("neutre.map", emplacement + "/mapfile/"); |
---|
54 | carte.setOTFRaster("donnees", "null"); |
---|
55 | carte.setOTFLineLayer("monde", "world", 150, 150, 150); |
---|
56 | carte.setOTFRasterCopyright("copyright", emplacement + "/images/logo_copy.jpg"); |
---|
57 | } |
---|
58 | |
---|
59 | /** |
---|
60 | * Cette fonction génÚre une image de carte pour la grille passée en paramêtre avec la table de couleur demandée |
---|
61 | * @param id_grille |
---|
62 | * @param id_colormap |
---|
63 | * @param zone_affiche |
---|
64 | * @param zone_selection |
---|
65 | * @param identifiant_unique |
---|
66 | * @return l'emplacement et le nom de l'image générée |
---|
67 | * @throws PersistanceException |
---|
68 | */ |
---|
69 | public StatReponse getCarte(long id_grille, long id_colormap, Zone zone_aff, String identifiant_unique){ |
---|
70 | String img_temporaire = new String(); |
---|
71 | ServiceRaster serviceRaster = new ServiceRaster(); |
---|
72 | |
---|
73 | StatReponse statReponse = serviceRaster.genereRaster(id_grille, id_colormap, zone_aff); |
---|
74 | img_temporaire = statReponse.getUrlImage(); |
---|
75 | double center = statReponse.getCenter(); |
---|
76 | |
---|
77 | |
---|
78 | // TODO: placer le generateur de worldfile au niveau du service raster |
---|
79 | GenerateurWorldFile gen_worldfile = (GenerateurWorldFile) xmlbean.getBean("worldfile"); |
---|
80 | |
---|
81 | //initialiser la resolution |
---|
82 | double resolution= getResolution(id_grille); |
---|
83 | |
---|
84 | gen_worldfile.setPasX(resolution); |
---|
85 | gen_worldfile.setPasY(-resolution); |
---|
86 | |
---|
87 | |
---|
88 | gen_worldfile.writeWorldFile("/tmp/" + id_grille + "." + id_colormap); |
---|
89 | |
---|
90 | String urlImage = gestionImageCarte(img_temporaire, zone_aff, identifiant_unique); |
---|
91 | LoggerPerso.log(ServiceCarte.class, LoggerPerso.DEBUG, "urlImage " +urlImage); |
---|
92 | |
---|
93 | statReponse.setUrlImage(urlImage); |
---|
94 | |
---|
95 | return statReponse; |
---|
96 | } |
---|
97 | |
---|
98 | public double getResolution(long id_grille){ |
---|
99 | // initialiser la resolution |
---|
100 | double resolution=0; |
---|
101 | GrilleDAO grilleDAO = (GrilleDAO) xmlbean.getBean("grilleDAO"); |
---|
102 | |
---|
103 | try {resolution = grilleDAO.getGrilleResolution(id_grille);}catch (PersistanceException e) {LoggerPerso.log(ServiceRaster.class, LoggerPerso.ERROR, "ERROR generateRaster " + e.getM_message(), e);} |
---|
104 | LoggerPerso.log(ServiceCarte.class, LoggerPerso.DEBUG, "resolution------------------------------------------------------> :: " + resolution); |
---|
105 | |
---|
106 | return resolution; |
---|
107 | } |
---|
108 | |
---|
109 | |
---|
110 | |
---|
111 | |
---|
112 | |
---|
113 | |
---|
114 | public StatReponse getStatReponse(long id_grille, long id_colormap, Zone zone_aff){ |
---|
115 | ServiceRaster serviceRaster = new ServiceRaster(); |
---|
116 | StatReponse statReponse = serviceRaster.genereRaster(id_grille, id_colormap, zone_aff); |
---|
117 | |
---|
118 | LoggerPerso.log(ServiceCarte.class, LoggerPerso.DEBUG, "********************getStatReponse***************************"); |
---|
119 | return statReponse; |
---|
120 | } |
---|
121 | |
---|
122 | |
---|
123 | |
---|
124 | /* |
---|
125 | public StatReponse getCenter(long id_grille, long id_colormap, Zone zone_affiche, String identifiant_unique){ |
---|
126 | |
---|
127 | } |
---|
128 | */ |
---|
129 | |
---|
130 | |
---|
131 | |
---|
132 | /** |
---|
133 | * Genere une carte calculé à partir des données moyennes, somme, min, max ou ecart-type |
---|
134 | * @param criteres |
---|
135 | * @param id_param |
---|
136 | * @param id_produit |
---|
137 | * @param id_colormap |
---|
138 | * @param fonction |
---|
139 | * @param id_unique |
---|
140 | * @return |
---|
141 | */ |
---|
142 | public StatReponse getCarteStat(Map<String, String> criteres, long id_param, long id_produit, long id_colormap, String fonction, String id_unique, Zone selection) { |
---|
143 | String img_temporaire = new String(); |
---|
144 | |
---|
145 | StatReponse statReponse = serviceStat.getCarteStat(criteres, id_param, id_produit, id_colormap, fonction, id_unique); |
---|
146 | img_temporaire = statReponse.getUrlImage(); |
---|
147 | // TODO: placer le generateur de worldfile au niveau du service raster |
---|
148 | GenerateurWorldFile gen_worldfile = (GenerateurWorldFile) xmlbean.getBean("worldfile"); |
---|
149 | gen_worldfile.writeWorldFile(img_temporaire); |
---|
150 | |
---|
151 | |
---|
152 | // initialiser la resolution |
---|
153 | float resolution=0; |
---|
154 | GrilleDAO grilleDAO = (GrilleDAO) xmlbean.getBean("grilleDAO"); |
---|
155 | |
---|
156 | try {resolution = grilleDAO.getGrilleResolution(id_produit);}catch (PersistanceException e) {LoggerPerso.log(ServiceRaster.class, LoggerPerso.ERROR, "ERROR generateRaster " + e.getM_message(), e);} |
---|
157 | LoggerPerso.log(ServiceCarte.class, LoggerPerso.DEBUG, "resolution------------------------------------------------------> :: " + resolution); |
---|
158 | gen_worldfile.setPasX(resolution); |
---|
159 | gen_worldfile.setPasY(-resolution); |
---|
160 | |
---|
161 | |
---|
162 | |
---|
163 | String urlImage = gestionImageCarte(img_temporaire, selection, id_unique); |
---|
164 | LoggerPerso.log(ServiceCarte.class, LoggerPerso.DEBUG, "urlImage " +urlImage); |
---|
165 | |
---|
166 | statReponse.setUrlImage(urlImage); |
---|
167 | |
---|
168 | return statReponse; |
---|
169 | |
---|
170 | } |
---|
171 | |
---|
172 | public StatReponse getStatReponse(Map<String, String> criteres, long id_param, long id_produit, long id_colormap, String fonction, String id_unique, Zone selection) { |
---|
173 | |
---|
174 | StatReponse statReponse = serviceStat.getCarteStat(criteres, id_param, id_produit, id_colormap, fonction, id_unique); |
---|
175 | |
---|
176 | return statReponse; |
---|
177 | |
---|
178 | } |
---|
179 | |
---|
180 | |
---|
181 | |
---|
182 | public String getLegendStat(long id_grille, long id_colormap, String fonction, String id) { |
---|
183 | if (!fonction.equals("ecartCarte") && !fonction.equals("moyenneCarte") &&!fonction.equals("sommeCarte")) |
---|
184 | return getLegend(id_grille, id_colormap); |
---|
185 | String legend = serviceStat.getLegend(emplacement + "/images/cartes_temporaires/" + id_grille + "_" + id_colormap +"_" + id , id_colormap, id_grille); |
---|
186 | LoggerPerso.log(ServiceCarte.class, LoggerPerso.DEBUG, "getLegendCarte :: " + legend); |
---|
187 | return "/images/cartes_temporaires/" + id_grille + "_" + id_colormap +"_" + id +".legend.png"; |
---|
188 | } |
---|
189 | /** |
---|
190 | * |
---|
191 | * @param id_grille |
---|
192 | * @param id_colormap |
---|
193 | * @param image_temporaire |
---|
194 | * @param zone_affiche |
---|
195 | * @param identifiant_unique |
---|
196 | * @return |
---|
197 | */ |
---|
198 | public String gestionImageCarte(String image_temporaire,Zone zone_aff, String identifiant_unique) { |
---|
199 | LoggerPerso.log(LoggerPerso.class, LoggerPerso.DEBUG, "gestion image carte"); |
---|
200 | String img_erreur = "/images/nodata.png"; |
---|
201 | String nom_image_final; |
---|
202 | |
---|
203 | try { |
---|
204 | LoggerPerso.log(LoggerPerso.class, LoggerPerso.DEBUG, "modif layer : donnees " + image_temporaire); |
---|
205 | carte.modifDataLayer("donnees", image_temporaire); |
---|
206 | LoggerPerso.log(LoggerPerso.class, LoggerPerso.DEBUG, "modif layer ok"); |
---|
207 | if (zone_aff != null) |
---|
208 | carte.zoneAffiche(zone_aff.getNorthbc(), zone_aff.getSouthbc(), zone_aff.getEastbc(), zone_aff.getWestbc()); |
---|
209 | //carte.changeProjection(""); |
---|
210 | nom_image_final = carte.dessinerCarte(emplacement + "/images/cartes_temporaires/", identifiant_unique); |
---|
211 | } |
---|
212 | catch (Exception e) { |
---|
213 | LoggerPerso.log(ServiceCarte.class, LoggerPerso.ERROR, "erreur lors de la création de la carte", e); |
---|
214 | return img_erreur; |
---|
215 | } |
---|
216 | |
---|
217 | GestionFichier effaceur = new GestionFichier(); |
---|
218 | |
---|
219 | //effaceur.effacerFichier(image_temporaire.substring(0, image_temporaire.length()-4) +".wld"); |
---|
220 | effaceur.effacerFichierAncien(emplacement+"/images/cartes_temporaires/"); |
---|
221 | |
---|
222 | return ("/images/cartes_temporaires/" + nom_image_final); |
---|
223 | } |
---|
224 | |
---|
225 | /** |
---|
226 | * Retourne la légende associée à la colormap pour la grille donnée |
---|
227 | * @param id_grille |
---|
228 | * @param id_colormap |
---|
229 | * @return |
---|
230 | */ |
---|
231 | public String getLegend(long id_grille, long id_colormap) { |
---|
232 | File legend_img = new File(emplacement + "/images/cartes_temporaires/" + id_grille + "_" + id_colormap +".legend.png"); |
---|
233 | |
---|
234 | if (legend_img.exists()) |
---|
235 | return "/images/cartes_temporaires/" + id_grille + "_" + id_colormap +".legend.png"; |
---|
236 | |
---|
237 | ServiceRaster serviceRaster = new ServiceRaster(); |
---|
238 | serviceRaster.genereLegend(id_grille, id_colormap, emplacement + "/images/cartes_temporaires/" + id_grille + "_" + id_colormap); |
---|
239 | |
---|
240 | return "/images/cartes_temporaires/" + id_grille + "_" + id_colormap +".legend.png"; |
---|
241 | } |
---|
242 | |
---|
243 | /** |
---|
244 | * Permet d'obtenir la zone que la carte couvre |
---|
245 | * @return la Zone couverte |
---|
246 | */ |
---|
247 | public Zone getCarteCoordinate() { |
---|
248 | if (carte == null) |
---|
249 | return new Zone(); |
---|
250 | |
---|
251 | Zone zone = carte.getZoneCouverte(); |
---|
252 | |
---|
253 | LoggerPerso.log(LoggerPerso.class, LoggerPerso.DEBUG, "zonnnnnnnnnnnnnnnnnnnnnnnne : getEastbc "+ zone.getEastbc()); |
---|
254 | LoggerPerso.log(LoggerPerso.class, LoggerPerso.DEBUG, "zonnnnnnnnnnnnnnnnnnnnnnnne : getWestbc "+ zone.getWestbc()); |
---|
255 | LoggerPerso.log(LoggerPerso.class, LoggerPerso.DEBUG, "zonnnnnnnnnnnnnnnnnnnnnnnne : getNorthbc "+ zone.getNorthbc()); |
---|
256 | LoggerPerso.log(LoggerPerso.class, LoggerPerso.DEBUG, "zonnnnnnnnnnnnnnnnnnnnnnnne : getSouthbc "+ zone.getSouthbc()); |
---|
257 | |
---|
258 | /*if (zone.getEastbc() <180) { if((zone.getEastbc()- ((int)zone.getEastbc())) < 0.5) zone.setEastbc(((int)zone.getEastbc()+ 0.25)); else zone.setEastbc(((int)zone.getEastbc()+ 0.75));}; |
---|
259 | if (zone.getWestbc() > -180 ) { if((zone.getWestbc()- ((int)zone.getWestbc())) < 0.5) zone.setWestbc(((int)zone.getWestbc()+ 0.25)); else zone.setWestbc(((int)zone.getWestbc()+ 0.75));}; |
---|
260 | if (zone.getNorthbc()< 90) { if((zone.getNorthbc()- ((int)zone.getNorthbc())) < 0.5) zone.setNorthbc(((int)zone.getNorthbc()+ 0.25)); else zone.setNorthbc(((int)zone.getNorthbc()+ 0.75));}; |
---|
261 | if (zone.getSouthbc() >-90) { if((zone.getSouthbc()- ((int)zone.getSouthbc())) < 0.5) zone.setSouthbc(((int)zone.getSouthbc()+ 0.25)); else zone.setSouthbc(((int)zone.getSouthbc()+ 0.75));}; |
---|
262 | */ |
---|
263 | LoggerPerso.log(LoggerPerso.class, LoggerPerso.DEBUG, "zonnnnnnnnnnnnnnnnnnnnnnnne : getEastbcN "+ zone.getEastbcN()); |
---|
264 | LoggerPerso.log(LoggerPerso.class, LoggerPerso.DEBUG, "zonnnnnnnnnnnnnnnnnnnnnnnne : getWestbcN "+ zone.getWestbcN()); |
---|
265 | LoggerPerso.log(LoggerPerso.class, LoggerPerso.DEBUG, "zonnnnnnnnnnnnnnnnnnnnnnnne : getNorthbcN "+ zone.getNorthbcN()); |
---|
266 | LoggerPerso.log(LoggerPerso.class, LoggerPerso.DEBUG, "zonnnnnnnnnnnnnnnnnnnnnnnne : getSouthbcN "+ zone.getSouthbcN()); |
---|
267 | |
---|
268 | |
---|
269 | |
---|
270 | return zone; |
---|
271 | } |
---|
272 | |
---|
273 | /** |
---|
274 | * Permet de zoomer sur la carte suivant les coordonnées passées |
---|
275 | * @param coef coefficient de zoom |
---|
276 | * @param clicX position en x du point central du zoom |
---|
277 | * @param clicY position en y du point central du zoom |
---|
278 | */ |
---|
279 | public void zoomCarte(int coef, int clicX, int clicY, double resol) { |
---|
280 | carte.definirZoom(coef, new Point(clicX, clicY), resol); |
---|
281 | } |
---|
282 | |
---|
283 | |
---|
284 | } |
---|