clip/3 and overlay/3 with GeoPandas semantics.
Both are built from row positions, like the spatial joins: the R-tree finds the candidate
pairs (GeoFrame.GeoSeries.sindex_query/4), the geometry work runs in Rust on whole
columns, and attributes are gathered afterwards from the same positions, so attribute and
geometry rows cannot drift apart.
Differences from GeoPandas
- No warnings: where GeoPandas emits a
UserWarning(keep_geom_type: nildropping geometries,keep_geom_type: trueon a mixed frame) we callLogger.warning/1. - The pandas index does not exist, so the result has fresh rows only; use an id column to keep identity.
- A CRS mismatch raises
ArgumentError(GeoPandas warns), like every binary operation here. - Overlay results are not passed through
make_valid(GeoPandas does that for polygon results): the boolean operations return valid polygons. clip/3returns rows in their original order (GeoPandas' R-tree order is unspecified).
Summary
Functions
Clips points, lines or polygons to a polygon mask, like geopandas.clip.
Spatial overlay of two frames, like geopandas.overlay.
Functions
@spec clip(GeoFrame.GeoDataFrame.t() | GeoFrame.GeoSeries.t(), term(), keyword()) :: GeoFrame.GeoDataFrame.t() | GeoFrame.GeoSeries.t()
Clips points, lines or polygons to a polygon mask, like geopandas.clip.
data is a GeoFrame.GeoDataFrame or GeoFrame.GeoSeries. mask is a polygon or
multipolygon as WKT, a GeoSeries or GeoDataFrame (its geometries are merged with
union_all), or a bounding box {minx, miny, maxx, maxy}, which clips with the fast
rectangle algorithm (GeoFrame.GeoSeries.clip_by_rect/5, results may be invalid).
Rows are first filtered with the R-tree (intersects), then intersected with the mask
(points are returned as they are). Rows that end up empty are dropped. A GeoSeries gives a
GeoSeries, a frame gives a frame with the attributes of the surviving rows; its secondary
geometry columns follow their rows unclipped (only the active geometry is clipped).
Options:
:keep_geom_type-false(default). Whentrue, a result holding parts of another geometry type than the data (a line clipped to a mask it only touches gives a point; a polygon gives a collection) is reduced to the parts of the original type: collections are exploded and the foreign parts removed. Warns (logs) and does nothing for mixed frames or frames containing collections, as GeoPandas does.
Raises ArgumentError for a mask that is not polygonal, an inverted box, or a CRS mismatch.
Example
iex> s = GeoFrame.GeoSeries.from_wkt!(["POLYGON ((0 0, 4 0, 4 4, 0 4, 0 0))"])
iex> mask = "POLYGON ((2 2, 6 2, 6 6, 2 6, 2 2))"
iex> s |> GeoFrame.Overlay.clip(mask) |> GeoFrame.GeoSeries.to_wkt()
["POLYGON((2 4,2 2,4 2,4 4,2 4))"]
@spec overlay(GeoFrame.GeoDataFrame.t(), GeoFrame.GeoDataFrame.t(), keyword()) :: GeoFrame.GeoDataFrame.t()
Spatial overlay of two frames, like geopandas.overlay.
Options:
:how-:intersection(default),:union,:identity,:symmetric_differenceor:difference.:keep_geom_type-nil(default),trueorfalse. Withtrueonly geometries of the geometry type ofdf1are returned: other parts of collections are removed and rows without a part of that type are dropped (a shared edge between two polygons is a line and goes).nilbehaves liketruebut logs a warning withLogger.warning/1when something is dropped.falsereturns everything, possibly mixed types.:make_valid-true(default): invalid polygons in the inputs are repaired withGeoFrame.GeoSeries.make_valid/1(only their polygonal parts are kept);falseraisesArgumentErroron invalid input.
Each frame must hold a single geometry family (polygons, lines or points). The result keeps
the CRS and the geometry column name of df1.
Result columns and row order
Secondary geometry columns (GeoFrame.GeoDataFrame.put_geometry/3) of both frames are
columns like the attributes: _1 / _2 on a name clash, null for the missing side, their
own CRS kept (for :difference only df1's remain). They come after the active geometry.
:intersection,:union,:identity,:symmetric_difference: the attributes ofdf1, then those ofdf2, then the geometry. A name present in both gets_1/_2.:difference: only the attributes ofdf1(names unchanged).- Rows come in blocks, as in GeoPandas. Intersection: one row per intersecting pair (by
df1row, thendf2row). Union: the intersection rows, the parts of eachdf1row outsidedf2, then the parts of eachdf2row outsidedf1. Identity: the intersection rows, then thedf1remainders. Symmetric difference: thedf1remainders, then thedf2remainders. A row from only one side has nulls for the other side's attributes.
A row with a null geometry meets nothing, so it never appears in an intersection. Where a
side's remainder is part of the result (:difference, :identity, :union,
:symmetric_difference) its null rows are kept, with a null geometry and in place among the
remainders, exactly as geopandas.overlay does (checked against it for every :how).
Implemented as GeoPandas does it: candidate pairs from the R-tree, pairwise intersections, and, for the differences, each row minus the union of the rows of the other frame it meets.
Example
iex> alias GeoFrame.{GeoDataFrame, GeoSeries}
iex> sq = fn x -> "POLYGON ((#{x} 0, #{x + 2} 0, #{x + 2} 2, #{x} 2, #{x} 0))" end
iex> df1 = GeoDataFrame.new(Explorer.DataFrame.new(a: [1]), GeoSeries.from_wkt!([sq.(0)]))
iex> df2 = GeoDataFrame.new(Explorer.DataFrame.new(b: [2]), GeoSeries.from_wkt!([sq.(1)]))
iex> out = GeoFrame.Overlay.overlay(df1, df2)
iex> {GeoDataFrame.names(out), GeoSeries.to_wkt(out.geometry)}
{["a", "b", "geometry"], ["POLYGON((1 2,1 0,2 0,2 2,1 2))"]}