Revision 32088

View differences:

branches/v2_0_0_prep/libraries/libGeocoding/src/org/gvsig/geocoding/impl/LuceneGeocoderImpl.java
60 60
import org.gvsig.geocoding.address.ComposedAddress;
61 61
import org.gvsig.geocoding.address.Literal;
62 62
import org.gvsig.geocoding.address.NumberAddress;
63
import org.gvsig.geocoding.index.FeatureIndexedEvent;
64
import org.gvsig.geocoding.index.IndexListener;
63 65
import org.gvsig.geocoding.pattern.GeocodingPattern;
64 66
import org.gvsig.geocoding.result.GeocodingResult;
65 67
import org.gvsig.geocoding.result.ScoredFeature;
......
85 87
	private Logger log = LoggerFactory.getLogger(LuceneGeocoderImpl.class);
86 88
	private GeocodingPattern pattern = null;
87 89
	private DataStore store = null;
90
	@SuppressWarnings("unused")
88 91
	private DataManager manager = null;
92
	private int position = 0;
93
	
94

  
95
	private IndexListener indexListener;
96
	
89 97
	/*
90 98
	 * This path is a temporal folder. This code is a copy of the
91 99
	 * libRemoteServices and LibRaster
......
153 161
	}
154 162

  
155 163
	/**
156
	 * Geocoding process
157 164
	 * 
158
	 * @param address
159
	 * @return results
160 165
	 */
161 166
	public Set<GeocodingResult> geocode(Address address)
162 167
			throws LocatorException, DataException {
......
231 236
	}
232 237

  
233 238
	/**
234
	 * First scouting process, search literal elements in data store and return
235
	 * a list with ok scored features
236 239
	 * 
237 240
	 * @param relationsLiteral
238 241
	 * @param addressLiteral
......
247 250

  
248 251
		double minScore = getPattern().getSettings().getScore();
249 252

  
250
		// Create index writer
251
		IndexWriter indexWriter = null;
252
		try {
253
			indexWriter = createIndex(store, relationsLiteral);
254
		} catch (Exception e) {
255
			log.error("Error creating index", e);
256
		}
257
		// if index writer is not null run search process
258
		if (indexWriter != null) {
259
			// search
260
			Hits hits = null;
253
		// search
254
		Hits hits =  seachOnIndex(relationsLiteral, addressLiteral);
255
	
256
		// if there are hits in search process
257
		if (hits != null) {
258
			// filter results
261 259
			try {
262
				hits = seachOnIndex(relationsLiteral, addressLiteral);
260
				scorefeats = filterHitsByScore(store, hits, minScore);
263 261
			} catch (Exception e) {
264
				log.error("Error searching on index", e);
262
				log.error("Error filtering hits by score", e);
263
				scorefeats.clear();
265 264
			}
266
			// if there are hits on search process
267
			if (hits != null || hits.length() > 0) {
268
				// filter
269
				try {
270
					scorefeats = filterHitsByScore(store, hits, minScore);
271
				} catch (Exception e) {
272
					log.error("Error filtering hits by score", e);
273
				}
274

  
275
			}
276 265
		}
277 266

  
278 267
		return scorefeats;
......
283 272
	 * @param relationsLiteral
284 273
	 * @param addressLiteral
285 274
	 * @return
286
	 * @throws CorruptIndexException
287
	 * @throws IOException
288
	 * @throws ParseException
289 275
	 */
290 276
	private Hits seachOnIndex(Literal relationsLiteral, Literal addressLiteral)
