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

Coverage #60

Merged
merged 13 commits into from
Mar 5, 2024
Merged

Coverage #60

merged 13 commits into from
Mar 5, 2024

Conversation

skygering
Copy link
Collaborator

@skygering skygering commented Feb 29, 2024

This calculates the area of a geometry within a bounding box defined by the minimum and maximum x and y values. So this should be similar to coverage within rasters, just for one polygon at a time.

I haven't tested, but right now for multi-geometries and collections, I think it would be equivalent to Raster's coverage with the sum operation.

I am not sure if area.jl is the right place for this or if it should have its own file...

@skygering skygering requested a review from rafaqz February 29, 2024 20:33
@asinghvi17
Copy link
Member

IMO coverage should probably be a separate file here?

@skygering
Copy link
Collaborator Author

coverage_benchmarks

@asinghvi17
Copy link
Member

How many vertices does p have there?

@rafaqz
Copy link
Member

rafaqz commented Mar 1, 2024

Could be good to accept an Extents.Extent for x/y min/max

@skygering
Copy link
Collaborator Author

Could be good to accept an Extents.Extent for x/y min/max

Oh! That is a great idea. Then I don't need the point_in_cell functions! I will work swapping that!

@skygering
Copy link
Collaborator Author

How many vertices does p have there?

Not many. Like 10 or something. I deal with pretty small polygons (at least small numbers of points, if not area) most of the time so I don't have good "large" polygons to test on. If you have some and open a PR to put them in the test folder as an input that would be super cool.

@skygering
Copy link
Collaborator Author

skygering commented Mar 4, 2024

Could be good to accept an Extents.Extent for x/y min/max

Oh! That is a great idea. Then I don't need the point_in_cell functions! I will work swapping that!

@rafaqz In hindsight, I don't know if this is a good idea. On one hand, it would be a nicer input. On the other hand, we now have to make Extent objects, which is very, very cheap, but it still costs something. And if folks are dealing with cells, they would now need to make an Extent object for every cell. Thoughts?

If we use:

function f1(xmin, xmax, ymin, ymax, p)
       p_ext = GI.extent(p)
       cell_ext = Extents.Extent(X = (xmin, xmax), Y = (ymin, ymax))
       return Extents.intersects(p_ext, cell_ext)
end

f2(xmin, xmax, ymin, ymax, p) = xmin  GI.x(p)  xmax && ymin  GI.y(p)  ymax

then we get:
Screenshot 2024-03-04 at 12 52 27 PM

@rafaqz
Copy link
Member

rafaqz commented Mar 4, 2024

It should cost nothing at all unless you have mixed types somehow? They will promote. Otherwise its a bug.

I actually just meant we should let users pass in an extent if they want to

@rafaqz
Copy link
Member

rafaqz commented Mar 4, 2024

Oh actually by making the point an extent you are doubling the number of comparisons!!

@skygering
Copy link
Collaborator Author

skygering commented Mar 4, 2024

Okay, I moved the code and I also used applyreduce for multi-geometries. It is now type unstable due to that, but I am hoping that @asinghvi17's fix in his PR will fix that...

But I feel good other than that. It can also take in an extent rather than the min/max values if desired.

If you guys think it is ready to go then I will merge.

@rafaqz
Copy link
Member

rafaqz commented Mar 4, 2024

Whatever happens we will make apply and applyreduce type stable eventually

Copy link
Member

@asinghvi17 asinghvi17 left a comment

Choose a reason for hiding this comment

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

Looks good to me! Once it's merged, I'll pull master into my branch and test it.

Comment on lines +62 to +64
applyreduce(+, _COVERAGE_TARGETS, geom; threaded, init=zero(T)) do g
_coverage(T, GI.trait(g), g, T(xmin), T(xmax), T(ymin), T(ymax))
end
Copy link
Member

Choose a reason for hiding this comment

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

This is the only thing I'm not sure about, since the function is inside the applyreduce call. Still, it will operate at a pretty high level and hopefully that shouldn't cause too many issues.

A potential solution might be to accept a tuple (xmin, xmax, ymin, ymax) in _coverage, and then just use Base.Fix2 to create a function.

Copy link
Member

Choose a reason for hiding this comment

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

Giving the anonymous function a name just on the line above can help the compiler. Surprisingly.

All the T types we are using everywhere worry me a but too, the compiler likes to forget what they are... sometimes it can help to wrap them in Val further out to force them through

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I don't think I understand the problem. Can you elaborate? Is this something we need to fix pre-merge?

Copy link
Member

Choose a reason for hiding this comment

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

This would be:

Suggested change
applyreduce(+, _COVERAGE_TARGETS, geom; threaded, init=zero(T)) do g
_coverage(T, GI.trait(g), g, T(xmin), T(xmax), T(ymin), T(ymax))
end
coverage_for_subgeom(g) = _coverage(T, GI.trait(g), g, T(xmin), T(xmax), T(ymin), T(ymax))
applyreduce(coverage_for_subgeom, +, _COVERAGE_TARGETS, geom; threaded, init=zero(T))

which gets rid of the anonymous function created by the do block.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Should I change this in area as well then, since I copied this format from there?

Copy link
Member

@asinghvi17 asinghvi17 Mar 5, 2024

Choose a reason for hiding this comment

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

I can do that in my PR.

@asinghvi17
Copy link
Member

asinghvi17 commented Mar 5, 2024

I can merge this now and we'll get to the optimization in #69

@rafaqz rafaqz merged commit e4109f3 into JuliaGeo:main Mar 5, 2024
3 checks passed
@skygering skygering deleted the sg/poly_rect_inter_area branch March 5, 2024 19:13
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.

3 participants