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

**URL:** <https://discourse.julialang.org/t/randomly-generate-a-gps-location-in-wgs-84-format-from-china/24579>\
**Category:** General Usage\
**Tags:** geo, gmt, geodesy\
**Created:** [May 24, 2019, 5:33pm UTC](https://discourse.julialang.org/t/randomly-generate-a-gps-location-in-wgs-84-format-from-china/24579 "2019-05-24T17:33:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bsnyh](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bsnyh](https://discourse.julialang.org/u/bsnyh)\
**Post date:** [May 24, 2019, 5:33pm UTC](https://discourse.julialang.org/t/randomly-generate-a-gps-location-in-wgs-84-format-from-china/24579/1 "2019-05-24T17:33:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![evanfields](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evanfields/32/1744_2.png) [@evanfields](https://discourse.julialang.org/u/evanfields)\
**Post date:** [May 24, 2019, 6:31pm UTC](https://discourse.julialang.org/t/randomly-generate-a-gps-location-in-wgs-84-format-from-china/24579/2 "2019-05-24T18:31:57Z")

</div>

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](https://arthur-e.github.io/Wicket/sandbox-gmaps3.html) to sketch a boundary yourself.
2. Pick a coordinate system over which you’ll sample points. A quick moment of searching suggests that [China Albers Equal Area Conic: SR-ORG Projection -- Spatial Reference](https://spatialreference.org/ref/sr-org/7564/) might be a reasonable choice. For lower precision, even [WGS 84 / Pseudo-Mercator - Spherical Mercator, Google Maps, OpenStreetMap, Bing, ArcGIS, ESRI - EPSG:3857](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)](http://yeesian.com/ArchGDAL.jl/latest/) can do this.
4. Use an [acceptance-rejection algorithm](https://en.wikipedia.org/wiki/Rejection_sampling) 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](https://github.com/JuliaGeo/LibGEOS.jl).
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!

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [May 24, 2019, 8:31pm UTC](https://discourse.julialang.org/t/randomly-generate-a-gps-location-in-wgs-84-format-from-china/24579/3 "2019-05-24T20:31:44Z")

</div>

With [GMT.jl](https://github.com/GenericMappingTools/GMT.jl) you can get the China polygons (many) with

```julia
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).

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [May 24, 2019, 10:02pm UTC](https://discourse.julialang.org/t/randomly-generate-a-gps-location-in-wgs-84-format-from-china/24579/4 "2019-05-24T22:02:50Z")

</div>

> [@evanfields](#):
>
> Pick a coordinate system over which you’ll sample points. A quick moment of searching suggests that [China Albers Equal Area Conic: SR-ORG Projection -- Spatial Reference](https://spatialreference.org/ref/sr-org/7564/) might be a reasonable choice. For lower precision, even [WGS 84 / Pseudo-Mercator - Spherical Mercator, Google Maps, OpenStreetMap, Bing, ArcGIS, ESRI - EPSG:3857](https://epsg.io/3857) could be okay.

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.
