View Javadoc
1   package com.gisgraphy.domain.valueobject;
2   
3   import java.util.HashSet;
4   import java.util.Set;
5   
6   import org.slf4j.Logger;
7   import org.slf4j.LoggerFactory;
8   
9   import com.gisgraphy.domain.geoloc.entity.Adm;
10  import com.gisgraphy.domain.geoloc.entity.Country;
11  import com.gisgraphy.domain.geoloc.entity.GisFeature;
12  import com.gisgraphy.domain.geoloc.entity.Street;
13  import com.gisgraphy.domain.geoloc.entity.ZipCode;
14  import com.gisgraphy.helper.URLUtils;
15  
16  public class GisFeatureDistanceFactory {
17      
18      protected static final Logger logger = LoggerFactory
19  	    .getLogger(GisFeatureDistance.class);
20      
21      public GisFeatureDistance fromGisFeature(GisFeature gisFeature, Double distance) {
22  	GisFeatureDistance gisFeatureDistance = new GisFeatureDistance();
23  	gisFeatureDistance.setDistance(distance) ;
24  	if (gisFeature != null) {
25  		
26  	    gisFeatureDistance.setId(gisFeature.getId());
27  	    gisFeatureDistance.setAdm1Code(gisFeature.getAdm1Code());
28  	    gisFeatureDistance.setAdm2Code(gisFeature.getAdm2Code());
29  	    gisFeatureDistance.setAdm3Code(gisFeature.getAdm3Code());
30  	    gisFeatureDistance.setAdm4Code(gisFeature.getAdm4Code());
31  
32  	    gisFeatureDistance.setAdm1Name(gisFeature.getAdm1Name());
33  	    gisFeatureDistance.setAdm2Name(gisFeature.getAdm2Name());
34  	    gisFeatureDistance.setAdm3Name(gisFeature.getAdm3Name());
35  	    gisFeatureDistance.setAdm4Name(gisFeature.getAdm4Name());
36  
37  	    if (gisFeature.getAsciiName() != null) {
38  		gisFeatureDistance.setAsciiName(gisFeature.getAsciiName().trim());
39  	    }
40  	    if (gisFeature.getCountryCode() != null) {
41  		gisFeatureDistance.setCountryCode(gisFeature.getCountryCode().toUpperCase());
42  	    }
43  	    gisFeatureDistance.setElevation(gisFeature.getElevation());
44  	    gisFeatureDistance.setFeatureClass(gisFeature.getFeatureClass());
45  	    gisFeatureDistance.setFeatureCode(gisFeature.getFeatureCode());
46  	    gisFeatureDistance.setFeatureId(gisFeature.getFeatureId());
47  	    gisFeatureDistance.setGtopo30(gisFeature.getGtopo30());
48  	    gisFeatureDistance.setLocation(gisFeature.getLocation());
49  	    gisFeatureDistance.setName(gisFeature.getName().trim());
50  	    gisFeatureDistance.setPopulation(gisFeature.getPopulation());
51  	    gisFeatureDistance.setTimezone(gisFeature.getTimezone());
52  	    gisFeatureDistance.setOpenstreetmapId(gisFeature.getOpenstreetmapId());
53  	    gisFeatureDistance.setAmenity(gisFeature.getAmenity());
54  	    gisFeatureDistance.setZipCodes(new HashSet<String>());//TODO tests zip without zipcode
55  		Set<ZipCode> gisFeatureZipCodes = gisFeature.getZipCodes();
56  			if (gisFeatureZipCodes != null){
57  				for (ZipCode zipCode :gisFeatureZipCodes){
58  				    gisFeatureDistance.getZipCodes().add(zipCode.getCode());
59  			    }
60  			}
61  		gisFeatureDistance.setPlaceType(gisFeature.getClass().getSimpleName()
62  		    .toLowerCase());
63  	    updateFields(gisFeatureDistance,null);
64  	    }
65  	return gisFeatureDistance;
66      }
67      
68      public GisFeatureDistance fromAdm(Adm adm, Double distance) {
69  	GisFeatureDistance gisFeatureDistance = fromGisFeature(adm, distance);
70  	gisFeatureDistance.setLevel(adm.getLevel());
71  	return gisFeatureDistance;
72      }
73      
74      public GisFeatureDistance fromStreet(Street street, Double distance) {
75  	GisFeatureDistance gisFeatureDistance = fromGisFeature(street,distance);
76  	gisFeatureDistance.setLength(street.getLength());
77  	gisFeatureDistance.setOneWay(street.isOneWay());
78  	gisFeatureDistance.setStreetType(street.getStreetType());
79  	gisFeatureDistance.setIsIn(street.getIsIn());
80  	gisFeatureDistance.setIsInAdm(street.getIsInAdm());
81  	if (street.getIsInZip()!=null && street.getIsInZip().size()>=1){
82  		//we took the first one
83  		gisFeatureDistance.setIsInZip(street.getIsInZip().iterator().next());
84  	}
85  	gisFeatureDistance.setIsInPlace(street.getIsInPlace());
86  	gisFeatureDistance.setFullyQualifiedAddress(street.getFullyQualifiedAddress());
87  	return gisFeatureDistance;
88      }
89      
90      public GisFeatureDistance fromCountry(Country country, Double distance) {
91  	GisFeatureDistance gisFeatureDistance = fromGisFeature(country, distance);
92  	gisFeatureDistance.setElevation(country.getElevation());
93  	gisFeatureDistance.setFeatureClass(country.getFeatureClass());
94  	gisFeatureDistance.setFeatureCode(country.getFeatureCode());
95  	gisFeatureDistance.setFeatureId(country.getFeatureId());
96  	gisFeatureDistance.setGtopo30(country.getGtopo30());
97  	gisFeatureDistance.setLocation(country.getLocation());
98  	gisFeatureDistance.setName(country.getName().trim());
99  	gisFeatureDistance.setPopulation(country.getPopulation());
100 	gisFeatureDistance.setTimezone(country.getTimezone());
101 	gisFeatureDistance.setArea(country.getArea());
102 	gisFeatureDistance.setTld(country.getTld());
103 	gisFeatureDistance.setCapitalName(country.getCapitalName());
104 	gisFeatureDistance.setContinent(country.getContinent());
105 	gisFeatureDistance.setPostalCodeMask(country.getPostalCodeMask());
106 	gisFeatureDistance.setPostalCodeRegex(country.getPostalCodeRegex());
107 	gisFeatureDistance.setCurrencyCode(country.getCurrencyCode());
108 	gisFeatureDistance.setCurrencyName(country.getCurrencyName());
109 	gisFeatureDistance.setEquivalentFipsCode(country.getEquivalentFipsCode());
110 	gisFeatureDistance.setFipsCode(country.getFipsCode());
111 	gisFeatureDistance.setIso3166Alpha2Code(country.getIso3166Alpha2Code());
112 	gisFeatureDistance.setIso3166Alpha3Code(country.getIso3166Alpha3Code());
113 	gisFeatureDistance.setIso3166NumericCode(country.getIso3166NumericCode());
114 	gisFeatureDistance.setPhonePrefix(country.getPhonePrefix());
115 	return gisFeatureDistance;
116     }
117     
118     /**
119      * update the calculated fields (GoogleMapUrl,YahooMapURL,CountryFlag,...)
120      * 
121      */
122     public  void  updateFields(GisFeatureDistance gisFeatureDistance,Class clazz) {
123    	gisFeatureDistance.setOpenstreetmap_map_url(URLUtils.createOpenstreetmapMapUrl(gisFeatureDistance.getLocation()));
124 	gisFeatureDistance.setGoogle_map_url(URLUtils.createGoogleMapUrl(gisFeatureDistance.getLocation()));
125 	gisFeatureDistance.setYahoo_map_url(URLUtils.createYahooMapUrl(gisFeatureDistance.getLocation()));
126 	gisFeatureDistance.setCountry_flag_url(URLUtils.createCountryFlagUrl(gisFeatureDistance.getCountryCode()));
127 	if (gisFeatureDistance.getLocation() != null) {
128 	    gisFeatureDistance.setLat(gisFeatureDistance.getLocation().getY());
129 	    gisFeatureDistance.setLng(gisFeatureDistance.getLocation().getX());
130 	}
131 	if (gisFeatureDistance.getFeatureClass() != null && gisFeatureDistance.getFeatureCode() != null) {
132 	    try {
133 		gisFeatureDistance.setPlaceType(FeatureCode.valueOf(
134 			gisFeatureDistance.getFeatureClass() + "_" + gisFeatureDistance.getFeatureCode()).getObject()
135 			.getClass().getSimpleName().toLowerCase());
136 	    } catch (RuntimeException e) {
137 		logger.warn("no feature code for "+gisFeatureDistance.getFeatureClass() + "_" + gisFeatureDistance.getFeatureCode());
138 	    }
139 	} else if (clazz!=null){
140 		gisFeatureDistance.setPlaceType(clazz.getSimpleName().toLowerCase());
141 	}
142     }
143 
144 }