diff --git a/docs/make.jl b/docs/make.jl index 80f6b6246..c2e20725b 100644 --- a/docs/make.jl +++ b/docs/make.jl @@ -97,6 +97,7 @@ makedocs(; "Tutorials" => [ "Creating Geometry" => "tutorials/creating_geometry.md", "Spatial Joins" => "tutorials/spatial_joins.md", + "Clipping a FeatureCollection" => "tutorials/clipping_featurecollection.md", ], "Explanations" => [ "Paradigms" => "explanations/paradigms.md", diff --git a/docs/src/tutorials/clipping_featurecollection.md b/docs/src/tutorials/clipping_featurecollection.md new file mode 100644 index 000000000..38d9f5624 --- /dev/null +++ b/docs/src/tutorials/clipping_featurecollection.md @@ -0,0 +1,41 @@ +# Clipping a FeatureCollection by a polygon + +Often, one might want to clip a feature collection (maybe from Shapefile or GeoJSON) + +```@example clipping_featurecollection +function clip_or_empty(polygon, clipper) + #= + result = GO.intersection(polygon, clipper; target = GI.PolygonTrait()) + if isempty(result) + null_point = GI.is3d(polygon) ? (GI.ismeasured(polygon) ? (NaN, NaN, NaN, NaN) : (NaN, NaN, NaN)) : (NaN, NaN) + contents = GI.LinearRing.([[null_point, null_point, null_point]]) + return GI.Polygon{GI.is3d(polygon),GI.ismeasured(polygon),typeof(contents),Nothing, typeof(GI.crs(polygon))}(contents, nothing, GI.crs(polygon)) + else + return GI.MultiPolygon(result; crs = GI.crs(polygon)) + end + =# + return GO.intersection(GO.GEOS(), polygon, clipper) +end +``` +First, let's load our data: +```@example clipping_featurecollection +df = nothing # DataFrame(Shapefile.Table(...)) +``` +and plot it: +```@example clipping_featurecollection +f, a, p = poly(df.geometry) +``` +Now, we can define some polygon in that space, that we want to use to clip all geometries by! +```@example clipping_featurecollection +clipping_poly = GI.Polygon([[(880_000, 990_000), (910_000, 990_000), (910_000, 1030_000), (880_000, 1030_000), (880_000, 990_000)]]) +poly!(a, clipping_poly; color = Makie.Cycled(2)) +f +``` +Finally, we clip, and show the output: +```@example clipping_featurecollection +clipped_geoms = clip_or_empty.(df.geometry, (clipping_poly,)) +``` +```@example clipping_featurecollection +poly!(a, clipped_geoms; color = Makie.Cycled(3), strokewidth = 0.75, strokecolor = :forestgreen) +f +```