Skip to content

Instantly share code, notes, and snippets.

@fuxingloh
Last active January 27, 2024 17:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fuxingloh/5f53a618ce3c80b0abaf to your computer and use it in GitHub Desktop.
Save fuxingloh/5f53a618ce3c80b0abaf to your computer and use it in GitHub Desktop.
Generate random lat lng
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);
}
}
@dimaslanjaka
Copy link

how to use?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment