View Javadoc
1   /*******************************************************************************
2    *   Gisgraphy Project 
3    * 
4    *   This library is free software; you can redistribute it and/or
5    *   modify it under the terms of the GNU Lesser General Public
6    *   License as published by the Free Software Foundation; either
7    *   version 2.1 of the License, or (at your option) any later version.
8    * 
9    *   This library is distributed in the hope that it will be useful,
10   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   *   Lesser General Public License for more details.
13   * 
14   *   You should have received a copy of the GNU Lesser General Public
15   *   License along with this library; if not, write to the Free Software
16   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
17   * 
18   *  Copyright 2008  Gisgraphy project 
19   *  David Masclet <davidmasclet@gisgraphy.com>
20   *  
21   *  
22   *******************************************************************************/
23  package com.gisgraphy.reversegeocoding;
24  
25  import java.util.SortedSet;
26  
27  import org.springframework.stereotype.Service;
28  
29  import com.gisgraphy.addressparser.Address;
30  import com.gisgraphy.addressparser.commons.GeocodingLevels;
31  import com.gisgraphy.domain.geoloc.entity.HouseNumber;
32  import com.gisgraphy.domain.geoloc.entity.OpenStreetMap;
33  import com.gisgraphy.helper.GeolocHelper;
34  import com.vividsolutions.jts.geom.Point;
35  
36  
37  /**
38   * Some useful method for housenumber
39   * 
40   * @author <a href="mailto:david.masclet@gisgraphy.com">David Masclet</a>
41   */
42  @Service
43  public class AddressHelper {
44  	
45  	public  HouseNumberDistance getNearestHouse(SortedSet<HouseNumber> houses, Point location){
46  		if (location==null || houses==null || houses.size()==0){
47  			return null;
48  		}
49  		if (houses.size()==1){
50  			HouseNumber housenumber = houses.first();
51  			if (housenumber!=null){
52  				if (housenumber.getLocation()!=null){
53  					return new HouseNumberDistance(housenumber, GeolocHelper.distance(housenumber.getLocation(), location));
54  				} else {
55  					return new HouseNumberDistance(housenumber, null);
56  				}
57  			} else {
58  				return null;
59  			}
60  			
61  		} else {
62  			Double smallestDistance = null;
63  			HouseNumber nearestHouse = null;
64  			for (HouseNumber house:houses){
65  				if (house!=null && house.getLocation()!=null){
66  					Double distance = GeolocHelper.distance(house.getLocation(), location) ;
67  					if (nearestHouse==null){
68  						smallestDistance=distance;
69  						nearestHouse= house;
70  						continue;
71  					} else if (distance <= smallestDistance){
72  						nearestHouse = house;
73  						smallestDistance=distance;
74  					} //house number are sorted by name, we can may be consider that if distance is greater is increase the last smallest distance is the nearest, but it is not,
75  					//the street can have circle or strange shape
76  				}
77  			}
78  			return new HouseNumberDistance(nearestHouse, smallestDistance);
79  		}
80  	}
81  	
82  	public  Address buildAddressFromOpenstreetMap(OpenStreetMap openStreetMap) {
83  		if (openStreetMap==null){
84  			return null;
85  		}
86  		Address address = new Address();
87  		if (openStreetMap.getName()!=null){
88  			address.setStreetName(openStreetMap.getName());
89  		}
90  		if (openStreetMap.getIsIn()!=null){
91  			address.setCity(openStreetMap.getIsIn());
92  		}
93  		if (openStreetMap.getIsInPlace()!=null){
94  			address.setCitySubdivision(openStreetMap.getIsInPlace());
95  		}
96  		if (openStreetMap.getIsInAdm()!=null){
97  			address.setState(openStreetMap.getIsInAdm());
98  		}
99  		if (openStreetMap.getIsInZip()!=null && openStreetMap.getIsInZip().size() >=1){  
100 			address.setZipCode(openStreetMap.getIsInZip().iterator().next());
101 		}
102 		if (openStreetMap.getLocation()!=null){
103 			address.setLng(openStreetMap.getLongitude());
104 			address.setLat(openStreetMap.getLatitude());
105 		}
106 		if (openStreetMap.getCountryCode()!=null){
107 			address.setCountryCode(openStreetMap.getCountryCode());
108 		}
109 		address.setGeocodingLevel(GeocodingLevels.STREET);//We set it and don't calculate it cause if streetname is null
110 		//geocoding level will be street
111 		
112 		return address;
113 	}
114 
115 	public  Address buildAddressFromOpenstreetMapAndPoint(OpenStreetMap openStreetMap, Point point) {
116 		if (openStreetMap==null || point == null){
117 			return null;
118 		}
119 		Address address = buildAddressFromOpenstreetMap(openStreetMap);
120 		if (openStreetMap.getLocation()!=null){
121 			address.setDistance(GeolocHelper.distance(point, openStreetMap.getLocation()));
122 		}
123 		
124 		return address;
125 	}
126 
127 	public  Address buildAddressFromHouseNumberDistance(HouseNumberDistance houseNumberDistance) {
128 		if (houseNumberDistance==null || houseNumberDistance.getHouseNumber()==null){
129 			return null;
130 		}
131 		Address address = buildAddressFromOpenstreetMap(houseNumberDistance.getHouseNumber().getStreet());
132 		if (houseNumberDistance.getHouseNumber().getLatitude()!=null){
133 			address.setLat(houseNumberDistance.getHouseNumber().getLatitude());
134 		}
135 		if (houseNumberDistance.getHouseNumber().getLongitude()!=null){
136 			address.setLng(houseNumberDistance.getHouseNumber().getLongitude());
137 		}
138 		if (houseNumberDistance.getHouseNumber().getNumber()!=null){
139 			address.setHouseNumber(houseNumberDistance.getHouseNumber().getNumber());
140 		}
141 		if (houseNumberDistance.getHouseNumber().getName()!=null){
142 			address.setName(houseNumberDistance.getHouseNumber().getName());
143 		}
144 		address.setDistance(houseNumberDistance.getDistance());
145 		address.setGeocodingLevel(GeocodingLevels.HOUSE_NUMBER);
146 		return address;
147 	}
148 
149 }