Randomly generate a GPS location in WGS 84 format from China?


Hi, this is not exactly a Julia question I guess. But I really like this forum. So I post it here. I was wondering, how to randomly generate a GPS location from China in WGS 84 format? I guess we will need to find the range of GPS locations of China and randomly generate a location from this range. Could anyone provide some reference links for doing this?
Many thanks indeed.

Do you need the distribution of the point to be uniform over all of China? Or how close to uniform do you need the distribution to be?

One good workflow could be:

  1. Find or generate the description of a polygon defining the boundaries of China. This can be in any coordinate system you want. Depending on the precision you need, you could use a tool like this to sketch a boundary yourself.
  2. Pick a coordinate system over which you’ll sample points. A quick moment of searching suggests that https://spatialreference.org/ref/sr-org/7564/ might be a reasonable choice. For lower precision, even https://epsg.io/3857 could be okay.
  3. If your polygon from #1 isn’t already in this coordinate system, project it. I haven’t used it myself but believe ArchGDAL.jl (docs) can do this.
  4. Use an acceptance-rejection algorithm to generate a point within the defined polygon in your projected coordinate system. In brief, this would mean generating a point over the bounding box of the polygon and checking if the point is actually in the China polygon. If yes, move on to the next step; if no, generate a new point and try again. For checking if a point is in a polygon, I would recommend LibGEOS.
  5. Project the point you found back to WGS84 lat-lon coordinates.

It’s worth noting that China is big [citation needed], so any projected coordinate system will have some slight inaccuracy somewhere!


With GMT.jl you can get the China polygons (many) with

china=coast(DCW=:CN, dump=true);

The gmtselect module (option F) will let you select only the points inside the polygons.
Sorry have no time now to elaborate more (maybe later).


The fact that this preserves area is a good thing. If you sample in a coordinate system in which dy,dx varies in area (such as latitude-longitude) you will get nonuniform sampling… more points per unit area in the regions in the north where the area is smaller per dlat,dlong and fewer points per area in regions near the equator.