Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / kml / KmlFMapDriver.java @ 11704

History | View | Annotate | Download (11.3 KB)

1
package com.iver.cit.gvsig.fmap.drivers.kml;
2

    
3
import java.awt.Color;
4
import java.awt.Font;
5
import java.io.BufferedInputStream;
6
import java.io.File;
7
import java.io.FileInputStream;
8
import java.io.FileNotFoundException;
9
import java.io.IOException;
10
import java.io.InputStream;
11
import java.util.Random;
12
import java.util.zip.ZipEntry;
13
import java.util.zip.ZipFile;
14
import java.util.zip.ZipInputStream;
15

    
16
import org.apache.log4j.Logger;
17
import org.gvsig.exceptions.BaseException;
18
import org.gvsig.remoteClient.gml.engine.IFeaturesIterator;
19
import org.gvsig.remoteClient.gml.exceptions.GMLException;
20
import org.gvsig.remoteClient.gml.factories.IGeometriesFactory;
21
import org.gvsig.remoteClient.kml.KmlDriver;
22
import org.gvsig.remoteClient.kml.KmlTags;
23
import org.gvsig.remoteClient.kml.exceptions.KmlException;
24

    
25
import com.hardcode.gdbms.driver.exceptions.InitializeDriverException;
26
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
27
import com.hardcode.gdbms.engine.data.driver.ObjectDriver;
28
import com.hardcode.gdbms.engine.values.IntValue;
29
import com.hardcode.gdbms.engine.values.StringValue;
30
import com.hardcode.gdbms.engine.values.ValueFactory;
31
import com.iver.cit.gvsig.fmap.core.FShape;
32
import com.iver.cit.gvsig.fmap.core.IGeometry;
33
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
34
import com.iver.cit.gvsig.fmap.core.symbols.IFillSymbol;
35
import com.iver.cit.gvsig.fmap.core.symbols.ILineSymbol;
36
import com.iver.cit.gvsig.fmap.core.symbols.IMarkerSymbol;
37
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
38
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
39
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
40
import com.iver.cit.gvsig.fmap.drivers.ConcreteMemoryDriver;
41
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver;
42
import com.iver.cit.gvsig.fmap.drivers.WithDefaultLegend;
43
import com.iver.cit.gvsig.fmap.drivers.gml.FMAPGeometryFactory;
44
import com.iver.cit.gvsig.fmap.drivers.gml.FMAPGeometryFactory.FeatureWithAttributes;
45
import com.iver.cit.gvsig.fmap.rendering.ILegend;
46
import com.iver.cit.gvsig.fmap.rendering.LegendFactory;
47
import com.iver.cit.gvsig.fmap.rendering.VectorialUniqueValueLegend;
48
import com.iver.cit.gvsig.fmap.rendering.styling.ILabelingStrategy;
49

    
50
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
51
 *
52
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
53
 *
54
 * This program is free software; you can redistribute it and/or
55
 * modify it under the terms of the GNU General Public License
56
 * as published by the Free Software Foundation; either version 2
57
 * of the License, or (at your option) any later version.
58
 *
59
 * This program is distributed in the hope that it will be useful,
60
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
61
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
62
 * GNU General Public License for more details.
63
 *
64
 * You should have received a copy of the GNU General Public License
65
 * along with this program; if not, write to the Free Software
66
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
67
 *
68
 * For more information, contact:
69
 *
70
 *  Generalitat Valenciana
71
 *   Conselleria d'Infraestructures i Transport
72
 *   Av. Blasco Ib??ez, 50
73
 *   46010 VALENCIA
74
 *   SPAIN
75
 *
76
 *      +34 963862235
77
 *   gvsig@gva.es
78
 *      www.gvsig.gva.es
79
 *
80
 *    or
81
 *
82
 *   IVER T.I. S.A
83
 *   Salamanca 50
84
 *   46005 Valencia
85
 *   Spain
86
 *
87
 *   +34 963163400
88
 *   dac@iver.es
89
 */
90
/* CVS MESSAGES:
91
 *
92
 * $Id: KmlFMapDriver.java 11704 2007-05-17 09:43:35Z jaume $
93
 * $Log$
94
 * Revision 1.5  2007-05-17 09:32:06  jaume
95
 * *** empty log message ***
96
 *
97
 * Revision 1.4  2007/05/10 09:44:09  jaume
98
 * Refactored legend interface names
99
 *
100
 * Revision 1.3  2007/03/20 16:09:53  jaume
101
 * refactored to use ISymbol instead of FSymbol
102
 *
103
 * Revision 1.2  2007/03/06 17:09:00  caballero
104
 * Exceptions
105
 *
106
 * Revision 1.1  2007/02/12 13:50:08  jorpiell
107
 * A?adido el driver de KML
108
 *
109
 *
110
 */
111
/**
112
 * @author Jorge Piera Llodr? (piera_jor@gva.es)
113
 */
