5

I'm using https://randomuser.me/ to generate some random user data for testing. I'd like to geocode the latitude and longitude that provides to attach the real city/state/country information instead of the totally fake info that RandomUser provides. However, many of the actual lat/long values on these objects turn out to be in the middle of the ocean.

Is there an API, or some other method, that will let me get random coordinates which are always over land? I've seen this: Random geographic coordinates (on land, avoid ocean) But surely there must be some more efficient way than to just guess-and-check with constant geocoding until you find one that works, right?

IceMetalPunk
  • 5,476
  • 3
  • 19
  • 26
  • > real city/state/country information ... Don't you just want a list of cities of the world including their centre point? (pick one at random, weighted by its total surface area) Or is it really important to distribute within each city? – drekbour Jun 18 '19 at 21:27
  • @drekbour No, because cities don't cover all the land area. Far from it. – David Knipe Jun 18 '19 at 21:40
  • Here is a thought: download a clear blue tile from a map tile provider. sha1 hash this PNG. randomly create the lat/long coordinates. grap a tile on this location. sha1 hash this tile and check if the hashes match. there is a 1 in 3 chance its not the same clear blue tile right? – dehart Jun 18 '19 at 22:00
  • You ask for an "efficient" solution without saying what your metric for efficiency is. Efficiency is defined as output quality divided by cost; what are your metrics for quality and cost? – Eric Lippert Jun 18 '19 at 23:54
  • @EricLippert Efficient as in "not even potentially unbounded" – IceMetalPunk Jun 19 '19 at 15:10
  • @dehart The problem is that guess-and-check method is potentially unbounded in how long it could take to find a valid point, which is a big issue. – IceMetalPunk Jun 19 '19 at 15:11
  • @IceMetalPunk It's a non-issue. Oceans are 71% of Earth surface, so the expected number of tries you'll need is 3.45, and in 99% of cases you'll need less than 14 tries to find a point on the land. – Joker_vD Jun 19 '19 at 16:22
  • @joker_vd: Whether it is a non-issue or not is up to the original poster. The problem is not that their requirements are *wrong*; the problem is that the requirement is phrased in the question in terms of amortized cost -- "efficiency" -- when the requirement has nothing to do with amortized cost. The requirement has to do with *bounding maximum possible cost*, which is very different! The question should be phrased more clearly; if the original poster is concerned about bounding the cost then the cost bound should be stated. – Eric Lippert Jun 19 '19 at 17:46

3 Answers3

8

https://3geonames.org/ Click on "Pick a random location at or near land"

The API is here: https://api.3geonames.org/?randomland=yes (for a random location on land)

or https://api.3geonames.org/?random=yes for any other kind of random location.

Update: As of September 2020 you can pick a random location in any given country. For example https://3geonames.org/randomland.NL will return a random location in the Netherlands.

Ervin Ruci
  • 829
  • 6
  • 10
  • Ooh! Not only does this do what I want, but it also includes the city/state/country information in the same response with the lat/long, so I don't even need to do another lookup for reverse geocoding! Thank you! :) – IceMetalPunk Jun 19 '19 at 15:13
  • Glad I could help. Forgot to mention that you can also get the response in JSON format by adding the json=1 parameter. for eg: https://api.3geonames.org/?randomland=yes&json=1 – Ervin Ruci Jun 20 '19 at 17:44
  • This is cool but it would be nice if I could specify criteria. For example, "random location with Sweden" (or whatever). I don't suppose anyone knows of alternatives that can do this? – ashleedawg Mar 04 '20 at 00:32
1

You can also utilise JSON format output to locate where the International Space Station currently is: https://api.wheretheiss.at/v1/satellites/25544

The out would be something like:

{"name":"iss","id":25544,"latitude":10.648591418478,"longitude":-123.40906685608,"altitude":418.80257051697,"velocity":27578.808941543,"visibility":"eclipsed","footprint":4501.3664529662,"timestamp":1658400620,"daynum":2459781.9516204,"solar_lat":20.423294903459,"solar_lon":19.032241274318,"units":"kilometers"}
Milind Deore
  • 2,887
  • 5
  • 25
  • 40
0

For what is worth, there exists an open source library called OpenAddresses that can be used for your exact purpose as well as all kinds of purposes. I'm using it in several projects.

Using Javascript fetch

fetch('https://batch.openaddresses.io/api/data', {
    method: 'GET',
    withCredentials: true,
    credentials: 'include',
    headers: {
        'Authorization': 'oa.1234-your-token-here-5678',
        'Content-Type': 'application/json'
    }
});

With the list data function you can get the latest successful run of a given geographic area of your liking.

Grogu
  • 2,097
  • 15
  • 36