Last active
January 27, 2024 17:58
-
-
Save fuxingloh/5f53a618ce3c80b0abaf to your computer and use it in GitHub Desktop.
Generate random lat lng
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import co.threelines.puffin.util.GeoSpatialUtil; | |
import com.spatial4j.core.shape.Point; | |
import org.apache.commons.lang3.tuple.Pair; | |
import java.util.Random; | |
/** | |
* Created by Fuxing | |
* Date: 5/7/2015 | |
* Time: 3:22 AM | |
* Project: jarvis | |
*/ | |
public class RandomLatLngUtils { | |
private static Point randomPoint(double lat, double lng, int radius) { | |
Pair<Double, Double> latLng = random(lat, lng, radius); | |
return GeoSpatialUtil.makePoint(latLng.getLeft(), latLng.getRight()); | |
} | |
/** | |
* random point in a circle | |
* @param lat lat of circle | |
* @param lng lng of circle | |
* @param radius radius of circle in meters | |
* @return random lat,lng point | |
*/ | |
private static Pair<Double, Double> random(double lat, double lng, int radius) { | |
Random random = new Random(); | |
// Convert radius from meters to degrees | |
double radiusInDegrees = radius / 111000f; | |
double u = random.nextDouble(); | |
double v = random.nextDouble(); | |
double w = radiusInDegrees * Math.sqrt(u); | |
double t = 2 * Math.PI * v; | |
double x = w * Math.cos(t); | |
double y = w * Math.sin(t); | |
// Adjust the x-coordinate for the shrinking of the east-west distances | |
double new_x = x / Math.cos(lat); | |
return Pair.of(y + lat, new_x + lng); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how to use?