View Javadoc
1   package nl.mineleni.cbsviewer.servlet.gazetteer.lusclient;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   
6   import nl.mineleni.openls.databinding.openls.Address;
7   import nl.mineleni.openls.databinding.openls.GeocodeResponse;
8   import nl.mineleni.openls.databinding.openls.GeocodeResponseList;
9   import nl.mineleni.openls.databinding.openls.GeocodedAddress;
10  
11  /**
12   * Utility klasse OpenLSClientUtil.
13   * 
14   * @author mprins
15   * @since 1.7
16   */
17  public final class OpenLSClientUtil {
18  
19  	/** De constante PLACE_TYPE_COUNTRYSUBDIVISION. {@value} */
20  	public static final String PLACE_TYPE_COUNTRYSUBDIVISION = "CountrySubdivision";
21  
22  	/** De constante PLACE_TYPE_MUNICIPALITY. {@value} */
23  	public static final String PLACE_TYPE_MUNICIPALITY = "Municipality";
24  
25  	/** De constante PLACE_TYPE_MUNICIPALITYSUBDIVISION. {@value} */
26  	public static final String PLACE_TYPE_MUNICIPALITYSUBDIVISION = "MunicipalitySubdivision";
27  
28  	/**
29  	 * private constructor voor deze utility klasse.
30  	 */
31  	private OpenLSClientUtil() {
32  		/* private constructor voor deze utility klasse. */
33  	}
34  
35  	/**
36  	 * Gets the geocoded address list.
37  	 * 
38  	 * @param gcr
39  	 *            the gcr
40  	 * @return the geocoded address list
41  	 */
42  	public static List<GeocodedAddress> getGeocodedAddressList(
43  			final GeocodeResponse gcr) {
44  		final List<GeocodedAddress> addressList = new ArrayList<>();
45  		for (int i = 0; i < gcr.getGeocodeResponseListSize(); i++) {
46  			final GeocodeResponseList gcrl = gcr.getGeocodeResponseListAt(i);
47  			for (int j = 0; j < gcrl.getGeocodedAddressSize(); j++) {
48  				final GeocodedAddress gca = gcrl.getGeocodedAddressAt(j);
49  				addressList.add(gca);
50  			}
51  		}
52  		return addressList;
53  	}
54  
55  	/**
56  	 * Gets the client address list uit een GeocodeResponse.
57  	 * 
58  	 * @param gcr
59  	 *            de GeocodeResponse
60  	 * @param max
61  	 *            het maximum aantal adressen om terug te geven
62  	 * @return the open ls client address list
63  	 */
64  	public static List<OpenLSClientAddress> getOpenLSClientAddressList(
65  			final GeocodeResponse gcr, final int... max) {
66  		final List<OpenLSClientAddress> addressList = new ArrayList<>();
67  		final List<GeocodedAddress> gcal = getGeocodedAddressList(gcr);
68  
69  		int listSize = gcal.size();
70  		if (max != null && max.length > 0) {
71  			listSize = (max[0] > listSize) ? listSize : max[0];
72  		}
73  
74  		for (int i = 0; i < listSize; i++) {
75  			final GeocodedAddress gca = gcal.get(i);
76  			final OpenLSClientAddress addr = new OpenLSClientAddress();
77  
78  			if (!gca.hasPoint() || (gca.getPoint().getPosSize() == 0)) {
79  				continue;
80  			}
81  
82  			if (gca.getPoint().getSrsName().equalsIgnoreCase("EPSG:28992")) {
83  				// afronden naar meters in het geval van Rijksdriekhoek
84  				// WORKAROUND voor bug in PDOK gazetteer service die belachelijk
85  				// hoge nauwkeurigheid hanteerd voor centroiden
86  				addr.setxCoord((gca.getPoint().getPosAt(0).getX()).intValue()
87  						+ "");
88  				addr.setyCoord((gca.getPoint().getPosAt(0).getY()).intValue()
89  						+ "");
90  			} else {
91  				addr.setxCoord((gca.getPoint().getPosAt(0).getX()).toString());
92  				addr.setyCoord((gca.getPoint().getPosAt(0).getY()).toString());
93  			}
94  
95  			if (gca.getAddress() != null) {
96  				final Address adr = gca.getAddress();
97  				if (adr.hasStreetAddress()
98  						&& adr.getStreetAddress().hasStreet()) {
99  					addr.setStreetName(adr.getStreetAddress().getStreet()
100 							.getStreet());
101 				}
102 				if (adr.hasStreetAddress()
103 						&& adr.getStreetAddress().hasBuilding()
104 						&& adr.getStreetAddress().getBuilding().hasNumber()) {
105 					addr.setStreetNumber(adr.getStreetAddress().getBuilding()
106 							.getNumber());
107 				}
108 				if (adr.hasPostalCode() && adr.getPostalCode().hasPostalCode()) {
109 					addr.setPostalCode(adr.getPostalCode().getPostalCode());
110 				}
111 				if (adr.getPlaceByType(PLACE_TYPE_COUNTRYSUBDIVISION) != null) {
112 					addr.setCountrySubdivision(adr
113 							.getPlaceByType(PLACE_TYPE_COUNTRYSUBDIVISION));
114 				}
115 				if (adr.getPlaceByType(PLACE_TYPE_MUNICIPALITY) != null) {
116 					addr.setMunicipality(adr
117 							.getPlaceByType(PLACE_TYPE_MUNICIPALITY));
118 				}
119 				if (adr.getPlaceByType(PLACE_TYPE_MUNICIPALITYSUBDIVISION) != null) {
120 					addr.setMunicipalitySubdivision(adr
121 							.getPlaceByType(PLACE_TYPE_MUNICIPALITYSUBDIVISION));
122 				}
123 			}
124 			if (addr.isValidClientAddress()) {
125 				addressList.add(addr);
126 			}
127 		}
128 		return addressList;
129 	}
130 }