Statistics
| Revision:

gvsig-osm / org.gvsig.raster.osm / trunk / org.gvsig.raster.osm / org.gvsig.raster.osm.app.osmclient / src / main / java / org / gvsig / raster / osm / app / osmclient / OSMClientExtension.java @ 85

History | View | Annotate | Download (5.36 KB)

1
/* OSM layers for gvSIG. 
2
 * Geographic Information System of the Valencian Government
3
*
4
* Copyright (C) 2012 Nacho Brodin
5
* 
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
10
* 
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
* GNU General Public License for more details.
15
* 
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
* MA  02110-1301, USA.
20
* 
21
*/
22
package org.gvsig.raster.osm.app.osmclient;
23

    
24
import java.io.File;
25
import java.io.IOException;
26
import java.sql.ResultSet;
27

    
28
import javax.swing.JOptionPane;
29

    
30
import org.cresques.cts.IProjection;
31
import org.gvsig.about.AboutManager;
32
import org.gvsig.about.AboutParticipant;
33
import org.gvsig.andami.IconThemeHelper;
34
import org.gvsig.andami.plugins.Extension;
35
import org.gvsig.app.ApplicationLocator;
36
import org.gvsig.app.ApplicationManager;
37
import org.gvsig.app.extension.AddLayer;
38
import org.gvsig.fmap.crs.CRSFactory;
39
import org.gvsig.i18n.Messages;
40
import org.gvsig.raster.osm.app.osmclient.layer.FLyrOSM;
41
import org.slf4j.Logger;
42
import org.slf4j.LoggerFactory;
43

    
44
/**
45
 * Extension for adding open street map support to gvSIG.
46
 * @author Nacho Brodin (nachobrodin@gmail.com)
47
 */
48
public class OSMClientExtension extends Extension {
49
        private Logger              log           = LoggerFactory.getLogger(OSMClientExtension.class);
50
        private String              fileCheck     = System.getProperty("user.home") + File.separator + "gvSIG" + File.separator + "osm_epsg_error";
51
        private String              epsgCode      = "3857";
52
        
53
        public void initialize() {
54
                /*
55
                         #Thu May 23 15:49:23 CEST 2013
56
                        Map_Quest=Map Quest*http\://oatile1.mqcdn.com/tiles/1.0.0/map*20*png
57
                        Stamen=Stamen*http\://a.tile.stamen.com/terrain*17*jpg
58
                        Open_Cycle_Map=Open Cycle Map*http\://tile.opencyclemap.org/cycle/*16*png
59
                        Stamen_Toner=Stamen Toner*http\://tile.stamen.com/toner*17*jpg
60
                        Map_Quest_Open_Aerial=Map Quest Open Aerial*http\://otile1.mqcdn.com/tiles/1.0.0/sat*20*jpg
61
                        Mapnik=Mapnik*http\://tile.openstreetmap.org/*18*png
62
                        Stamen_Water_Color=Stamen Water Color*http\://tile.stamen.com/watercolor*17*jpg
63
                 */
64
                
65
                IconThemeHelper.registerIcon(null, "icon-layer-osm", this);
66
        }
67

    
68
        public void postInitialize() {
69
                FLyrOSM.registerPersistent();
70
                addAboutInfo();
71
                AddLayer.addWizard(AddOSMLayerWizard.class);
72
                loadEpsgCodeInJCRSUserDatabase();
73
        }
74
        
75
        private void loadEpsgCodeInJCRSUserDatabase() {
76
                IProjection proj = null;
77
                try {
78
                        proj = CRSFactory.getCRS("EPSG:" + epsgCode);
79
                } catch(Exception e) {
80
                }
81
                
82
                if(proj != null)
83
                        return;
84
                
85
                String wkt = "'PROJCS[\"WGS 84 / Pseudo-Mercator\",GEOGCS[\"Popular Visualisation CRS\",DATUM[\"D_Popular_Visualisation_Datum\",SPHEROID[\"Popular_Visualisation_Sphere\",6378137,0]],PRIMEM[\"Greenwich\",0],UNIT[\"Degree\",0.017453292519943295]],PROJECTION[\"Mercator\"],PARAMETER[\"central_meridian\",0],PARAMETER[\"scale_factor\",1],PARAMETER[\"false_easting\",0],PARAMETER[\"false_northing\",0],UNIT[\"Meter\",1]]'";
86
                try {
87
                        es.idr.teledeteccion.connection.EpsgConnection conn = new es.idr.teledeteccion.connection.EpsgConnection();
88
                        conn.setConnectionUsr();
89

    
90
                        String query = "SELECT usr_code FROM USR WHERE usr_code='" + epsgCode + "'";
91
                        ResultSet result = es.idr.teledeteccion.connection.Query.select(query, conn.getConnection());
92

    
93
                        //Si no existe en c?digo se inserta para que la proyecci?n est? disponible
94

    
95
                        if(result == null || !result.next()) { 
96
                                String sentence = "INSERT INTO USR VALUES(" + epsgCode + ", " + wkt + ",'WGS 84 / Pseudo-Mercator','Usuario2','Popular_Visualisation_Datum')";
97
                                conn.update(sentence);
98
                                conn.shutdown();
99
                        }
100
                } catch (Throwable e) {
101
                        File f = new File(fileCheck);
102
                        if(!f.exists())
103
                                messageErrorEpsg("error_loading_epsg", f);
104
                        log.info("Error accediendo a JCRS", e);
105
                        return;
106
                } 
107
        }
108
        
109
        public void messageErrorEpsg(String msg, File f) {
110
                String string1 = Messages.getText("dont_remind_me_anymore");
111
                String string2 = Messages.getText("ok");
112
                Object[] options = {string1, string2};
113
                int n = JOptionPane.showOptionDialog(null,
114
                                        "<html>" + Messages.getText(msg).replaceAll("\n", "<br>") + "</html>",
115
                                        Messages.getText("confirmacion"),
116
                                        JOptionPane.YES_NO_OPTION,
117
                                        JOptionPane.QUESTION_MESSAGE,
118
                                        null,
119
                                        options,
120
                                        string1);
121
                if (n == 0) {
122
                        try {
123
                                f.createNewFile();
124
                        } catch (IOException e) {
125
                        }
126
                }
127
        }
128

    
129
        public void execute(String actionCommand) {
130

    
131
        }
132

    
133
        public boolean isEnabled() {
134
                return true;
135
        }
136

    
137
        public boolean isVisible() {
138
                return true;
139
        }
140
        
141
        private void addAboutInfo() {
142
        ApplicationManager application = ApplicationLocator.getManager();
143
        
144
        AboutManager about = application.getAbout();
145
        about.addDeveloper("Nacho Brodin", getClass().getClassLoader()
146
            .getResource("about/osm-about.html"), 2);
147

    
148
        AboutParticipant participant = about.getDeveloper("Nacho Brodin");
149
        participant.addContribution(
150
            "OSM",
151
            "Open Street Map client for gvSIG", 
152
            2012,1,1, 
153
            2012,1,15
154
        );      
155
    }
156
}