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.helper;
24  
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  
28  import com.gisgraphy.domain.geoloc.entity.GisFeature;
29  import com.gisgraphy.domain.valueobject.Constants;
30  import com.gisgraphy.domain.valueobject.FeatureCode;
31  import com.gisgraphy.domain.valueobject.SRID;
32  
33  /**
34   * Provides useful methods for geolocalisation
35   * 
36   * @author <a href="mailto:david.masclet@gisgraphy.com">David Masclet</a>
37   */
38  public class GisHelper {
39  
40      private static final double COS0 = Math.cos(0);
41      private static final double SIN90 = Math.sin(90);
42      
43      private static final String INTERSECTION = "&&";
44      private static final String BBOX = "BOX3D";
45      
46      /**
47       * The logger
48       */
49      protected static final Logger logger = LoggerFactory
50  	    .getLogger(GeolocHelper.class);
51  
52  
53      /**
54       * Return the class corresponding to the specified String or null if not
55       * found. The Class will be searched in the 'entity' package. The search is
56       * not case sensitive. This method is mainly used to determine an entity
57       * Class from a web parameter
58       * 
59       * @param classNameWithoutPackage
60       *                the simple name of the Class we want to retrieve
61       * @return The class corresponding to the specified String or null if not
62       *         found.
63       */
64      @SuppressWarnings("unchecked")
65      public static Class<? extends GisFeature> getClassEntityFromString(
66  	    String classNameWithoutPackage) {
67  	if (classNameWithoutPackage != null) {
68  	    return FeatureCode.entityClass.get(classNameWithoutPackage
69  		    .toLowerCase());
70  	}
71  	return null;
72      }
73  
74     
75  
76     
77      
78  
79      /**
80       * @param alias the
81       *                sql alias
82       * @param latInDegree
83       *                the latitude in degree
84       * @param longInDegree
85       *                the longitude in degree
86       * @param distance
87       *                the boundingbox distance
88       * @return a sql String that represents the bounding box
89       */
90      public static String getBoundingBox(String alias, double latInDegree, double longInDegree,
91  	    double distance) {
92      
93      double lat = Math.toRadians(latInDegree);
94      double lon = Math.toRadians(longInDegree);
95  
96  	double deltaXInDegrees = Math.abs(Math.asin(Math
97  		.sin(distance / Constants.RADIUS_OF_EARTH_IN_METERS)
98  		/ Math.cos(lat)));
99  	double deltaYInDegrees = Math.abs(distance
100 		/ Constants.RADIUS_OF_EARTH_IN_METERS);
101 
102 	double minX = Math.toDegrees(lon - deltaXInDegrees);
103 	double maxX = Math.toDegrees(lon + deltaXInDegrees);
104 	double minY = Math.toDegrees(lat - deltaYInDegrees);
105 	double maxY = Math.toDegrees(lat + deltaYInDegrees);
106 
107 	StringBuffer sb = new StringBuffer();
108 	// {alias}.location && setSRID(BOX3D(...), 4326)
109 	sb.append(alias);
110 	sb.append(".").append(GisFeature.LOCATION_COLUMN_NAME);
111 	sb.append(" ");
112 	sb.append(INTERSECTION);
113 	sb.append(" st_setSRID(");
114 
115 	// Construct the BBOX : 'BOX3D(-119.2705528794688
116 	// 33.15289952334886,-117.2150071205312 34.95154047665114)'::box3d
117 	sb.append("cast (");
118 	sb.append("'");
119 	sb.append(BBOX);
120 	sb.append("(");
121 	sb.append(minX); // minX
122 	sb.append(" ");
123 	sb.append(minY); // minY
124 	sb.append(",");
125 	sb.append(maxX); // maxX
126 	sb.append(" ");
127 	sb.append(maxY); // maxY
128 	sb.append(")'as box3d)"); // cannot use the ::box3d notation, since
129 	// nativeSQL interprets :param as a named
130 	// parameter
131 
132 	// end of the BBOX, finish the setSRID
133 	sb.append(", ");
134 	sb.append(SRID.WGS84_SRID.getSRID());
135 	sb.append(") ");
136 
137 	return sb.toString();
138 
139     }
140 
141     
142   
143 
144 }