5

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?

AndW
  • 726
  • 6
  • 31

1 Answers1

7

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
RJ Adriaansen
  • 9,131
  • 2
  • 12
  • 26
  • Couldn't seem to find the answer in the docs - do you know if sample_points will sample points along the road network (edges) as well, or will it be constrained to the nodes on the OSMNX graph? – AndW Jul 14 '21 at 20:03
  • The original [issue](https://github.com/gboeing/osmnx/issues/639) suggests that the sample points are generated along the road network (edges). – RJ Adriaansen Jul 14 '21 at 22:06
  • 2
    OSMnx developer here. Yes, it will sample random points constrained to the street network edges' geometries. – gboeing Jul 15 '21 at 01:31
  • 2
    Thanks! however I can't quite interpret the data output, an example is POINT (585155.331 4507719.832) which is not lat long; how could it be converted/what coordinate system is this? – AndW Jul 15 '21 at 18:39
  • 3
    Edit: it is WGS84 UTM coordinates in Zone 18. It can be converted with python code found here: https://stackoverflow.com/questions/343865/how-to-convert-from-utm-to-latlng-in-python-or-javascript – AndW Jul 15 '21 at 18:52
  • @a6623 What about situation when points are in many zones, not only Zone 18? – Ilkiv Yuriy Mar 20 '22 at 21:37