291
			throws CorruptIndexException, IOException, ParseException {
277
			 {
292 278

  
293 279
		log.info("Buscar ...");
294 280

  
295
		IndexSearcher searcher = new IndexSearcher(tempDirectoryPath);
281
		IndexSearcher searcher = null;
282
		try {
283
			searcher = new IndexSearcher(tempDirectoryPath);
284
		} catch (Exception e) {
285
			log.debug("Error building index searcher", e);
286
			log.debug("Rebuild the index file because the initial index is wrong");
287
			try {
288
				this.indexer(store, relationsLiteral);
289
			} catch (Exception e1) {
290
				log.debug("Error building index searcher", e1);
291
				return null;
292
			}
293
		}
294
		if (searcher != null) {
295
			String[] input = addressLiteral.getStringValuesArray();
296
			String[] fields = relationsLiteral.getStringValuesArray();
296 297

  
297
		String[] input = addressLiteral.getStringValuesArray();
298
		String[] fields = relationsLiteral.getStringValuesArray();
299

  
300
		Query query = MultiFieldQueryParser.parse(input, fields,
301
				new StandardAnalyzer());
302
		Hits hits = searcher.search(query);
303

  
304
		return hits;
298
			Query query = null;
299
			try {
300
				query = MultiFieldQueryParser.parse(input, fields,
301
						new StandardAnalyzer());
302
			} catch (ParseException e) {
303
				log.debug("Error building the query", e);
304
				return null;
305
			}
306
			Hits hits = null;
307
			if(query != null){
308
				try {
309
					hits = searcher.search(query);
310
				} catch (IOException e) {
311
					log.debug("Error in the searching process", e);
312
					return null;
313
				}
314
			}
315
			return hits;
316
		} else {
317
			return null;
318
		}
305 319
	}
306 320

  
307 321
	/**
308 322
	 * 
323
	 * @param store
309 324
	 * @param hits
325
	 * @param score
310 326
	 * @return
311 327
	 * @throws IOException
312 328
	 * @throws DataException
......
317 333
		FeatureStore fstore = (FeatureStore) store;
318 334
		List<ScoredFeature> scoreFeats = new ArrayList<ScoredFeature>();
319 335
		for (int i = 0; i < hits.length(); i++) {
320
			if (hits.score(i) > score) {
336
			if (hits.score(i) * 100 > score) {
321 337
				Document doc = hits.doc(i);
322 338
				String id = doc.get(GeocoID);
323 339
				FeatureSet fset = fstore.getFeatureSet();
......
336 352
	 * 
337 353
	 * @param store
338 354
	 * @param relationsLiteral
339
	 * @return
340 355
	 * @throws DataException
341 356
	 * @throws CorruptIndexException
342 357
	 * @throws LockObtainFailedException
343 358
	 * @throws IOException
344 359
	 */
345
	private IndexWriter createIndex(DataStore store, Literal relationsLiteral)
360
	public void indexer(DataStore store, Literal relationsLiteral)
346 361
			throws DataException, CorruptIndexException,
347 362
			LockObtainFailedException, IOException {
348 363

  
349
		log.info("Create indexes ...");
364
		log.debug("Create indexes ...");
350 365

  
351 366
		IndexWriter indexWriter = new IndexWriter(tempDirectoryPath,
352 367
				new StandardAnalyzer(), true);
......
358 373
		Feature feat = null;
359 374

  
360 375
		Iterator it = features.fastIterator();
376
		// process situation
377
		this.position = 0;
361 378
		while (it.hasNext()) {
362 379

  
363 380
			Document doc = new org.apache.lucene.document.Document();
......
377 394
				doc.add(field);
378 395
			}
379 396
			indexWriter.addDocument(doc);
397
			// throw indexed event
398
			if(this.indexListener != null){
399
				this.indexListener.featureIndexed(new FeatureIndexedEvent(this));
400
			}			
401
			// new position
402
			this.position++;
380 403
		}
381 404
		indexWriter.optimize();
382

  
383
		return indexWriter;
384 405
	}
406
	
407
	
408
	
409
	/**
410
	 * 
411
	 * @param listener
412
	 */
413
	public void registreIndexListener(IndexListener listener){
414
		this.indexListener = listener;
415
	}
416
	
417
	/**
418
	 * 
419
	 * @return
420
	 */
