# Sampling random points in LINESTRING geometry

**URL:** <https://discourse.julialang.org/t/sampling-random-points-in-linestring-geometry/121429>\
**Category:** Geo\
**Tags:** geostatistics, libgeos\
**Created:** [October 17, 2024, 6:16pm UTC](https://discourse.julialang.org/t/sampling-random-points-in-linestring-geometry/121429 "2024-10-17T18:16:10Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![miguelborrero](https://avatars.discourse-cdn.com/v4/letter/m/eb9ed0/32.png) [@miguelborrero](https://discourse.julialang.org/u/miguelborrero)\
**Post date:** [October 17, 2024, 6:16pm UTC](https://discourse.julialang.org/t/sampling-random-points-in-linestring-geometry/121429/1 "2024-10-17T18:16:10Z")

</div>

Hi there,

I a new user of the geo-tools in general, not only within the Julia ecosystem and I am trying to sample randomly from a LINESTRING LibGEOS geometry but the only function that seemed potentially useful is `pointOnSurface`. However it does not sample. I am aware of the function in Python: `geopandas.GeoSeries.sample_points` and I was curious whether something like this existed in Julia or otherwise whether someone with experience in this environment could provide efficient ways of doing so adapted to the current exported function by LibGEOS.jl

Thanks in advance!

---

<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:** [October 17, 2024, 6:54pm UTC](https://discourse.julialang.org/t/sampling-random-points-in-linestring-geometry/121429/2 "2024-10-17T18:54:03Z")

</div>

Not randomly, but at a constant step. GMT.jl wrapper of the GDAL (possibly GEOS at the bottom) [pointalongline](https://www.generic-mapping-tools.org/GMTjl_doc/documentation/gdalfuns/#pointalongline) function.

---

<div class="post-metadata">

**Author:** ![mike.ingold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike.ingold/32/203749_2.png) [@mike.ingold](https://discourse.julialang.org/u/mike.ingold)\
**Post date:** [October 17, 2024, 7:14pm UTC](https://discourse.julialang.org/t/sampling-random-points-in-linestring-geometry/121429/3 "2024-10-17T19:14:31Z")

</div>

cc: @juliohm

Have you considered other geometry libraries?

Using Meshes.jl you should be able to do something like

```julia
using Meshes

# Construct a rope of line segments (a - b - c)
a = Point(...)
b = Point(...)
c = Point(...)
linestring = Rope(a, b, c)

# Uniform random sampling
n_sample_points = ...
sampler = HomogeneousSampling(n_sample_points)
points = collect(sample(linestring, sampler))

```

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [October 17, 2024, 7:28pm UTC](https://discourse.julialang.org/t/sampling-random-points-in-linestring-geometry/121429/4 "2024-10-17T19:28:28Z")

</div>

Thank you for the ping @mike.ingold 🙂

@miguelborrero if you are not tied to these external C libraries, I highly recommend the GeoStats.jl stack (superset of Meshes.jl) for geospatial data science. You can do much more and we are here to help with quick bug fixes.

---

<div class="post-metadata">

**Author:** ![miguelborrero](https://avatars.discourse-cdn.com/v4/letter/m/eb9ed0/32.png) [@miguelborrero](https://discourse.julialang.org/u/miguelborrero)\
**Post date:** [October 17, 2024, 11:54pm UTC](https://discourse.julialang.org/t/sampling-random-points-in-linestring-geometry/121429/5 "2024-10-17T23:54:44Z")

</div>

Thanks for the pointer. Quick question to assess feasibility of the move? Can I export LibGEOS geometries or define polygons easily from vector of coordinate pairs in this library? That would be awesome.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [October 17, 2024, 11:57pm UTC](https://discourse.julialang.org/t/sampling-random-points-in-linestring-geometry/121429/6 "2024-10-17T23:57:24Z")

</div>

@miguelborrero you mean loading and saving geometries into popular file formats like shp, geojson, etc? If that is the question, the answer is yes.

The GDSJL book can give you an overview of the basics:

[https://juliaearth.github.io/geospatial-data-science-with-julia](https://juliaearth.github.io/geospatial-data-science-with-julia)

The book doesn’t cover all the features, but organizes the concepts well.

---

<div class="post-metadata">

**Author:** ![miguelborrero](https://avatars.discourse-cdn.com/v4/letter/m/eb9ed0/32.png) [@miguelborrero](https://discourse.julialang.org/u/miguelborrero)\
**Post date:** [October 18, 2024, 12:03am UTC](https://discourse.julialang.org/t/sampling-random-points-in-linestring-geometry/121429/7 "2024-10-18T00:03:45Z")

</div>

I mean like: I have a vector of (x,y) coordinates which I read from a shapefile and I want to form a polygon geometry using these points as vertices and then calculate the area or sample within the polygon etc?

Thanks for the book link and sorry that Im being a bit lazy here but I need to do the task urgently and therefore want to assess the use as quickly as possible.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [October 18, 2024, 12:07am UTC](https://discourse.julialang.org/t/sampling-random-points-in-linestring-geometry/121429/8 "2024-10-18T00:07:33Z")

</div>

> [@miguelborrero](#):
>
> I mean like: I have a vector of (x,y) coordinates which I read from a shapefile and I want to form a polygon geometry using these points as vertices and then calculate the area or sample within the polygon etc?

You can load the polygons directly with GeoIO.jl (explained in the book):

```julia
using GeoIO

geotable = GeoIO.load("file.shp")

area.(geotable.geometry)

```
