Coordinate distance calculation, determine whether the coordinates are within a polygon

/****
 * Get data tool class according to latitude and longitude
 * @author xuli
 *
 */
public class LngLatDistanceUtils {
	private static final Logger log = LoggerFactory.getLogger(LngLatDistanceUtils.class);
	
	/***
	 * Longitude and latitude to calculate distance
	 * @author xuli
	 *
	 * @param lat1 latitude 1
	 * @param lng1 longitude 1
	 * @param lat2 latitude 2
	 * @param lng2 longitude 2
	 */
	public static Float getDistance(Double lat1, Double lng1, Double lat2, Double lng2){
		double EARTH_RADIUS = 6378.137;// Earth radius

		double radLat1 = rad(lat1);
		double radLat2 = rad(lat2);
		double a = radLat1 - radLat2;
		double b = rad(lng1) - rad(lng2);

		double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
				+ Math.cos(radLat1) * Math.cos(radLat2)
				* Math.pow(Math.sin(b / 2), 2)));
		s = s * EARTH_RADIUS;
		Float ss = (float) (Math.round(s * 1000))/1000;

		return ss;
	}
	
	private static double rad(Double d){
		return d * Math.PI / 180.0;
	}
	

	
	
	/****
	 * Determine whether the point is inside the polygon (excluding the boundary)
	 *
	 * @author wanwenjun
	 * @param point point
	 * @param polygon polygon
	 * @return
	 */
	public static boolean checkWithJdkGeneralPath(Point2D.Double point, List<Point2D.Double> polygon) {
		java.awt.geom.GeneralPath p = new java.awt.geom.GeneralPath();
		
		// Initialize starting point coordinates
		Point2D.Double first = polygon.get(0);
		p.moveTo(first.x, first.y);
		for (Point2D.Double d : polygon) {
			
			// Traverse the points and draw lines in the traversed order
			p.lineTo(dx, dy);
		}
		p.lineTo(first.x, first.y);
		
		p.closePath();
		return p.contains(point);
	}
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326378844&siteId=291194637