421
	public int getIndexPosition() {
422
		return position;
423
	}
385 424

  
386 425
}
branches/v2_0_0_prep/libraries/libGeocoding/src/org/gvsig/geocoding/styles/impl/Composed.java
63 63

  
64 64
public class Composed extends AbstractGeocodingStyle {
65 65

  
66
	private static final Logger log = LoggerFactory.getLogger(Composed.class);	
66
	private static final Logger log = LoggerFactory.getLogger(Composed.class);
67 67

  
68 68
	private List<Literal> intersectsLiterals = new ArrayList<Literal>();
69 69

  
......
117 117
									.geometryToJts(secFeat.getFeature()
118 118
											.getDefaultGeometry());
119 119
							if (mainGeomJTS.touches(secGeomJTS)) {
120
								Point pto = MatcherUtils.intersectTwoLinesJTS(
121
										mainGeomJTS, secGeomJTS);
122
								GeomMatchResult res = new GeomMatchResult();
123
								// geom
124
								res.setGeom(pto);
125
								// features scored
126
								List<ScoredFeature> mainSources = new ArrayList<ScoredFeature>();
127
								mainSources.add(mainFeat);
128
								res.setMainSources(mainSources);
129
								List<ScoredFeature> secSources = new ArrayList<ScoredFeature>();
130
								secSources.add(secFeat);
131
								res.setSecondSources(secSources);
132
								// address
133
								res.setAddress(getResultAddress(mainSources,
134
										secSources, null));
135
								results.add(res);
120
								List<Point> ptos = MatcherUtils
121
										.intersectTwoLinesJTS(mainGeomJTS,
122
												secGeomJTS);
123
								if (!(ptos.isEmpty())) {
124
									for (int i = 0; i < ptos.size(); i++) {
125
										GeomMatchResult res = new GeomMatchResult();
126
										// geom
127
										res.setGeom(ptos.get(i));
128
										// features scored
129
										List<ScoredFeature> mainSources = new ArrayList<ScoredFeature>();
130
										mainSources.add(mainFeat);
131
										res.setMainSources(mainSources);
132
										List<ScoredFeature> secSources = new ArrayList<ScoredFeature>();
133
										secSources.add(secFeat);
134
										res.setSecondSources(secSources);
135
										// address
136
										res.setAddress(getResultAddress(
137
												mainSources, secSources, null));
138
										results.add(res);
139
									}
140
								}
136 141
							}
137 142
						}
138 143
					}
......
201 206
		return results;
202 207
	}
203 208

  
204
	
205 209
	/**
206 210
	 * 
207 211
	 * @param mainSources
208 212
	 * @param secSources
209 213
	 * @return
210
	 * @throws DataException 
214
	 * @throws DataException
211 215
	 */
212 216
	private ComposedAddress getResultAddress(List<ScoredFeature> mainSources,
213
			List<ScoredFeature> secSources) throws DataException{
217
			List<ScoredFeature> secSources) throws DataException {
214 218
		// get the first element
215 219
		Feature mainfeat = mainSources.get(0).getFeature();
216 220
		Feature secfeat = secSources.get(0).getFeature();
217 221

  
218 222
		Literal relLiteral = this.getRelationsLiteral();
219
		Literal mainliteral = createLiteralFromFeature(mainfeat,relLiteral);
220
		Literal secliteral = createLiteralFromFeature(secfeat,relLiteral);
221
		
223
		Literal mainliteral = createLiteralFromFeature(mainfeat, relLiteral);
224
		Literal secliteral = createLiteralFromFeature(secfeat, relLiteral);
225

  
222 226
		ComposedAddress address = (ComposedAddress) new DefaultComposedAddress();
223 227
		address.setMainLiteral(mainliteral);
224
		
228

  
225 229
		List<Literal> intersectionLiteralList = new ArrayList<Literal>();
226 230
		intersectionLiteralList.add(secliteral);
227 231
		address.setIntersectionLiterals(intersectionLiteralList);
228
		
232

  
229 233
		return address;
230 234
	}
231
	
232
	
233
	
235

  
234 236
	/**
235 237
	 * 
236 238
	 * @return
237
	 * @throws DataException 
239
	 * @throws DataException
238 240
	 */
239 241
	private Address getResultAddress(List<ScoredFeature> mainSources,
240
			List<ScoredFeature> secSources, List<ScoredFeature> thiSources) throws DataException {
242
			List<ScoredFeature> secSources, List<ScoredFeature> thiSources)
243
			throws DataException {
241 244

  
242
		ComposedAddress address = this.getResultAddress(mainSources, secSources);
245
		ComposedAddress address = this
246
				.getResultAddress(mainSources, secSources);
247

  
248
		// get the first element
243 249
		
244
		
245
		// get the first element
246
		Feature thifeat = thiSources.get(0).getFeature();
250
		if (thiSources != null) {
251
			Feature thifeat = thiSources.get(0).getFeature();
252
			Literal relLiteral = this.getRelationsLiteral();
253
			Literal thiliteral = createLiteralFromFeature(thifeat, relLiteral);
247 254

  
248
		Literal relLiteral = this.getRelationsLiteral();		
249
		Literal thiliteral = createLiteralFromFeature(thifeat,relLiteral);		
250
		
251
		address.getIntersectionLiterals().add(thiliteral);
252
		
255
			address.getIntersectionLiterals().add(thiliteral);
256
		}
257

  
253 258
		return address;
254 259
	}
255 260

  
......
279 284
		super.getRelationsLiteral().clear();
280 285
		XMLEntity xml2 = xml.getChild(0);
281 286
		super.getRelationsLiteral().setXMLEntity(xml2);
282
		
287

  
283 288
		this.intersectsLiterals.clear();
284 289
		for (int i = 1; i < xml.getChildrenCount(); i++) {
285 290
			Literal lit = new DefaultLiteral();
branches/v2_0_0_prep/libraries/libGeocoding/src/org/gvsig/geocoding/address/impl/DefaultComposedAddress.java
28 28
package org.gvsig.geocoding.address.impl;
29 29

  
30 30
import java.util.ArrayList;
31
import java.util.Iterator;
31 32
import java.util.List;
32 33
import java.util.Map;
34
import java.util.Map.Entry;
33 35

  
34 36
import org.gvsig.geocoding.address.ComposedAddress;
35 37
import org.gvsig.geocoding.address.Literal;
......
106 108
	 * TODO check newliteral components match mainliteral ones
107 109
	 */
108 110
	public boolean isValidate(Literal newLiteral) {
109

  
110
		return false;
111
				
112
		Iterator it = super.getMainLiteral().entrySet().iterator();
113
		Iterator it2 = newLiteral.entrySet().iterator();
114
		try{
115
		while (it.hasNext()) {
116
			Entry entry1 = (Entry)it.next();			
117
			Entry entry2 = (Entry)it2.next();
118
			String key1 = (String)entry1.getKey();
119
			String key2 = (String)entry2.getKey();
120
			
121
			if(key1.trim().compareTo(key2.trim())==0){
122
				// true
123
			}
124
			else{
125
				return false;
126
			}
127
		}
128
		}catch(Exception e){
129
			return false;
130
		}
131
		
132
		
133
		return true;
111 134
	}
112 135
	
113 136
	/**
branches/v2_0_0_prep/libraries/libGeocoding/src/org/gvsig/geocoding/Geocoder.java
29 29

  
30 30
import java.util.Set;
31 31

  
32
import org.gvsig.fmap.dal.DataStore;
32 33
import org.gvsig.fmap.dal.exception.DataException;
33 34
import org.gvsig.fmap.dal.exception.InitializeException;
34 35
import org.gvsig.geocoding.address.Address;
36
import org.gvsig.geocoding.pattern.GeocodingPattern;
35 37
import org.gvsig.geocoding.result.GeocodingResult;
36 38
import org.gvsig.tools.locator.LocatorException;
37 39

  
......
55 57
	 */
56 58
	public Set<GeocodingResult> geocode(Address address)
57 59
			throws InitializeException, LocatorException, DataException;
60
	
61
	/**
62
	 * 
63
	 * @param pattern
64
	 */
65
	public void setPattern(GeocodingPattern pattern);
66
	
67
	/**
68
	 * 
69
	 * @param store
70
	 */
71
	public void setStore(DataStore store);
58 72

  
59 73
}
branches/v2_0_0_prep/libraries/libGeocoding/src/org/gvsig/geocoding/index/FeatureInxededEvent.java
1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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

  
23
/*
24
 * AUTHORS (In addition to CIT):
25
 * 2008 Prodevelop S.L. main development
26
 */
