Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Sampling points from geometry #184

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

[WIP] Sampling points from geometry #184

wants to merge 2 commits into from

Conversation

asinghvi17
Copy link
Member

import GeoInterface as GI, GeometryOps as GO
p1 = GI.Polygon([[[-55965.680060140774, -31588.16072168928], [-55956.50771556479, -31478.09258677756], [-31577.548550575284, -6897.015828572996], [-15286.184961223798, -15386.952072224134], [-9074.387601621409, -27468.20712382156], [-8183.4538916097845, -31040.003969070774], [-27011.85123029944, -38229.02388009402], [-54954.72822634951, -32258.9734800704], [-55965.680060140774, -31588.16072168928]]])
points = GO.sample(p1, 100)
using CairoMakie
f, a, p = poly(p1)
scatter!(a, points)
f

download-11


```@example sample
import GeoInterface as GI, GeometryOps as GO
p1 = GI.Polygon([[[-55965.680060140774, -31588.16072168928], [-55956.50771556479, -31478.09258677756], [-31577.548550575284, -6897.015828572996], [-15286.184961223798, -15386.952072224134], [-9074.387601621409, -27468.20712382156], [-8183.4538916097845, -31040.003969070774], [-27011.85123029944, -38229.02388009402], [-54954.72822634951, -32258.9734800704], [-55965.680060140774, -31588.16072168928]]])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use tuple points in examples? (To teach good habits)

end

function sample(alg, geom, n)
return apply(x -> _sample(alg, GI.trait(x), x, n), application_level(alg), geom)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we even use apply for this? What happens to a feature collection or DataFrame?

My intuitive understanding would be to sample points from all geometries of all of the features weighted by area.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this point each feature will be sampled independently, so it's a dataframe to dataframe transfer. Since everything else also works the same way (centroid etc) I think this is at least consistent?

Am open to discussion though, not sure how people generally use this stuff.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GeoPandas samples per geometry, and returns a multipoint:
https://geopandas.org/en/stable/docs/reference/api/geopandas.GeoSeries.sample_points.html

GO.centroid does return a single point even if you pass in a feature collection or table. (There's an issue with the Natural Earth north Korea geometry as well :D)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aah, yeah apply won't work here since it will try to reconstruct.

I guess this is a good time to figure out the generic apply iterator thing?

src/methods/sample.jl Outdated Show resolved Hide resolved
Co-authored-by: Rafael Schouten <[email protected]>
end

function sample(alg, geom, n)
return apply(x -> _sample(alg, GI.trait(x), x, n), application_level(alg), geom)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aah, yeah apply won't work here since it will try to reconstruct.

I guess this is a good time to figure out the generic apply iterator thing?

Comment on lines +52 to +59
return map(1:n) do _
edge_idx = sample(edge_idxs, edge_probabilities)
x1, y1 = edges[edge_idx][1]
x2, y2 = edges[edge_idx][2]
distance = edge_lengths[edge_idx]
t = rand() * distance
(x1 + t * (x2 - x1), y1 + t * (y2 - y1))
end
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The better way to do this is probably to define a vector of points, and then set indices in that vector. Could also open the way for multithreading in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants