Statistics
| Revision:

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

History | View | Annotate | Download (11.4 KB)

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

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

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

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

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

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

    
147
        }
148

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

    
162
                int index = 0;
163

    
164
                IntValue clave;
165

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

    
198
                try {
199
                        createLegend();
200
                } catch (ReadDriverException e) {
201
                        throw new InitializeDriverException(getName(),e);
202
                }
203
        }
204

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

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

    
245
                                defaultLegend.addSymbol(clave, theSymbol);
246
                        }
247
                }
248
        }
249

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

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

    
270

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

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

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

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

    
315
                        Color c = getRandomColor();
316
                        if (defaultSymbol instanceof IMarkerSymbol) {
317
                                ((IMarkerSymbol) defaultSymbol).setColor(c);
318
                                ((IMarkerSymbol) defaultSymbol).setSize(3);
319

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

    
333
        /*
334
         *  (non-Javadoc)
335
         * @see com.iver.cit.gvsig.fmap.drivers.WithDefaultLegend#getDefaultLegend()
336
         */
337
        public ILegend getDefaultLegend() {
338
                if (defaultLegend == null){
339
                        defaultLegend = LegendFactory.createVectorialUniqueValueLegend(getShapeType());
340
                        defaultLegend.setClassifyingFieldNames(new String[] {KmlTags.FOLDER_NAME});
341
//                        this is never more handled by the legend, use a LabelingStrategy instead
342
//                        defaultLegend.setLabelField(null);
343
//                        defaultLegend.setLabelHeightField(null);
344
//                        defaultLegend.setLabelRotationField(null);
345
                        defaultLegend.setDefaultSymbol(getDefaultSymbol());
346
                }
347
                return defaultLegend;
348
        }
349

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

    
381
        public ILabelingStrategy getDefaultLabelingStrategy() {
382
                return labeler;
383
        }
384
}