27

  
28
package org.gvsig.geocoding.index;
29

  
30
import java.util.EventObject;
31

  
32

  
33

  
34
public class FeatureInxededEvent extends EventObject {
35

  
36
	
37
	private static final long serialVersionUID = 1L;
38
	
39
	private long positionFeature;
40
	
41
	public long getPositionFeature() {
42
		return positionFeature;
43
	}
44

  
45
	public void setPositionFeature(long positionFeature) {
46
		this.positionFeature = positionFeature;
47
	}
48

  
49
	public FeatureInxededEvent(Object source) {
50
		super(source);
51
		// TODO Auto-generated constructor stub
52
	}
53

  
54
}
branches/v2_0_0_prep/libraries/libGeocoding/src/org/gvsig/geocoding/index/IndexListener.java
31 31

  
32 32
public interface IndexListener extends EventListener {
33 33

  
34
	public void featureIndexed(FeatureInxededEvent e);
34
	public void featureIndexed(FeatureIndexedEvent e);
35 35
}
branches/v2_0_0_prep/libraries/libGeocoding/src/org/gvsig/geocoding/index/FeatureIndexedEvent.java
29 29

  
30 30
import java.util.EventObject;
31 31

  
32
import org.gvsig.geocoding.impl.LuceneGeocoderImpl;
32 33

  
33 34

  
35

  
34 36
public class FeatureIndexedEvent extends EventObject {
35 37

  
36 38
	
......
40 42
	
41 43
	public long getPositionFeature() {
42 44
		return positionFeature;
43
	}
45
	}	
44 46

  
45
	public void setPositionFeature(long positionFeature) {
46
		this.positionFeature = positionFeature;
47
	}
48

  
49 47
	public FeatureIndexedEvent(Object source) {
50 48
		super(source);
51
		// TODO Auto-generated constructor stub
52
	}
49
		this.positionFeature = ((LuceneGeocoderImpl)source).getIndexPosition();
50
	}	
53 51

  
54 52
}
branches/v2_0_0_prep/libraries/libGeocoding/src/org/gvsig/geocoding/geommatches/MatcherUtils.java
144 144
	 * @param geom2Jts
145 145
	 * @return
146 146
	 */
