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

feat(Gridintersect): new grid intersection options #1468

Merged
merged 25 commits into from
Jul 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
de874b0
refactor(make-release): Software citation developed from version.py
jdhughes-usgs Jun 11, 2020
10def41
release: update mf6 v6.1.1 dfn and flopy mf6 classes (#1)
jdhughes-usgs Jun 12, 2020
145081d
docs(misc): clean up docstrings
langevin-usgs Jun 17, 2020
266d2e7
Merge pull request #7 from langevin-usgs/develop-patch
langevin-usgs Jun 17, 2020
b5da38b
refactor(GridIntersect): fix error message
dbrakenhoff Jun 18, 2020
9a29cbb
bug(GridIntersect): fix #10
dbrakenhoff Jun 18, 2020
9ffdd7d
Merge remote-tracking branch 'upstream/develop' into develop
dbrakenhoff Jun 22, 2020
97ddc53
bug(gridintersect): fixes #917
dbrakenhoff Jun 22, 2020
a7a07ee
Merge remote-tracking branch 'upstream/develop' into develop
dbrakenhoff Jul 24, 2020
8be569d
Merge remote-tracking branch 'upstream/develop' into develop
dbrakenhoff Sep 15, 2020
d586a55
Merge remote-tracking branch 'upstream/develop' into develop
dbrakenhoff Oct 16, 2020
027ddfa
Merge remote-tracking branch 'upstream/develop' into develop
dbrakenhoff Jan 13, 2021
0d59533
fix(GridIntersect): #1035
dbrakenhoff Jan 13, 2021
a1cb34f
Merge remote-tracking branch 'upstream/develop' into develop
dbrakenhoff Jul 16, 2021
ea42175
Merge remote-tracking branch 'upstream/develop' into develop
dbrakenhoff Nov 19, 2021
aa1c8b8
feat(GridIntersect): add shapetype kwarg
dbrakenhoff Nov 19, 2021
7330ab2
add kwargs to gridintersect for polygons
dbrakenhoff Jul 9, 2022
30e7488
fix(GridIntersect): #1467
dbrakenhoff Jul 26, 2022
79da97e
feat(GridIntersect): add return_all_intersections option
dbrakenhoff Jul 26, 2022
bf044ea
feat(GridIntersect): update example notebook
dbrakenhoff Jul 26, 2022
2c75b7b
Merge remote-tracking branch 'upstream/develop' into develop
dbrakenhoff Jul 26, 2022
3a8e487
Merge branch 'develop' into gridintersect
dbrakenhoff Jul 26, 2022
600a28b
fix(GridIntersect): #1216
dbrakenhoff Jul 26, 2022
b10ba6c
fix import
dbrakenhoff Jul 26, 2022
6746000
add test for #1216
dbrakenhoff Jul 26, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
226 changes: 221 additions & 5 deletions autotest/t065_test_gridintersect.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import sys

sys.path.insert(1, "..")
jlarsen-usgs marked this conversation as resolved.
Show resolved Hide resolved
import matplotlib.pyplot as plt
import numpy as np
from descartes import PolygonPatch
Expand Down Expand Up @@ -268,6 +269,21 @@ def test_rect_grid_multipoint_in_multiple_cells():
return result


def test_rect_grid_point_on_all_vertices_return_all_ix(rtree=True):
# avoid test fail when shapely not available
try:
import shapely
except:
return
gr = get_rect_grid()
ix = GridIntersect(gr, method="structured", rtree=rtree)
n_intersections = [1, 2, 1, 2, 4, 2, 1, 2, 1]
for v, n in zip(gr.verts, n_intersections):
r = ix.intersect(Point(*v), return_all_intersections=True)
assert len(r) == n
return


# %% test point shapely


Expand Down Expand Up @@ -364,6 +380,21 @@ def test_rect_grid_multipoint_in_multiple_cells_shapely(rtree=True):
return result


def test_rect_grid_point_on_all_vertices_return_all_ix_shapely(rtree=True):
# avoid test fail when shapely not available
try:
import shapely
except:
return
gr = get_rect_grid()
ix = GridIntersect(gr, method="vertex", rtree=rtree)
n_intersections = [1, 2, 1, 2, 4, 2, 1, 2, 1]
for v, n in zip(gr.verts, n_intersections):
r = ix.intersect(Point(*v), return_all_intersections=True)
assert len(r) == n
return


def test_tri_grid_point_outside(rtree=True):
# avoid test fail when shapely not available
try:
Expand Down Expand Up @@ -444,6 +475,21 @@ def test_tri_grid_multipoint_in_multiple_cells(rtree=True):
return result


def test_tri_grid_points_on_all_vertices_return_all_ix_shapely(rtree=True):
# avoid test fail when shapely not available
try:
import shapely
except:
return
gr = get_tri_grid()
ix = GridIntersect(gr, method="vertex", rtree=rtree)
n_intersections = [2, 2, 2, 2, 8, 2, 2, 2, 2]
for v, n in zip(gr.verts, n_intersections):
r = ix.intersect(Point(*v), return_all_intersections=True)
assert len(r) == n
return


# %% test linestring structured


Expand Down Expand Up @@ -564,6 +610,38 @@ def test_rect_grid_linestring_in_and_out_of_cell2():
return result


def test_rect_grid_linestrings_on_boundaries_return_all_ix():
# avoid test fail when shapely not available
try:
import shapely
except:
return
gr = get_rect_grid()
ix = GridIntersect(gr, method="structured")
x, y = ix._rect_grid_to_shape_list()[0].exterior.xy
n_intersections = [1, 2, 2, 1]
for i in range(4):
ls = LineString([(x[i], y[i]), (x[i + 1], y[i + 1])])
r = ix.intersect(ls, return_all_intersections=True)
assert len(r) == n_intersections[i]
return


def test_rect_grid_linestring_starting_on_vertex():
# avoid test fail when shapely not available
try:
import shapely
except:
return
gr = get_rect_grid()
ix = GridIntersect(gr, method="structured")
result = ix.intersect(LineString([(10.0, 10.0), (15.0, 5.0)]))
assert len(result) == 1
assert np.allclose(result.lengths.sum(), np.sqrt(50))
assert result.cellids[0] == (1, 1)
return result


# %% test linestring shapely


Expand Down Expand Up @@ -666,6 +744,23 @@ def test_rect_grid_linestring_in_and_out_of_cell_shapely(rtree=True):
return result


def test_rect_grid_linestrings_on_boundaries_return_all_ix_shapely(rtree=True):
# avoid test fail when shapely not available
try:
import shapely
except:
return
gr = get_rect_grid()
ix = GridIntersect(gr, method="vertex", rtree=rtree)
x, y = ix._rect_grid_to_shape_list()[0].exterior.xy
n_intersections = [1, 2, 2, 1]
for i in range(4):
ls = LineString([(x[i], y[i]), (x[i + 1], y[i + 1])])
r = ix.intersect(ls, return_all_intersections=True)
assert len(r) == n_intersections[i]
return


def test_tri_grid_linestring_outside(rtree=True):
# avoid test fail when shapely not available
try:
Expand Down Expand Up @@ -759,6 +854,23 @@ def test_tri_grid_multilinestring_in_one_cell(rtree=True):
return result


def test_tri_grid_linestrings_on_boundaries_return_all_ix(rtree=True):
# avoid test fail when shapely not available
try:
import shapely
except:
return
tgr = get_tri_grid()
ix = GridIntersect(tgr, method="vertex", rtree=rtree)
x, y = ix._vtx_grid_to_shape_list()[0].exterior.xy
n_intersections = [2, 1, 2]
for i in range(len(x) - 1):
ls = LineString([(x[i], y[i]), (x[i + 1], y[i + 1])])
r = ix.intersect(ls, return_all_intersections=True)
assert len(r) == n_intersections[i]
return


# %% test polygon structured


Expand Down Expand Up @@ -806,6 +918,21 @@ def test_rect_grid_polygon_on_outer_boundary():
return result


def test_rect_grid_polygon_running_along_boundary():
# avoid test fail when shapely not available
try:
import shapely
except:
return
gr = get_rect_grid()
ix = GridIntersect(gr, method="structured")
result = ix.intersect(
Polygon([(5.0, 5.0), (5.0, 10.0), (9.0, 10.0), (9.0, 15.0),
(1.0, 15.0), (1.0, 5.0)])
)
return result


def test_rect_grid_polygon_on_inner_boundary():
# avoid test fail when shapely not available
try:
Expand Down Expand Up @@ -874,6 +1001,57 @@ def test_rect_grid_polygon_with_hole():
return result


def test_rect_grid_polygon_contains_centroid(rtree=True):
# avoid test fail when shapely not available
try:
import shapely
except:
return
gr = get_rect_grid()
ix = GridIntersect(gr)
p = Polygon(
[(6.0, 5.0), (4.0, 16.0), (25.0, 14.0), (25.0, -5.0), (6.0, -5.0)],
holes=[[(9.0, -1), (9, 11), (21, 11), (21, -1)]],
)
result = ix.intersect(p, contains_centroid=True)
assert len(result) == 1
return


def test_rect_grid_polygon_min_area(rtree=True):
# avoid test fail when shapely not available
try:
import shapely
except:
return
gr = get_rect_grid()
ix = GridIntersect(gr)
p = Polygon(
[(5.0, 5.0), (5.0, 15.0), (25.0, 15.0), (25.0, -5.0), (5.0, -5.0)],
holes=[[(9.0, -1), (9, 11), (21, 11), (21, -1)]],
)
result = ix.intersect(p, min_area_fraction=0.4)
assert len(result) == 2
return


def test_rect_grid_polygon_centroid_and_min_area():
# avoid test fail when shapely not available
try:
import shapely
except:
return
gr = get_rect_grid()
ix = GridIntersect(gr)
p = Polygon(
[(5.0, 5.0), (5.0, 15.0), (25.0, 14.0), (25.0, -5.0), (5.0, -5.0)],
holes=[[(9.0, -1), (9, 11), (21, 11), (21, -1)]],
)
result = ix.intersect(p, min_area_fraction=0.35, contains_centroid=True)
assert len(result) == 1
return


# %% test polygon shapely


Expand Down Expand Up @@ -1134,6 +1312,44 @@ def test_tri_grid_polygon_with_hole(rtree=True):
return result


def test_tri_grid_polygon_min_area(rtree=True):
# avoid test fail when shapely not available
try:
import shapely
except:
return
gr = get_tri_grid()
if gr == -1:
return
ix = GridIntersect(gr, rtree=rtree)
p = Polygon(
[(5.0, 5.0), (5.0, 15.0), (25.0, 15.0), (25.0, -5.0), (5.0, -5.0)],
holes=[[(9.0, -1), (9, 11), (21, 11), (21, -1)]],
)
result = ix.intersect(p, min_area_fraction=0.5)
assert len(result) == 2
return


def test_tri_grid_polygon_contains_centroid(rtree=True):
# avoid test fail when shapely not available
try:
import shapely
except:
return
gr = get_tri_grid()
if gr == -1:
return
ix = GridIntersect(gr, rtree=rtree)
p = Polygon(
[(5.0, 5.0), (6.0, 14.0), (25.0, 15.0), (25.0, -5.0), (5.0, -5.0)],
holes=[[(9.0, -1), (9, 11), (21, 11), (21, -1)]],
)
result = ix.intersect(p, contains_centroid=True)
assert len(result) == 2
return


# %% test rotated offset grids


Expand Down Expand Up @@ -1299,6 +1515,8 @@ def test_all_intersections_shapely_no_strtree():


def test_rasters():
import os

import flopy as fp
from flopy.utils import Raster

Expand Down Expand Up @@ -1365,6 +1583,8 @@ def test_rasters():


def test_raster_sampling_methods():
import os

import flopy as fp
from flopy.utils import Raster

Expand Down Expand Up @@ -1414,7 +1634,3 @@ def test_raster_sampling_methods():
raise AssertionError(
f"{method} resampling returning incorrect values"
)


if __name__ == "__main__":
test_raster_sampling_methods()
Loading