Sampling random points in LINESTRING geometry

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!

Not randomly, but at a constant step. GMT.jl wrapper of the GDAL (possibly GEOS at the bottom) pointalongline function.

cc: @juliohm

Have you considered other geometry libraries?

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

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))
2 Likes

Thank you for the ping @mike.ingold :slight_smile:

@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.

1 Like

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.

@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

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

2 Likes

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.

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

using GeoIO

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

area.(geotable.geometry)
1 Like