147
	public static Point intersectTwoLinesJTS(
147
	public static List<Point> intersectTwoLinesJTS(
148 148
			com.vividsolutions.jts.geom.Geometry geom1Jts,
149 149
			com.vividsolutions.jts.geom.Geometry geom2Jts) {
150 150

  
151
		com.vividsolutions.jts.geom.Geometry interBBoxJTS = null;
152
		com.vividsolutions.jts.geom.Geometry geomPointJTS = null;
151
//		com.vividsolutions.jts.geom.Geometry interBBoxJTS = null;
152
//		com.vividsolutions.jts.geom.Geometry geomPointJTS = null;
153
		
154
		List<Point> points = new ArrayList<Point>();
153 155

  
154 156
		if (geom1Jts != null && geom2Jts != null) {
155 157

  
156
			interBBoxJTS = OverlayOp.overlayOp(geom1Jts.getEnvelope(), geom2Jts
157
					.getEnvelope(), OverlayOp.INTERSECTION);
158
			if (interBBoxJTS.getGeometryType().compareTo("LineString") == 0
159
					|| interBBoxJTS.getGeometryType().compareTo("Polygon") == 0) {
160
				log.debug("Intersect: Intersect two BBOX");
161
				geomPointJTS = OverlayOp.overlayOp(geom1Jts, geom2Jts,
162
						OverlayOp.INTERSECTION);
163

  
164
				if (geomPointJTS.getGeometryType().compareTo("Point") == 0) {
165
					log.debug("Intersect: Intersect in the point X= "
166
							+ geomPointJTS.getCoordinate().x + " Y= "
167
							+ geomPointJTS.getCoordinate().y);
168
					Point pto = getPoint(geomPointJTS.getCoordinate().x,
169
							geomPointJTS.getCoordinate().y);
170

  
171
					return pto;
172
				} else {
173
					log.debug("Intersect: Two lines don't intersect");
174
					return null;
158
			com.vividsolutions.jts.geom.Geometry ugeoms = geom1Jts.intersection(geom2Jts);
159
			
160
			if(ugeoms instanceof com.vividsolutions.jts.geom.Point){
161
				com.vividsolutions.jts.geom.Point ptojts = (com.vividsolutions.jts.geom.Point)ugeoms;
162
				Point pto = getPoint(ptojts.getCoordinate().x,
163
						ptojts.getCoordinate().y);
164
				points.clear();
165
				points.add(pto);
166
				return points;
167
			}else if(ugeoms instanceof com.vividsolutions.jts.geom.MultiPoint){
168
				com.vividsolutions.jts.geom.MultiPoint ptosjts = (com.vividsolutions.jts.geom.MultiPoint)ugeoms;
169
				Coordinate[] coords = ptosjts.getCoordinates();
170
				points.clear();
171
				for (int i = 0; i < coords.length; i++) {
172
					Coordinate coor = coords[i];
173
					Point pto = getPoint(coor.x,coor.y);
174
					points.add(pto);
175 175
				}
176
			} else {
177
				log.debug("Intersect: Two BBOX don't intersect");
178
				return null;
176
				
177
				return points;
179 178
			}
179
			
180
//			interBBoxJTS = OverlayOp.overlayOp(geom1Jts.getEnvelope(), geom2Jts
181
//					.getEnvelope(), OverlayOp.INTERSECTION);
182
//			if (interBBoxJTS.getGeometryType().compareTo("LineString") == 0
183
//					|| interBBoxJTS.getGeometryType().compareTo("Polygon") == 0) {
184
//				log.debug("Intersect: Intersect two BBOX");
185
//				geomPointJTS = OverlayOp.overlayOp(geom1Jts, geom2Jts,
186
//						OverlayOp.INTERSECTION);
187
//
188
//				if (geomPointJTS.getGeometryType().compareTo("Point") == 0) {
189
//					
190
//					Point pto = getPoint(geomPointJTS.getCoordinate().x,
191
//							geomPointJTS.getCoordinate().y);
192
//					points.add(pto);
193
//					return points;
194
//				} else if (geomPointJTS.getGeometryType().compareTo("MultiPoint") == 0) {
195
//					log.debug("Intersect: Intersect );
196
//					Point pto = getPoint(geomPointJTS.getCoordinate().x,
197
//							geomPointJTS.getCoordinate().y);
198
//					points.add(pto);
199
//					return points;
200
//				}
201
//			} else {
202
//				log.debug("Intersect: Two BBOX don't intersect");
203
//				return null;
204
//			}
180 205
		}
181
		log.debug("Some Geometries are NULL  ......");
182
		return null;
206
		log.debug("Intersect: Two lines nor intersect or not touch");
207
		return points;
183 208
	}
184 209

  
185 210
	/**

Also available in: Unified diff