-
Notifications
You must be signed in to change notification settings - Fork 4
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
base: main
Are you sure you want to change the base?
Conversation
asinghvi17
commented
Jul 21, 2024
|
||
```@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]]]) |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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?
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) |
There was a problem hiding this comment.
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?
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 |
There was a problem hiding this comment.
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.
Some interesting links about triangle sampling:
1D floating point sampling: |