Revision 10627 trunk/libraries/libFMap/src/com/iver/cit/gvsig/fmap/drivers/kml/KmlFMapDriver.java

View differences:

KmlFMapDriver.java
22 22
import org.gvsig.remoteClient.kml.KmlTags;
23 23
import org.gvsig.remoteClient.kml.exceptions.KmlException;
24 24

  
25
import com.hardcode.gdbms.engine.data.driver.DriverException;
25
import com.hardcode.gdbms.driver.exceptions.InitializeDriverException;
26
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
26 27
import com.hardcode.gdbms.engine.data.driver.ObjectDriver;
27 28
import com.hardcode.gdbms.engine.values.IntValue;
28 29
import com.hardcode.gdbms.engine.values.StringValue;
......
84 85
 *
85 86
 * $Id$
86 87
 * $Log$
87
 * Revision 1.1  2007-02-12 13:50:08  jorpiell
88
 * Revision 1.2  2007-03-06 17:09:00  caballero
89
 * Exceptions
90
 *
91
 * Revision 1.1  2007/02/12 13:50:08  jorpiell
88 92
 * A?adido el driver de KML
89 93
 *
90 94
 *
......
100 104
	private IGeometriesFactory factory = null;
101 105
	private KmlDriver driver = null;
102 106
	private IFeaturesIterator iterator = null;
103
	private static final float heightText = 10;	
104
	private Random r = null;	
107
	private static final float heightText = 10;
108
	private Random r = null;
105 109
	//it contains the position for the folder name.
106 110
	private int FOLDER_NAME_POSITION = 0;
107
	
111

  
108 112
	/*
109 113
	 *  (non-Javadoc)
110 114
	 * @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#open(java.io.File)
111 115
	 */
112
	public void open(File f) throws IOException {
116
	public void open(File f) {
113 117
		m_Fich = f;
114
		factory = new FMAPGeometryFactory();		
118
		factory = new FMAPGeometryFactory();
115 119
		r = new Random();
116 120
	}
117
	
121

  
118 122
	/*
119 123
	 *  (non-Javadoc)
120 124
	 * @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#close()
121 125
	 */
122
	public void close() throws IOException {
126
	public void close() {
123 127
		// TODO Auto-generated method stub
124
		
128

  
125 129
	}
126
	
130

  
127 131
	/*
128 132
	 *  (non-Javadoc)
129 133
	 * @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#initialize()
130 134
	 */
131
	public void initialize() throws IOException {	
135
	public void initialize() throws InitializeDriverException {
132 136
		try {
133 137
			driver = new KmlDriver(createInputStream(),factory);
134 138
			iterator = 	driver.getFeaturesIterator();
135 139
		} catch (BaseException e) {
136 140
			logger.error("Error openning the kml file",e);
137
			throw new IOException(e.toString());
138
		}				
139
					
140
		int index = 0;		
141
		
142
		IntValue clave;		
143
					
144
		boolean setModel = false;	
145
		FeatureWithAttributes feature;		
141
			throw new InitializeDriverException(getName(),e);
142
		}
143

  
144
		int index = 0;
145

  
146
		IntValue clave;
147

  
148
		boolean setModel = false;
149
		FeatureWithAttributes feature;
146 150
		try {
147
			while (iterator.hasNext()) {						
148
				try{	
151
			while (iterator.hasNext()) {
152
				try{
149 153
					feature = (FeatureWithAttributes)iterator.next();
150 154
					//There are features without geometry
151
					if (feature.getFeature().getGeometry() != null){						
155
					if (feature.getFeature().getGeometry() != null){
152 156
						if (!setModel){
153 157
							getTableModel().setColumnIdentifiers(feature.getAttributeName());
154 158
							setModel = true;
155 159
						}
156 160
						clave = ValueFactory.createValue(index);
157 161
						IGeometry geom = feature.getFeature().getGeometry();
158
						if (geom != null){							
159
							FSymbol theSymbol = null;							
162
						if (geom != null){
163
							FSymbol theSymbol = null;
160 164
							theSymbol = getDefaultSymbol();
161
							theSymbol.setDescription(clave.toString());													
162
							addGeometry(geom, feature.getAttributeValue());						
163
						}		
165
							theSymbol.setDescription(clave.toString());
166
							addGeometry(geom, feature.getAttributeValue());
167
						}
164 168
					}
165
					index++;				
169
					index++;
166 170
				}catch(GMLException e){
167 171
					logger.error(e.getMessage(),e);
168 172
					e.printStackTrace();
169 173
				}
170
			}			
174
			}
171 175
		}catch (BaseException e) {
172 176
			logger.error("Error retrieving the geometries",e);
173 177
			e.printStackTrace();
174
		}	
175
		
176
		createLegend();
178
		}
179

  
180
		try {
181
			createLegend();
182
		} catch (ReadDriverException e) {
183
			throw new InitializeDriverException(getName(),e);
184
		}
177 185
	}
178 186

  
179 187
	/**
180 188
	 * Creates the legend. The unique value is the field
181 189
	 * that is specified in the KmlTags.FOLDER_NAME constant
182
	 * @throws IOException 
190
	 * @throws IOException
183 191
	 */
184
	private void createLegend() throws IOException{
192
	private void createLegend() throws ReadDriverException{
185 193
		ObjectDriver rs = this;
186 194
		StringValue clave;
187
		FSymbol theSymbol = null;		
188
		try {
189
			for (long j = 0; j < rs.getRowCount(); j++) {
190
				clave = (StringValue) rs.getFieldValue(j, FOLDER_NAME_POSITION);
191
				if (((VectorialUniqueValueLegend)getDefaultLegend()).getSymbolByValue(clave) == null) {
192
					theSymbol = new FSymbol(getShapeType());
193
					theSymbol.setDescription(clave.toString());
194
					Color c = getRandomColor();
195
					theSymbol.setColor(c);					
196
					theSymbol.setStyle(FConstant.SYMBOL_STYLE_FILL_SOLID);
197
					theSymbol.setSize(3);
198
					theSymbol.setSizeInPixels(true);					
199
					defaultLegend.addSymbol(clave, theSymbol);
200
				}
201
			} 
202
		} catch (DriverException e) {
203
			e.printStackTrace();
204
			throw new IOException("Error creating the legend for the Kml file");			
195
		FSymbol theSymbol = null;
196
		for (long j = 0; j < rs.getRowCount(); j++) {
197
			clave = (StringValue) rs.getFieldValue(j, FOLDER_NAME_POSITION);
198
			if (((VectorialUniqueValueLegend)getDefaultLegend()).getSymbolByValue(clave) == null) {
199
				theSymbol = new FSymbol(getShapeType());
200
				theSymbol.setDescription(clave.toString());
201
				Color c = getRandomColor();
202
				theSymbol.setColor(c);
203
				theSymbol.setStyle(FConstant.SYMBOL_STYLE_FILL_SOLID);
204
				theSymbol.setSize(3);
205
				theSymbol.setSizeInPixels(true);
206
				defaultLegend.addSymbol(clave, theSymbol);
207
			}
205 208
		}
206 209
	}
207
	
210

  
208 211
	/*
209 212
	 *  (non-Javadoc)
210 213
	 * @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#accept(java.io.File)
......
216 219
		}
217 220
		return false;
218 221
	}
219
	
222

  
220 223
	/*
221 224
	 *  (non-Javadoc)
222 225
	 * @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#getFile()
......
224 227
	public File getFile() {
225 228
		return m_Fich;
226 229
	}
227
	
228
	
230

  
231

  
229 232
	/*
230 233
	 *  (non-Javadoc)
231 234
	 * @see com.hardcode.driverManager.Driver#getName()
......
233 236
	public String getName() {
234 237
		return "gvSIG KML Memory Driver";
235 238
	}
236
	
239

  
237 240
	/*
238 241
	 *  (non-Javadoc)
239 242
	 * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getShapeType()
......
241 244
	public int getShapeType() {
242 245
		return FShape.MULTI;
243 246
	}
244
	
247

  
245 248
	/**
246
	 * It returns a random color 
249
	 * It returns a random color
247 250
	 * @return
248 251
	 */
249 252
	private Color getRandomColor(){
250
		return new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255));		
251
	}	
252
	
253
		return new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255));
254
	}
255

  
253 256
	/**
254 257
	 * Creates the default symbol
255 258
	 * @return
......
280 283
			defaultLegend.setLabelField(null);
281 284
			defaultLegend.setLabelHeightField(null);
282 285
			defaultLegend.setLabelRotationField(null);
283
			defaultLegend.setDefaultSymbol(getDefaultSymbol());				
286
			defaultLegend.setDefaultSymbol(getDefaultSymbol());
284 287
		}
285 288
		return defaultLegend;
286
	}  
287
	
289
	}
290

  
288 291
	/**
289
	 * It creates an InputStream. The Kml file can have the 
290
	 * KML or the KMZ extension 
292
	 * It creates an InputStream. The Kml file can have the
293
	 * KML or the KMZ extension
291 294
	 * @return
292
	 * @throws KmlException 
295
	 * @throws KmlException
293 296
	 */
294 297
	private InputStream createInputStream() throws KmlException{
295 298
		try {
296 299
			if (getFile().getName().toUpperCase().endsWith("KML")){
297
				return new FileInputStream(getFile());				
300
				return new FileInputStream(getFile());
298 301
			}else if(getFile().getName().toUpperCase().endsWith("KMZ")){
299 302
				FileInputStream fis = new FileInputStream(getFile());
300
				ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));				
303
				ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
301 304
				ZipEntry entry = null;
302 305
				while((entry = zis.getNextEntry()) != null){
303 306
					if (!entry.isDirectory()){
......
312 315
			throw new KmlException(getFile(),e);
313 316
		} catch (IOException e) {
314 317
			throw new KmlException(getFile(),e);
315
		}	
318
		}
316 319
		return null;
317
	} 
320
	}
318 321
}

Also available in: Unified diff