AndW
AndW

Reputation: 846

How to generate random lat-long points within geographical boundaries?

I want to generate k lat/long points at random within geographic boundary, specifically Manhattan. One solution I thought of was to generate points based on radius from a center point, but this is either too inaccurate (doesn't cover enough space) or will have points ending up in the ocean (I want them to only end up on land). How might one achieve this, are there any python libraries to help with this geographic constraint?

Upvotes: 5

Views: 1837

Answers (1)

RJ Adriaansen
RJ Adriaansen

Reputation: 9619

You can use osmnx. It will load the road network from Openstreetmap. The road network graph can then be fed to osmnx.utils_geo.sample_points to create a uniform random sample of points. They won't end up in the ocean, since they're bound to the road network.

import osmnx as ox

G = ox.graph_from_place("Manhattan, New York")
Gp = ox.project_graph(G)
points = ox.utils_geo.sample_points(ox.get_undirected(Gp), 20) # generate 20 random points

Upvotes: 7

Related Questions