114
public class KmlFMapDriver extends ConcreteMemoryDriver implements VectorialFileDriver,WithDefaultLegend{
115
        private static Logger logger = Logger.getLogger(KmlDriver.class.getName());
116
        private File m_Fich;
117
        private VectorialUniqueValueLegend defaultLegend = null;
118
        private ISymbol defaultSymbol = null;
119
        private IGeometriesFactory factory = null;
120
        private KmlDriver driver = null;
121
        private IFeaturesIterator iterator = null;
122
        private static final float heightText = 10;
123
        private Random r = null;
124
        //it contains the position for the folder name.
125
        private int FOLDER_NAME_POSITION = 0;
126
        private ILabelingStrategy labeler;
127
        /*
128
         *  (non-Javadoc)
129
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#open(java.io.File)
130
         */
131
        public void open(File f) {
132
                m_Fich = f;
133
                factory = new FMAPGeometryFactory();
134
                r = new Random();
135
        }
136

    
137
        /*
138
         *  (non-Javadoc)
139
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#close()
140
         */
141
        public void close() {
142
                // TODO Auto-generated method stub
143

    
144
        }
145

    
146
        /*
147
         *  (non-Javadoc)
148
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#initialize()
149
         */
150
        public void initialize() throws InitializeDriverException {
151
                try {
152
                        driver = new KmlDriver(createInputStream(),factory);
153
                        iterator =         driver.getFeaturesIterator();
154
                } catch (BaseException e) {
155
                        logger.error("Error openning the kml file",e);
156
                        throw new InitializeDriverException(getName(),e);
157
                }
158

    
159
                int index = 0;
160

    
161
                IntValue clave;
162

    
163
                boolean setModel = false;
164
                FeatureWithAttributes feature;
165
                try {
166
                        while (iterator.hasNext()) {
167
                                try{
168
                                        feature = (FeatureWithAttributes)iterator.next();
169
                                        //There are features without geometry
170
                                        if (feature.getFeature().getGeometry() != null){
171
                                                if (!setModel){
172
                                                        getTableModel().setColumnIdentifiers(feature.getAttributeName());
173
                                                        setModel = true;
174
                                                }
175
                                                clave = ValueFactory.createValue(index);
176
                                                IGeometry geom = feature.getFeature().getGeometry();
177
                                                if (geom != null){
178
                                                        ISymbol theSymbol = null;
179
                                                        theSymbol = getDefaultSymbol();
180
                                                        theSymbol.setDescription(clave.toString());
181
                                                        addGeometry(geom, feature.getAttributeValue());
182
                                                }
183
                                        }
184
                                        index++;
185
                                }catch(GMLException e){
186
                                        logger.error(e.getMessage(),e);
187
                                        e.printStackTrace();
188
                                }
189
                        }
190
                }catch (BaseException e) {
191
                        logger.error("Error retrieving the geometries",e);
192
                        e.printStackTrace();
193
                }
194

    
195
                try {
196
                        createLegend();
197
                } catch (ReadDriverException e) {
198
                        throw new InitializeDriverException(getName(),e);
199
                }
200
        }
201

    
202
        /**
203
         * Creates the legend. The unique value is the field
204
         * that is specified in the KmlTags.FOLDER_NAME constant
205
         * @throws IOException
206
         */
207
        private void createLegend() throws ReadDriverException{
208
                ObjectDriver rs = this;
209
                StringValue clave;
210
                ISymbol theSymbol = null;
211
                for (long j = 0; j < rs.getRowCount(); j++) {
212
                        clave = (StringValue) rs.getFieldValue(j, FOLDER_NAME_POSITION);
213
                        if (((VectorialUniqueValueLegend)getDefaultLegend()).getSymbolByValue(clave) == null) {
214
                                /*
215
                                 * jaume (moved to ISymbol)
216
                                theSymbol = new FSymbol(getShapeType());
217
                                theSymbol.setDescription(clave.toString());
218
                                Color c = getRandomColor();
219
                                theSymbol.setColor(c);
220
                                theSymbol.setStyle(FConstant.SYMBOL_STYLE_FILL_SOLID);
221
                                theSymbol.setSize(3);
222
                                theSymbol.setSizeInPixels(true);
223
                                */
224
                                theSymbol = SymbologyFactory.
225
                                        createDefaultSymbolByShapeType(getShapeType());
226
                                theSymbol.setDescription(clave.toString());
227
                                Color c = getRandomColor();
228
                                if (theSymbol instanceof IMarkerSymbol) {
229
                                        ((IMarkerSymbol) theSymbol).setColor(c);
230
                                        ((IMarkerSymbol) theSymbol).setSize(3);
231

    
232
                                }
233
                                if (theSymbol instanceof ILineSymbol) {
234
                                        ((ILineSymbol) theSymbol).setLineColor(c);
235
                                        ((ILineSymbol) theSymbol).setLineWidth(3);
236
                                }
237
                                if (theSymbol instanceof IFillSymbol) {
238
                                        ((IFillSymbol) theSymbol).setFillColor(c);
239
                                        ((IFillSymbol) theSymbol).getOutline().setLineWidth(3);
240
                                }
241

    
242
                                defaultLegend.addSymbol(clave, theSymbol);
243
                        }
244
                }
245
        }
246

    
247
        /*
248
         *  (non-Javadoc)
249
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#accept(java.io.File)
250
         */
251
        public boolean accept(File f) {
252
                if ((f.getName().toUpperCase().endsWith("KML")) ||
253
                                (f.getName().toUpperCase().endsWith("KMZ"))){
254
                        return true;
255
                }
256
                return false;
257
        }
258

    
259
        /*
260
         *  (non-Javadoc)
261
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#getFile()
262
         */
263
        public File getFile() {
264
                return m_Fich;
265
        }
266

    
267

    
268
        /*
269
         *  (non-Javadoc)
270
         * @see com.hardcode.driverManager.Driver#getName()
271
         */
272
        public String getName() {
273
                return "gvSIG KML Memory Driver";
274
        }
275

    
276
        /*
277
         *  (non-Javadoc)
278
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getShapeType()
279
         */
280
        public int getShapeType() {
281
                return FShape.MULTI;
282
        }
283

    
284
        /**
285
         * It returns a random color
286
         * @return
287
         */
288
        private Color getRandomColor(){
289
                return new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255));
290
        }
291

    
292
        /**
293
         * Creates the default symbol
294
         * @return
295
         */
296
        private ISymbol getDefaultSymbol(){
297
                if (defaultSymbol == null){
298
                        /* jaume (moved to ISymbol)
299
                         * defaultSymbol = new FSymbol(getShapeType());
300
                        defaultSymbol.setShapeVisible(false);
301
                        defaultSymbol.setFontSizeInPixels(false);
302
                        defaultSymbol.setFont(new Font("SansSerif",Font.PLAIN, 9));
303
                        defaultSymbol.setFontColor(Color.BLACK);
304
                        defaultSymbol.setFontSize(heightText);
305
                        defaultSymbol.setStyle(FConstant.SYMBOL_STYLE_FILL_SOLID);
306
                        defaultSymbol.setSize(3);
307
                        defaultSymbol.setSizeInPixels(true);
308
                        */
309
                        defaultSymbol = SymbologyFactory.
310
                        createDefaultSymbolByShapeType(getShapeType());
311

    
312
                        Color c = getRandomColor();
313
                        if (defaultSymbol instanceof IMarkerSymbol) {
314
                                ((IMarkerSymbol) defaultSymbol).setColor(c);
315
                                ((IMarkerSymbol) defaultSymbol).setSize(3);
316

    
317
                        }
318
                        if (defaultSymbol instanceof ILineSymbol) {
319
                                ((ILineSymbol) defaultSymbol).setLineColor(c);
320
                                ((ILineSymbol) defaultSymbol).setLineWidth(3);
321
                        }
322
                        if (defaultSymbol instanceof IFillSymbol) {
323
                                ((IFillSymbol) defaultSymbol).setFillColor(c);
324
                                ((IFillSymbol) defaultSymbol).getOutline().setLineWidth(3);
325
                        }
326
                }
327
                return defaultSymbol;
328
        }
329

    
330
        /*
331
         *  (non-Javadoc)
332
         * @see com.iver.cit.gvsig.fmap.drivers.WithDefaultLegend#getDefaultLegend()
333
         */
334
        public ILegend getDefaultLegend() {
335
                if (defaultLegend == null){
336
                        defaultLegend = LegendFactory.createVectorialUniqueValueLegend(getShapeType());
337
                        defaultLegend.setFieldNames(new String[] {KmlTags.FOLDER_NAME});
338
                        defaultLegend.setLabelField(null);
339
                        defaultLegend.setLabelHeightField(null);
340
                        defaultLegend.setLabelRotationField(null);
341
                        defaultLegend.setDefaultSymbol(getDefaultSymbol());
342
                }
343
                return defaultLegend;
344
        }
345

    
346
        /**
347
         * It creates an InputStream. The Kml file can have the
348
         * KML or the KMZ extension
349
         * @return
350
         * @throws KmlException
351
         */
352
        private InputStream createInputStream() throws KmlException{
353
                try {
354
                        if (getFile().getName().toUpperCase().endsWith("KML")){
355
                                return new FileInputStream(getFile());
356
                        }else if(getFile().getName().toUpperCase().endsWith("KMZ")){
357
                                FileInputStream fis = new FileInputStream(getFile());
358
                                ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
359
                                ZipEntry entry = null;
360
                                while((entry = zis.getNextEntry()) != null){
361
                                        if (!entry.isDirectory()){
362
                                                ZipFile fz = new ZipFile(getFile());
363
                                                return fz.getInputStream(entry);
364
                                        }
365
                                }
366
                        }else{
367
                                throw new KmlException(getFile());
368
                        }
369
                } catch (FileNotFoundException e) {
370
                        throw new KmlException(getFile(),e);
371
                } catch (IOException e) {
372
                        throw new KmlException(getFile(),e);
373
                }
374
                return null;
375
        }
376

    
377
        public ILabelingStrategy getDefaultLabelingStrategy() {
378
                return labeler;
379
        }
380
}