Abstract supertype for barycentric coordinate methods. The subtypes may serve as dispatch types, or may cache some information about the target polygon.
API
The following methods must be implemented for all subtypes:
Two curves are equal if they share the same set of point, representing the same geometry. Both curves must must be composed of the same set of points, however, they do not have to wind in the same direction, or start on the same point to be equivalent. Inputs: c1 first geometry c2 second geometry closedtype1::Bool true if c1 is closed by definition (polygon, linear ring) closedtype2::Bool true if c2 is closed by definition (polygon, linear ring)
Calculates the intersection point between two lines if it exists, and as if the line extended to infinity, and the fractional component of each line from the initial end point to the intersection point. Inputs: (a1, a2)::Tuple{Tuple{::Real, ::Real}, Tuple{::Real, ::Real}} first line (b1, b2)::Tuple{Tuple{::Real, ::Real}, Tuple{::Real, ::Real}} second line Outputs: (x, y)::Tuple{::Real, ::Real} intersection point (t, u)::Tuple{::Real, ::Real} fractional length of lines to intersection Both are ::Nothing if point doesn't exist!
Calculation derivation can be found here: https://stackoverflow.com/questions/563198/
Reconstruct a geometry, feature, feature collection, or nested vectors of either using the function f on the target trait.
f(target_geom) => x where x also has the target trait, or a trait that can be substituted. For example, swapping PolgonTrait to MultiPointTrait will fail if the outer object has MultiPolygonTrait, but should work if it has FeatureTrait.
Objects "shallower" than the target trait are always completely rebuilt, like a Vector of FeatureCollectionTrait of FeatureTrait when the target has PolygonTrait and is held in the features. But "deeper" objects may remain unchanged - such as points and linear rings if the target is the same PolygonTrait.
The result is a functionally similar geometry with values depending on f
threaded: true or false. Whether to use multithreading. Defaults to false.
crs: The CRS to attach to geometries. Defaults to nothing.
calc_extent: true or false. Whether to calculate the extent. Defaults to false.
Example
Flipped point the order in any feature or geometry, or iterables of either:
```juia import GeoInterface as GI import GeometryOps as GO geom = GI.Polygon([GI.LinearRing([(1, 2), (3, 4), (5, 6), (1, 2)]), GI.LinearRing([(3, 4), (5, 6), (6, 7), (3, 4)])])
flipped_geom = GO.apply(GI.PointTrait, geom) do p (GI.y(p), GI.x(p)) end
Returns the area of the geometry. This is computed slighly differently for different geometries: - The area of a point/multipoint is always zero. - The area of a curve/multicurve is always zero. - The area of a polygon is the absolute value of the signed area. - The area multi-polygon is the sum of the areas of all of the sub-polygons. - The area of a geometry collection is the sum of the areas of all of the sub-geometries.
Result will be of type T, where T is an optional argument with a default value of Float64.
Return true if the second geometry is completely contained by the first geometry. The interiors of both geometries must intersect and, the interior and boundary of the secondary (geometry b) must not intersect the exterior of the primary (geometry a). contains returns the exact opposite result of within.
Examples
import GeometryOps as GO, GeoInterface as GI
+line = GI.LineString([(1, 1), (1, 2), (1, 3), (1, 4)])
+point = (1, 2)
+
+GO.contains(line, point)
+# output
+true
Return true if the intersection results in a geometry whose dimension is one less than the maximum dimension of the two source geometries and the intersection set is interior to both source geometries.
TODO: broken
Examples
import GeoInterface as GI, GeometryOps as GO
+
+line1 = GI.LineString([(1, 1), (1, 2), (1, 3), (1, 4)])
+line2 = GI.LineString([(-2, 2), (4, 2)])
+
+GO.crosses(line1, line2)
+# output
+true
Calculates the ditance from the geometry g1 to the point. The distance will always be positive or zero.
The method will differ based on the type of the geometry provided: - The distance from a point to a point is just the Euclidean distance between the points. - The distance from a point to a line is the minimum distance from the point to the closest point on the given line. - The distance from a point to a linestring is the minimum distance from the point to the closest segment of the linestring. - The distance from a point to a linear ring is the minimum distance from the point to the closest segment of the linear ring. - The distance from a point to a polygon is zero if the point is within the polygon and otherwise is the minimum distance from the point to an edge of the polygon. This includes edges created by holes. - The distance from a point to a multigeometry or a geometry collection is the minimum distance between the point and any of the sub-geometries.
Result will be of type T, where T is an optional argument with a default value of Float64.
Recursively wrap the object with a GeoInterface.jl geometry, calculating and adding an Extents.Extent to all objects.
This can improve performance when extents need to be checked multiple times, such when needing to check if many points are in geometries, and using their extents as a quick filter for obviously exterior points.
Keywords
threaded: true or false. Whether to use multithreading. Defaults to false.
crs: The CRS to attach to geometries. Defaults to nothing.
Two linear rings are equal if they share the same set of points going along the curve. Note that rings are closed by definition, so they can have, but don't need, a repeated last point to be equal.
A linear ring and a line/linestring are equal if they share the same set of points going along the curve. Note that lines aren't closed by defintion, but rings are, so the line must have a repeated last point to be equal
A line/linestring and a linear ring are equal if they share the same set of points going along the curve. Note that lines aren't closed by defintion, but rings are, so the line must have a repeated last point to be equal
Lazily flatten any AbstractArray, iterator, FeatureCollectionTrait, FeatureTrait or AbstractGeometryTrait object obj, so that objects with the target trait are returned by the iterator.
If f is passed in it will be applied to the target geometries.
Return an intersection point between two geometries. Return nothing if none are found. Else, the return type depends on the input. It will be a union between: a point, a line, a linear ring, a polygon, or a multipolygon
Example
import GeoInterface as GI, GeometryOps as GO
+
+line1 = GI.Line([(124.584961,-12.768946), (126.738281,-17.224758)])
+line2 = GI.Line([(123.354492,-15.961329), (127.22168,-14.008696)])
+GO.intersection(line1, line2)
+
+# output
+(125.58375366067547, -14.83572303404496)
Return a list of intersection points between two geometries. If no intersection point was possible given geometry extents, return nothing. If none are found, return an empty list.
Calculates the list of intersection points between two geometries, inlcuding line segments, line strings, linear rings, polygons, and multipolygons. If no intersection points were possible given geometry extents, return nothing. If none are found, return an empty list.
intersects(::GI.AbstractTrait, a, ::GI.AbstractTrait, b)::Bool
Returns true if two geometries intersect with one another and false otherwise. For all geometries but lines, convert the geometry to a list of edges and cross compare the edges for intersections.
Compare two Geometries of the same dimension and return true if their intersection set results in a geometry different from both but of the same dimension. This means one geometry cannot be within or contain the other and they cannot be equal
Examples
import GeometryOps as GO, GeoInterface as GI
+poly1 = GI.Polygon([[(0,0), (0,5), (5,5), (5,0), (0,0)]])
+poly2 = GI.Polygon([[(1,1), (1,6), (6,6), (6,1), (1,1)]])
+
+GO.overlaps(poly1, poly2)
+# output
+true
Take a Point and a Polygon and determine if the point resides inside the polygon. The polygon can be convex or concave. The function accounts for holes.
Examples
import GeoInterface as GI, GeometryOps as GO
+
+point = (-77.0, 44.0)
+poly = GI.Polygon([[(-81, 41), (-81, 47), (-72, 47), (-72, 41), (-81, 41)]])
+GO.point_in_polygon(point, poly)
+
+# output
+true
By default geometries will be rebuilt as a GeoInterface.Wrappers geometry, but rebuild can have methods added to it to dispatch on geometries from other packages and specify how to rebuild them.
Reproject any GeoInterface.jl compatible geometry from source_crs to target_crs.
The returned object will be constructed from GeoInterface.WrapperGeometry geometries, wrapping views of a Vector{Proj.Point{D}}, where D is the dimension.
Arguments
geometry: Any GeoInterface.jl compatible geometries.
source_crs: the source coordinate referece system, as a GeoFormatTypes.jl object or a string.
target_crs: the target coordinate referece system, as a GeoFormatTypes.jl object or a string.
If these a passed as keywords, transform will take priority. Without it target_crs is always needed, and source_crs is needed if it is not retreivable from the geometry with GeoInterface.crs(geometry).
Keywords
always_xy: force x, y coordinate order, true by default. false will expect and return points in the crs coordinate order.
time: the time for the coordinates. Inf by default.
threaded: true or false. Whether to use multithreading. Defaults to false.
crs: The CRS to attach to geometries. Defaults to nothing.
calc_extent: true or false. Whether to calculate the extent. Defaults to false.
Returns the signed area of the geometry, based on winding order. This is computed slighly differently for different geometries: - The signed area of a point is always zero. - The signed area of a curve is always zero. - The signed area of a polygon is computed with the shoelace formula and is positive if the polygon coordinates wind clockwise and negative if counterclockwise. - You cannot compute the signed area of a multipolygon as it doesn't have a meaning as each sub-polygon could have a different winding order.
Result will be of type T, where T is an optional argument with a default value of Float64.
Calculates the signed distance from the geometry geom to the given point. Points within geom have a negative signed distance, and points outside of geom have a positive signed distance. - The signed distance from a point to a point, line, linestring, or linear ring is equal to the distance between the two. - The signed distance from a point to a polygon is negative if the point is within the polygon and is positive otherwise. The value of the distance is the minimum distance from the point to an edge of the polygon. This includes edges created by holes. - The signed distance from a point to a multigeometry or a geometry collection is the minimum signed distance between the point and any of the sub-geometries.
Result will be of type T, where T is an optional argument with a default value of Float64.
HormannPresentationK. Hormann and N. Sukumar. Generalized Barycentric Coordinates in Computer Graphics and Computational Mechanics. Taylor & Fancis, CRC Press, 2017.
Settings
This document was generated with Documenter.jl version 0.27.24 on Monday 1 January 2024. Using Julia version 1.10.0.
This document was generated with Documenter.jl version 0.27.24 on Monday 1 January 2024. Using Julia version 1.10.0.
diff --git a/previews/PR36/search_index.js b/previews/PR36/search_index.js
new file mode 100644
index 000000000..3487a57c4
--- /dev/null
+++ b/previews/PR36/search_index.js
@@ -0,0 +1,3 @@
+var documenterSearchIndex = {"docs":
+[{"location":"source/GeometryOps/#GeometryOps.jl","page":"GeometryOps.jl","title":"GeometryOps.jl","text":"","category":"section"},{"location":"source/GeometryOps/","page":"GeometryOps.jl","title":"GeometryOps.jl","text":"module GeometryOps\n\nusing GeoInterface\nusing GeometryBasics\nimport Proj\nusing LinearAlgebra\nimport ExactPredicates\n\nusing GeoInterface.Extents: Extents\n\nconst GI = GeoInterface\nconst GB = GeometryBasics\n\nconst TuplePoint = Tuple{Float64,Float64}\nconst Edge = Tuple{TuplePoint,TuplePoint}\n\ninclude(\"primitives.jl\")\ninclude(\"utils.jl\")\n\ninclude(\"methods/bools.jl\")\ninclude(\"methods/distance.jl\")\ninclude(\"methods/area.jl\")\ninclude(\"methods/centroid.jl\")\ninclude(\"methods/intersects.jl\")\ninclude(\"methods/contains.jl\")\ninclude(\"methods/crosses.jl\")\ninclude(\"methods/disjoint.jl\")\ninclude(\"methods/overlaps.jl\")\ninclude(\"methods/within.jl\")\ninclude(\"methods/polygonize.jl\")\ninclude(\"methods/barycentric.jl\")\ninclude(\"methods/equals.jl\")\n\ninclude(\"transformations/extent.jl\")\ninclude(\"transformations/flip.jl\")\ninclude(\"transformations/simplify.jl\")\ninclude(\"transformations/reproject.jl\")\ninclude(\"transformations/tuples.jl\")\n\nend","category":"page"},{"location":"source/GeometryOps/","page":"GeometryOps.jl","title":"GeometryOps.jl","text":"","category":"page"},{"location":"source/GeometryOps/","page":"GeometryOps.jl","title":"GeometryOps.jl","text":"This page was generated using Literate.jl.","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"const THREADED_KEYWORD = \"- `threaded`: `true` or `false`. Whether to use multithreading. Defaults to `false`.\"\nconst CRS_KEYWORD = \"- `crs`: The CRS to attach to geometries. Defaults to `nothing`.\"\nconst CALC_EXTENT_KEYWORD = \"- `calc_extent`: `true` or `false`. Whether to calculate the extent. Defaults to `false`.\"\n\nconst APPLY_KEYWORDS = \"\"\"\n$THREADED_KEYWORD\n$CRS_KEYWORD\n$CALC_EXTENT_KEYWORD\n\"\"\"","category":"page"},{"location":"source/primitives/#Primitive-functions","page":"Primitive functions","title":"Primitive functions","text":"","category":"section"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"This file mainly defines the apply function.","category":"page"},{"location":"source/primitives/#What-is-apply?","page":"Primitive functions","title":"What is apply?","text":"","category":"section"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"apply applies some function to every geometry matching the Target GeoInterface trait, in some arbitrarily nested object made up of:","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"AbstractArrays (we also try to iterate other non-GeoInteface compatible object)\nFeatureCollectionTrait objects\nFeatureTrait objects\nAbstractGeometryTrait objects","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"apply recursively calls itself through these nested layers until it reaches objects with the Target GeoInterface trait. When found apply applies the function f, and stops.","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"The outer recursive functions then progressively rebuild the object using GeoInterface objects matching the original traits.","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"If PointTrait is found but it is not the Target, an error is thrown. This likely means the object contains a different geometry trait to the target, such as MultiPointTrait when LineStringTrait was specified.","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"To handle this possibility it may be necessary to make Target a Union of traits found at the same level of nesting, and define methods of f to handle all cases.","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"Be careful making a union across \"levels\" of nesting, e.g. Union{FeatureTrait,PolygonTrait}, as _apply will just never reach PolygonTrait when all the polygons are wrapped in a FeatureTrait object.","category":"page"},{"location":"source/primitives/#Embedding:","page":"Primitive functions","title":"Embedding:","text":"","category":"section"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"extent and crs can be embedded in all geometries, features, and feature collections as part of apply. Geometries deeper than Target will of course not have new extent or crs embedded.","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"calc_extent signals to recalculate an Extent and embed it.\ncrs will be embedded as-is","category":"page"},{"location":"source/primitives/#Threading","page":"Primitive functions","title":"Threading","text":"","category":"section"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"Threading is used at the outermost level possible - over an array, feature collection, or e.g. a MultiPolygonTrait where each PolygonTrait sub-geometry may be calculated on a different thread.","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"\"\"\"\n apply(f, target::Type{<:AbstractTrait}, obj; kw...)\n\nReconstruct a geometry, feature, feature collection, or nested vectors of\neither using the function `f` on the `target` trait.\n\n`f(target_geom) => x` where `x` also has the `target` trait, or a trait that can\nbe substituted. For example, swapping `PolgonTrait` to `MultiPointTrait` will fail\nif the outer object has `MultiPolygonTrait`, but should work if it has `FeatureTrait`.\n\nObjects \"shallower\" than the target trait are always completely rebuilt, like\na `Vector` of `FeatureCollectionTrait` of `FeatureTrait` when the target\nhas `PolygonTrait` and is held in the features. But \"deeper\" objects may remain\nunchanged - such as points and linear rings if the target is the same `PolygonTrait`.\n\nThe result is a functionally similar geometry with values depending on `f`\n\n$APPLY_KEYWORDS","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"Example","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"Flipped point the order in any feature or geometry, or iterables of either:\n\n```juia\nimport GeoInterface as GI\nimport GeometryOps as GO\ngeom = GI.Polygon([GI.LinearRing([(1, 2), (3, 4), (5, 6), (1, 2)]),\n GI.LinearRing([(3, 4), (5, 6), (6, 7), (3, 4)])])\n\nflipped_geom = GO.apply(GI.PointTrait, geom) do p\n (GI.y(p), GI.x(p))\nend\n\"\"\"\napply(f, ::Type{Target}, geom; kw...) where Target = _apply(f, Target, geom; kw...)","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"Call _apply again with the trait of geom","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"_apply(f, ::Type{Target}, geom; kw...) where Target =\n _apply(f, Target, GI.trait(geom), geom; kw...)","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"There is no trait and this is an AbstractArray - so just iterate over it calling _apply on the contents","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"function _apply(f, ::Type{Target}, ::Nothing, A::AbstractArray; threaded=false, kw...) where Target","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"For an Array there is nothing else to do but map _apply over all values maptasks may run this level threaded if threaded==true, but deeper `apply` called in the closure will not be threaded","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":" _maptasks(eachindex(A); threaded) do i\n _apply(f, Target, A[i]; threaded=false, kw...)\n end\nend","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"There is no trait and this is not an AbstractArray. Try to call _apply over it. We can't use threading as we don't know if we can can index into it. So just map. (TODO: maybe collect first if threaded=true so we can thread?)","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"_apply(f, ::Type{Target}, ::Nothing, iterable; kw...) where Target =\n map(x -> _apply(f, Target, x; kw...), iterable)","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"Rewrap all FeatureCollectionTrait feature collections as GI.FeatureCollection","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"function _apply(f, ::Type{Target}, ::GI.FeatureCollectionTrait, fc;\n crs=GI.crs(fc), calc_extent=false, threaded=false\n) where Target","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"Run _apply on all features in the feature collection, possibly threaded","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":" features = _maptasks(1:GI.nfeature(fc); threaded) do i\n feature = GI.getfeature(fc, i)\n _apply(f, Target, feature; crs, calc_extent, threaded=false)::GI.Feature\n end\n if calc_extent","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"Calculate the extent of the features","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":" extent = mapreduce(GI.extent, Extents.union, features)","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"Return a FeatureCollection with features, crs and caculated extent","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":" return GI.FeatureCollection(features; crs, extent)\n else","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"Return a FeatureCollection with features and crs","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":" return GI.FeatureCollection(features; crs)\n end\nend","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"Rewrap all FeatureTrait features as GI.Feature, keeping the properties","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"function _apply(f, ::Type{Target}, ::GI.FeatureTrait, feature;\n crs=GI.crs(feature), calc_extent=false, threaded=false\n) where Target","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"Run _apply on the contained geometry","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":" geometry = _apply(f, Target, GI.geometry(feature); crs, calc_extent, threaded)","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"Get the feature properties","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":" properties = GI.properties(feature)\n if calc_extent","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"Calculate the extent of the geometry","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":" extent = GI.extent(geometry)","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"Return a new Feature with the new geometry and calculated extent, but the oroginal properties and crs","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":" return GI.Feature(geometry; properties, crs, extent)\n else","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"Return a new Feature with the new geometry, but the oroginal properties and crs","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":" return GI.Feature(geometry; properties, crs)\n end\nend","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"Reconstruct nested geometries","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"function _apply(f, ::Type{Target}, trait, geom;\n crs=GI.crs(geom), calc_extent=false, threaded=false\n)::(GI.geointerface_geomtype(trait)) where Target","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"Map _apply over all sub geometries of geom to create a new vector of geometries TODO handle zero length","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":" geoms = _maptasks(1:GI.ngeom(geom); threaded) do i\n _apply(f, Target, GI.getgeom(geom, i); crs, calc_extent, threaded=false)\n end\n if calc_extent","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"Calculate the extent of the sub geometries","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":" extent = mapreduce(GI.extent, Extents.union, geoms)","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"Return a new geometry of the same trait as geom, holding tnew geoms with crs and calcualted extent","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":" return rebuild(geom, geoms; crs, extent)\n else","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"Return a new geometryof the same trait as geom, holding the new geoms with crs","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":" return rebuild(geom, geoms; crs)\n end\nend","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"Fail loudly if we hit PointTrait without running f (after PointTrait there is no further to dig with _apply)","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"_apply(f, ::Type{Target}, trait::GI.PointTrait, geom; crs=nothing, kw...) where Target =\n throw(ArgumentError(\"target $Target not found, but reached a `PointTrait` leaf\"))","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"Finally, these short methods are the main purpose of apply. The Trait is a subtype of the Target (or identical to it) So the Target is found. We apply f to geom and return it to previous _apply calls to be wrapped with the outer geometries/feature/featurecollection/array.","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"_apply(f, ::Type{Target}, ::Trait, geom; crs=GI.crs(geom), kw...) where {Target,Trait<:Target} = f(geom)","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"Define some specific cases of this match to avoid method ambiguity","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"_apply(f, ::Type{GI.PointTrait}, trait::GI.PointTrait, geom; kw...) = f(geom)\n_apply(f, ::Type{GI.FeatureTrait}, ::GI.FeatureTrait, feature; kw...) = f(feature)\n_apply(f, ::Type{GI.FeatureCollectionTrait}, ::GI.FeatureCollectionTrait, fc; kw...) = f(fc)\n\n\"\"\"\n unwrap(target::Type{<:AbstractTrait}, obj)\n unwrap(f, target::Type{<:AbstractTrait}, obj)\n\nUnwrap the object newst to vectors, down to the target trait.\n\nIf `f` is passed in it will be applied to the target geometries\nas they are found.\n\"\"\"\nfunction unwrap end\nunwrap(target::Type, geom) = unwrap(identity, target, geom)","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"Add dispatch argument for trait","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"unwrap(f, target::Type, geom) = unwrap(f, target, GI.trait(geom), geom)","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"Try to unwrap over iterables","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"unwrap(f, target::Type, ::Nothing, iterable) =\n map(x -> unwrap(f, target, x), iterable)","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"Rewrap feature collections","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"unwrap(f, target::Type, ::GI.FeatureCollectionTrait, fc) =\n map(x -> unwrap(f, target, x), GI.getfeature(fc))\nunwrap(f, target::Type, ::GI.FeatureTrait, feature) = unwrap(f, target, GI.geometry(feature))\nunwrap(f, target::Type, trait, geom) = map(g -> unwrap(f, target, g), GI.getgeom(geom))","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"Apply f to the target geometry","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"unwrap(f, ::Type{Target}, ::Trait, geom) where {Target,Trait<:Target} = f(geom)","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"Fail if we hit PointTrait","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"unwrap(f, target::Type, trait::GI.PointTrait, geom) =\n throw(ArgumentError(\"target $target not found, but reached a `PointTrait` leaf\"))","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"Specific cases to avoid method ambiguity","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"unwrap(f, target::Type{GI.PointTrait}, trait::GI.PointTrait, geom) = f(geom)\nunwrap(f, target::Type{GI.FeatureTrait}, ::GI.FeatureTrait, feature) = f(feature)\nunwrap(f, target::Type{GI.FeatureCollectionTrait}, ::GI.FeatureCollectionTrait, fc) = f(fc)\n\n\"\"\"\n flatten(target::Type{<:GI.AbstractTrait}, obj)\n flatten(f, target::Type{<:GI.AbstractTrait}, obj)\n\nLazily flatten any `AbstractArray`, iterator, `FeatureCollectionTrait`,\n`FeatureTrait` or `AbstractGeometryTrait` object `obj`, so that objects\nwith the `target` trait are returned by the iterator.\n\nIf `f` is passed in it will be applied to the target geometries.\n\"\"\"\nflatten(::Type{Target}, geom) where {Target<:GI.AbstractTrait} = flatten(identity, Target, geom)\nflatten(f, ::Type{Target}, geom) where {Target<:GI.AbstractTrait} = _flatten(f, Target, geom)\n\n_flatten(f, ::Type{Target}, geom) where Target = _flatten(f, Target, GI.trait(geom), geom)","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"Try to flatten over iterables","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"_flatten(f, ::Type{Target}, ::Nothing, iterable) where Target =\n Iterators.flatten(Iterators.map(x -> _flatten(f, Target, x), iterable))","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"Flatten feature collections","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"function _flatten(f, ::Type{Target}, ::GI.FeatureCollectionTrait, fc) where Target\n Iterators.map(GI.getfeature(fc)) do feature\n _flatten(f, Target, feature)\n end |> Iterators.flatten\nend\n_flatten(f, ::Type{Target}, ::GI.FeatureTrait, feature) where Target =\n _flatten(f, Target, GI.geometry(feature))","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"Apply f to the target geometry","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"_flatten(f, ::Type{Target}, ::Trait, geom) where {Target,Trait<:Target} = (f(geom),)\n_flatten(f, ::Type{Target}, trait, geom) where Target =\n Iterators.flatten(Iterators.map(g -> _flatten(f, Target, g), GI.getgeom(geom)))","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"Fail if we hit PointTrait without running f","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"_flatten(f, ::Type{Target}, trait::GI.PointTrait, geom) where Target =\n throw(ArgumentError(\"target $Target not found, but reached a `PointTrait` leaf\"))","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"Specific cases to avoid method ambiguity","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"_flatten(f, ::Type{<:GI.PointTrait}, ::GI.PointTrait, geom) = (f(geom),)\n_flatten(f, ::Type{<:GI.FeatureTrait}, ::GI.FeatureTrait, feature) = (f(feature),)\n_flatten(f, ::Type{<:GI.FeatureCollectionTrait}, ::GI.FeatureCollectionTrait, fc) = (f(fc),)\n\n\n\"\"\"\n reconstruct(geom, components)\n\nReconstruct `geom` from an iterable of component objects that match its structure.\n\nAll objects in `components` must have the same `GeoInterface.trait`.\n\nUsusally used in combination with `flatten`.\n\"\"\"\nfunction reconstruct(geom, components)\n obj, iter = _reconstruct(geom, components)\n return obj\nend\n\n_reconstruct(geom, components) =\n _reconstruct(typeof(GI.trait(first(components))), geom, components, 1)\n_reconstruct(::Type{Target}, geom, components, iter) where Target =\n _reconstruct(Target, GI.trait(geom), geom, components, iter)","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"Try to reconstruct over iterables","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"function _reconstruct(::Type{Target}, ::Nothing, iterable, components, iter) where Target\n vect = map(iterable) do x","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"iter is updated by _reconstruct here","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":" obj, iter = _reconstruct(Target, x, components, iter)\n obj\n end\n return vect, iter\nend","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"Reconstruct feature collections","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"function _reconstruct(::Type{Target}, ::GI.FeatureCollectionTrait, fc, components, iter) where Target\n features = map(GI.getfeature(fc)) do feature","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"iter is updated by _reconstruct here","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":" newfeature, iter = _reconstruct(Target, feature, components, iter)\n newfeature\n end\n return GI.FeatureCollection(features; crs=GI.crs(fc)), iter\nend\nfunction _reconstruct(::Type{Target}, ::GI.FeatureTrait, feature, components, iter) where Target\n geom, iter = _reconstruct(Target, GI.geometry(feature), components, iter)\n return GI.Feature(geom; properties=GI.properties(feature), crs=GI.crs(feature)), iter\nend\nfunction _reconstruct(::Type{Target}, trait, geom, components, iter) where Target\n geoms = map(GI.getgeom(geom)) do subgeom","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"iter is updated by _reconstruct here","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":" subgeom1, iter = _reconstruct(Target, GI.trait(subgeom), subgeom, components, iter)\n subgeom1\n end\n return rebuild(geom, geoms), iter\nend","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"Apply f to the target geometry","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"_reconstruct(::Type{Target}, ::Trait, geom, components, iter) where {Target,Trait<:Target} =\n iterate(components, iter)","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"Specific cases to avoid method ambiguity","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"_reconstruct(::Type{<:GI.PointTrait}, ::GI.PointTrait, geom, components, iter) = iterate(components, iter)\n_reconstruct(::Type{<:GI.FeatureTrait}, ::GI.FeatureTrait, feature, components, iter) = iterate(feature, iter)\n_reconstruct(::Type{<:GI.FeatureCollectionTrait}, ::GI.FeatureCollectionTrait, fc, components, iter) = iterate(fc, iter)","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"Fail if we hit PointTrait without running f","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"_reconstruct(::Type{Target}, trait::GI.PointTrait, geom, components, iter) where Target =\n throw(ArgumentError(\"target $Target not found, but reached a `PointTrait` leaf\"))\n\n\nconst BasicsGeoms = Union{GB.AbstractGeometry,GB.AbstractFace,GB.AbstractPoint,GB.AbstractMesh,\n GB.AbstractPolygon,GB.LineString,GB.MultiPoint,GB.MultiLineString,GB.MultiPolygon,GB.Mesh}\n\n\"\"\"\n rebuild(geom, child_geoms)\n\nRebuild a geometry from child geometries.\n\nBy default geometries will be rebuilt as a `GeoInterface.Wrappers`\ngeometry, but `rebuild` can have methods added to it to dispatch\non geometries from other packages and specify how to rebuild them.\n\n(Maybe it should go into GeoInterface.jl)\n\"\"\"\nrebuild(geom, child_geoms; kw...) = rebuild(GI.trait(geom), geom, child_geoms; kw...)\nfunction rebuild(trait::GI.AbstractTrait, geom, child_geoms; crs=GI.crs(geom), extent=nothing)\n T = GI.geointerface_geomtype(trait)\n if GI.is3d(geom)","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"The Boolean type parameters here indicate 3d-ness and measure coordinate presence respectively.","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":" return T{true,false}(child_geoms; crs, extent)\n else\n return T{false,false}(child_geoms; crs, extent)\n end\nend","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"So that GeometryBasics geoms rebuild as themselves","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"function rebuild(trait::GI.AbstractTrait, geom::BasicsGeoms, child_geoms; crs=nothing)\n GB.geointerface_geomtype(trait)(child_geoms)\nend\nfunction rebuild(trait::GI.AbstractTrait, geom::Union{GB.LineString,GB.MultiPoint}, child_geoms; crs=nothing)\n GB.geointerface_geomtype(trait)(GI.convert.(GB.Point, child_geoms))\nend\nfunction rebuild(trait::GI.PolygonTrait, geom::GB.Polygon, child_geoms; crs=nothing)\n Polygon(child_geoms[1], child_geoms[2:end])\nend\n\nusing Base.Threads: nthreads, @threads, @spawn","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"Threading utility, modified Mason Protters threading PSA run f over ntasks, where f recieves an AbstractArray/range of linear indices","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"function _maptasks(f, taskrange; threaded=false)\n if threaded\n ntasks = length(taskrange)","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"Customize this as needed. More tasks have more overhead, but better load balancing","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":" tasks_per_thread = 2\n chunk_size = max(1, ntasks ÷ (tasks_per_thread * nthreads()))","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"partition the range into chunks","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":" task_chunks = Iterators.partition(taskrange, chunk_size)","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"Map over the chunks","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":" tasks = map(task_chunks) do chunk","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"Spawn a task to process this chunk","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":" @spawn begin","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"Where we map f over the chunk indices","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":" map(f, chunk)\n end\n end","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"Finally we join the results into a new vector","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":" return mapreduce(fetch, vcat, tasks)\n else\n return map(f, taskrange)\n end\nend","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"","category":"page"},{"location":"source/primitives/","page":"Primitive functions","title":"Primitive functions","text":"This page was generated using Literate.jl.","category":"page"},{"location":"source/methods/polygonize/#Polygonizing-raster-data","page":"Polygonizing raster data","title":"Polygonizing raster data","text":"","category":"section"},{"location":"source/methods/polygonize/","page":"Polygonizing raster data","title":"Polygonizing raster data","text":"export polygonize","category":"page"},{"location":"source/methods/polygonize/","page":"Polygonizing raster data","title":"Polygonizing raster data","text":"The methods in this file are able to convert a raster image into a set of polygons, by contour detection using a clockwise Moore neighborhood method.","category":"page"},{"location":"source/methods/polygonize/","page":"Polygonizing raster data","title":"Polygonizing raster data","text":"The main entry point is the polygonize function.","category":"page"},{"location":"source/methods/polygonize/","page":"Polygonizing raster data","title":"Polygonizing raster data","text":"polygonize","category":"page"},{"location":"source/methods/polygonize/#Example","page":"Polygonizing raster data","title":"Example","text":"","category":"section"},{"location":"source/methods/polygonize/","page":"Polygonizing raster data","title":"Polygonizing raster data","text":"Here's a basic implementation, using the Makie.peaks() function. First, let's investigate the nature of the function:","category":"page"},{"location":"source/methods/polygonize/","page":"Polygonizing raster data","title":"Polygonizing raster data","text":"using Makie, GeometryOps\nn = 49\nxs, ys = LinRange(-3, 3, n), LinRange(-3, 3, n)\nzs = Makie.peaks(n)\nz_max_value = maximum(abs.(extrema(zs)))\nf, a, p = heatmap(\n xs, ys, zs;\n axis = (; aspect = DataAspect(), title = \"Exact function\")\n)\ncb = Colorbar(f[1, 2], p; label = \"Z-value\")\nf","category":"page"},{"location":"source/methods/polygonize/","page":"Polygonizing raster data","title":"Polygonizing raster data","text":"Now, we can use the polygonize function to convert the raster data into polygons.","category":"page"},{"location":"source/methods/polygonize/","page":"Polygonizing raster data","title":"Polygonizing raster data","text":"For this particular example, we chose a range of z-values between 0.8 and 3.2, which would provide two distinct polyogns with holes.","category":"page"},{"location":"source/methods/polygonize/","page":"Polygonizing raster data","title":"Polygonizing raster data","text":"polygons = polygonize(xs, ys, 0.8 .< zs .< 3.2)","category":"page"},{"location":"source/methods/polygonize/","page":"Polygonizing raster data","title":"Polygonizing raster data","text":"This returns a list of GeometryBasics.Polygon, which can be plotted immediately, or wrapped directly in a GeometryBasics.MultiPolygon. Let's see how these look:","category":"page"},{"location":"source/methods/polygonize/","page":"Polygonizing raster data","title":"Polygonizing raster data","text":"f, a, p = poly(polygons; label = \"Polygonized polygons\", axis = (; aspect = DataAspect()))","category":"page"},{"location":"source/methods/polygonize/","page":"Polygonizing raster data","title":"Polygonizing raster data","text":"Finally, let's plot the Makie contour lines on top, to see how well the polygonization worked:","category":"page"},{"location":"source/methods/polygonize/","page":"Polygonizing raster data","title":"Polygonizing raster data","text":"contour!(a, zs; labels = true, levels = [0.8, 3.2], label = \"Contour lines\")\nf","category":"page"},{"location":"source/methods/polygonize/#Implementation","page":"Polygonizing raster data","title":"Implementation","text":"","category":"section"},{"location":"source/methods/polygonize/","page":"Polygonizing raster data","title":"Polygonizing raster data","text":"The implementation follows:","category":"page"},{"location":"source/methods/polygonize/","page":"Polygonizing raster data","title":"Polygonizing raster data","text":"\"\"\"\n polygonize(A; minpoints=10)\n polygonize(xs, ys, A; minpoints=10)\n\nConvert matrix `A` to polygons.\n\nIf `xs` and `ys` are passed in they are used as the pixel center points.","category":"page"},{"location":"source/methods/polygonize/","page":"Polygonizing raster data","title":"Polygonizing raster data","text":"Keywords","category":"page"},{"location":"source/methods/polygonize/","page":"Polygonizing raster data","title":"Polygonizing raster data","text":"- `minpoints`: ignore polygons with less than `minpoints` points.\n\"\"\"\npolygonize(A::AbstractMatrix; kw...) = polygonize(axes(A)..., A; kw...)\n\nfunction polygonize(xs, ys, A::AbstractMatrix; minpoints=10)\n # This function uses a lazy map to get contours.\n contours = Iterators.map(get_contours(A)) do contour\n poly = map(contour) do xy\n x, y = Tuple(xy)\n Point2f(x + first(xs) - 1, y + first(ys) - 1)\n end\n end\n # If we filter off the minimum points, then it's a hair more efficient\n # not to convert contours with length < missingpoints to polygons.\n if minpoints > 1\n contours = Iterators.filter(contours) do contour\n length(contour) > minpoints\n end\n return map(Polygon, contours)\n else\n return map(Polygon, contours)\n end\nend\n\n# rotate direction clockwise\nrot_clockwise(dir) = (dir) % 8 + 1\n# rotate direction counterclockwise\nrot_counterclockwise(dir) = (dir + 6) % 8 + 1\n\n# move from current pixel to next in given direction\nfunction move(pixel, image, dir, dir_delta)\n newp = pixel + dir_delta[dir]\n height, width = size(image)\n if (0 < newp[1] <= height) && (0 < newp[2] <= width)\n if image[newp] != 0\n return newp\n end\n end\n return CartesianIndex(0, 0)\nend\n\n# finds direction between two given pixels\nfunction from_to(from, to, dir_delta)\n delta = to - from\n return findall(x -> x == delta, dir_delta)[1]\nend\n\nfunction detect_move(image, p0, p2, nbd, border, done, dir_delta)\n dir = from_to(p0, p2, dir_delta)\n moved = rot_clockwise(dir)\n p1 = CartesianIndex(0, 0)\n while moved != dir ## 3.1\n newp = move(p0, image, moved, dir_delta)\n if newp[1] != 0\n p1 = newp\n break\n end\n moved = rot_clockwise(moved)\n end\n\n if p1 == CartesianIndex(0, 0)\n return\n end\n\n p2 = p1 ## 3.2\n p3 = p0 ## 3.2\n done .= false\n while true\n dir = from_to(p3, p2, dir_delta)\n moved = rot_counterclockwise(dir)\n p4 = CartesianIndex(0, 0)\n done .= false\n while true ## 3.3\n p4 = move(p3, image, moved, dir_delta)\n if p4[1] != 0\n break\n end\n done[moved] = true\n moved = rot_counterclockwise(moved)\n end\n push!(border, p3) ## 3.4\n if p3[1] == size(image, 1) || done[3]\n image[p3] = -nbd\n elseif image[p3] == 1\n image[p3] = nbd\n end\n\n if (p4 == p0 && p3 == p1) ## 3.5\n break\n end\n p2 = p3\n p3 = p4\n end\nend\n\n\"\"\"\n get_contours(A::AbstractMatrix)\n\nReturns contours as vectors of `CartesianIndex`.\n\"\"\"\nfunction get_contours(image::AbstractMatrix)\n nbd = 1\n lnbd = 1\n image = Float64.(image)\n contour_list = Vector{typeof(CartesianIndex[])}()\n done = [false, false, false, false, false, false, false, false]\n\n # Clockwise Moore neighborhood.\n dir_delta = (CartesianIndex(-1, 0), CartesianIndex(-1, 1), CartesianIndex(0, 1), CartesianIndex(1, 1),\n CartesianIndex(1, 0), CartesianIndex(1, -1), CartesianIndex(0, -1), CartesianIndex(-1, -1))\n\n height, width = size(image)\n\n for i = 1:height\n lnbd = 1\n for j = 1:width\n fji = image[i, j]\n is_outer = (image[i, j] == 1 && (j == 1 || image[i, j-1] == 0)) ## 1 (a)\n is_hole = (image[i, j] >= 1 && (j == width || image[i, j+1] == 0))\n\n if is_outer || is_hole\n # 2\n border = CartesianIndex[]\n from = CartesianIndex(i, j)\n\n if is_outer\n nbd += 1\n from -= CartesianIndex(0, 1)\n\n else\n nbd += 1\n if fji > 1\n lnbd = fji\n end\n from += CartesianIndex(0, 1)\n end\n\n p0 = CartesianIndex(i, j)\n detect_move(image, p0, from, nbd, border, done, dir_delta) ## 3\n if isempty(border) ##TODO\n push!(border, p0)\n image[p0] = -nbd\n end\n push!(contour_list, border)\n end\n if fji != 0 && fji != 1\n lnbd = abs(fji)\n end\n\n end\n end\n\n return contour_list\nend","category":"page"},{"location":"source/methods/polygonize/","page":"Polygonizing raster data","title":"Polygonizing raster data","text":"","category":"page"},{"location":"source/methods/polygonize/","page":"Polygonizing raster data","title":"Polygonizing raster data","text":"This page was generated using Literate.jl.","category":"page"},{"location":"source/transformations/reproject/#Geometry-reprojection","page":"Geometry reprojection","title":"Geometry reprojection","text":"","category":"section"},{"location":"source/transformations/reproject/","page":"Geometry reprojection","title":"Geometry reprojection","text":"export reproject","category":"page"},{"location":"source/transformations/reproject/","page":"Geometry reprojection","title":"Geometry reprojection","text":"This file is pretty simple - it simply reprojects a geometry pointwise from one CRS to another. It uses the Proj package for the transformation, but this could be moved to an extension if needed.","category":"page"},{"location":"source/transformations/reproject/","page":"Geometry reprojection","title":"Geometry reprojection","text":"This works using the apply functionality.","category":"page"},{"location":"source/transformations/reproject/","page":"Geometry reprojection","title":"Geometry reprojection","text":"\"\"\"\n reproject(geometry; source_crs, target_crs, transform, always_xy, time)\n reproject(geometry, source_crs, target_crs; always_xy, time)\n reproject(geometry, transform; always_xy, time)\n\nReproject any GeoInterface.jl compatible `geometry` from `source_crs` to `target_crs`.\n\nThe returned object will be constructed from `GeoInterface.WrapperGeometry`\ngeometries, wrapping views of a `Vector{Proj.Point{D}}`, where `D` is the dimension.\n\n# Arguments\n\n- `geometry`: Any GeoInterface.jl compatible geometries.\n- `source_crs`: the source coordinate referece system, as a GeoFormatTypes.jl object or a string.\n- `target_crs`: the target coordinate referece system, as a GeoFormatTypes.jl object or a string.\n\nIf these a passed as keywords, `transform` will take priority.\nWithout it `target_crs` is always needed, and `source_crs` is\nneeded if it is not retreivable from the geometry with `GeoInterface.crs(geometry)`.\n\n# Keywords\n\n- `always_xy`: force x, y coordinate order, `true` by default.\n `false` will expect and return points in the crs coordinate order.\n- `time`: the time for the coordinates. `Inf` by default.\n$APPLY_KEYWORDS\n\"\"\"\nfunction reproject(geom;\n source_crs=nothing, target_crs=nothing, transform=nothing, kw...\n)\n if isnothing(transform)\n if isnothing(source_crs)\n source_crs = if GI.trait(geom) isa Nothing && geom isa AbstractArray\n GeoInterface.crs(first(geom))\n else\n GeoInterface.crs(geom)\n end\n end","category":"page"},{"location":"source/transformations/reproject/","page":"Geometry reprojection","title":"Geometry reprojection","text":"If its still nothing, error","category":"page"},{"location":"source/transformations/reproject/","page":"Geometry reprojection","title":"Geometry reprojection","text":" isnothing(source_crs) && throw(ArgumentError(\"geom has no crs attatched. Pass a `source_crs` keyword\"))","category":"page"},{"location":"source/transformations/reproject/","page":"Geometry reprojection","title":"Geometry reprojection","text":"Otherwise reproject","category":"page"},{"location":"source/transformations/reproject/","page":"Geometry reprojection","title":"Geometry reprojection","text":" reproject(geom, source_crs, target_crs; kw...)\n else\n reproject(geom, transform; kw...)\n end\nend\nfunction reproject(geom, source_crs, target_crs;\n time=Inf,\n always_xy=true,\n transform=Proj.Transformation(Proj.CRS(source_crs), Proj.CRS(target_crs); always_xy),\n kw...\n)\n reproject(geom, transform; time, target_crs, kw...)\nend\nfunction reproject(geom, transform::Proj.Transformation; time=Inf, target_crs=nothing, kw...)\n if _is3d(geom)\n return apply(PointTrait, geom; crs=target_crs, kw...) do p\n transform(GI.x(p), GI.y(p), GI.z(p))\n end\n else\n return apply(PointTrait, geom; crs=target_crs, kw...) do p\n transform(GI.x(p), GI.y(p))\n end\n end\nend","category":"page"},{"location":"source/transformations/reproject/","page":"Geometry reprojection","title":"Geometry reprojection","text":"","category":"page"},{"location":"source/transformations/reproject/","page":"Geometry reprojection","title":"Geometry reprojection","text":"This page was generated using Literate.jl.","category":"page"},{"location":"source/methods/overlaps/#Overlaps","page":"Overlaps","title":"Overlaps","text":"","category":"section"},{"location":"source/methods/overlaps/","page":"Overlaps","title":"Overlaps","text":"export overlaps","category":"page"},{"location":"source/methods/overlaps/#What-is-overlaps?","page":"Overlaps","title":"What is overlaps?","text":"","category":"section"},{"location":"source/methods/overlaps/","page":"Overlaps","title":"Overlaps","text":"The overlaps function checks if two geometries overlap. Two geometries can only overlap if they have the same dimension, and if they overlap, but one is not contained, within, or equal to the other.","category":"page"},{"location":"source/methods/overlaps/","page":"Overlaps","title":"Overlaps","text":"Note that this means it is impossible for a single point to overlap with a single point and a line only overlaps with another line if only a section of each line is colinear.","category":"page"},{"location":"source/methods/overlaps/","page":"Overlaps","title":"Overlaps","text":"To provide an example, consider these two lines:","category":"page"},{"location":"source/methods/overlaps/","page":"Overlaps","title":"Overlaps","text":"using GeometryOps\nusing GeometryOps.GeometryBasics\nusing Makie\nusing CairoMakie\n\nl1 = GI.LineString([(0.0, 0.0), (0.0, 10.0)])\nl2 = GI.LineString([(0.0, -10.0), (0.0, 3.0)])\nf, a, p = lines(GI.getpoint(l1), color = :blue)\nscatter!(GI.getpoint(l1), color = :blue)\nlines!(GI.getpoint(l2), color = :orange)\nscatter!(GI.getpoint(l2), color = :orange)","category":"page"},{"location":"source/methods/overlaps/","page":"Overlaps","title":"Overlaps","text":"We can see that the two lines overlap in the plot:","category":"page"},{"location":"source/methods/overlaps/","page":"Overlaps","title":"Overlaps","text":"overlap(l1, l2)","category":"page"},{"location":"source/methods/overlaps/#Implementation","page":"Overlaps","title":"Implementation","text":"","category":"section"},{"location":"source/methods/overlaps/","page":"Overlaps","title":"Overlaps","text":"This is the GeoInterface-compatible implementation.","category":"page"},{"location":"source/methods/overlaps/","page":"Overlaps","title":"Overlaps","text":"First, we implement a wrapper method that dispatches to the correct implementation based on the geometry trait. This is also used in the implementation, since it's a lot less work!","category":"page"},{"location":"source/methods/overlaps/","page":"Overlaps","title":"Overlaps","text":"Note that that since only elements of the same dimension can overlap, any two geometries with traits that are of different dimensions autmoatically can return false.","category":"page"},{"location":"source/methods/overlaps/","page":"Overlaps","title":"Overlaps","text":"For geometries with the same trait dimension, we must make sure that they share a point, an edge, or area for points, lines, and polygons/multipolygons respectivly, without being contained.","category":"page"},{"location":"source/methods/overlaps/","page":"Overlaps","title":"Overlaps","text":"\"\"\"\n overlaps(geom1, geom2)::Bool\n\nCompare two Geometries of the same dimension and return true if their\nintersection set results in a geometry different from both but of the same\ndimension. This means one geometry cannot be within or contain the other and\nthey cannot be equal\n\n# Examples\n```jldoctest\nimport GeometryOps as GO, GeoInterface as GI\npoly1 = GI.Polygon([[(0,0), (0,5), (5,5), (5,0), (0,0)]])\npoly2 = GI.Polygon([[(1,1), (1,6), (6,6), (6,1), (1,1)]])\n\nGO.overlaps(poly1, poly2)","category":"page"},{"location":"source/methods/overlaps/","page":"Overlaps","title":"Overlaps","text":"output","category":"page"},{"location":"source/methods/overlaps/","page":"Overlaps","title":"Overlaps","text":"true\n```\n\"\"\"\noverlaps(geom1, geom2)::Bool = overlaps(\n GI.trait(geom1),\n geom1,\n GI.trait(geom2),\n geom2,\n)\n\n\"\"\"\n overlaps(::GI.AbstractTrait, geom1, ::GI.AbstractTrait, geom2)::Bool\n\nFor any non-specified pair, all have non-matching dimensions, return false.\n\"\"\"\noverlaps(::GI.AbstractTrait, geom1, ::GI.AbstractTrait, geom2) = false\n\n\"\"\"\n overlaps(\n ::GI.MultiPointTrait, points1,\n ::GI.MultiPointTrait, points2,\n )::Bool\n\nIf the multipoints overlap, meaning some, but not all, of the points within the\nmultipoints are shared, return true.\n\"\"\"\nfunction overlaps(\n ::GI.MultiPointTrait, points1,\n ::GI.MultiPointTrait, points2,\n)\n one_diff = false # assume that all the points are the same\n one_same = false # assume that all points are different\n for p1 in GI.getpoint(points1)\n match_point = false\n for p2 in GI.getpoint(points2)\n if equals(p1, p2) # Point is shared\n one_same = true\n match_point = true\n break\n end\n end\n one_diff |= !match_point # Point isn't shared\n one_same && one_diff && return true\n end\n return false\nend\n\n\"\"\"\n overlaps(::GI.LineTrait, line1, ::GI.LineTrait, line)::Bool\n\nIf the lines overlap, meaning that they are colinear but each have one endpoint\noutside of the other line, return true. Else false.\n\"\"\"\noverlaps(::GI.LineTrait, line1, ::GI.LineTrait, line) =\n _overlaps((a1, a2), (b1, b2))\n\n\"\"\"\n overlaps(\n ::Union{GI.LineStringTrait, GI.LinearRing}, line1,\n ::Union{GI.LineStringTrait, GI.LinearRing}, line2,\n )::Bool\n\nIf the curves overlap, meaning that at least one edge of each curve overlaps,\nreturn true. Else false.\n\"\"\"\nfunction overlaps(\n ::Union{GI.LineStringTrait, GI.LinearRing}, line1,\n ::Union{GI.LineStringTrait, GI.LinearRing}, line2,\n)\n edges_a, edges_b = map(sort! ∘ to_edges, (line1, line2))\n for edge_a in edges_a\n for edge_b in edges_b\n _overlaps(edge_a, edge_b) && return true\n end\n end\n return false\nend\n\n\"\"\"\n overlaps(\n trait_a::GI.PolygonTrait, poly_a,\n trait_b::GI.PolygonTrait, poly_b,\n )::Bool\n\nIf the two polygons intersect with one another, but are not equal, return true.\nElse false.\n\"\"\"\nfunction overlaps(\n trait_a::GI.PolygonTrait, poly_a,\n trait_b::GI.PolygonTrait, poly_b,\n)\n edges_a, edges_b = map(sort! ∘ to_edges, (poly_a, poly_b))\n return _line_intersects(edges_a, edges_b) &&\n !equals(trait_a, poly_a, trait_b, poly_b)\nend\n\n\"\"\"\n overlaps(\n ::GI.PolygonTrait, poly1,\n ::GI.MultiPolygonTrait, polys2,\n )::Bool\n\nReturn true if polygon overlaps with at least one of the polygons within the\nmultipolygon. Else false.\n\"\"\"\nfunction overlaps(\n ::GI.PolygonTrait, poly1,\n ::GI.MultiPolygonTrait, polys2,\n)\n for poly2 in GI.getgeom(polys2)\n overlaps(poly1, poly2) && return true\n end\n return false\nend\n\n\"\"\"\n overlaps(\n ::GI.MultiPolygonTrait, polys1,\n ::GI.PolygonTrait, poly2,\n )::Bool\n\nReturn true if polygon overlaps with at least one of the polygons within the\nmultipolygon. Else false.\n\"\"\"\noverlaps(trait1::GI.MultiPolygonTrait, polys1, trait2::GI.PolygonTrait, poly2) =\n overlaps(trait2, poly2, trait1, polys1)\n\n\"\"\"\n overlaps(\n ::GI.MultiPolygonTrait, polys1,\n ::GI.MultiPolygonTrait, polys2,\n )::Bool\n\nReturn true if at least one pair of polygons from multipolygons overlap. Else\nfalse.\n\"\"\"\nfunction overlaps(\n ::GI.MultiPolygonTrait, polys1,\n ::GI.MultiPolygonTrait, polys2,\n)\n for poly1 in GI.getgeom(polys1)\n overlaps(poly1, polys2) && return true\n end\n return false\nend\n\n\"\"\"\n _overlaps(\n (a1, a2)::Edge,\n (b1, b2)::Edge\n )::Bool\n\nIf the edges overlap, meaning that they are colinear but each have one endpoint\noutside of the other edge, return true. Else false.\n\"\"\"\nfunction _overlaps(\n (a1, a2)::Edge,\n (b1, b2)::Edge\n)","category":"page"},{"location":"source/methods/overlaps/","page":"Overlaps","title":"Overlaps","text":"meets in more than one point","category":"page"},{"location":"source/methods/overlaps/","page":"Overlaps","title":"Overlaps","text":" on_top = ExactPredicates.meet(a1, a2, b1, b2) == 0","category":"page"},{"location":"source/methods/overlaps/","page":"Overlaps","title":"Overlaps","text":"one end point is outside of other segment","category":"page"},{"location":"source/methods/overlaps/","page":"Overlaps","title":"Overlaps","text":" a_fully_within = point_on_seg(a1, b1, b2) && point_on_seg(a2, b1, b2)\n b_fully_within = point_on_seg(b1, a1, a2) && point_on_seg(b2, a1, a2)\n return on_top && (!a_fully_within && !b_fully_within)\nend","category":"page"},{"location":"source/methods/overlaps/","page":"Overlaps","title":"Overlaps","text":"","category":"page"},{"location":"source/methods/overlaps/","page":"Overlaps","title":"Overlaps","text":"This page was generated using Literate.jl.","category":"page"},{"location":"source/methods/barycentric/#Barycentric-coordinates","page":"Barycentric coordinates","title":"Barycentric coordinates","text":"","category":"section"},{"location":"source/methods/barycentric/","page":"Barycentric coordinates","title":"Barycentric coordinates","text":"export barycentric_coordinates, barycentric_coordinates!, barycentric_interpolate\nexport MeanValue","category":"page"},{"location":"source/methods/barycentric/","page":"Barycentric coordinates","title":"Barycentric coordinates","text":"Generalized barycentric coordinates are a generalization of barycentric coordinates, which are typically used in triangles, to arbitrary polygons.","category":"page"},{"location":"source/methods/barycentric/","page":"Barycentric coordinates","title":"Barycentric coordinates","text":"They provide a way to express a point within a polygon as a weighted average of the polygon's vertices.","category":"page"},{"location":"source/methods/barycentric/","page":"Barycentric coordinates","title":"Barycentric coordinates","text":"In the case of a triangle, barycentric coordinates are a set of three numbers (λ_1 λ_2 λ_3), each associated with a vertex of the triangle. Any point within the triangle can be expressed as a weighted average of the vertices, where the weights are the barycentric coordinates. The weights sum to 1, and each is non-negative.","category":"page"},{"location":"source/methods/barycentric/","page":"Barycentric coordinates","title":"Barycentric coordinates","text":"For a polygon with n vertices, generalized barycentric coordinates are a set of n numbers (λ_1 λ_2 λ_n), each associated with a vertex of the polygon. Any point within the polygon can be expressed as a weighted average of the vertices, where the weights are the generalized barycentric coordinates.","category":"page"},{"location":"source/methods/barycentric/","page":"Barycentric coordinates","title":"Barycentric coordinates","text":"As with the triangle case, the weights sum to 1, and each is non-negative.","category":"page"},{"location":"source/methods/barycentric/#Example","page":"Barycentric coordinates","title":"Example","text":"","category":"section"},{"location":"source/methods/barycentric/","page":"Barycentric coordinates","title":"Barycentric coordinates","text":"This example was taken from this page of CGAL's documentation.","category":"page"},{"location":"source/methods/barycentric/","page":"Barycentric coordinates","title":"Barycentric coordinates","text":"using GeometryOps, Makie\nusing GeometryOps.GeometryBasics\n# Define a polygon\npolygon_points = Point3f[\n(0.03, 0.05, 0.00), (0.07, 0.04, 0.02), (0.10, 0.04, 0.04),\n(0.14, 0.04, 0.06), (0.17, 0.07, 0.08), (0.20, 0.09, 0.10),\n(0.22, 0.11, 0.12), (0.25, 0.11, 0.14), (0.27, 0.10, 0.16),\n(0.30, 0.07, 0.18), (0.31, 0.04, 0.20), (0.34, 0.03, 0.22),\n(0.37, 0.02, 0.24), (0.40, 0.03, 0.26), (0.42, 0.04, 0.28),\n(0.44, 0.07, 0.30), (0.45, 0.10, 0.32), (0.46, 0.13, 0.34),\n(0.46, 0.19, 0.36), (0.47, 0.26, 0.38), (0.47, 0.31, 0.40),\n(0.47, 0.35, 0.42), (0.45, 0.37, 0.44), (0.41, 0.38, 0.46),\n(0.38, 0.37, 0.48), (0.35, 0.36, 0.50), (0.32, 0.35, 0.52),\n(0.30, 0.37, 0.54), (0.28, 0.39, 0.56), (0.25, 0.40, 0.58),\n(0.23, 0.39, 0.60), (0.21, 0.37, 0.62), (0.21, 0.34, 0.64),\n(0.23, 0.32, 0.66), (0.24, 0.29, 0.68), (0.27, 0.24, 0.70),\n(0.29, 0.21, 0.72), (0.29, 0.18, 0.74), (0.26, 0.16, 0.76),\n(0.24, 0.17, 0.78), (0.23, 0.19, 0.80), (0.24, 0.22, 0.82),\n(0.24, 0.25, 0.84), (0.21, 0.26, 0.86), (0.17, 0.26, 0.88),\n(0.12, 0.24, 0.90), (0.07, 0.20, 0.92), (0.03, 0.15, 0.94),\n(0.01, 0.10, 0.97), (0.02, 0.07, 1.00)]\n# Plot it!\n# First, we'll plot the polygon using Makie's rendering:\nf, a1, p1 = poly(\n polygon_points;\n color = last.(polygon_points), colormap = cgrad(:jet, 18; categorical = true),\n axis = (;\n aspect = DataAspect(), title = \"Makie mesh based polygon rendering\", subtitle = \"CairoMakie\"\n ),\n figure = (; resolution = (800, 400),)\n)\n\nMakie.update_state_before_display!(f) # We have to call this explicitly, to get the axis limits correct\n# Now that we've plotted the first polygon,\n# we can render it using barycentric coordinates.\na1_bbox = a1.finallimits[] # First we get the extent of the axis\next = GeometryOps.GI.Extent(NamedTuple{(:X, :Y)}(zip(minimum(a1_bbox), maximum(a1_bbox))))\n\na2, p2box = poly( # Now, we plot a cropping rectangle around the axis so we only show the polygon\n f[1, 2],\n GeometryOps.GeometryBasics.Polygon( # This is a rectangle with an internal hole shaped like the polygon.\n Point2f[(ext.X[1], ext.Y[1]), (ext.X[2], ext.Y[1]), (ext.X[2], ext.Y[2]), (ext.X[1], ext.Y[2]), (ext.X[1], ext.Y[1])],\n [reverse(Point2f.(polygon_points))]\n );\n color = :white, xautolimits = false, yautolimits = false,\n axis = (;\n aspect = DataAspect(), title = \"Barycentric coordinate based polygon rendering\", subtitle = \"GeometryOps\",\n limits = (ext.X, ext.Y),\n )\n)\nhidedecorations!(a1)\nhidedecorations!(a2)\ncb = Colorbar(f[2, :], p1.plots[1]; vertical = false, flipaxis = true)\n# Finally, we perform barycentric interpolation on a grid,\nxrange = LinRange(ext.X..., widths(a2.scene.px_area[])[1] * 4) # 2 rendered pixels per \"physical\" pixel\nyrange = LinRange(ext.Y..., widths(a2.scene.px_area[])[2] * 4) # 2 rendered pixels per \"physical\" pixel\n@time mean_values = barycentric_interpolate.(\n (MeanValue(),), # The barycentric coordinate algorithm (MeanValue is the only one for now)\n (Point2f.(polygon_points),), # The polygon points as `Point2f`\n (last.(polygon_points,),), # The values per polygon point - can be anything which supports addition and division\n Point2f.(xrange, yrange') # The points at which to interpolate\n)\n# and render!\nhm = heatmap!(\n a2, xrange, yrange, mean_values;\n colormap = p1.colormap, # Use the same colormap as the original polygon plot\n colorrange = p1.plots[1].colorrange[], # Access the rendered mesh plot's colorrange directly\n transformation = (; translation = Vec3f(0,0,-1)), # This gets the heatmap to render \"behind\" the previously plotted polygon\n xautolimits = false, yautolimits = false\n)\nf","category":"page"},{"location":"source/methods/barycentric/#Barycentric-coordinate-API","page":"Barycentric coordinates","title":"Barycentric-coordinate API","text":"","category":"section"},{"location":"source/methods/barycentric/","page":"Barycentric coordinates","title":"Barycentric coordinates","text":"In some cases, we actually want barycentric interpolation, and have no interest in the coordinates themselves.","category":"page"},{"location":"source/methods/barycentric/","page":"Barycentric coordinates","title":"Barycentric coordinates","text":"However, the coordinates can be useful for debugging, and when performing 3D rendering, multiple barycentric values (depth, uv) are needed for depth buffering.","category":"page"},{"location":"source/methods/barycentric/","page":"Barycentric coordinates","title":"Barycentric coordinates","text":"const _VecTypes = Union{Tuple{Vararg{T, N}}, GeometryBasics.StaticArraysCore.StaticArray{Tuple{N}, T, 1}} where {N, T}\n\n\"\"\"\n abstract type AbstractBarycentricCoordinateMethod\n\nAbstract supertype for barycentric coordinate methods.\nThe subtypes may serve as dispatch types, or may cache\nsome information about the target polygon.\n\n# API\nThe following methods must be implemented for all subtypes:\n- `barycentric_coordinates!(λs::Vector{<: Real}, method::AbstractBarycentricCoordinateMethod, exterior::Vector{<: Point{2, T1}}, point::Point{2, T2})`\n- `barycentric_interpolate(method::AbstractBarycentricCoordinateMethod, exterior::Vector{<: Point{2, T1}}, values::Vector{V}, point::Point{2, T2})::V`\n- `barycentric_interpolate(method::AbstractBarycentricCoordinateMethod, exterior::Vector{<: Point{2, T1}}, interiors::Vector{<: Vector{<: Point{2, T1}}} values::Vector{V}, point::Point{2, T2})::V`\nThe rest of the methods will be implemented in terms of these, and have efficient dispatches for broadcasting.\n\"\"\"\nabstract type AbstractBarycentricCoordinateMethod end\n\n\nBase.@propagate_inbounds function barycentric_coordinates!(λs::Vector{<: Real}, method::AbstractBarycentricCoordinateMethod, polypoints::AbstractVector{<: Point{N1, T1}}, point::Point{N2, T2}) where {N1, N2, T1 <: Real, T2 <: Real}\n @boundscheck @assert length(λs) == length(polypoints)\n @boundscheck @assert length(polypoints) >= 3\n\n @error(\"Not implemented yet for method $(method).\")\nend\nBase.@propagate_inbounds barycentric_coordinates!(λs::Vector{<: Real}, polypoints::AbstractVector{<: Point{N1, T1}}, point::Point{N2, T2}) where {N1, N2, T1 <: Real, T2 <: Real} = barycentric_coordinates!(λs, MeanValue(), polypoints, point)\n\nBase.@propagate_inbounds function barycentric_coordinates(method::AbstractBarycentricCoordinateMethod, polypoints::AbstractVector{<: Point{N1, T1}}, point::Point{N2, T2}) where {N1, N2, T1 <: Real, T2 <: Real}\n λs = zeros(promote_type(T1, T2), length(polypoints))\n barycentric_coordinates!(λs, method, polypoints, point)\n return λs\nend\nBase.@propagate_inbounds barycentric_coordinates(polypoints::AbstractVector{<: Point{N1, T1}}, point::Point{N2, T2}) where {N1, N2, T1 <: Real, T2 <: Real} = barycentric_coordinates(MeanValue(), polypoints, point)\n\nBase.@propagate_inbounds function barycentric_interpolate(method::AbstractBarycentricCoordinateMethod, polypoints::AbstractVector{<: Point{N, T1}}, values::AbstractVector{V}, point::Point{N, T2}) where {N, T1 <: Real, T2 <: Real, V}\n @boundscheck @assert length(values) == length(polypoints)\n @boundscheck @assert length(polypoints) >= 3\n λs = barycentric_coordinates(method, polypoints, point)\n return sum(λs .* values)\nend\nBase.@propagate_inbounds barycentric_interpolate(polypoints::AbstractVector{<: Point{N, T1}}, values::AbstractVector{V}, point::Point{N, T2}) where {N, T1 <: Real, T2 <: Real, V} = barycentric_interpolate(MeanValue(), polypoints, values, point)\n\nBase.@propagate_inbounds function barycentric_interpolate(method::AbstractBarycentricCoordinateMethod, exterior::AbstractVector{<: Point{N, T1}}, interiors::AbstractVector{<: Point{N, T1}}, values::AbstractVector{V}, point::Point{N, T2}) where {N, T1 <: Real, T2 <: Real, V}\n @boundscheck @assert length(values) == length(exterior) + isempty(interiors) ? 0 : sum(length.(interiors))\n @boundscheck @assert length(exterior) >= 3\n λs = barycentric_coordinates(method, exterior, interiors, point)\n return sum(λs .* values)\nend\nBase.@propagate_inbounds barycentric_interpolate(exterior::AbstractVector{<: Point{N, T1}}, interiors::AbstractVector{<: Point{N, T1}}, values::AbstractVector{V}, point::Point{N, T2}) where {N, T1 <: Real, T2 <: Real, V} = barycentric_interpolate(MeanValue(), exterior, interiors, values, point)\n\nBase.@propagate_inbounds function barycentric_interpolate(method::AbstractBarycentricCoordinateMethod, polygon::Polygon{2, T1}, values::AbstractVector{V}, point::Point{2, T2}) where {T1 <: Real, T2 <: Real, V}\n exterior = decompose(Point{2, promote_type(T1, T2)}, polygon.exterior)\n if isempty(polygon.interiors)\n @boundscheck @assert length(values) == length(exterior)\n return barycentric_interpolate(method, exterior, values, point)\n else # the poly has interiors\n interiors = reverse.(decompose.((Point{2, promote_type(T1, T2)},), polygon.interiors))\n @boundscheck @assert length(values) == length(exterior) + sum(length.(interiors))\n return barycentric_interpolate(method, exterior, interiors, values, point)\n end\nend\nBase.@propagate_inbounds barycentric_interpolate(polygon::Polygon{2, T1}, values::AbstractVector{V}, point::Point{2, T2}) where {T1 <: Real, T2 <: Real, V} = barycentric_interpolate(MeanValue(), polygon, values, point)","category":"page"},{"location":"source/methods/barycentric/","page":"Barycentric coordinates","title":"Barycentric coordinates","text":"3D polygons are considered to have their vertices in the XY plane, and the Z coordinate must represent some value. This is to say that the Z coordinate is interpreted as an M coordinate.","category":"page"},{"location":"source/methods/barycentric/","page":"Barycentric coordinates","title":"Barycentric coordinates","text":"Base.@propagate_inbounds function barycentric_interpolate(method::AbstractBarycentricCoordinateMethod, polygon::Polygon{3, T1}, point::Point{2, T2}) where {T1 <: Real, T2 <: Real}\n exterior_point3s = decompose(Point{3, promote_type(T1, T2)}, polygon.exterior)\n exterior_values = getindex.(exterior_point3s, 3)\n exterior_points = Point2f.(exterior_point3s)\n if isempty(polygon.interiors)\n return barycentric_interpolate(method, exterior_points, exterior_values, point)\n else # the poly has interiors\n interior_point3s = decompose.((Point{3, promote_type(T1, T2)},), polygon.interiors)\n interior_values = collect(Iterators.flatten((getindex.(point3s, 3) for point3s in interior_point3s)))\n interior_points = map(point3s -> Point2f.(point3s), interior_point3s)\n return barycentric_interpolate(method, exterior_points, interior_points, vcat(exterior_values, interior_values), point)\n end\nend\nBase.@propagate_inbounds barycentric_interpolate(polygon::Polygon{3, T1}, point::Point{2, T2}) where {T1 <: Real, T2 <: Real} = barycentric_interpolate(MeanValue(), polygon, point)","category":"page"},{"location":"source/methods/barycentric/","page":"Barycentric coordinates","title":"Barycentric coordinates","text":"This method is the one which supports GeoInterface.","category":"page"},{"location":"source/methods/barycentric/","page":"Barycentric coordinates","title":"Barycentric coordinates","text":"Base.@propagate_inbounds function barycentric_interpolate(method::AbstractBarycentricCoordinateMethod, polygon, values::AbstractVector{V}, point) where V\n @assert GeoInterface.trait(polygon) isa GeoInterface.PolygonTrait\n @assert GeoInterface.trait(point) isa GeoInterface.PointTrait\n passable_polygon = GeoInterface.convert(GeometryBasics, polygon)\n @assert passable_polygon isa GeometryBasics.Polygon \"The polygon was converted to a $(typeof(passable_polygon)), which is not a `GeometryBasics.Polygon`.\"\n # first_poly_point = GeoInterface.getpoint(GeoInterface.getexterior(polygon))\n passable_point = GeoInterface.convert(GeometryBasics, point)\n return barycentric_interpolate(method, passable_polygon, Point2(passable_point))\nend\nBase.@propagate_inbounds barycentric_interpolate(polygon, values::AbstractVector{V}, point) where V = barycentric_interpolate(MeanValue(), polygon, values, point)\n\n\"\"\"\n weighted_mean(weight::Real, x1, x2)\n\nReturns the weighted mean of `x1` and `x2`, where `weight` is the weight of `x1`.\n\nSpecifically, calculates `x1 * weight + x2 * (1 - weight)`.\n\n!!! note\n The idea for this method is that you can override this for custom types, like Color types, in extension modules.\n\"\"\"\nfunction weighted_mean(weight::WT, x1, x2) where {WT <: Real}\n return muladd(x1, weight, x2 * (oneunit(WT) - weight))\nend\n\n\n\"\"\"\n MeanValue() <: AbstractBarycentricCoordinateMethod\n\nThis method calculates barycentric coordinates using the mean value method.\n\n# References\n\n\"\"\"\nstruct MeanValue <: AbstractBarycentricCoordinateMethod\nend","category":"page"},{"location":"source/methods/barycentric/","page":"Barycentric coordinates","title":"Barycentric coordinates","text":"Before we go to the actual implementation, there are some quick and simple utility functions that we need to implement. These are mainly for convenience and code brevity.","category":"page"},{"location":"source/methods/barycentric/","page":"Barycentric coordinates","title":"Barycentric coordinates","text":"\"\"\"\n _det(s1::Point2{T1}, s2::Point2{T2}) where {T1 <: Real, T2 <: Real}\n\nReturns the determinant of the matrix formed by `hcat`'ing two points `s1` and `s2`.\n\nSpecifically, this is:\n```julia\ns1[1] * s2[2] - s1[2] * s2[1]\n```\n\"\"\"\nfunction _det(s1::_VecTypes{2, T1}, s2::_VecTypes{2, T2}) where {T1 <: Real, T2 <: Real}\n return s1[1] * s2[2] - s1[2] * s2[1]\nend\n\n\"\"\"\n t_value(sᵢ, sᵢ₊₁, rᵢ, rᵢ₊₁)\n\nReturns the \"T-value\" as described in Hormann's presentation [^HormannPresentation] on how to calculate\nthe mean-value coordinate.\n\nHere, `sᵢ` is the vector from vertex `vᵢ` to the point, and `rᵢ` is the norm (length) of `sᵢ`.\n`s` must be `Point` and `r` must be real numbers.\n\n```math\ntᵢ = \\\\frac{\\\\mathrm{det}\\\\left(sᵢ, sᵢ₊₁\\\\right)}{rᵢ * rᵢ₊₁ + sᵢ ⋅ sᵢ₊₁}\n```\n\n[^HormannPresentation]: K. Hormann and N. Sukumar. Generalized Barycentric Coordinates in Computer Graphics and Computational Mechanics. Taylor & Fancis, CRC Press, 2017.\n```\n\n\"\"\"\nfunction t_value(sᵢ::_VecTypes{N, T1}, sᵢ₊₁::_VecTypes{N, T1}, rᵢ::T2, rᵢ₊₁::T2) where {N, T1 <: Real, T2 <: Real}\n return _det(sᵢ, sᵢ₊₁) / muladd(rᵢ, rᵢ₊₁, dot(sᵢ, sᵢ₊₁))\nend\n\n\nfunction barycentric_coordinates!(λs::Vector{<: Real}, ::MeanValue, polypoints::AbstractVector{<: Point{2, T1}}, point::Point{2, T2}) where {T1 <: Real, T2 <: Real}\n @boundscheck @assert length(λs) == length(polypoints)\n @boundscheck @assert length(polypoints) >= 3\n n_points = length(polypoints)\n # Initialize counters and register variables\n # Points - these are actually vectors from point to vertices\n # polypoints[i-1], polypoints[i], polypoints[i+1]\n sᵢ₋₁ = polypoints[end] - point\n sᵢ = polypoints[begin] - point\n sᵢ₊₁ = polypoints[begin+1] - point\n # radius / Euclidean distance between points.\n rᵢ₋₁ = norm(sᵢ₋₁)\n rᵢ = norm(sᵢ )\n rᵢ₊₁ = norm(sᵢ₊₁)\n # Perform the first computation explicitly, so we can cut down on\n # a mod in the loop.\n λs[1] = (t_value(sᵢ₋₁, sᵢ, rᵢ₋₁, rᵢ) + t_value(sᵢ, sᵢ₊₁, rᵢ, rᵢ₊₁)) / rᵢ\n # Loop through the rest of the vertices, compute, store in λs\n for i in 2:n_points\n # Increment counters + set variables\n sᵢ₋₁ = sᵢ\n sᵢ = sᵢ₊₁\n sᵢ₊₁ = polypoints[mod1(i+1, n_points)] - point\n rᵢ₋₁ = rᵢ\n rᵢ = rᵢ₊₁\n rᵢ₊₁ = norm(sᵢ₊₁) # radius / Euclidean distance between points.\n λs[i] = (t_value(sᵢ₋₁, sᵢ, rᵢ₋₁, rᵢ) + t_value(sᵢ, sᵢ₊₁, rᵢ, rᵢ₊₁)) / rᵢ\n end\n # Normalize λs to the 1-norm (sum=1)\n λs ./= sum(λs)\n return λs\nend","category":"page"},{"location":"source/methods/barycentric/","page":"Barycentric coordinates","title":"Barycentric coordinates","text":"function barycentric_coordinates(::MeanValue, polypoints::NTuple{N, Point{2, T2}}, point::Point{2, T1},) where {N, T1, T2}\n ## Initialize counters and register variables\n ## Points - these are actually vectors from point to vertices\n ## polypoints[i-1], polypoints[i], polypoints[i+1]\n sᵢ₋₁ = polypoints[end] - point\n sᵢ = polypoints[begin] - point\n sᵢ₊₁ = polypoints[begin+1] - point\n ## radius / Euclidean distance between points.\n rᵢ₋₁ = norm(sᵢ₋₁)\n rᵢ = norm(sᵢ )\n rᵢ₊₁ = norm(sᵢ₊₁)\n λ₁ = (t_value(sᵢ₋₁, sᵢ, rᵢ₋₁, rᵢ) + t_value(sᵢ, sᵢ₊₁, rᵢ, rᵢ₊₁)) / rᵢ\n λs = ntuple(N) do i\n if i == 1\n return λ₁\n end\n ## Increment counters + set variables\n sᵢ₋₁ = sᵢ\n sᵢ = sᵢ₊₁\n sᵢ₊₁ = polypoints[mod1(i+1, N)] - point\n rᵢ₋₁ = rᵢ\n rᵢ = rᵢ₊₁\n rᵢ₊₁ = norm(sᵢ₊₁) # radius / Euclidean distance between points.\n return (t_value(sᵢ₋₁, sᵢ, rᵢ₋₁, rᵢ) + t_value(sᵢ, sᵢ₊₁, rᵢ, rᵢ₊₁)) / rᵢ\n end\n\n ∑λ = sum(λs)\n\n return ntuple(N) do i\n λs[i] / ∑λ\n end\nend","category":"page"},{"location":"source/methods/barycentric/","page":"Barycentric coordinates","title":"Barycentric coordinates","text":"This performs an inplace accumulation, using less memory and is faster. That's particularly good if you are using a polygon with a large number of points...","category":"page"},{"location":"source/methods/barycentric/","page":"Barycentric coordinates","title":"Barycentric coordinates","text":"function barycentric_interpolate(::MeanValue, polypoints::AbstractVector{<: Point{2, T1}}, values::AbstractVector{V}, point::Point{2, T2}) where {T1 <: Real, T2 <: Real, V}\n @boundscheck @assert length(values) == length(polypoints)\n @boundscheck @assert length(polypoints) >= 3\n\n n_points = length(polypoints)\n # Initialize counters and register variables\n # Points - these are actually vectors from point to vertices\n # polypoints[i-1], polypoints[i], polypoints[i+1]\n sᵢ₋₁ = polypoints[end] - point\n sᵢ = polypoints[begin] - point\n sᵢ₊₁ = polypoints[begin+1] - point\n # radius / Euclidean distance between points.\n rᵢ₋₁ = norm(sᵢ₋₁)\n rᵢ = norm(sᵢ )\n rᵢ₊₁ = norm(sᵢ₊₁)\n # Now, we set the interpolated value to the first point's value, multiplied\n # by the weight computed relative to the first point in the polygon.\n wᵢ = (t_value(sᵢ₋₁, sᵢ, rᵢ₋₁, rᵢ) + t_value(sᵢ, sᵢ₊₁, rᵢ, rᵢ₊₁)) / rᵢ\n wₜₒₜ = wᵢ\n interpolated_value = values[begin] * wᵢ\n for i in 2:n_points\n # Increment counters + set variables\n sᵢ₋₁ = sᵢ\n sᵢ = sᵢ₊₁\n sᵢ₊₁ = polypoints[mod1(i+1, n_points)] - point\n rᵢ₋₁ = rᵢ\n rᵢ = rᵢ₊₁\n rᵢ₊₁ = norm(sᵢ₊₁)\n # Now, we calculate the weight:\n wᵢ = (t_value(sᵢ₋₁, sᵢ, rᵢ₋₁, rᵢ) + t_value(sᵢ, sᵢ₊₁, rᵢ, rᵢ₊₁)) / rᵢ\n # perform a weighted sum with the interpolated value:\n interpolated_value += values[i] * wᵢ\n # and add the weight to the total weight accumulator.\n wₜₒₜ += wᵢ\n end\n # Return the normalized interpolated value.\n return interpolated_value / wₜₒₜ\nend","category":"page"},{"location":"source/methods/barycentric/","page":"Barycentric coordinates","title":"Barycentric coordinates","text":"When you have holes, then you have to be careful about the order you iterate around points.","category":"page"},{"location":"source/methods/barycentric/","page":"Barycentric coordinates","title":"Barycentric coordinates","text":"Specifically, you have to iterate around each linear ring separately and ensure there are no degenerate/repeated points at the start and end!","category":"page"},{"location":"source/methods/barycentric/","page":"Barycentric coordinates","title":"Barycentric coordinates","text":"function barycentric_interpolate(::MeanValue, exterior::AbstractVector{<: Point{N, T1}}, interiors::AbstractVector{<: AbstractVector{<: Point{N, T1}}}, values::AbstractVector{V}, point::Point{N, T2}) where {N, T1 <: Real, T2 <: Real, V}\n # @boundscheck @assert length(values) == (length(exterior) + isempty(interiors) ? 0 : sum(length.(interiors)))\n # @boundscheck @assert length(exterior) >= 3\n\n current_index = 1\n l_exterior = length(exterior)\n\n sᵢ₋₁ = exterior[end] - point\n sᵢ = exterior[begin] - point\n sᵢ₊₁ = exterior[begin+1] - point\n rᵢ₋₁ = norm(sᵢ₋₁) # radius / Euclidean distance between points.\n rᵢ = norm(sᵢ ) # radius / Euclidean distance between points.\n rᵢ₊₁ = norm(sᵢ₊₁) # radius / Euclidean distance between points.","category":"page"},{"location":"source/methods/barycentric/","page":"Barycentric coordinates","title":"Barycentric coordinates","text":"Now, we set the interpolated value to the first point's value, multiplied by the weight computed relative to the first point in the polygon.","category":"page"},{"location":"source/methods/barycentric/","page":"Barycentric coordinates","title":"Barycentric coordinates","text":" wᵢ = (t_value(sᵢ₋₁, sᵢ, rᵢ₋₁, rᵢ) + t_value(sᵢ, sᵢ₊₁, rᵢ, rᵢ₊₁)) / rᵢ\n wₜₒₜ = wᵢ\n interpolated_value = values[begin] * wᵢ\n\n for i in 2:l_exterior","category":"page"},{"location":"source/methods/barycentric/","page":"Barycentric coordinates","title":"Barycentric coordinates","text":"Increment counters + set variables","category":"page"},{"location":"source/methods/barycentric/","page":"Barycentric coordinates","title":"Barycentric coordinates","text":" sᵢ₋₁ = sᵢ\n sᵢ = sᵢ₊₁\n sᵢ₊₁ = exterior[mod1(i+1, l_exterior)] - point\n rᵢ₋₁ = rᵢ\n rᵢ = rᵢ₊₁\n rᵢ₊₁ = norm(sᵢ₊₁) # radius / Euclidean distance between points.\n wᵢ = (t_value(sᵢ₋₁, sᵢ, rᵢ₋₁, rᵢ) + t_value(sᵢ, sᵢ₊₁, rᵢ, rᵢ₊₁)) / rᵢ","category":"page"},{"location":"source/methods/barycentric/","page":"Barycentric coordinates","title":"Barycentric coordinates","text":"Updates - first the interpolated value,","category":"page"},{"location":"source/methods/barycentric/","page":"Barycentric coordinates","title":"Barycentric coordinates","text":" interpolated_value += values[current_index] * wᵢ","category":"page"},{"location":"source/methods/barycentric/","page":"Barycentric coordinates","title":"Barycentric coordinates","text":"then the accumulators for total weight and current index.","category":"page"},{"location":"source/methods/barycentric/","page":"Barycentric coordinates","title":"Barycentric coordinates","text":" wₜₒₜ += wᵢ\n current_index += 1\n\n end\n for hole in interiors\n l_hole = length(hole)\n sᵢ₋₁ = hole[end] - point\n sᵢ = hole[begin] - point\n sᵢ₊₁ = hole[begin+1] - point\n rᵢ₋₁ = norm(sᵢ₋₁) # radius / Euclidean distance between points.\n rᵢ = norm(sᵢ ) # radius / Euclidean distance between points.\n rᵢ₊₁ = norm(sᵢ₊₁) # radius / Euclidean distance between points.\n # Now, we set the interpolated value to the first point's value, multiplied\n # by the weight computed relative to the first point in the polygon.\n wᵢ = (t_value(sᵢ₋₁, sᵢ, rᵢ₋₁, rᵢ) + t_value(sᵢ, sᵢ₊₁, rᵢ, rᵢ₊₁)) / rᵢ\n\n interpolated_value += values[current_index] * wᵢ\n\n wₜₒₜ += wᵢ\n current_index += 1\n\n for i in 2:l_hole\n # Increment counters + set variables\n sᵢ₋₁ = sᵢ\n sᵢ = sᵢ₊₁\n sᵢ₊₁ = hole[mod1(i+1, l_hole)] - point\n rᵢ₋₁ = rᵢ\n rᵢ = rᵢ₊₁\n rᵢ₊₁ = norm(sᵢ₊₁) ## radius / Euclidean distance between points.\n wᵢ = (t_value(sᵢ₋₁, sᵢ, rᵢ₋₁, rᵢ) + t_value(sᵢ, sᵢ₊₁, rᵢ, rᵢ₊₁)) / rᵢ\n interpolated_value += values[current_index] * wᵢ\n wₜₒₜ += wᵢ\n current_index += 1\n end\n end\n return interpolated_value / wₜₒₜ\n\nend\n\nstruct Wachspress <: AbstractBarycentricCoordinateMethod\nend","category":"page"},{"location":"source/methods/barycentric/","page":"Barycentric coordinates","title":"Barycentric coordinates","text":"","category":"page"},{"location":"source/methods/barycentric/","page":"Barycentric coordinates","title":"Barycentric coordinates","text":"This page was generated using Literate.jl.","category":"page"},{"location":"source/methods/disjoint/#Disjointness-checks","page":"Disjointness checks","title":"Disjointness checks","text":"","category":"section"},{"location":"source/methods/disjoint/","page":"Disjointness checks","title":"Disjointness checks","text":"\"\"\"\n disjoint(geom1, geom2)::Bool\n\nReturn `true` if the intersection of the two geometries is an empty set.","category":"page"},{"location":"source/methods/disjoint/","page":"Disjointness checks","title":"Disjointness checks","text":"Examples","category":"page"},{"location":"source/methods/disjoint/","page":"Disjointness checks","title":"Disjointness checks","text":"```jldoctest\nimport GeometryOps as GO, GeoInterface as GI\n\npoly = GI.Polygon([[(-1, 2), (3, 2), (3, 3), (-1, 3), (-1, 2)]])\npoint = (1, 1)\nGO.disjoint(poly, point)","category":"page"},{"location":"source/methods/disjoint/","page":"Disjointness checks","title":"Disjointness checks","text":"output","category":"page"},{"location":"source/methods/disjoint/","page":"Disjointness checks","title":"Disjointness checks","text":"true\n```\n\"\"\"\ndisjoint(g1, g2)::Bool = disjoint(trait(g1), g1, trait(g2), g2)\ndisjoint(::FeatureTrait, g1, ::Any, g2)::Bool = disjoint(GI.geometry(g1), g2)\ndisjoint(::Any, g1, t2::FeatureTrait, g2)::Bool = disjoint(g1, geometry(g2))\ndisjoint(::PointTrait, g1, ::PointTrait, g2)::Bool = !point_equals_point(g1, g2)\ndisjoint(::PointTrait, g1, ::LineStringTrait, g2)::Bool = !point_on_line(g1, g2)\ndisjoint(::PointTrait, g1, ::PolygonTrait, g2)::Bool = !point_in_polygon(g1, g2)\ndisjoint(::LineStringTrait, g1, ::PointTrait, g2)::Bool = !point_on_line(g2, g1)\ndisjoint(::LineStringTrait, g1, ::LineStringTrait, g2)::Bool = !line_on_line(g1, g2)\ndisjoint(::LineStringTrait, g1, ::PolygonTrait, g2)::Bool = !line_in_polygon(g2, g1)\ndisjoint(::PolygonTrait, g1, ::PointTrait, g2)::Bool = !point_in_polygon(g2, g1)\ndisjoint(::PolygonTrait, g1, ::LineStringTrait, g2)::Bool = !line_in_polygon(g2, g1)\ndisjoint(::PolygonTrait, g1, ::PolygonTrait, g2)::Bool = polygon_disjoint(g2, g1)\n\nfunction polygon_disjoint(poly1, poly2)\n for point in GI.getpoint(poly1)\n point_in_polygon(point, poly2) && return false\n end\n for point in GI.getpoint(poly2)\n point_in_polygon(point, poly1) && return false\n end\n return !intersects(poly1, poly2)\nend","category":"page"},{"location":"source/methods/disjoint/","page":"Disjointness checks","title":"Disjointness checks","text":"","category":"page"},{"location":"source/methods/disjoint/","page":"Disjointness checks","title":"Disjointness checks","text":"This page was generated using Literate.jl.","category":"page"},{"location":"source/methods/crosses/#Crossing-checks","page":"Crossing checks","title":"Crossing checks","text":"","category":"section"},{"location":"source/methods/crosses/","page":"Crossing checks","title":"Crossing checks","text":"\"\"\"\n crosses(geom1, geom2)::Bool\n\nReturn `true` if the intersection results in a geometry whose dimension is one less than\nthe maximum dimension of the two source geometries and the intersection set is interior to\nboth source geometries.\n\nTODO: broken\n\n# Examples\n```julia\nimport GeoInterface as GI, GeometryOps as GO\n\nline1 = GI.LineString([(1, 1), (1, 2), (1, 3), (1, 4)])\nline2 = GI.LineString([(-2, 2), (4, 2)])\n\nGO.crosses(line1, line2)","category":"page"},{"location":"source/methods/crosses/","page":"Crossing checks","title":"Crossing checks","text":"output","category":"page"},{"location":"source/methods/crosses/","page":"Crossing checks","title":"Crossing checks","text":"true\n```\n\"\"\"\ncrosses(g1, g2)::Bool = crosses(trait(g1), g1, trait(g2), g2)::Bool\ncrosses(t1::FeatureTrait, g1, t2, g2)::Bool = crosses(GI.geometry(g1), g2)\ncrosses(t1, g1, t2::FeatureTrait, g2)::Bool = crosses(g1, geometry(g2))\ncrosses(::MultiPointTrait, g1, ::LineStringTrait, g2)::Bool = multipoint_crosses_line(g1, g2)\ncrosses(::MultiPointTrait, g1, ::PolygonTrait, g2)::Bool = multipoint_crosses_poly(g1, g2)\ncrosses(::LineStringTrait, g1, ::MultiPointTrait, g2)::Bool = multipoint_crosses_lines(g2, g1)\ncrosses(::LineStringTrait, g1, ::PolygonTrait, g2)::Bool = line_crosses_poly(g1, g2)\ncrosses(::LineStringTrait, g1, ::LineStringTrait, g2)::Bool = line_crosses_line(g1, g2)\ncrosses(::PolygonTrait, g1, ::MultiPointTrait, g2)::Bool = multipoint_crosses_poly(g2, g1)\ncrosses(::PolygonTrait, g1, ::LineStringTrait, g2)::Bool = line_crosses_poly(g2, g1)\n\nfunction multipoint_crosses_line(geom1, geom2)\n int_point = false\n ext_point = false\n i = 1\n np2 = GI.npoint(geom2)\n\n while i < GI.npoint(geom1) && !int_point && !ext_point\n for j in 1:GI.npoint(geom2) - 1\n exclude_boundary = (j === 1 || j === np2 - 2) ? :none : :both\n if point_on_segment(GI.getpoint(geom1, i), (GI.getpoint(geom2, j), GI.getpoint(geom2, j + 1)); exclude_boundary)\n int_point = true\n else\n ext_point = true\n end\n end\n i += 1\n end\n\n return int_point && ext_point\nend\n\nfunction line_crosses_line(line1, line2)\n np2 = GI.npoint(line2)\n if intersects(line1, line2)\n for i in 1:GI.npoint(line1) - 1\n for j in 1:GI.npoint(line2) - 1\n exclude_boundary = (j === 1 || j === np2 - 2) ? :none : :both\n pa = GI.getpoint(line1, i)\n pb = GI.getpoint(line1, i + 1)\n p = GI.getpoint(line2, j)\n point_on_segment(p, (pa, pb); exclude_boundary) && return true\n end\n end\n end\n return false\nend\n\nfunction line_crosses_poly(line, poly)\n for l in flatten(AbstractCurveTrait, poly)\n intersects(line, l) && return true\n end\n return false\nend\n\nfunction multipoint_crosses_poly(mp, poly)\n int_point = false\n ext_point = false\n\n for p in GI.getpoint(mp)\n if point_in_polygon(p, poly)\n int_point = true\n else\n ext_point = true\n end\n int_point && ext_point && return true\n end\n return false\nend","category":"page"},{"location":"source/methods/crosses/","page":"Crossing checks","title":"Crossing checks","text":"","category":"page"},{"location":"source/methods/crosses/","page":"Crossing checks","title":"Crossing checks","text":"This page was generated using Literate.jl.","category":"page"},{"location":"source/methods/centroid/#Centroid","page":"Centroid","title":"Centroid","text":"","category":"section"},{"location":"source/methods/centroid/","page":"Centroid","title":"Centroid","text":"export centroid, centroid_and_length, centroid_and_area","category":"page"},{"location":"source/methods/centroid/#What-is-the-centroid?","page":"Centroid","title":"What is the centroid?","text":"","category":"section"},{"location":"source/methods/centroid/","page":"Centroid","title":"Centroid","text":"The centroid is the geometric center of a line string or area(s). Note that the centroid does not need to be inside of a concave area.","category":"page"},{"location":"source/methods/centroid/","page":"Centroid","title":"Centroid","text":"Further note that by convention a line, or linear ring, is calculated by weighting the line segments by their length, while polygons and multipolygon centroids are calculated by weighting edge's by their 'area components'.","category":"page"},{"location":"source/methods/centroid/","page":"Centroid","title":"Centroid","text":"To provide an example, consider this concave polygon in the shape of a 'C':","category":"page"},{"location":"source/methods/centroid/","page":"Centroid","title":"Centroid","text":"using GeometryOps\nusing GeometryOps.GeometryBasics\nusing Makie\nusing CairoMakie\n\ncshape = Polygon([\n Point(0,0), Point(0,3), Point(3,3), Point(3,2), Point(1,2),\n Point(1,1), Point(3,1), Point(3,0), Point(0,0),\n])\nf, a, p = poly(cshape; axis = (; aspect = DataAspect()))","category":"page"},{"location":"source/methods/centroid/","page":"Centroid","title":"Centroid","text":"Let's see what the centroid looks like (plotted in red):","category":"page"},{"location":"source/methods/centroid/","page":"Centroid","title":"Centroid","text":"cent = centroid(cshape)\nscatter!(a, GI.x(cent), GI.y(cent), color = :red)\nf","category":"page"},{"location":"source/methods/centroid/#Implementation","page":"Centroid","title":"Implementation","text":"","category":"section"},{"location":"source/methods/centroid/","page":"Centroid","title":"Centroid","text":"This is the GeoInterface-compatible implementation.","category":"page"},{"location":"source/methods/centroid/","page":"Centroid","title":"Centroid","text":"First, we implement a wrapper method that dispatches to the correct implementation based on the geometry trait. This is also used in the implementation, since it's a lot less work!","category":"page"},{"location":"source/methods/centroid/","page":"Centroid","title":"Centroid","text":"Note that if you call centroid on a LineString or LinearRing, the centroidandlength function will be called due to the weighting scheme described above, while centroidandarea is called for polygons and multipolygons. However, centroidandarea can still be called on a LineString or LinearRing when they are closed, for example as the interior hole of a polygon.","category":"page"},{"location":"source/methods/centroid/","page":"Centroid","title":"Centroid","text":"The helper functions centroidandlength and centroidandarea are made availible just in case the user also needs the area or length to decrease repeat computation.","category":"page"},{"location":"source/methods/centroid/","page":"Centroid","title":"Centroid","text":"\"\"\"\n centroid(geom)::Tuple{T, T}\n\nReturns the centroid of a given line segment, linear ring, polygon, or\nmutlipolygon.\n\"\"\"\ncentroid(geom) = centroid(GI.trait(geom), geom)\n\n\"\"\"\n centroid(\n trait::Union{GI.LineStringTrait, GI.LinearRingTrait},\n geom,\n )::Tuple{T, T}\n\nReturns the centroid of a line string or linear ring, which is calculated by\nweighting line segments by their length by convention.\n\"\"\"\ncentroid(\n trait::Union{GI.LineStringTrait, GI.LinearRingTrait},\n geom,\n) = centroid_and_length(trait, geom)[1]\n\n\"\"\"\n centroid(trait, geom)::Tuple{T, T}\n\nReturns the centroid of a polygon or multipolygon, which is calculated by\nweighting edges by their `area component` by convention.\n\"\"\"\ncentroid(trait, geom) = centroid_and_area(trait, geom)[1]\n\n\"\"\"\n centroid_and_length(geom)::(::Tuple{T, T}, ::Real)\n\nReturns the centroid and length of a given line/ring. Note this is only valid\nfor line strings and linear rings.\n\"\"\"\ncentroid_and_length(geom) = centroid_and_length(GI.trait(geom), geom)\n\n\"\"\"\n centroid_and_area(\n ::Union{GI.LineStringTrait, GI.LinearRingTrait},\n geom,\n )::(::Tuple{T, T}, ::Real)\n\nReturns the centroid and area of a given geom.\n\"\"\"\ncentroid_and_area(geom) = centroid_and_area(GI.trait(geom), geom)\n\n\"\"\"\n centroid_and_length(geom)::(::Tuple{T, T}, ::Real)\n\nReturns the centroid and length of a given line/ring. Note this is only valid\nfor line strings and linear rings.\n\"\"\"\nfunction centroid_and_length(\n ::Union{GI.LineStringTrait, GI.LinearRingTrait},\n geom,\n)\n T = typeof(GI.x(GI.getpoint(geom, 1)))","category":"page"},{"location":"source/methods/centroid/","page":"Centroid","title":"Centroid","text":"Initialize starting values","category":"page"},{"location":"source/methods/centroid/","page":"Centroid","title":"Centroid","text":" xcentroid = T(0)\n ycentroid = T(0)\n length = T(0)\n point₁ = GI.getpoint(geom, 1)","category":"page"},{"location":"source/methods/centroid/","page":"Centroid","title":"Centroid","text":"Loop over line segments of line string","category":"page"},{"location":"source/methods/centroid/","page":"Centroid","title":"Centroid","text":" for point₂ in GI.getpoint(geom)","category":"page"},{"location":"source/methods/centroid/","page":"Centroid","title":"Centroid","text":"Calculate length of line segment","category":"page"},{"location":"source/methods/centroid/","page":"Centroid","title":"Centroid","text":" length_component = sqrt(\n (GI.x(point₂) - GI.x(point₁))^2 +\n (GI.y(point₂) - GI.y(point₁))^2\n )","category":"page"},{"location":"source/methods/centroid/","page":"Centroid","title":"Centroid","text":"Accumulate the line segment length into length","category":"page"},{"location":"source/methods/centroid/","page":"Centroid","title":"Centroid","text":" length += length_component","category":"page"},{"location":"source/methods/centroid/","page":"Centroid","title":"Centroid","text":"Weighted average of line segment centroids","category":"page"},{"location":"source/methods/centroid/","page":"Centroid","title":"Centroid","text":" xcentroid += (GI.x(point₁) + GI.x(point₂)) * (length_component / 2)\n ycentroid += (GI.y(point₁) + GI.y(point₂)) * (length_component / 2)\n #centroid = centroid .+ ((point₁ .+ point₂) .* (length_component / 2))","category":"page"},{"location":"source/methods/centroid/","page":"Centroid","title":"Centroid","text":"Advance the point buffer by 1 point to move to next line segment","category":"page"},{"location":"source/methods/centroid/","page":"Centroid","title":"Centroid","text":" point₁ = point₂\n end\n xcentroid /= length\n ycentroid /= length\n return (xcentroid, ycentroid), length\nend\n\n\"\"\"\n centroid_and_area(\n ::Union{GI.LineStringTrait, GI.LinearRingTrait},\n geom,\n )::(::Tuple{T, T}, ::Real)\n\nReturns the centroid and area of a given a line string or a linear ring.\nNote that this is only valid if the line segment or linear ring is closed.\n\"\"\"\nfunction centroid_and_area(\n ::Union{GI.LineStringTrait, GI.LinearRingTrait},\n geom,\n)\n T = typeof(GI.x(GI.getpoint(geom, 1)))","category":"page"},{"location":"source/methods/centroid/","page":"Centroid","title":"Centroid","text":"Check that the geometry is closed","category":"page"},{"location":"source/methods/centroid/","page":"Centroid","title":"Centroid","text":" @assert(\n GI.getpoint(geom, 1) == GI.getpoint(geom, GI.ngeom(geom)),\n \"centroid_and_area should only be used with closed geometries\"\n )","category":"page"},{"location":"source/methods/centroid/","page":"Centroid","title":"Centroid","text":"Initialize starting values","category":"page"},{"location":"source/methods/centroid/","page":"Centroid","title":"Centroid","text":" xcentroid = T(0)\n ycentroid = T(0)\n area = T(0)\n point₁ = GI.getpoint(geom, 1)","category":"page"},{"location":"source/methods/centroid/","page":"Centroid","title":"Centroid","text":"Loop over line segments of linear ring","category":"page"},{"location":"source/methods/centroid/","page":"Centroid","title":"Centroid","text":" for point₂ in GI.getpoint(geom)\n area_component = GI.x(point₁) * GI.y(point₂) -\n GI.x(point₂) * GI.y(point₁)","category":"page"},{"location":"source/methods/centroid/","page":"Centroid","title":"Centroid","text":"Accumulate the area component into area","category":"page"},{"location":"source/methods/centroid/","page":"Centroid","title":"Centroid","text":" area += area_component","category":"page"},{"location":"source/methods/centroid/","page":"Centroid","title":"Centroid","text":"Weighted average of centroid components","category":"page"},{"location":"source/methods/centroid/","page":"Centroid","title":"Centroid","text":" xcentroid += (GI.x(point₁) + GI.x(point₂)) * area_component\n ycentroid += (GI.y(point₁) + GI.y(point₂)) * area_component","category":"page"},{"location":"source/methods/centroid/","page":"Centroid","title":"Centroid","text":"Advance the point buffer by 1 point","category":"page"},{"location":"source/methods/centroid/","page":"Centroid","title":"Centroid","text":" point₁ = point₂\n end\n area /= 2\n xcentroid /= 6area\n ycentroid /= 6area\n return (xcentroid, ycentroid), abs(area)\nend\n\n\"\"\"\n centroid_and_area(::GI.PolygonTrait, geom)::(::Tuple{T, T}, ::Real)\n\nReturns the centroid and area of a given polygon.\n\"\"\"\nfunction centroid_and_area(::GI.PolygonTrait, geom)","category":"page"},{"location":"source/methods/centroid/","page":"Centroid","title":"Centroid","text":"Exterior ring's centroid and area","category":"page"},{"location":"source/methods/centroid/","page":"Centroid","title":"Centroid","text":" (xcentroid, ycentroid), area = centroid_and_area(GI.getexterior(geom))","category":"page"},{"location":"source/methods/centroid/","page":"Centroid","title":"Centroid","text":"Weight exterior centroid by area","category":"page"},{"location":"source/methods/centroid/","page":"Centroid","title":"Centroid","text":" xcentroid *= area\n ycentroid *= area","category":"page"},{"location":"source/methods/centroid/","page":"Centroid","title":"Centroid","text":"Loop over any holes within the polygon","category":"page"},{"location":"source/methods/centroid/","page":"Centroid","title":"Centroid","text":" for hole in GI.gethole(geom)","category":"page"},{"location":"source/methods/centroid/","page":"Centroid","title":"Centroid","text":"Hole polygon's centroid and area","category":"page"},{"location":"source/methods/centroid/","page":"Centroid","title":"Centroid","text":" (xinterior, yinterior), interior_area = centroid_and_area(hole)","category":"page"},{"location":"source/methods/centroid/","page":"Centroid","title":"Centroid","text":"Accumulate the area component into area","category":"page"},{"location":"source/methods/centroid/","page":"Centroid","title":"Centroid","text":" area -= interior_area","category":"page"},{"location":"source/methods/centroid/","page":"Centroid","title":"Centroid","text":"Weighted average of centroid components","category":"page"},{"location":"source/methods/centroid/","page":"Centroid","title":"Centroid","text":" xcentroid -= xinterior * interior_area\n ycentroid -= yinterior * interior_area\n end\n xcentroid /= area\n ycentroid /= area\n return (xcentroid, ycentroid), area\nend\n\n\"\"\"\n centroid_and_area(::GI.MultiPolygonTrait, geom)::(::Tuple{T, T}, ::Real)\n\nReturns the centroid and area of a given multipolygon.\n\"\"\"\nfunction centroid_and_area(::GI.MultiPolygonTrait, geom)","category":"page"},{"location":"source/methods/centroid/","page":"Centroid","title":"Centroid","text":"First polygon's centroid and area","category":"page"},{"location":"source/methods/centroid/","page":"Centroid","title":"Centroid","text":" (xcentroid, ycentroid), area = centroid_and_area(GI.getpolygon(geom, 1))","category":"page"},{"location":"source/methods/centroid/","page":"Centroid","title":"Centroid","text":"Weight first polygon's centroid by area","category":"page"},{"location":"source/methods/centroid/","page":"Centroid","title":"Centroid","text":" xcentroid *= area\n ycentroid *= area","category":"page"},{"location":"source/methods/centroid/","page":"Centroid","title":"Centroid","text":"Loop over any polygons within the multipolygon","category":"page"},{"location":"source/methods/centroid/","page":"Centroid","title":"Centroid","text":" for i in 2:GI.ngeom(geom)","category":"page"},{"location":"source/methods/centroid/","page":"Centroid","title":"Centroid","text":"Polygon centroid and area","category":"page"},{"location":"source/methods/centroid/","page":"Centroid","title":"Centroid","text":" (xpoly, ypoly), poly_area = centroid_and_area(GI.getpolygon(geom, i))","category":"page"},{"location":"source/methods/centroid/","page":"Centroid","title":"Centroid","text":"Accumulate the area component into area","category":"page"},{"location":"source/methods/centroid/","page":"Centroid","title":"Centroid","text":" area += poly_area","category":"page"},{"location":"source/methods/centroid/","page":"Centroid","title":"Centroid","text":"Weighted average of centroid components","category":"page"},{"location":"source/methods/centroid/","page":"Centroid","title":"Centroid","text":" xcentroid += xpoly * poly_area\n ycentroid += ypoly * poly_area\n end\n xcentroid /= area\n ycentroid /= area\n return (xcentroid, ycentroid), area\nend","category":"page"},{"location":"source/methods/centroid/","page":"Centroid","title":"Centroid","text":"","category":"page"},{"location":"source/methods/centroid/","page":"Centroid","title":"Centroid","text":"This page was generated using Literate.jl.","category":"page"},{"location":"source/transformations/extent/","page":"-","title":"-","text":"\"\"\"\n embed_extent(obj)\n\nRecursively wrap the object with a GeoInterface.jl geometry,\ncalculating and adding an `Extents.Extent` to all objects.\n\nThis can improve performance when extents need to be checked multiple times,\nsuch when needing to check if many points are in geometries, and using their extents\nas a quick filter for obviously exterior points.","category":"page"},{"location":"source/transformations/extent/","page":"-","title":"-","text":"Keywords","category":"page"},{"location":"source/transformations/extent/","page":"-","title":"-","text":"$THREADED_KEYWORD\n$CRS_KEYWORD\n\"\"\"\nembed_extent(x; threaded=false, crs=nothing) =\n apply(identity, GI.PointTrait, x; calc_extent=true, threaded, crs)","category":"page"},{"location":"source/transformations/extent/","page":"-","title":"-","text":"","category":"page"},{"location":"source/transformations/extent/","page":"-","title":"-","text":"This page was generated using Literate.jl.","category":"page"},{"location":"source/methods/contains/#Containment","page":"Containment","title":"Containment","text":"","category":"section"},{"location":"source/methods/contains/","page":"Containment","title":"Containment","text":"export contains\n\n\"\"\"\n contains(ft1::AbstractGeometry, ft2::AbstractGeometry)::Bool\n\nReturn true if the second geometry is completely contained by the first geometry.\nThe interiors of both geometries must intersect and, the interior and boundary of the secondary (geometry b)\nmust not intersect the exterior of the primary (geometry a).\n`contains` returns the exact opposite result of `within`.\n\n# Examples\n\n```jldoctest\nimport GeometryOps as GO, GeoInterface as GI\nline = GI.LineString([(1, 1), (1, 2), (1, 3), (1, 4)])\npoint = (1, 2)\n\nGO.contains(line, point)","category":"page"},{"location":"source/methods/contains/","page":"Containment","title":"Containment","text":"output","category":"page"},{"location":"source/methods/contains/","page":"Containment","title":"Containment","text":"true\n```\n\"\"\"\ncontains(g1, g2)::Bool = within(g2, g1)","category":"page"},{"location":"source/methods/contains/","page":"Containment","title":"Containment","text":"","category":"page"},{"location":"source/methods/contains/","page":"Containment","title":"Containment","text":"This page was generated using Literate.jl.","category":"page"},{"location":"source/transformations/flip/#Coordinate-flipping","page":"Coordinate flipping","title":"Coordinate flipping","text":"","category":"section"},{"location":"source/transformations/flip/","page":"Coordinate flipping","title":"Coordinate flipping","text":"This is a simple example of how to use the apply functionality in a function, by flipping the x and y coordinates of a geometry.","category":"page"},{"location":"source/transformations/flip/","page":"Coordinate flipping","title":"Coordinate flipping","text":"\"\"\"\n flip(obj)\n\nSwap all of the x and y coordinates in obj, otherwise\nkeeping the original structure (but not necessarily the\noriginal type).\n\n# Keywords\n\n$APPLY_KEYWORDS\n\"\"\"\nfunction flip(geom; kw...)\n if _is3d(geom)\n return apply(PointTrait, geom; kw...) do p\n (GI.y(p), GI.x(p), GI.z(p))\n end\n else\n return apply(PointTrait, geom; kw...) do p\n (GI.y(p), GI.x(p))\n end\n end\nend","category":"page"},{"location":"source/transformations/flip/","page":"Coordinate flipping","title":"Coordinate flipping","text":"","category":"page"},{"location":"source/transformations/flip/","page":"Coordinate flipping","title":"Coordinate flipping","text":"This page was generated using Literate.jl.","category":"page"},{"location":"source/transformations/simplify/#Geometry-simplification","page":"Geometry simplification","title":"Geometry simplification","text":"","category":"section"},{"location":"source/transformations/simplify/","page":"Geometry simplification","title":"Geometry simplification","text":"This file holds implementations for the Douglas-Peucker and Visvalingam-Whyatt algorithms for simplifying geometries (specifically polygons and lines).","category":"page"},{"location":"source/transformations/simplify/","page":"Geometry simplification","title":"Geometry simplification","text":"export simplify, VisvalingamWhyatt, DouglasPeucker, RadialDistance\n\n\n\"\"\"\n abstract type SimplifyAlg\n\nAbstract type for simplification algorithms.\n\n# API\n\nFor now, the algorithm must hold the `number`, `ratio` and `tol` properties.\n\nSimplification algorithm types can hook into the interface by implementing\nthe `_simplify(trait, alg, geom)` methods for whichever traits are necessary.\n\"\"\"\nabstract type SimplifyAlg end\n\nconst SIMPLIFY_ALG_KEYWORDS = \"\"\"\n# Keywords\n\n- `ratio`: the fraction of points that should remain after `simplify`.\n Useful as it will generalise for large collections of objects.\n- `number`: the number of points that should remain after `simplify`.\n Less useful for large collections of mixed size objects.\n\"\"\"\n\nconst MIN_POINTS = 3\n\nfunction checkargs(number, ratio, tol)\n count(isnothing, (number, ratio, tol)) == 2 ||\n error(\"Must provide one of `number`, `ratio` or `tol` keywords\")\n if !isnothing(ratio)\n if ratio <= 0 || ratio > 1\n error(\"`ratio` must be 0 < ratio <= 1. Got $ratio\")\n end\n end\n if !isnothing(number)\n if number < MIN_POINTS\n error(\"`number` must be $MIN_POINTS or larger. Got $number\")\n end\n end\n return nothing\nend\n\n\"\"\"\n simplify(obj; kw...)\n simplify(::SimplifyAlg, obj; kw...)\n\nSimplify a geometry, feature, feature collection,\nor nested vectors or a table of these.\n\n`RadialDistance`, `DouglasPeucker`, or\n`VisvalingamWhyatt` algorithms are available,\nlisted in order of increasing quality but decreaseing performance.\n\n`PoinTrait` and `MultiPointTrait` are returned unchanged.\n\nThe default behaviour is `simplify(DouglasPeucker(; kw...), obj)`.\nPass in other `SimplifyAlg` to use other algorithms.","category":"page"},{"location":"source/transformations/simplify/","page":"Geometry simplification","title":"Geometry simplification","text":"Keywords","category":"page"},{"location":"source/transformations/simplify/","page":"Geometry simplification","title":"Geometry simplification","text":"$APPLY_KEYWORDS\n\nKeywords for DouglasPeucker are allowed when no algorithm is specified:\n\n$SIMPLIFY_ALG_KEYWORDS","category":"page"},{"location":"source/transformations/simplify/","page":"Geometry simplification","title":"Geometry simplification","text":"Example","category":"page"},{"location":"source/transformations/simplify/","page":"Geometry simplification","title":"Geometry simplification","text":"Simplify a polygon to have six points:\n\n```jldoctest\nimport GeoInterface as GI\nimport GeometryOps as GO\n\npoly = GI.Polygon([[\n [-70.603637, -33.399918],\n [-70.614624, -33.395332],\n [-70.639343, -33.392466],\n [-70.659942, -33.394759],\n [-70.683975, -33.404504],\n [-70.697021, -33.419406],\n [-70.701141, -33.434306],\n [-70.700454, -33.446339],\n [-70.694274, -33.458369],\n [-70.682601, -33.465816],\n [-70.668869, -33.472117],\n [-70.646209, -33.473835],\n [-70.624923, -33.472117],\n [-70.609817, -33.468107],\n [-70.595397, -33.458369],\n [-70.587158, -33.442901],\n [-70.587158, -33.426283],\n [-70.590591, -33.414248],\n [-70.594711, -33.406224],\n [-70.603637, -33.399918]]])\n\nsimple = GO.simplify(poly; number=6)\nGI.npoint(simple)","category":"page"},{"location":"source/transformations/simplify/","page":"Geometry simplification","title":"Geometry simplification","text":"output","category":"page"},{"location":"source/transformations/simplify/","page":"Geometry simplification","title":"Geometry simplification","text":"6\n```\n\"\"\"\nsimplify(data; calc_extent=false, threaded=false, crs=nothing, kw...) =\n _simplify(DouglasPeucker(; kw...), data; calc_extent, threaded, crs)\nsimplify(alg::SimplifyAlg, data; kw...) = _simplify(alg, data; kw...)\n\nfunction _simplify(alg::SimplifyAlg, data; kw...)\n # Apply simplication to all curves, multipoints, and points,\n # reconstructing everything else around them.\n simplifier(geom) = _simplify(trait(geom), alg, geom)\n apply(simplifier, Union{PolygonTrait,AbstractCurveTrait,MultiPoint,PointTrait}, data; kw...)\nend\n# For Point and MultiPoint traits we do nothing\n_simplify(::PointTrait, alg, geom) = geom\n_simplify(::MultiPointTrait, alg, geom) = geom\nfunction _simplify(::PolygonTrait, alg, geom)\n # Force treating children as LinearRing\n rebuilder(g) = rebuild(g, _simplify(LinearRingTrait(), alg, g))\n lrs = map(rebuilder, GI.getgeom(geom))\n return rebuild(geom, lrs)\nend\n# For curves and rings we simplify\n_simplify(::AbstractCurveTrait, alg, geom) = rebuild(geom, simplify(alg, tuple_points(geom)))\n\n\"\"\"\n RadialDistance <: SimplifyAlg\n\nSimplifies geometries by removing points less than\n`tol` distance from the line between its neighboring points.\n\n$SIMPLIFY_ALG_KEYWORDS\n- `tol`: the minimum distance between points.\n\"\"\"\nstruct RadialDistance <: SimplifyAlg\n number::Union{Int64,Nothing}\n ratio::Union{Float64,Nothing}\n tol::Union{Float64,Nothing}\nend\nfunction RadialDistance(; number=nothing, ratio=nothing, tol=nothing)\n checkargs(number, ratio, tol)\n return RadialDistance(number, ratio, tol)\nend\n\nsettol(alg::RadialDistance, tol) = RadialDistance(alg.number, alg.ratio, tol)\n\nfunction _simplify(alg::RadialDistance, points::Vector)\n previous = first(points)\n distances = Array{Float64}(undef, length(points))\n for i in eachindex(points)\n point = points[i]\n distances[i] = _squared_dist(point, previous)\n previous = point\n end\n # Never remove the end points\n distances[begin] = distances[end] = Inf\n # This avoids taking the square root of each distance above\n if !isnothing(alg.tol)\n alg = settol(alg, (alg.tol::Float64)^2)\n end\n return _get_points(alg, points, distances)\nend\n\nfunction _squared_dist(p1, p2)\n dx = GI.x(p1) - GI.x(p2)\n dy = GI.y(p1) - GI.y(p2)\n return dx^2 + dy^2\nend\n\n\"\"\"\n DouglasPeucker <: SimplifyAlg\n\n DouglasPeucker(; number, ratio, tol)\n\nSimplifies geometries by removing points below `tol`\ndistance from the line between its neighboring points.\n\n$SIMPLIFY_ALG_KEYWORDS\n- `tol`: the minimum distance a point will be from the line\n joining its neighboring points.\n\"\"\"\nstruct DouglasPeucker <: SimplifyAlg\n number::Union{Int64,Nothing}\n ratio::Union{Float64,Nothing}\n tol::Union{Float64,Nothing}\n prefilter::Bool\nend\nfunction DouglasPeucker(; number=nothing, ratio=nothing, tol=nothing, prefilter=false)\n checkargs(number, ratio, tol)\n return DouglasPeucker(number, ratio, tol, prefilter)\nend\n\nsettol(alg::DouglasPeucker, tol) = DouglasPeucker(alg.number, alg.ratio, tol, alg.prefilter)\n\nfunction _simplify(alg::DouglasPeucker, points::Vector)\n length(points) <= MIN_POINTS && return points\n # TODO do we need this?\n # points = alg.prefilter ? simplify(RadialDistance(alg.tol), points) : points\n\n distances = _build_tolerances(_squared_segdist, points)\n return _get_points(alg, points, distances)\nend\n\nfunction _squared_segdist(l1, p, l2)\n x, y = GI.x(l1), GI.y(l1)\n dx = GI.x(l2) - x\n dy = GI.y(l2) - y\n\n if !iszero(dx) || !iszero(dy)\n t = ((GI.x(p) - x) * dx + (GI.y(p) - y) * dy) / (dx * dx + dy * dy)\n if t > 1\n x = GI.x(l2)\n y = GI.y(l2)\n elseif t > 0\n x += dx * t\n y += dy * t\n end\n end\n\n dx = GI.x(p) - x\n dy = GI.y(p) - y\n\n return dx^2 + dy^2\nend\n\n\n\"\"\"\n VisvalingamWhyatt <: SimplifyAlg\n\n VisvalingamWhyatt(; kw...)\n\nSimplifies geometries by removing points below `tol`\ndistance from the line between its neighboring points.\n\n$SIMPLIFY_ALG_KEYWORDS\n- `tol`: the minimum area of a triangle made with a point and\n its neighboring points.\n\"\"\"\nstruct VisvalingamWhyatt <: SimplifyAlg\n number::Union{Int,Nothing}\n ratio::Union{Float64,Nothing}\n tol::Union{Float64,Nothing}\n prefilter::Bool\nend\nfunction VisvalingamWhyatt(; number=nothing, ratio=nothing, tol=nothing, prefilter=false)\n checkargs(number, ratio, tol)\n return VisvalingamWhyatt(number, ratio, tol, prefilter)\nend\n\nsettol(alg::VisvalingamWhyatt, tol) = VisvalingamWhyatt(alg.number, alg.ratio, tol, alg.prefilter)\n\nfunction _simplify(alg::VisvalingamWhyatt, points::Vector)\n length(points) <= MIN_POINTS && return points\n areas = _build_tolerances(_triangle_double_area, points)\n\n # This avoids diving everything by two\n if !isnothing(alg.tol)\n alg = settol(alg, (alg.tol::Float64)*2)\n end\n return _get_points(alg, points, areas)\nend\n\n# calculates the area of a triangle given its vertices\n_triangle_double_area(p1, p2, p3) =\n abs(p1[1] * (p2[2] - p3[2]) + p2[1] * (p3[2] - p1[2]) + p3[1] * (p1[2] - p2[2]))","category":"page"},{"location":"source/transformations/simplify/#Shared-utils","page":"Geometry simplification","title":"Shared utils","text":"","category":"section"},{"location":"source/transformations/simplify/","page":"Geometry simplification","title":"Geometry simplification","text":"function _build_tolerances(f, points)\n nmax = length(points)\n real_tolerances = _flat_tolerances(f, points)\n\n tolerances = copy(real_tolerances)\n i = collect(1:nmax)\n\n min_vert = argmin(tolerances)\n this_tolerance = tolerances[min_vert]\n _remove!(tolerances, min_vert)\n deleteat!(i, min_vert)\n\n while this_tolerance < Inf\n skip = false\n\n if min_vert < length(i)\n right_tolerance = f(\n points[i[min_vert - 1]],\n points[i[min_vert]],\n points[i[min_vert + 1]],\n )\n if right_tolerance <= this_tolerance\n right_tolerance = this_tolerance\n skip = min_vert == 1\n end\n\n real_tolerances[i[min_vert]] = right_tolerance\n tolerances[min_vert] = right_tolerance\n end\n\n if min_vert > 2\n left_tolerance = f(\n points[i[min_vert - 2]],\n points[i[min_vert - 1]],\n points[i[min_vert]],\n )\n if left_tolerance <= this_tolerance\n left_tolerance = this_tolerance\n skip = min_vert == 2\n end\n real_tolerances[i[min_vert - 1]] = left_tolerance\n tolerances[min_vert - 1] = left_tolerance\n end\n\n if !skip\n min_vert = argmin(tolerances)\n end\n deleteat!(i, min_vert)\n this_tolerance = tolerances[min_vert]\n _remove!(tolerances, min_vert)\n end\n\n return real_tolerances\nend\n\nfunction tuple_points(geom)\n points = Array{Tuple{Float64,Float64}}(undef, GI.ngeom(geom))\n for (i, p) in enumerate(GI.getpoint(geom))\n points[i] = (GI.x(p), GI.y(p))\n end\n return points\nend\n\nfunction _get_points(alg, points, tolerances)\n # This assumes that `alg` has the properties\n # `tol`, `number`, and `ratio` available...\n tol = alg.tol\n number = alg.number\n ratio = alg.ratio\n bit_indices = if !isnothing(tol)\n _tol_indices(alg.tol::Float64, points, tolerances)\n elseif !isnothing(number)\n _number_indices(alg.number::Int64, points, tolerances)\n else\n _ratio_indices(alg.ratio::Float64, points, tolerances)\n end\n return points[bit_indices]\nend\n\nfunction _tol_indices(tol, points, tolerances)\n tolerances .>= tol\nend\n\nfunction _number_indices(n, points, tolerances)\n tol = partialsort(tolerances, length(points) - n + 1)\n bit_indices = _tol_indices(tol, points, tolerances)\n nselected = sum(bit_indices)\n # If there are multiple values exactly at `tol` we will get\n # the wrong output length. So we need to remove some.\n while nselected > n\n min_tol = Inf\n min_i = 0\n for i in eachindex(bit_indices)\n bit_indices[i] || continue\n if tolerances[i] < min_tol\n min_tol = tolerances[i]\n min_i = i\n end\n end\n nselected -= 1\n bit_indices[min_i] = false\n end\n return bit_indices\nend\n\nfunction _ratio_indices(r, points, tolerances)\n n = max(3, round(Int, r * length(points)))\n return _number_indices(n, points, tolerances)\nend\n\nfunction _flat_tolerances(f, points)\n result = Array{Float64}(undef, length(points))\n result[1] = result[end] = Inf\n\n for i in 2:length(result) - 1\n result[i] = f(points[i-1], points[i], points[i+1])\n end\n return result\nend\n\n_remove!(s, i) = s[i:end-1] .= s[i+1:end]","category":"page"},{"location":"source/transformations/simplify/","page":"Geometry simplification","title":"Geometry simplification","text":"","category":"page"},{"location":"source/transformations/simplify/","page":"Geometry simplification","title":"Geometry simplification","text":"This page was generated using Literate.jl.","category":"page"},{"location":"source/methods/within/#Containment/withinness","page":"Containment/withinness","title":"Containment/withinness","text":"","category":"section"},{"location":"source/methods/within/","page":"Containment/withinness","title":"Containment/withinness","text":"export within\n\n\n\"\"\"\n within(geom1, geom)::Bool\n\nReturn `true` if the first geometry is completely within the second geometry.\nThe interiors of both geometries must intersect and, the interior and boundary of the primary (geometry a)\nmust not intersect the exterior of the secondary (geometry b).\n`within` returns the exact opposite result of `contains`.\n\n# Examples\n```jldoctest setup=:(using GeometryOps, GeometryBasics)\nimport GeometryOps as GO, GeoInterface as GI\n\nline = GI.LineString([(1, 1), (1, 2), (1, 3), (1, 4)])\npoint = (1, 2)\nGO.within(point, line)","category":"page"},{"location":"source/methods/within/","page":"Containment/withinness","title":"Containment/withinness","text":"output","category":"page"},{"location":"source/methods/within/","page":"Containment/withinness","title":"Containment/withinness","text":"true\n```\n\"\"\"","category":"page"},{"location":"source/methods/within/","page":"Containment/withinness","title":"Containment/withinness","text":"Syntactic sugar","category":"page"},{"location":"source/methods/within/","page":"Containment/withinness","title":"Containment/withinness","text":"within(g1, g2)::Bool = within(trait(g1), g1, trait(g2), g2)::Bool\nwithin(::GI.FeatureTrait, g1, ::Any, g2)::Bool = within(GI.geometry(g1), g2)\nwithin(::Any, g1, t2::GI.FeatureTrait, g2)::Bool = within(g1, GI.geometry(g2))","category":"page"},{"location":"source/methods/within/","page":"Containment/withinness","title":"Containment/withinness","text":"Points in geometries","category":"page"},{"location":"source/methods/within/","page":"Containment/withinness","title":"Containment/withinness","text":"within(::GI.PointTrait, g1, ::GI.LineStringTrait, g2)::Bool = point_on_line(g1, g2; ignore_end_vertices=true)\nwithin(::GI.PointTrait, g1, ::GI.LinearRingTrait, g2)::Bool = point_on_line(g1, g2; ignore_end_vertices=true)\nwithin(::GI.PointTrait, g1, ::GI.PolygonTrait, g2)::Bool = point_in_polygon(g1, g2; ignore_boundary=true)","category":"page"},{"location":"source/methods/within/","page":"Containment/withinness","title":"Containment/withinness","text":"Lines in geometries","category":"page"},{"location":"source/methods/within/","page":"Containment/withinness","title":"Containment/withinness","text":"within(::GI.LineStringTrait, g1, ::GI.LineStringTrait, g2)::Bool = line_on_line(g1, g2)\nwithin(::GI.LineStringTrait, g1, ::GI.LinearRingTrait, g2)::Bool = line_on_line(g1, g2)\nwithin(::GI.LineStringTrait, g1, ::GI.PolygonTrait, g2)::Bool = line_in_polygon(g1, g2)","category":"page"},{"location":"source/methods/within/","page":"Containment/withinness","title":"Containment/withinness","text":"Polygons within geometries","category":"page"},{"location":"source/methods/within/","page":"Containment/withinness","title":"Containment/withinness","text":"within(::GI.PolygonTrait, g1, ::GI.PolygonTrait, g2)::Bool = polygon_in_polygon(g1, g2)","category":"page"},{"location":"source/methods/within/","page":"Containment/withinness","title":"Containment/withinness","text":"Everything not specified TODO: Add multipolygons","category":"page"},{"location":"source/methods/within/","page":"Containment/withinness","title":"Containment/withinness","text":"within(::GI.AbstractTrait, g1, ::GI.AbstractCurveTrait, g2)::Bool = false","category":"page"},{"location":"source/methods/within/","page":"Containment/withinness","title":"Containment/withinness","text":"","category":"page"},{"location":"source/methods/within/","page":"Containment/withinness","title":"Containment/withinness","text":"This page was generated using Literate.jl.","category":"page"},{"location":"source/methods/distance/#Distance-and-signed-distance","page":"Distance and signed distance","title":"Distance and signed distance","text":"","category":"section"},{"location":"source/methods/distance/","page":"Distance and signed distance","title":"Distance and signed distance","text":"export distance, signed_distance","category":"page"},{"location":"source/methods/distance/#What-is-distance?-What-is-signed-distance?","page":"Distance and signed distance","title":"What is distance? What is signed distance?","text":"","category":"section"},{"location":"source/methods/distance/","page":"Distance and signed distance","title":"Distance and signed distance","text":"Distance is the distance of a point to another geometry. This is always a positive number. If a point is inside of geometry, so on a curve or inside of a polygon, the distance will be zero. Signed distance is mainly used for polygons and multipolygons. If a point is outside of a geometry, signed distance has the same value as distance. However, points within the geometry have a negative distance representing the distance of a point to the closest boundary. Therefore, for all \"non-filled\" geometries, like curves, the distance will either be postitive or 0.","category":"page"},{"location":"source/methods/distance/","page":"Distance and signed distance","title":"Distance and signed distance","text":"To provide an example, consider this rectangle:","category":"page"},{"location":"source/methods/distance/","page":"Distance and signed distance","title":"Distance and signed distance","text":"using GeometryOps\nusing GeometryOps.GeometryBasics\nusing Makie\n\nrect = Polygon([Point(0,0), Point(0,1), Point(1,1), Point(1,0), Point(0, 0)])\npoint_in = Point(0.5, 0.5)\npoint_out = Point(0.5, 1.5)\nf, a, p = poly(rect; axis = (; aspect = DataAspect()))\nscatter!(f, point_in)\nscatter!(f, point_out)\nf","category":"page"},{"location":"source/methods/distance/","page":"Distance and signed distance","title":"Distance and signed distance","text":"This is clearly a rectangle with one point inside and one point outside. The points are both an equal distance to the polygon. The distance to pointin is negative while the distance to pointout is positive.","category":"page"},{"location":"source/methods/distance/","page":"Distance and signed distance","title":"Distance and signed distance","text":"distance(point_in, poly) # == 0\nsigned_distance(point_in, poly) # < 0\nsigned_distance(point_out, poly) # > 0","category":"page"},{"location":"source/methods/distance/#Implementation","page":"Distance and signed distance","title":"Implementation","text":"","category":"section"},{"location":"source/methods/distance/","page":"Distance and signed distance","title":"Distance and signed distance","text":"This is the GeoInterface-compatible implementation. First, we implement a wrapper method that dispatches to the correct implementation based on the geometry trait. This is also used in the implementation, since it's a lot less work!","category":"page"},{"location":"source/methods/distance/","page":"Distance and signed distance","title":"Distance and signed distance","text":"Distance and signed distance are only implemented for points to other geometries right now. This could be extended to include distance from other geometries in the future.","category":"page"},{"location":"source/methods/distance/","page":"Distance and signed distance","title":"Distance and signed distance","text":"The distance calculated is the Euclidean distance using the Pythagorean theorem. Also note that singed_distance only makes sense for \"filled-in\" shapes, like polygons, so it isn't implemented for curves.","category":"page"},{"location":"source/methods/distance/","page":"Distance and signed distance","title":"Distance and signed distance","text":"\"\"\"\n distance(point, geom, ::Type{T} = Float64)::T\n\nCalculates the ditance from the geometry `g1` to the `point`. The distance\nwill always be positive or zero.\n\nThe method will differ based on the type of the geometry provided:\n - The distance from a point to a point is just the Euclidean distance\n between the points.\n - The distance from a point to a line is the minimum distance from the point\n to the closest point on the given line.\n - The distance from a point to a linestring is the minimum distance from the\n point to the closest segment of the linestring.\n - The distance from a point to a linear ring is the minimum distance from\n the point to the closest segment of the linear ring.\n - The distance from a point to a polygon is zero if the point is within the\n polygon and otherwise is the minimum distance from the point to an edge of\n the polygon. This includes edges created by holes.\n - The distance from a point to a multigeometry or a geometry collection is\n the minimum distance between the point and any of the sub-geometries.\n\nResult will be of type T, where T is an optional argument with a default value\nof Float64.\n\"\"\"\ndistance(point, geom, ::Type{T} = Float64) where T <: AbstractFloat =\n _distance(T, GI.trait(point), point, GI.trait(geom), geom)\n\n\"\"\"\n signed_distance(point, geom, ::Type{T} = Float64)::T\n\nCalculates the signed distance from the geometry `geom` to the given point.\nPoints within `geom` have a negative signed distance, and points outside of\n`geom` have a positive signed distance.\n - The signed distance from a point to a point, line, linestring, or linear\n ring is equal to the distance between the two.\n - The signed distance from a point to a polygon is negative if the point is\n within the polygon and is positive otherwise. The value of the distance is\n the minimum distance from the point to an edge of the polygon. This includes\n edges created by holes.\n - The signed distance from a point to a multigeometry or a geometry\n collection is the minimum signed distance between the point and any of the\n sub-geometries.\n\nResult will be of type T, where T is an optional argument with a default value\nof Float64.\n\"\"\"\nsigned_distance(point, geom, ::Type{T} = Float64) where T<:AbstractFloat =\n _signed_distance(T, GI.trait(point), point, GI.trait(geom), geom)","category":"page"},{"location":"source/methods/distance/","page":"Distance and signed distance","title":"Distance and signed distance","text":"Swap argument order to point as first argument","category":"page"},{"location":"source/methods/distance/","page":"Distance and signed distance","title":"Distance and signed distance","text":"_distance(\n ::Type{T},\n gtrait::GI.AbstractTrait, geom,\n ptrait::GI.PointTrait, point,\n) where T = _distance(T, ptrait, point, gtrait, geom)\n\n_signed_distance(\n ::Type{T},\n gtrait::GI.AbstractTrait, geom,\n ptrait::GI.PointTrait, point,\n) where T = _signed_distance(T, ptrait, point, gtrait, geom)","category":"page"},{"location":"source/methods/distance/","page":"Distance and signed distance","title":"Distance and signed distance","text":"Point-Point, Point-Line, Point-LineString, Point-LinearRing","category":"page"},{"location":"source/methods/distance/","page":"Distance and signed distance","title":"Distance and signed distance","text":"_distance(::Type{T}, ::GI.PointTrait, point, ::GI.PointTrait, geom) where T =\n _euclid_distance(T, point, geom)\n\n_distance(::Type{T}, ::GI.PointTrait, point, ::GI.LineTrait, geom) where T =\n _distance_line(T, point, GI.getpoint(geom, 1), GI.getpoint(geom, 2))\n\n_distance(::Type{T}, ::GI.PointTrait, point, ::GI.LineStringTrait, geom) where T =\n _distance_curve(T, point, geom, close_curve = false)\n\n_distance(::Type{T}, ::GI.PointTrait, point, ::GI.LinearRingTrait, geom) where T =\n _distance_curve(T, point, geom, close_curve = true)\n\n_signed_distance(::Type{T}, ptrait::GI.PointTrait, point, gtrait::GI.AbstractTrait, geom) where T =\n _distance(T, ptrait, point, gtrait, geom)","category":"page"},{"location":"source/methods/distance/","page":"Distance and signed distance","title":"Distance and signed distance","text":"Point-Polygon","category":"page"},{"location":"source/methods/distance/","page":"Distance and signed distance","title":"Distance and signed distance","text":"function _distance(::Type{T}, ::GI.PointTrait, point, ::GI.PolygonTrait, geom) where T\n GI.within(point, geom) && return zero(T)\n return _distance_polygon(T, point, geom)\nend\n\nfunction _signed_distance(::Type{T}, ::GI.PointTrait, point, ::GI.PolygonTrait, geom) where T\n min_dist = _distance_polygon(T, point, geom)","category":"page"},{"location":"source/methods/distance/","page":"Distance and signed distance","title":"Distance and signed distance","text":"negative if point is inside polygon","category":"page"},{"location":"source/methods/distance/","page":"Distance and signed distance","title":"Distance and signed distance","text":" return GI.within(point, geom) ? -min_dist : min_dist\nend","category":"page"},{"location":"source/methods/distance/","page":"Distance and signed distance","title":"Distance and signed distance","text":"Point-MultiGeometries / Point-GeometryCollections","category":"page"},{"location":"source/methods/distance/","page":"Distance and signed distance","title":"Distance and signed distance","text":"function _distance(\n ::Type{T},\n ::GI.PointTrait,\n point,\n ::Union{\n GI.MultiPointTrait, GI.MultiCurveTrait,\n GI.MultiPolygonTrait, GI.GeometryCollectionTrait,\n },\n geoms,\n) where T\n min_dist = typemax(T)\n for g in GI.getgeom(geoms)\n dist = distance(point, g, T)\n min_dist = dist < min_dist ? dist : min_dist\n end\n return min_dist\nend\n\nfunction _signed_distance(\n ::Type{T},\n ::GI.PointTrait,\n point,\n ::Union{\n GI.MultiPointTrait, GI.MultiCurveTrait,\n GI.MultiPolygonTrait, GI.GeometryCollectionTrait,\n },\n geoms,\n) where T\n min_dist = typemax(T)\n for g in GI.getgeom(geoms)\n dist = signed_distance(point, g, T)\n min_dist = dist < min_dist ? dist : min_dist\n end\n return min_dist\nend","category":"page"},{"location":"source/methods/distance/","page":"Distance and signed distance","title":"Distance and signed distance","text":"Returns the Euclidean distance between two points.","category":"page"},{"location":"source/methods/distance/","page":"Distance and signed distance","title":"Distance and signed distance","text":"Base.@propagate_inbounds _euclid_distance(::Type{T}, p1, p2) where T =\n _euclid_distance(\n T,\n GeoInterface.x(p1), GeoInterface.y(p1),\n GeoInterface.x(p2), GeoInterface.y(p2),\n )","category":"page"},{"location":"source/methods/distance/","page":"Distance and signed distance","title":"Distance and signed distance","text":"Returns the Euclidean distance between two points given their x and y values.","category":"page"},{"location":"source/methods/distance/","page":"Distance and signed distance","title":"Distance and signed distance","text":"Base.@propagate_inbounds _euclid_distance(::Type{T}, x1, y1, x2, y2) where T =\n T(sqrt((x2 - x1)^2 + (y2 - y1)^2))","category":"page"},{"location":"source/methods/distance/","page":"Distance and signed distance","title":"Distance and signed distance","text":"Returns the minimum distance from point p0 to the line defined by endpoints p1 and p2.","category":"page"},{"location":"source/methods/distance/","page":"Distance and signed distance","title":"Distance and signed distance","text":"function _distance_line(::Type{T}, p0, p1, p2) where T\n x0, y0 = GeoInterface.x(p0), GeoInterface.y(p0)\n x1, y1 = GeoInterface.x(p1), GeoInterface.y(p1)\n x2, y2 = GeoInterface.x(p2), GeoInterface.y(p2)\n\n xfirst, yfirst, xlast, ylast = x1 < x2 ?\n (x1, y1, x2, y2) : (x2, y2, x1, y1)\n\n #=\n Vectors from first point to last point (v) and from first point to point of\n interest (w) to find the projection of w onto v to find closest point\n =#\n v = (xlast - xfirst, ylast - yfirst)\n w = (x0 - xfirst, y0 - yfirst)\n\n c1 = sum(w .* v)\n if c1 <= 0 # p0 is closest to first endpoint\n return _euclid_distance(T, x0, y0, xfirst, yfirst)\n end\n\n c2 = sum(v .* v)\n if c2 <= c1 # p0 is closest to last endpoint\n return _euclid_distance(T, x0, y0, xlast, ylast)\n end\n\n b2 = c1 / c2 # projection fraction\n return _euclid_distance(T, x0, y0, xfirst + (b2 * v[1]), yfirst + (b2 * v[2]))\nend","category":"page"},{"location":"source/methods/distance/","page":"Distance and signed distance","title":"Distance and signed distance","text":"Returns the minimum distance from the given point to the given curve. If close_curve is true, make sure to include the edge from the first to last point of the curve, even if it isn't explicitly repeated.","category":"page"},{"location":"source/methods/distance/","page":"Distance and signed distance","title":"Distance and signed distance","text":"function _distance_curve(::Type{T}, point, curve; close_curve = false) where T","category":"page"},{"location":"source/methods/distance/","page":"Distance and signed distance","title":"Distance and signed distance","text":"see if linear ring has explicitly repeated last point in coordinates","category":"page"},{"location":"source/methods/distance/","page":"Distance and signed distance","title":"Distance and signed distance","text":" np = GI.npoint(curve)\n first_last_equal = equals(GI.getpoint(curve, 1), GI.getpoint(curve, np))\n close_curve &= first_last_equal\n np -= first_last_equal ? 1 : 0","category":"page"},{"location":"source/methods/distance/","page":"Distance and signed distance","title":"Distance and signed distance","text":"find minimum distance","category":"page"},{"location":"source/methods/distance/","page":"Distance and signed distance","title":"Distance and signed distance","text":" min_dist = typemax(T)\n p1 = GI.getpoint(curve, close_curve ? np : 1)\n for i in (close_curve ? 1 : 2):np\n p2 = GI.getpoint(curve, i)\n dist = _distance_line(T, point, p1, p2)\n min_dist = dist < min_dist ? dist : min_dist\n p1 = p2\n end\n return min_dist\nend","category":"page"},{"location":"source/methods/distance/","page":"Distance and signed distance","title":"Distance and signed distance","text":"Returns the minimum distance from the given point to an edge of the given polygon, including from edges created by holes. Assumes polygon isn't filled and treats the exterior and each hole as a linear ring.","category":"page"},{"location":"source/methods/distance/","page":"Distance and signed distance","title":"Distance and signed distance","text":"function _distance_polygon(::Type{T}, point, poly) where T\n min_dist = _distance_curve(T, point, GI.getexterior(poly); close_curve = true)\n @inbounds for hole in GI.gethole(poly)\n dist = _distance_curve(T, point, hole; close_curve = true)\n min_dist = dist < min_dist ? dist : min_dist\n end\n return min_dist\nend","category":"page"},{"location":"source/methods/distance/","page":"Distance and signed distance","title":"Distance and signed distance","text":"","category":"page"},{"location":"source/methods/distance/","page":"Distance and signed distance","title":"Distance and signed distance","text":"This page was generated using Literate.jl.","category":"page"},{"location":"source/methods/intersects/#Intersection-checks","page":"Intersection checks","title":"Intersection checks","text":"","category":"section"},{"location":"source/methods/intersects/","page":"Intersection checks","title":"Intersection checks","text":"export intersects, intersection, intersection_points","category":"page"},{"location":"source/methods/intersects/#What-is-intersects-vs-intersection-vs-intersection_points?","page":"Intersection checks","title":"What is intersects vs intersection vs intersection_points?","text":"","category":"section"},{"location":"source/methods/intersects/","page":"Intersection checks","title":"Intersection checks","text":"The intersects methods check whether two geometries intersect with each other. The intersection methods return the geometry intersection between the two input geometries. The intersection_points method returns a list of intersection points between two geometries.","category":"page"},{"location":"source/methods/intersects/","page":"Intersection checks","title":"Intersection checks","text":"The intersects methods will always return a Boolean. However, note that the intersection methods will not all return the same type. For example, the intersection of two lines will be a point in most cases, unless the lines are parallel. On the other hand, the intersection of two polygons will be another polygon in most cases. Finally, the intersection_points method returns a list of tuple points.","category":"page"},{"location":"source/methods/intersects/","page":"Intersection checks","title":"Intersection checks","text":"To provide an example, consider these two lines:","category":"page"},{"location":"source/methods/intersects/","page":"Intersection checks","title":"Intersection checks","text":"using GeometryOps\nusing GeometryOps.GeometryBasics\nusing Makie\nusing CairoMakie\npoint1, point2 = Point(124.584961,-12.768946), Point(126.738281,-17.224758)\npoint3, point4 = Point(123.354492,-15.961329), Point(127.22168,-14.008696)\nline1 = Line(point1, point2)\nline2 = Line(point3, point4)\nf, a, p = lines([point1, point2])\nlines!([point3, point4])","category":"page"},{"location":"source/methods/intersects/","page":"Intersection checks","title":"Intersection checks","text":"We can see that they intersect, so we expect intersects to return true, and we can visualize the intersection point in red.","category":"page"},{"location":"source/methods/intersects/","page":"Intersection checks","title":"Intersection checks","text":"int_bool = GO.intersects(line1, line2)\nprintln(int_bool)\nint_point = GO.intersection(line1, line2)\nscatter!(int_point, color = :red)\nf","category":"page"},{"location":"source/methods/intersects/#Implementation","page":"Intersection checks","title":"Implementation","text":"","category":"section"},{"location":"source/methods/intersects/","page":"Intersection checks","title":"Intersection checks","text":"This is the GeoInterface-compatible implementation.","category":"page"},{"location":"source/methods/intersects/","page":"Intersection checks","title":"Intersection checks","text":"First, we implement a wrapper method for intersects, intersection, and intersectionpoints that dispatches to the correct implementation based on the geometry trait. The two underlying helper functions that are widely used in all geometry dispatches are _lineintersects, which determines if two line segments intersect and intersectionpoint which determines the intersection point between two line segments.","category":"page"},{"location":"source/methods/intersects/","page":"Intersection checks","title":"Intersection checks","text":"\"\"\"\n intersects(geom1, geom2)::Bool\n\nCheck if two geometries intersect, returning true if so and false otherwise.\n\n# Example\n\n```jldoctest\nimport GeoInterface as GI, GeometryOps as GO\n\nline1 = GI.Line([(124.584961,-12.768946), (126.738281,-17.224758)])\nline2 = GI.Line([(123.354492,-15.961329), (127.22168,-14.008696)])\nGO.intersects(line1, line2)","category":"page"},{"location":"source/methods/intersects/","page":"Intersection checks","title":"Intersection checks","text":"output","category":"page"},{"location":"source/methods/intersects/","page":"Intersection checks","title":"Intersection checks","text":"true\n```\n\"\"\"\nintersects(geom1, geom2) = intersects(\n GI.trait(geom1),\n geom1,\n GI.trait(geom2),\n geom2\n)\n\n\"\"\"\n intersects(::GI.LineTrait, a, ::GI.LineTrait, b)::Bool\n\nReturns true if two line segments intersect and false otherwise.\n\"\"\"\nfunction intersects(::GI.LineTrait, a, ::GI.LineTrait, b)\n a1 = _tuple_point(GI.getpoint(a, 1))\n a2 = _tuple_point(GI.getpoint(a, 2))\n b1 = _tuple_point(GI.getpoint(b, 1))\n b2 = _tuple_point(GI.getpoint(b, 2))\n meet_type = ExactPredicates.meet(a1, a2, b1, b2)\n return meet_type == 0 || meet_type == 1\nend\n\n\"\"\"\n intersects(::GI.AbstractTrait, a, ::GI.AbstractTrait, b)::Bool\n\nReturns true if two geometries intersect with one another and false\notherwise. For all geometries but lines, convert the geometry to a list of edges\nand cross compare the edges for intersections.\n\"\"\"\nfunction intersects(\n trait_a::GI.AbstractTrait, a_geom,\n trait_b::GI.AbstractTrait, b_geom,\n) edges_a, edges_b = map(sort! ∘ to_edges, (a_geom, b_geom))\n return _line_intersects(edges_a, edges_b) ||\n within(trait_a, a_geom, trait_b, b_geom) ||\n within(trait_b, b_geom, trait_a, a_geom)\nend\n\n\"\"\"\n _line_intersects(\n edges_a::Vector{Edge},\n edges_b::Vector{Edge}\n )::Bool\n\nReturns true if there is at least one intersection between edges within the\ntwo lists of edges.\n\"\"\"\nfunction _line_intersects(\n edges_a::Vector{Edge},\n edges_b::Vector{Edge}\n)","category":"page"},{"location":"source/methods/intersects/","page":"Intersection checks","title":"Intersection checks","text":"Extents.intersects(toextent(edgesa), toextent(edgesb)) || return false","category":"page"},{"location":"source/methods/intersects/","page":"Intersection checks","title":"Intersection checks","text":" for edge_a in edges_a\n for edge_b in edges_b\n _line_intersects(edge_a, edge_b) && return true\n end\n end\n return false\nend\n\n\"\"\"\n _line_intersects(\n edge_a::Edge,\n edge_b::Edge,\n )::Bool\n\nReturns true if there is at least one intersection between two edges.\n\"\"\"\nfunction _line_intersects(edge_a::Edge, edge_b::Edge)\n meet_type = ExactPredicates.meet(edge_a..., edge_b...)\n return meet_type == 0 || meet_type == 1\nend\n\n\"\"\"\n intersection(geom_a, geom_b)::Union{Tuple{::Real, ::Real}, ::Nothing}\n\nReturn an intersection point between two geometries. Return nothing if none are\nfound. Else, the return type depends on the input. It will be a union between:\na point, a line, a linear ring, a polygon, or a multipolygon\n\n# Example\n\n```jldoctest\nimport GeoInterface as GI, GeometryOps as GO\n\nline1 = GI.Line([(124.584961,-12.768946), (126.738281,-17.224758)])\nline2 = GI.Line([(123.354492,-15.961329), (127.22168,-14.008696)])\nGO.intersection(line1, line2)","category":"page"},{"location":"source/methods/intersects/","page":"Intersection checks","title":"Intersection checks","text":"output","category":"page"},{"location":"source/methods/intersects/","page":"Intersection checks","title":"Intersection checks","text":"(125.58375366067547, -14.83572303404496)\n```\n\"\"\"\nintersection(geom_a, geom_b) =\n intersection(GI.trait(geom_a), geom_a, GI.trait(geom_b), geom_b)\n\n\"\"\"\n intersection(\n ::GI.LineTrait, line_a,\n ::GI.LineTrait, line_b,\n )::Union{\n ::Tuple{::Real, ::Real},\n ::Nothing\n }\n\nCalculates the intersection between two line segments. Return nothing if\nthere isn't one.\n\"\"\"\nfunction intersection(::GI.LineTrait, line_a, ::GI.LineTrait, line_b)","category":"page"},{"location":"source/methods/intersects/","page":"Intersection checks","title":"Intersection checks","text":"Get start and end points for both lines","category":"page"},{"location":"source/methods/intersects/","page":"Intersection checks","title":"Intersection checks","text":" a1 = GI.getpoint(line_a, 1)\n a2 = GI.getpoint(line_a, 2)\n b1 = GI.getpoint(line_b, 1)\n b2 = GI.getpoint(line_b, 2)","category":"page"},{"location":"source/methods/intersects/","page":"Intersection checks","title":"Intersection checks","text":"Determine the intersection point","category":"page"},{"location":"source/methods/intersects/","page":"Intersection checks","title":"Intersection checks","text":" point, fracs = _intersection_point((a1, a2), (b1, b2))","category":"page"},{"location":"source/methods/intersects/","page":"Intersection checks","title":"Intersection checks","text":"Determine if intersection point is on line segments","category":"page"},{"location":"source/methods/intersects/","page":"Intersection checks","title":"Intersection checks","text":" if !isnothing(point) && 0 <= fracs[1] <= 1 && 0 <= fracs[2] <= 1\n return point\n end\n return nothing\nend\n\nintersection(\n trait_a::Union{GI.LineStringTrait, GI.LinearRingTrait},\n geom_a,\n trait_b::Union{GI.LineStringTrait, GI.LinearRingTrait},\n geom_b,\n) = intersection_points(trait_a, geom_a, trait_b, geom_b)\n\n\"\"\"\n intersection(\n ::GI.PolygonTrait, poly_a,\n ::GI.PolygonTrait, poly_b,\n )::Union{\n ::Vector{Vector{Tuple{::Real, ::Real}}}, # is this a good return type?\n ::Nothing\n }\n\nCalculates the intersection between two line segments. Return nothing if\nthere isn't one.\n\"\"\"\nfunction intersection(::GI.PolygonTrait, poly_a, ::GI.PolygonTrait, poly_b)\n @assert false \"Polygon intersection isn't implemented yet.\"\n return nothing\nend\n\n\"\"\"\n intersection(\n ::GI.AbstractTrait, geom_a,\n ::GI.AbstractTrait, geom_b,\n )::Union{\n ::Vector{Vector{Tuple{::Real, ::Real}}}, # is this a good return type?\n ::Nothing\n }\n\nCalculates the intersection between two line segments. Return nothing if\nthere isn't one.\n\"\"\"\nfunction intersection(\n trait_a::GI.AbstractTrait, geom_a,\n trait_b::GI.AbstractTrait, geom_b,\n)\n @assert(\n false,\n \"Intersection between $trait_a and $trait_b isn't implemented yet.\",\n )\n return nothing\nend\n\n\"\"\"\n intersection_points(\n geom_a,\n geom_b,\n )::Union{\n ::Vector{::Tuple{::Real, ::Real}},\n ::Nothing,\n }\n\nReturn a list of intersection points between two geometries. If no intersection\npoint was possible given geometry extents, return nothing. If none are found,\nreturn an empty list.\n\"\"\"\nintersection_points(geom_a, geom_b) =\n intersection_points(GI.trait(geom_a), geom_a, GI.trait(geom_b), geom_b)\n\n\"\"\"\n intersection_points(\n ::GI.AbstractTrait, geom_a,\n ::GI.AbstractTrait, geom_b,\n )::Union{\n ::Vector{::Tuple{::Real, ::Real}},\n ::Nothing,\n }\n\nCalculates the list of intersection points between two geometries, inlcuding\nline segments, line strings, linear rings, polygons, and multipolygons. If no\nintersection points were possible given geometry extents, return nothing. If\nnone are found, return an empty list.\n\"\"\"\nfunction intersection_points(::GI.AbstractTrait, a, ::GI.AbstractTrait, b)","category":"page"},{"location":"source/methods/intersects/","page":"Intersection checks","title":"Intersection checks","text":"Check if the geometries extents even overlap","category":"page"},{"location":"source/methods/intersects/","page":"Intersection checks","title":"Intersection checks","text":" Extents.intersects(GI.extent(a), GI.extent(b)) || return nothing","category":"page"},{"location":"source/methods/intersects/","page":"Intersection checks","title":"Intersection checks","text":"Create a list of edges from the two input geometries","category":"page"},{"location":"source/methods/intersects/","page":"Intersection checks","title":"Intersection checks","text":" edges_a, edges_b = map(sort! ∘ to_edges, (a, b))\n npoints_a, npoints_b = length(edges_a), length(edges_b)\n a_closed = npoints_a > 1 && edges_a[1][1] == edges_a[end][1]\n b_closed = npoints_b > 1 && edges_b[1][1] == edges_b[end][1]\n if npoints_a > 0 && npoints_b > 0","category":"page"},{"location":"source/methods/intersects/","page":"Intersection checks","title":"Intersection checks","text":"Initialize an empty list of points","category":"page"},{"location":"source/methods/intersects/","page":"Intersection checks","title":"Intersection checks","text":" T = typeof(edges_a[1][1][1]) # x-coordinate of first point in first edge\n result = Tuple{T,T}[]","category":"page"},{"location":"source/methods/intersects/","page":"Intersection checks","title":"Intersection checks","text":"Loop over pairs of edges and add any intersection points to results","category":"page"},{"location":"source/methods/intersects/","page":"Intersection checks","title":"Intersection checks","text":" for i in eachindex(edges_a)\n for j in eachindex(edges_b)\n point, fracs = _intersection_point(edges_a[i], edges_b[j])\n if !isnothing(point)\n #=\n Determine if point is on edge (all edge endpoints excluded\n except for the last edge for an open geometry)\n =#\n α, β = fracs\n on_a_edge = (!a_closed && i == npoints_a && 0 <= α <= 1) ||\n (0 <= α < 1)\n on_b_edge = (!b_closed && j == npoints_b && 0 <= β <= 1) ||\n (0 <= β < 1)\n if on_a_edge && on_b_edge\n push!(result, point)\n end\n end\n end\n end\n return result\n end\n return nothing\nend\n\n\"\"\"\n _intersection_point(\n (a1, a2)::Tuple,\n (b1, b2)::Tuple,\n )\n\nCalculates the intersection point between two lines if it exists, and as if the\nline extended to infinity, and the fractional component of each line from the\ninitial end point to the intersection point.\nInputs:\n (a1, a2)::Tuple{Tuple{::Real, ::Real}, Tuple{::Real, ::Real}} first line\n (b1, b2)::Tuple{Tuple{::Real, ::Real}, Tuple{::Real, ::Real}} second line\nOutputs:\n (x, y)::Tuple{::Real, ::Real} intersection point\n (t, u)::Tuple{::Real, ::Real} fractional length of lines to intersection\n Both are ::Nothing if point doesn't exist!\n\nCalculation derivation can be found here:\n https://stackoverflow.com/questions/563198/\n\"\"\"\nfunction _intersection_point((a1, a2)::Tuple, (b1, b2)::Tuple)","category":"page"},{"location":"source/methods/intersects/","page":"Intersection checks","title":"Intersection checks","text":"First line runs from p to p + r","category":"page"},{"location":"source/methods/intersects/","page":"Intersection checks","title":"Intersection checks","text":" px, py = GI.x(a1), GI.y(a1)\n rx, ry = GI.x(a2) - px, GI.y(a2) - py","category":"page"},{"location":"source/methods/intersects/","page":"Intersection checks","title":"Intersection checks","text":"Second line runs from q to q + s","category":"page"},{"location":"source/methods/intersects/","page":"Intersection checks","title":"Intersection checks","text":" qx, qy = GI.x(b1), GI.y(b1)\n sx, sy = GI.x(b2) - qx, GI.y(b2) - qy","category":"page"},{"location":"source/methods/intersects/","page":"Intersection checks","title":"Intersection checks","text":"Intersection will be where p + tr = q + us where 0 < t, u < 1 and","category":"page"},{"location":"source/methods/intersects/","page":"Intersection checks","title":"Intersection checks","text":" r_cross_s = rx * sy - ry * sx\n if r_cross_s != 0\n Δqp_x = qx - px\n Δqp_y = qy - py\n t = (Δqp_x * sy - Δqp_y * sx) / r_cross_s\n u = (Δqp_x * ry - Δqp_y * rx) / r_cross_s\n x = px + t * rx\n y = py + t * ry\n return (x, y), (t, u)\n end\n return nothing, nothing\nend","category":"page"},{"location":"source/methods/intersects/","page":"Intersection checks","title":"Intersection checks","text":"","category":"page"},{"location":"source/methods/intersects/","page":"Intersection checks","title":"Intersection checks","text":"This page was generated using Literate.jl.","category":"page"},{"location":"source/utils/#Utility-functions","page":"Utility functions","title":"Utility functions","text":"","category":"section"},{"location":"source/utils/","page":"Utility functions","title":"Utility functions","text":"_is3d(geom) = _is3d(GI.trait(geom), geom)\n_is3d(::GI.AbstractGeometryTrait, geom) = GI.is3d(geom)\n_is3d(::GI.FeatureTrait, feature) = _is3d(GI.geometry(feature))\n_is3d(::GI.FeatureCollectionTrait, fc) = _is3d(GI.getfeature(fc, 1))\n_is3d(::Nothing, geom) = _is3d(first(geom)) # Otherwise step into an itererable\n\n_npoint(x) = _npoint(trait(x), x)\n_npoint(::Nothing, xs::AbstractArray) = sum(_npoint, xs)\n_npoint(::GI.FeatureCollectionTrait, fc) = sum(_npoint, GI.getfeature(fc))\n_npoint(::GI.FeatureTrait, f) = _npoint(GI.geometry(f))\n_npoint(::GI.AbstractGeometryTrait, x) = GI.npoint(trait(x), x)\n\n_nedge(x) = _nedge(trait(x), x)\n_nedge(::Nothing, xs::AbstractArray) = sum(_nedge, xs)\n_nedge(::GI.FeatureCollectionTrait, fc) = sum(_nedge, GI.getfeature(fc))\n_nedge(::GI.FeatureTrait, f) = _nedge(GI.geometry(f))\nfunction _nedge(::GI.AbstractGeometryTrait, x)\n n = 0\n for g in GI.getgeom(x)\n n += _nedge(g)\n end\n return n\nend\n_nedge(::GI.AbstractCurveTrait, x) = GI.npoint(x) - 1\n_nedge(::GI.PointTrait, x) = error(\"Cant get edges from points\")\n\n\n\"\"\"\n polygon_to_line(poly::Polygon)\n\nConverts a Polygon to LineString or MultiLineString","category":"page"},{"location":"source/utils/","page":"Utility functions","title":"Utility functions","text":"Examples","category":"page"},{"location":"source/utils/","page":"Utility functions","title":"Utility functions","text":"```jldoctest\nimport GeometryOps as GO, GeoInterface as GI\n\npoly = GI.Polygon([[(-2.275543, 53.464547), (-2.275543, 53.489271), (-2.215118, 53.489271), (-2.215118, 53.464547), (-2.275543, 53.464547)]])\nGO.polygon_to_line(poly)","category":"page"},{"location":"source/utils/","page":"Utility functions","title":"Utility functions","text":"output","category":"page"},{"location":"source/utils/","page":"Utility functions","title":"Utility functions","text":"GeoInterface.Wrappers.LineString{false, false, Vector{Tuple{Float64, Float64}}, Nothing, Nothing}([(-2.275543, 53.464547), (-2.275543, 53.489271), (-2.215118, 53.489271), (-2.215118, 53.464547), (-2.275543, 53.464547)], nothing, nothing)\n```\n\"\"\"\nfunction polygon_to_line(poly)\n @assert GI.trait(poly) isa PolygonTrait\n GI.ngeom(poly) > 1 && return GI.MultiLineString(collect(GI.getgeom(poly)))\n return GI.LineString(collect(GI.getgeom(GI.getgeom(poly, 1))))\nend\n\n\n\"\"\"\n to_edges()\n\nConvert any geometry or collection of geometries into a flat\nvector of `Tuple{Tuple{Float64,Float64},Tuple{Float64,Float64}}` edges.\n\"\"\"\nfunction to_edges(x)\n edges = Vector{Edge}(undef, _nedge(x))\n _to_edges!(edges, x, 1)\n return edges\nend\n\n_to_edges!(edges::Vector, x, n) = _to_edges!(edges, trait(x), x, n)\nfunction _to_edges!(edges::Vector, ::GI.FeatureCollectionTrait, fc, n)\n for f in GI.getfeature(fc)\n n = _to_edges!(edges, f, n)\n end\nend\n_to_edges!(edges::Vector, ::GI.FeatureTrait, f, n) = _to_edges!(edges, GI.geometry(f), n)\nfunction _to_edges!(edges::Vector, ::GI.AbstractGeometryTrait, fc, n)\n for f in GI.getgeom(fc)\n n = _to_edges!(edges, f, n)\n end\nend\nfunction _to_edges!(edges::Vector, ::GI.AbstractCurveTrait, geom, n)\n p1 = GI.getpoint(geom, 1)\n p1x, p1y = GI.x(p1), GI.y(p1)\n for i in 2:GI.npoint(geom)\n p2 = GI.getpoint(geom, i)\n p2x, p2y = GI.x(p2), GI.y(p2)\n edges[n] = (p1x, p1y), (p2x, p2y)\n p1x, p1y = p2x, p2y\n n += 1\n end\n return n\nend\n\n_tuple_point(p) = GI.x(p), GI.y(p)\n\nfunction to_extent(edges::Vector{Edge})\n x, y = extrema(first, edges)\n Extents.Extent(X=x, Y=y)\nend\n\nfunction to_points(xs)\n points = Vector{TuplePoint}(undef, _npoint(x))\n _to_points!(points, x, 1)\n return points\nend\n\n_to_points!(points::Vector, x, n) = _to_points!(points, trait(x), x, n)\nfunction _to_points!(points::Vector, ::FeatureCollectionTrait, fc, n)\n for f in GI.getfeature(fc)\n n = _to_points!(points, f, n)\n end\nend\n_to_points!(points::Vector, ::FeatureTrait, f, n) = _to_points!(points, GI.geometry(f), n)\nfunction _to_points!(points::Vector, ::AbstractGeometryTrait, fc, n)\n for f in GI.getgeom(fc)\n n = _to_points!(points, f, n)\n end\nend\nfunction _to_points!(points::Vector, ::Union{AbstractCurveTrait,MultiPointTrait}, geom, n)\n p1 = GI.getpoint(geom, 1)\n p1x, p1y = GI.x(p1), GI.y(p1)\n for i in 2:GI.npoint(geom)\n p2 = GI.getpoint(geom, i)\n p2x, p2y = GI.x(p2), GI.y(p2)\n points[n] = (p1x, p1y), (p2x, p2y)\n p1 = p2\n n += 1\n end\n return n\nend","category":"page"},{"location":"source/utils/","page":"Utility functions","title":"Utility functions","text":"","category":"page"},{"location":"source/utils/","page":"Utility functions","title":"Utility functions","text":"This page was generated using Literate.jl.","category":"page"},{"location":"source/methods/bools/#Boolean-conditions","page":"Boolean conditions","title":"Boolean conditions","text":"","category":"section"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":"export isclockwise, isconcave\nexport point_on_line, point_in_polygon, point_in_ring\nexport line_on_line, line_in_polygon, polygon_in_polygon","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":"These are all adapted from Turf.jl.","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":"The may not necessarily be what want in the end but work for now!","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":"\"\"\"\n isclockwise(line::Union{LineString, Vector{Position}})::Bool\n\nTake a ring and return true or false whether or not the ring is clockwise or\ncounter-clockwise.\n\n# Example\n\n```jldoctest\nimport GeoInterface as GI, GeometryOps as GO\n\nring = GI.LinearRing([(0, 0), (1, 1), (1, 0), (0, 0)])\nGO.isclockwise(ring)","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":"output","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":"true\n```\n\"\"\"\nisclockwise(geom)::Bool = isclockwise(GI.trait(geom), geom)\n\nfunction isclockwise(::AbstractCurveTrait, line)::Bool\n sum = 0.0\n prev = GI.getpoint(line, 1)\n for p in GI.getpoint(line)","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":"sum will be zero for the first point as x is subtracted from itself","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":" sum += (GI.x(p) - GI.x(prev)) * (GI.y(p) + GI.y(prev))\n prev = p\n end\n\n return sum > 0.0\nend\n\n\"\"\"\n isconcave(poly::Polygon)::Bool\n\nTake a polygon and return true or false as to whether it is concave or not.\n\n# Examples\n```jldoctest\nimport GeoInterface as GI, GeometryOps as GO\n\npoly = GI.Polygon([[(0, 0), (0, 1), (1, 1), (1, 0), (0, 0)]])\nGO.isconcave(poly)","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":"output","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":"false\n```\n\"\"\"\nfunction isconcave(poly)::Bool\n sign = false\n\n exterior = GI.getexterior(poly)","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":"FIXME handle not closed polygons","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":" GI.npoint(exterior) <= 4 && return false\n n = GI.npoint(exterior) - 1\n\n for i in 1:n\n j = ((i + 1) % n) === 0 ? 1 : (i + 1) % n\n m = ((i + 2) % n) === 0 ? 1 : (i + 2) % n\n\n pti = GI.getpoint(exterior, i)\n ptj = GI.getpoint(exterior, j)\n ptm = GI.getpoint(exterior, m)\n\n dx1 = GI.x(ptm) - GI.x(ptj)\n dy1 = GI.y(ptm) - GI.y(ptj)\n dx2 = GI.x(pti) - GI.x(ptj)\n dy2 = GI.y(pti) - GI.y(ptj)\n\n cross = (dx1 * dy2) - (dy1 * dx2)\n\n if i === 0\n sign = cross > 0\n elseif sign !== (cross > 0)\n return true\n end\n end\n\n return false\nend","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":"\"\"\" isparallel(line1::LineString, line2::LineString)::Bool","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":"Return true if each segment of line1 is parallel to the correspondent segment of line2","category":"page"},{"location":"source/methods/bools/#Examples","page":"Boolean conditions","title":"Examples","text":"","category":"section"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":"import GeoInterface as GI, GeometryOps as GO\njulia> line1 = GI.LineString([(9.170356, 45.477985), (9.164434, 45.482551), (9.166644, 45.484003)])\nGeoInterface.Wrappers.LineString{false, false, Vector{Tuple{Float64, Float64}}, Nothing, Nothing}([(9.170356, 45.477985), (9.164434, 45.482551), (9.166644, 45.484003)], nothing, nothing)\n\njulia> line2 = GI.LineString([(9.169356, 45.477985), (9.163434, 45.482551), (9.165644, 45.484003)])\nGeoInterface.Wrappers.LineString{false, false, Vector{Tuple{Float64, Float64}}, Nothing, Nothing}([(9.169356, 45.477985), (9.163434, 45.482551), (9.165644, 45.484003)], nothing, nothing)\n\njulia>\nGO.isparallel(line1, line2)\ntrue","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":"\"\"\" function isparallel(line1, line2)::Bool seg1 = linesegment(line1) seg2 = linesegment(line2)","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":"for i in eachindex(seg1)\n coors2 = nothing\n coors1 = seg1[i]\n coors2 = seg2[i]\n _isparallel(coors1, coors2) == false && return false\nend\nreturn true","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":"end","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":"@inline function isparallel(p1, p2) slope1 = bearingtoazimuth(rhumbbearing(GI.x(p1), GI.x(p2))) slope2 = bearingtoazimuth(rhumb_bearing(GI.y(p1), GI.y(p2)))","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":"return slope1 === slope2","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":"end","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":"\"\"\"\n point_on_line(point::Point, line::LineString; ignore_end_vertices::Bool=false)::Bool\n\nReturn true if a point is on a line. Accept a optional parameter to ignore the\nstart and end vertices of the linestring.\n\n# Examples\n\n```jldoctest\nimport GeoInterface as GI, GeometryOps as GO\n\npoint = (1, 1)\nline = GI.LineString([(0, 0), (3, 3), (4, 4)])\nGO.point_on_line(point, line)","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":"output","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":"true\n```\n\"\"\"\nfunction point_on_line(point, line; ignore_end_vertices::Bool=false)::Bool\n line_points = tuple_points(line)\n n = length(line_points)\n\n exclude_boundary = :none\n for i in 1:n - 1\n if ignore_end_vertices\n if i === 1\n exclude_boundary = :start\n elseif i === n - 2\n exclude_boundary = :end\n elseif (i === 1 && i + 1 === n - 1)\n exclude_boundary = :both\n end\n end\n if point_on_segment(point, (line_points[i], line_points[i + 1]); exclude_boundary)\n return true\n end\n end\n return false\nend\n\nfunction point_on_seg(point, start, stop)","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":"Parse out points","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":" x, y = GI.x(point), GI.y(point)\n x1, y1 = GI.x(start), GI.y(start)\n x2, y2 = GI.x(stop), GI.y(stop)\n Δxl = x2 - x1\n Δyl = y2 - y1","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":"Determine if point is on segment","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":" cross = (x - x1) * Δyl - (y - y1) * Δxl\n if cross == 0 # point is on line extending to infinity","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":"is line between endpoints","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":" if abs(Δxl) >= abs(Δyl) # is line between endpoints\n return Δxl > 0 ? x1 <= x <= x2 : x2 <= x <= x1\n else\n return Δyl > 0 ? y1 <= y <= y2 : y2 <= y <= y1\n end\n end\n return false\nend\n\nfunction point_on_segment(point, (start, stop); exclude_boundary::Symbol=:none)::Bool\n x, y = GI.x(point), GI.y(point)\n x1, y1 = GI.x(start), GI.y(start)\n x2, y2 = GI.x(stop), GI.y(stop)\n\n dxc = x - x1\n dyc = y - y1\n dx1 = x2 - x1\n dy1 = y2 - y1","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":"TODO use better predicate for crossing here","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":" cross = dxc * dy1 - dyc * dx1\n cross != 0 && return false","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":"Will constprop optimise these away?","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":" if exclude_boundary === :none\n if abs(dx1) >= abs(dy1)\n return dx1 > 0 ? x1 <= x && x <= x2 : x2 <= x && x <= x1\n end\n return dy1 > 0 ? y1 <= y && y <= y2 : y2 <= y && y <= y1\n elseif exclude_boundary === :start\n if abs(dx1) >= abs(dy1)\n return dx1 > 0 ? x1 < x && x <= x2 : x2 <= x && x < x1\n end\n return dy1 > 0 ? y1 < y && y <= y2 : y2 <= y && y < y1\n elseif exclude_boundary === :end\n if abs(dx1) >= abs(dy1)\n return dx1 > 0 ? x1 <= x && x < x2 : x2 < x && x <= x1\n end\n return dy1 > 0 ? y1 <= y && y < y2 : y2 < y && y <= y1\n elseif exclude_boundary === :both\n if abs(dx1) >= abs(dy1)\n return dx1 > 0 ? x1 < x && x < x2 : x2 < x && x < x1\n end\n return dy1 > 0 ? y1 < y && y < y2 : y2 < y && y < y1\n end\n return false\nend\n\n\"\"\"\n point_in_polygon(point::Point, polygon::Union{Polygon, MultiPolygon}, ignore_boundary::Bool=false)::Bool\n\nTake a Point and a Polygon and determine if the point\nresides inside the polygon. The polygon can be convex or concave. The function accounts for holes.\n\n# Examples\n\n```jldoctest\nimport GeoInterface as GI, GeometryOps as GO\n\npoint = (-77.0, 44.0)\npoly = GI.Polygon([[(-81, 41), (-81, 47), (-72, 47), (-72, 41), (-81, 41)]])\nGO.point_in_polygon(point, poly)","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":"output","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":"true\n```\n\"\"\"\npoint_in_polygon(point, polygon; kw...)::Bool =\n point_in_polygon(GI.trait(point), point, GI.trait(polygon), polygon; kw...)\nfunction point_in_polygon(\n ::PointTrait, point,\n ::PolygonTrait, poly;\n ignore_boundary::Bool=false,\n check_extent::Bool=false,\n)::Bool","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":"Cheaply check that the point is inside the polygon extent","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":" if check_extent\n point_in_extent(point, GI.extent(poly)) || return false\n end","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":"Then check the point is inside the exterior ring","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":" point_in_polygon(\n point,GI.getexterior(poly);\n ignore_boundary, check_extent=false,\n ) || return false","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":"Finally make sure the point is not in any of the holes, flipping the boundary condition","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":" for ring in GI.gethole(poly)\n point_in_polygon(\n point, ring;\n ignore_boundary=!ignore_boundary,\n ) && return false\n end\n return true\nend\n\nfunction point_in_polygon(\n ::PointTrait, pt,\n ::Union{LineStringTrait,LinearRingTrait}, ring;\n ignore_boundary::Bool=false,\n check_extent::Bool=false,\n)::Bool\n x, y = GI.x(pt), GI.y(pt)","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":"Cheaply check that the point is inside the ring extent","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":" if check_extent\n point_in_extent(point, GI.extent(ring)) || return false\n end","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":"Then check the point is inside the ring","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":" inside = false\n n = GI.npoint(ring)\n p_start = GI.getpoint(ring, 1)\n p_end = GI.getpoint(ring, n)","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":"Handle closed vs opne rings","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":" if GI.x(p_start) == GI.x(p_end) && GI.y(p_start) == GI.y(p_end)\n n -= 1\n end","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":"Loop over all points in the ring","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":" for i in 1:(n - 1)","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":"First point on edge","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":" p_i = GI.getpoint(ring, i)\n xi, yi = GI.x(p_i), GI.y(p_i)","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":"Second point on edge (j = i + 1)","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":" p_j = GI.getpoint(ring, i + 1)\n xj, yj = GI.x(p_j), GI.y(p_j)","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":"Check if point is on the ring boundary","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":" on_boundary = ( # vertex to point has same slope as edge\n yi * (xj - x) + yj * (x - xi) == y * (xj - xi) &&\n (xi - x) * (xj - x) <= 0 && # x is between xi and xj\n (yi - y) * (yj - y) <= 0 # y is between yi and yj\n )\n on_boundary && return !ignore_boundary","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":"Check if ray from point passes through edge","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":" intersects = (\n (yi > y) !== (yj > y) &&\n (x < (xj - xi) * (y - yi) / (yj - yi) + xi)\n )\n if intersects\n inside = !inside\n end\n end\n return inside\nend\n\nfunction point_in_extent(p, extent::Extents.Extent)\n (x1, x2), (y1, y1) = extent.X, extent.Y\n return x1 <= GI.x(p) && y1 <= GI.y(p) && x2 >= GI.x(p) && y2 >= GI.y(p)\nend\n\nline_on_line(line1, line2) = line_on_line(trait(line1), line1, trait(line2), line2)\nfunction line_on_line(t1::GI.AbstractCurveTrait, line1, t2::AbstractCurveTrait, line2)\n for p in GI.getpoint(line1)","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":"FIXME: all points being on the line doesn't actually mean the whole line is on the line...","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":" point_on_line(p, line2) || return false\n end\n return true\nend\n\nline_in_polygon(line, poly) = line_in_polygon(trait(line), line, trait(poly), poly)\n\nfunction line_in_polygon(\n ::AbstractCurveTrait, line,\n ::Union{AbstractPolygonTrait,LinearRingTrait}, poly\n)\n Extents.intersects(GI.extent(poly), GI.extent(line)) || return false\n\n inside = false\n for i in 1:GI.npoint(line) - 1\n p = GI.getpoint(line, i)\n p2 = GI.getpoint(line, i + 1)\n point_in_polygon(p, poly) || return false\n if !inside\n inside = point_in_polygon(p, poly; ignore_boundary=true)\n end","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":"FIXME This seems like a hack, we should check for intersections rather than midpoint??","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":" if !inside\n mid = ((GI.x(p) + GI.x(p2)) / 2, (GI.y(p) + GI.y(p2)) / 2)\n inside = point_in_polygon(mid, poly; ignore_boundary=true)\n end\n end\n return inside\nend\n\nfunction polygon_in_polygon(poly1, poly2)","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":"edges1, edges2 = toedges(poly1), toedges(poly2) extent1, extent2 = toextent(edges1), toextent(edges2) Check the extents intersect","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":" Extents.intersects(GI.extent(poly1), GI.extent(poly2)) || return false","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":"Check all points in poly1 are in poly2","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":" for point in GI.getpoint(poly1)\n point_in_polygon(point, poly2) || return false\n end","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":"Check the line of poly1 does not intersect the line of poly2","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":" #intersects(poly1, poly2) && return false","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":"poly1 must be in poly2","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":" return true\n end","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":"","category":"page"},{"location":"source/methods/bools/","page":"Boolean conditions","title":"Boolean conditions","text":"This page was generated using Literate.jl.","category":"page"},{"location":"source/methods/equals/#Equals","page":"Equals","title":"Equals","text":"","category":"section"},{"location":"source/methods/equals/","page":"Equals","title":"Equals","text":"export equals","category":"page"},{"location":"source/methods/equals/#What-is-equals?","page":"Equals","title":"What is equals?","text":"","category":"section"},{"location":"source/methods/equals/","page":"Equals","title":"Equals","text":"The equals function checks if two geometries are equal. They are equal if they share the same set of points and edges to define the same shape.","category":"page"},{"location":"source/methods/equals/","page":"Equals","title":"Equals","text":"To provide an example, consider these two lines:","category":"page"},{"location":"source/methods/equals/","page":"Equals","title":"Equals","text":"using GeometryOps\nusing GeometryOps.GeometryBasics\nusing Makie\nusing CairoMakie\n\nl1 = GI.LineString([(0.0, 0.0), (0.0, 10.0)])\nl2 = GI.LineString([(0.0, -10.0), (0.0, 3.0)])\nf, a, p = lines(GI.getpoint(l1), color = :blue)\nscatter!(GI.getpoint(l1), color = :blue)\nlines!(GI.getpoint(l2), color = :orange)\nscatter!(GI.getpoint(l2), color = :orange)","category":"page"},{"location":"source/methods/equals/","page":"Equals","title":"Equals","text":"We can see that the two lines do not share a commen set of points and edges in the plot, so they are not equal:","category":"page"},{"location":"source/methods/equals/","page":"Equals","title":"Equals","text":"equals(l1, l2) # returns false","category":"page"},{"location":"source/methods/equals/#Implementation","page":"Equals","title":"Implementation","text":"","category":"section"},{"location":"source/methods/equals/","page":"Equals","title":"Equals","text":"This is the GeoInterface-compatible implementation.","category":"page"},{"location":"source/methods/equals/","page":"Equals","title":"Equals","text":"First, we implement a wrapper method that dispatches to the correct implementation based on the geometry trait. This is also used in the implementation, since it's a lot less work!","category":"page"},{"location":"source/methods/equals/","page":"Equals","title":"Equals","text":"Note that while we need the same set of points and edges, they don't need to be provided in the same order for polygons. For for example, we need the same set points for two multipoints to be equal, but they don't have to be saved in the same order. The winding order also doesn't have to be the same to represent the same geometry. This requires checking every point against every other point in the two geometries we are comparing. Also, some geometries must be \"closed\" like polygons and linear rings. These will be assumed to be closed, even if they don't have a repeated last point explicity written in the coordinates. Additionally, geometries and multi-geometries can be equal if the multi-geometry only includes that single geometry.","category":"page"},{"location":"source/methods/equals/","page":"Equals","title":"Equals","text":"\"\"\"\n equals(geom1, geom2)::Bool\n\nCompare two Geometries return true if they are the same geometry.\n\n# Examples\n```jldoctest\nimport GeometryOps as GO, GeoInterface as GI\npoly1 = GI.Polygon([[(0,0), (0,5), (5,5), (5,0), (0,0)]])\npoly2 = GI.Polygon([[(0,0), (0,5), (5,5), (5,0), (0,0)]])\n\nGO.equals(poly1, poly2)","category":"page"},{"location":"source/methods/equals/","page":"Equals","title":"Equals","text":"output","category":"page"},{"location":"source/methods/equals/","page":"Equals","title":"Equals","text":"true\n```\n\"\"\"\nequals(geom_a, geom_b) = equals(\n GI.trait(geom_a), geom_a,\n GI.trait(geom_b), geom_b,\n)\n\n\"\"\"\n equals(::T, geom_a, ::T, geom_b)::Bool\n\nTwo geometries of the same type, which don't have a equals function to dispatch\noff of should throw an error.\n\"\"\"\nequals(::T, geom_a, ::T, geom_b) where T = error(\"Cant compare $T yet\")\n\n\"\"\"\n equals(trait_a, geom_a, trait_b, geom_b)\n\nTwo geometries which are not of the same type cannot be equal so they always\nreturn false.\n\"\"\"\nequals(trait_a, geom_a, trait_b, geom_b) = false\n\n\"\"\"\n equals(::GI.PointTrait, p1, ::GI.PointTrait, p2)::Bool\n\nTwo points are the same if they have the same x and y (and z if 3D) coordinates.\n\"\"\"\nfunction equals(::GI.PointTrait, p1, ::GI.PointTrait, p2)\n GI.ncoord(p1) == GI.ncoord(p2) || return false\n GI.x(p1) == GI.x(p2) || return false\n GI.y(p1) == GI.y(p2) || return false\n if GI.is3d(p1)\n GI.z(p1) == GI.z(p2) || return false\n end\n return true\nend\n\n\"\"\"\n equals(::GI.PointTrait, p1, ::GI.MultiPointTrait, mp2)::Bool\n\nA point and a multipoint are equal if the multipoint is composed of a single\npoint that is equivalent to the given point.\n\"\"\"\nfunction equals(::GI.PointTrait, p1, ::GI.MultiPointTrait, mp2)\n GI.npoint(mp2) == 1 || return false\n return equals(p1, GI.getpoint(mp2, 1))\nend\n\n\"\"\"\n equals(::GI.MultiPointTrait, mp1, ::GI.PointTrait, p2)::Bool\n\nA point and a multipoint are equal if the multipoint is composed of a single\npoint that is equivalent to the given point.\n\"\"\"\nequals(trait1::GI.MultiPointTrait, mp1, trait2::GI.PointTrait, p2) =\n equals(trait2, p2, trait1, mp1)\n\n\"\"\"\n equals(::GI.MultiPointTrait, mp1, ::GI.MultiPointTrait, mp2)::Bool\n\nTwo multipoints are equal if they share the same set of points.\n\"\"\"\nfunction equals(::GI.MultiPointTrait, mp1, ::GI.MultiPointTrait, mp2)\n GI.npoint(mp1) == GI.npoint(mp2) || return false\n for p1 in GI.getpoint(mp1)\n has_match = false # if point has a matching point in other multipoint\n for p2 in GI.getpoint(mp2)\n if equals(p1, p2)\n has_match = true\n break\n end\n end\n has_match || return false # if no matching point, can't be equal\n end\n return true # all points had a match\nend\n\n\"\"\"\n _equals_curves(c1, c2, closed_type1, closed_type2)::Bool\n\nTwo curves are equal if they share the same set of point, representing the same\ngeometry. Both curves must must be composed of the same set of points, however,\nthey do not have to wind in the same direction, or start on the same point to be\nequivalent.\nInputs:\n c1 first geometry\n c2 second geometry\n closed_type1::Bool true if c1 is closed by definition (polygon, linear ring)\n closed_type2::Bool true if c2 is closed by definition (polygon, linear ring)\n\"\"\"\nfunction _equals_curves(c1, c2, closed_type1, closed_type2)","category":"page"},{"location":"source/methods/equals/","page":"Equals","title":"Equals","text":"Check if both curves are closed or not","category":"page"},{"location":"source/methods/equals/","page":"Equals","title":"Equals","text":" n1 = GI.npoint(c1)\n n2 = GI.npoint(c2)\n c1_repeat_point = GI.getpoint(c1, 1) == GI.getpoint(c1, n1)\n n2 = GI.npoint(c2)\n c2_repeat_point = GI.getpoint(c2, 1) == GI.getpoint(c2, n2)\n closed1 = closed_type1 || c1_repeat_point\n closed2 = closed_type2 || c2_repeat_point\n closed1 == closed2 || return false","category":"page"},{"location":"source/methods/equals/","page":"Equals","title":"Equals","text":"How many points in each curve","category":"page"},{"location":"source/methods/equals/","page":"Equals","title":"Equals","text":" n1 -= c1_repeat_point ? 1 : 0\n n2 -= c2_repeat_point ? 1 : 0\n n1 == n2 || return false\n n1 == 0 && return true","category":"page"},{"location":"source/methods/equals/","page":"Equals","title":"Equals","text":"Find offset between curves","category":"page"},{"location":"source/methods/equals/","page":"Equals","title":"Equals","text":" jstart = nothing\n p1 = GI.getpoint(c1, 1)\n for i in 1:n2\n if equals(p1, GI.getpoint(c2, i))\n jstart = i\n break\n end\n end","category":"page"},{"location":"source/methods/equals/","page":"Equals","title":"Equals","text":"no point matches the first point","category":"page"},{"location":"source/methods/equals/","page":"Equals","title":"Equals","text":" isnothing(jstart) && return false","category":"page"},{"location":"source/methods/equals/","page":"Equals","title":"Equals","text":"found match for only point","category":"page"},{"location":"source/methods/equals/","page":"Equals","title":"Equals","text":" n1 == 1 && return true","category":"page"},{"location":"source/methods/equals/","page":"Equals","title":"Equals","text":"if isn't closed and first or last point don't match, not same curve","category":"page"},{"location":"source/methods/equals/","page":"Equals","title":"Equals","text":" !closed_type1 && (jstart != 1 && jstart != n1) && return false","category":"page"},{"location":"source/methods/equals/","page":"Equals","title":"Equals","text":"Check if curves are going in same direction","category":"page"},{"location":"source/methods/equals/","page":"Equals","title":"Equals","text":" i = 2\n j = jstart + 1\n j -= j > n2 ? n2 : 0\n same_direction = equals(GI.getpoint(c1, i), GI.getpoint(c2, j))","category":"page"},{"location":"source/methods/equals/","page":"Equals","title":"Equals","text":"if only 2 points, we have already compared both","category":"page"},{"location":"source/methods/equals/","page":"Equals","title":"Equals","text":" n1 == 2 && return same_direction","category":"page"},{"location":"source/methods/equals/","page":"Equals","title":"Equals","text":"Check all remaining points are the same wrapping around line","category":"page"},{"location":"source/methods/equals/","page":"Equals","title":"Equals","text":" jstep = same_direction ? 1 : -1\n for i in 2:n1\n ip = GI.getpoint(c1, i)\n j = jstart + (i - 1) * jstep\n j += (0 < j <= n2) ? 0 : (n2 * -jstep)\n jp = GI.getpoint(c2, j)\n equals(ip, jp) || return false\n end\n return true\nend\n\n\"\"\"\n equals(\n ::Union{GI.LineTrait, GI.LineStringTrait}, l1,\n ::Union{GI.LineTrait, GI.LineStringTrait}, l2,\n )::Bool\n\nTwo lines/linestrings are equal if they share the same set of points going\nalong the curve. Note that lines/linestrings aren't closed by defintion.\n\"\"\"\nequals(\n ::Union{GI.LineTrait, GI.LineStringTrait}, l1,\n ::Union{GI.LineTrait, GI.LineStringTrait}, l2,\n) = _equals_curves(l1, l2, false, false)\n\n\"\"\"\n equals(\n ::Union{GI.LineTrait, GI.LineStringTrait}, l1,\n ::GI.LinearRingTrait, l2,\n )::Bool\n\nA line/linestring and a linear ring are equal if they share the same set of\npoints going along the curve. Note that lines aren't closed by defintion, but\nrings are, so the line must have a repeated last point to be equal\n\"\"\"\nequals(\n ::Union{GI.LineTrait, GI.LineStringTrait}, l1,\n ::GI.LinearRingTrait, l2,\n) = _equals_curves(l1, l2, false, true)\n\n\"\"\"\n equals(\n ::GI.LinearRingTrait, l1,\n ::Union{GI.LineTrait, GI.LineStringTrait}, l2,\n )::Bool\n\nA linear ring and a line/linestring are equal if they share the same set of\npoints going along the curve. Note that lines aren't closed by defintion, but\nrings are, so the line must have a repeated last point to be equal\n\"\"\"\nequals(\n ::GI.LinearRingTrait, l1,\n ::Union{GI.LineTrait, GI.LineStringTrait}, l2,\n) = _equals_curves(l1, l2, true, false)\n\n\"\"\"\n equals(\n ::GI.LinearRingTrait, l1,\n ::GI.LinearRingTrait, l2,\n )::Bool\n\nTwo linear rings are equal if they share the same set of points going along the\ncurve. Note that rings are closed by definition, so they can have, but don't\nneed, a repeated last point to be equal.\n\"\"\"\nequals(\n ::GI.LinearRingTrait, l1,\n ::GI.LinearRingTrait, l2,\n) = _equals_curves(l1, l2, true, true)\n\n\"\"\"\n equals(::GI.PolygonTrait, geom_a, ::GI.PolygonTrait, geom_b)::Bool\n\nTwo polygons are equal if they share the same exterior edge and holes.\n\"\"\"\nfunction equals(::GI.PolygonTrait, geom_a, ::GI.PolygonTrait, geom_b)","category":"page"},{"location":"source/methods/equals/","page":"Equals","title":"Equals","text":"Check if exterior is equal","category":"page"},{"location":"source/methods/equals/","page":"Equals","title":"Equals","text":" _equals_curves(\n GI.getexterior(geom_a), GI.getexterior(geom_b),\n true, true, # linear rings are closed by definition\n ) || return false","category":"page"},{"location":"source/methods/equals/","page":"Equals","title":"Equals","text":"Check if number of holes are equal","category":"page"},{"location":"source/methods/equals/","page":"Equals","title":"Equals","text":" GI.nhole(geom_a) == GI.nhole(geom_b) || return false","category":"page"},{"location":"source/methods/equals/","page":"Equals","title":"Equals","text":"Check if holes are equal","category":"page"},{"location":"source/methods/equals/","page":"Equals","title":"Equals","text":" for ihole in GI.gethole(geom_a)\n has_match = false\n for jhole in GI.gethole(geom_b)\n if _equals_curves(\n ihole, jhole,\n true, true, # linear rings are closed by definition\n )\n has_match = true\n break\n end\n end\n has_match || return false\n end\n return true\nend\n\n\"\"\"\n equals(::GI.PolygonTrait, geom_a, ::GI.MultiPolygonTrait, geom_b)::Bool\n\nA polygon and a multipolygon are equal if the multipolygon is composed of a\nsingle polygon that is equivalent to the given polygon.\n\"\"\"\nfunction equals(::GI.PolygonTrait, geom_a, ::MultiPolygonTrait, geom_b)\n GI.npolygon(geom_b) == 1 || return false\n return equals(geom_a, GI.getpolygon(geom_b, 1))\nend\n\n\"\"\"\n equals(::GI.MultiPolygonTrait, geom_a, ::GI.PolygonTrait, geom_b)::Bool\n\nA polygon and a multipolygon are equal if the multipolygon is composed of a\nsingle polygon that is equivalent to the given polygon.\n\"\"\"\nequals(trait_a::GI.MultiPolygonTrait, geom_a, trait_b::PolygonTrait, geom_b) =\n equals(trait_b, geom_b, trait_a, geom_a)\n\n\"\"\"\n equals(::GI.PolygonTrait, geom_a, ::GI.PolygonTrait, geom_b)::Bool\n\nTwo multipolygons are equal if they share the same set of polygons.\n\"\"\"\nfunction equals(::GI.MultiPolygonTrait, geom_a, ::GI.MultiPolygonTrait, geom_b)","category":"page"},{"location":"source/methods/equals/","page":"Equals","title":"Equals","text":"Check if same number of polygons","category":"page"},{"location":"source/methods/equals/","page":"Equals","title":"Equals","text":" GI.npolygon(geom_a) == GI.npolygon(geom_b) || return false","category":"page"},{"location":"source/methods/equals/","page":"Equals","title":"Equals","text":"Check if each polygon has a matching polygon","category":"page"},{"location":"source/methods/equals/","page":"Equals","title":"Equals","text":" for poly_a in GI.getpolygon(geom_a)\n has_match = false\n for poly_b in GI.getpolygon(geom_b)\n if equals(poly_a, poly_b)\n has_match = true\n break\n end\n end\n has_match || return false\n end\n return true\nend","category":"page"},{"location":"source/methods/equals/","page":"Equals","title":"Equals","text":"","category":"page"},{"location":"source/methods/equals/","page":"Equals","title":"Equals","text":"This page was generated using Literate.jl.","category":"page"},{"location":"source/transformations/tuples/#Tuple-conversion","page":"Tuple conversion","title":"Tuple conversion","text":"","category":"section"},{"location":"source/transformations/tuples/","page":"Tuple conversion","title":"Tuple conversion","text":"\"\"\"\n tuples(obj)\n\nConvert all points in `obj` to `Tuple`s, wherever the are nested.\n\nReturns a similar object or collection of objects using GeoInterface.jl\ngeometries wrapping `Tuple` points.","category":"page"},{"location":"source/transformations/tuples/","page":"Tuple conversion","title":"Tuple conversion","text":"Keywords","category":"page"},{"location":"source/transformations/tuples/","page":"Tuple conversion","title":"Tuple conversion","text":"$APPLY_KEYWORDS\n\"\"\"\nfunction tuples(geom; kw...)\n if _is3d(geom)\n return apply(PointTrait, geom; kw...) do p\n (Float64(GI.x(p)), Float64(GI.y(p)), Float64(GI.z(p)))\n end\n else\n return apply(PointTrait, geom; kw...) do p\n (Float64(GI.x(p)), Float64(GI.y(p)))\n end\n end\nend","category":"page"},{"location":"source/transformations/tuples/","page":"Tuple conversion","title":"Tuple conversion","text":"","category":"page"},{"location":"source/transformations/tuples/","page":"Tuple conversion","title":"Tuple conversion","text":"This page was generated using Literate.jl.","category":"page"},{"location":"source/methods/area/#Area-and-signed-area","page":"Area and signed area","title":"Area and signed area","text":"","category":"section"},{"location":"source/methods/area/","page":"Area and signed area","title":"Area and signed area","text":"export area, signed_area","category":"page"},{"location":"source/methods/area/#What-is-area?-What-is-signed-area?","page":"Area and signed area","title":"What is area? What is signed area?","text":"","category":"section"},{"location":"source/methods/area/","page":"Area and signed area","title":"Area and signed area","text":"Area is the amount of space occupied by a two-dimensional figure. It is always a positive value. Signed area is simply the integral over the exterior path of a polygon, minus the sum of integrals over its interior holes. It is signed such that a clockwise path has a positive area, and a counterclockwise path has a negative area. The area is the absolute value of the signed area.","category":"page"},{"location":"source/methods/area/","page":"Area and signed area","title":"Area and signed area","text":"To provide an example, consider this rectangle:","category":"page"},{"location":"source/methods/area/","page":"Area and signed area","title":"Area and signed area","text":"using GeometryOps\nusing GeometryOps.GeometryBasics\nusing Makie\n\nrect = Polygon([Point(0,0), Point(0,1), Point(1,1), Point(1,0), Point(0, 0)])\nf, a, p = poly(rect; axis = (; aspect = DataAspect()))","category":"page"},{"location":"source/methods/area/","page":"Area and signed area","title":"Area and signed area","text":"This is clearly a rectangle, etc. But now let's look at how the points look:","category":"page"},{"location":"source/methods/area/","page":"Area and signed area","title":"Area and signed area","text":"lines!(a, rect; color = 1:length(coordinates(rect))+1)\nf","category":"page"},{"location":"source/methods/area/","page":"Area and signed area","title":"Area and signed area","text":"The points are ordered in a clockwise fashion, which means that the signed area is negative. If we reverse the order of the points, we get a postive area.","category":"page"},{"location":"source/methods/area/#Implementation","page":"Area and signed area","title":"Implementation","text":"","category":"section"},{"location":"source/methods/area/","page":"Area and signed area","title":"Area and signed area","text":"This is the GeoInterface-compatible implementation. First, we implement a wrapper method that dispatches to the correct implementation based on the geometry trait. This is also used in the implementation, since it's a lot less work!","category":"page"},{"location":"source/methods/area/","page":"Area and signed area","title":"Area and signed area","text":"Note that area (and signed area) are zero for all points and curves, even if the curves are closed like with a linear ring. Also note that signed area really only makes sense for polygons, given with a multipolygon can have several polygons each with a different orientation and thus the absolute value of the signed area might not be the area. This is why signed area is only implemented for polygons.","category":"page"},{"location":"source/methods/area/","page":"Area and signed area","title":"Area and signed area","text":"\"\"\"\n area(geom, ::Type{T} = Float64)::T\n\nReturns the area of the geometry. This is computed slighly differently for\ndifferent geometries:\n - The area of a point/multipoint is always zero.\n - The area of a curve/multicurve is always zero.\n - The area of a polygon is the absolute value of the signed area.\n - The area multi-polygon is the sum of the areas of all of the sub-polygons.\n - The area of a geometry collection is the sum of the areas of all of the\n sub-geometries.\n\nResult will be of type T, where T is an optional argument with a default value\nof Float64.\n\"\"\"\narea(geom, ::Type{T} = Float64) where T <: AbstractFloat =\n _area(T, GI.trait(geom), geom)\n\n\"\"\"\n signed_area(geom, ::Type{T} = Float64)::T\n\nReturns the signed area of the geometry, based on winding order. This is\ncomputed slighly differently for different geometries:\n - The signed area of a point is always zero.\n - The signed area of a curve is always zero.\n - The signed area of a polygon is computed with the shoelace formula and is\n positive if the polygon coordinates wind clockwise and negative if\n counterclockwise.\n - You cannot compute the signed area of a multipolygon as it doesn't have a\n meaning as each sub-polygon could have a different winding order.\n\nResult will be of type T, where T is an optional argument with a default value\nof Float64.\n\"\"\"\nsigned_area(geom, ::Type{T} = Float64) where T <: AbstractFloat =\n _signed_area(T, GI.trait(geom), geom)","category":"page"},{"location":"source/methods/area/","page":"Area and signed area","title":"Area and signed area","text":"Points, MultiPoints, Curves, MultiCurves","category":"page"},{"location":"source/methods/area/","page":"Area and signed area","title":"Area and signed area","text":"_area(::Type{T}, ::GI.AbstractGeometryTrait, geom) where T = zero(T)\n\n_signed_area(::Type{T}, ::GI.AbstractGeometryTrait, geom) where T = zero(T)","category":"page"},{"location":"source/methods/area/","page":"Area and signed area","title":"Area and signed area","text":"Polygons","category":"page"},{"location":"source/methods/area/","page":"Area and signed area","title":"Area and signed area","text":"_area(::Type{T}, trait::GI.PolygonTrait, poly) where T =\n abs(_signed_area(T, trait, poly))\n\nfunction _signed_area(::Type{T}, ::GI.PolygonTrait, poly) where T\n GI.isempty(poly) && return zero(T)\n s_area = _signed_area(T, GI.getexterior(poly))\n area = abs(s_area)\n area == 0 && return area","category":"page"},{"location":"source/methods/area/","page":"Area and signed area","title":"Area and signed area","text":"Remove hole areas from total","category":"page"},{"location":"source/methods/area/","page":"Area and signed area","title":"Area and signed area","text":" for hole in GI.gethole(poly)\n area -= abs(_signed_area(T, hole))\n end","category":"page"},{"location":"source/methods/area/","page":"Area and signed area","title":"Area and signed area","text":"Winding of exterior ring determines sign","category":"page"},{"location":"source/methods/area/","page":"Area and signed area","title":"Area and signed area","text":" return area * sign(s_area)\nend","category":"page"},{"location":"source/methods/area/#MultiPolygons-and-GeometryCollections","page":"Area and signed area","title":"MultiPolygons and GeometryCollections","text":"","category":"section"},{"location":"source/methods/area/","page":"Area and signed area","title":"Area and signed area","text":"_area(\n ::Type{T},\n ::Union{GI.MultiPolygonTrait, GI.GeometryCollectionTrait},\n geoms,\n) where T =\n sum((area(geom, T) for geom in GI.getgeom(geoms)), init = zero(T))","category":"page"},{"location":"source/methods/area/","page":"Area and signed area","title":"Area and signed area","text":"Helper function:","category":"page"},{"location":"source/methods/area/","page":"Area and signed area","title":"Area and signed area","text":"Calculates the signed area of a given curve. This is equivalent to integrating to find the area under the curve. Even if curve isn't explicitly closed by repeating the first point at the end of the coordinates, curve is still assumed to be closed.","category":"page"},{"location":"source/methods/area/","page":"Area and signed area","title":"Area and signed area","text":"function _signed_area(::Type{T}, geom) where T\n area = zero(T)","category":"page"},{"location":"source/methods/area/","page":"Area and signed area","title":"Area and signed area","text":"Close curve, even if last point isn't explicitly repeated","category":"page"},{"location":"source/methods/area/","page":"Area and signed area","title":"Area and signed area","text":" np = GI.npoint(geom)\n np == 0 && return area","category":"page"},{"location":"source/methods/area/","page":"Area and signed area","title":"Area and signed area","text":"Integrate the area under the curve","category":"page"},{"location":"source/methods/area/","page":"Area and signed area","title":"Area and signed area","text":" p1 = GI.getpoint(geom, np)\n for p2 in GI.getpoint(geom)","category":"page"},{"location":"source/methods/area/","page":"Area and signed area","title":"Area and signed area","text":"Accumulate the area into area","category":"page"},{"location":"source/methods/area/","page":"Area and signed area","title":"Area and signed area","text":" area += GI.x(p1) * GI.y(p2) - GI.y(p1) * GI.x(p2)\n p1 = p2\n end\n return T(area / 2)\nend","category":"page"},{"location":"source/methods/area/","page":"Area and signed area","title":"Area and signed area","text":"","category":"page"},{"location":"source/methods/area/","page":"Area and signed area","title":"Area and signed area","text":"This page was generated using Literate.jl.","category":"page"},{"location":"","page":"Home","title":"Home","text":"CurrentModule = GeometryOps","category":"page"},{"location":"#GeometryOps","page":"Home","title":"GeometryOps","text":"","category":"section"},{"location":"","page":"Home","title":"Home","text":"Documentation for GeometryOps.","category":"page"},{"location":"","page":"Home","title":"Home","text":"","category":"page"},{"location":"","page":"Home","title":"Home","text":"Modules = [GeometryOps]","category":"page"},{"location":"#GeometryOps.AbstractBarycentricCoordinateMethod","page":"Home","title":"GeometryOps.AbstractBarycentricCoordinateMethod","text":"abstract type AbstractBarycentricCoordinateMethod\n\nAbstract supertype for barycentric coordinate methods. The subtypes may serve as dispatch types, or may cache some information about the target polygon. \n\nAPI\n\nThe following methods must be implemented for all subtypes:\n\nbarycentric_coordinates!(λs::Vector{<: Real}, method::AbstractBarycentricCoordinateMethod, exterior::Vector{<: Point{2, T1}}, point::Point{2, T2})\nbarycentric_interpolate(method::AbstractBarycentricCoordinateMethod, exterior::Vector{<: Point{2, T1}}, values::Vector{V}, point::Point{2, T2})::V\nbarycentric_interpolate(method::AbstractBarycentricCoordinateMethod, exterior::Vector{<: Point{2, T1}}, interiors::Vector{<: Vector{<: Point{2, T1}}} values::Vector{V}, point::Point{2, T2})::V\n\nThe rest of the methods will be implemented in terms of these, and have efficient dispatches for broadcasting.\n\n\n\n\n\n","category":"type"},{"location":"#GeometryOps.DouglasPeucker","page":"Home","title":"GeometryOps.DouglasPeucker","text":"DouglasPeucker <: SimplifyAlg\n\nDouglasPeucker(; number, ratio, tol)\n\nSimplifies geometries by removing points below tol distance from the line between its neighboring points.\n\nKeywords\n\nratio: the fraction of points that should remain after simplify. Useful as it will generalise for large collections of objects.\nnumber: the number of points that should remain after simplify. Less useful for large collections of mixed size objects.\ntol: the minimum distance a point will be from the line joining its neighboring points.\n\n\n\n\n\n","category":"type"},{"location":"#GeometryOps.MeanValue","page":"Home","title":"GeometryOps.MeanValue","text":"MeanValue() <: AbstractBarycentricCoordinateMethod\n\nThis method calculates barycentric coordinates using the mean value method.\n\nReferences\n\n\n\n\n\n","category":"type"},{"location":"#GeometryOps.RadialDistance","page":"Home","title":"GeometryOps.RadialDistance","text":"RadialDistance <: SimplifyAlg\n\nSimplifies geometries by removing points less than tol distance from the line between its neighboring points.\n\nKeywords\n\nratio: the fraction of points that should remain after simplify. Useful as it will generalise for large collections of objects.\nnumber: the number of points that should remain after simplify. Less useful for large collections of mixed size objects.\ntol: the minimum distance between points.\n\n\n\n\n\n","category":"type"},{"location":"#GeometryOps.SimplifyAlg","page":"Home","title":"GeometryOps.SimplifyAlg","text":"abstract type SimplifyAlg\n\nAbstract type for simplification algorithms.\n\nAPI\n\nFor now, the algorithm must hold the number, ratio and tol properties. \n\nSimplification algorithm types can hook into the interface by implementing the _simplify(trait, alg, geom) methods for whichever traits are necessary.\n\n\n\n\n\n","category":"type"},{"location":"#GeometryOps.VisvalingamWhyatt","page":"Home","title":"GeometryOps.VisvalingamWhyatt","text":"VisvalingamWhyatt <: SimplifyAlg\n\nVisvalingamWhyatt(; kw...)\n\nSimplifies geometries by removing points below tol distance from the line between its neighboring points.\n\nKeywords\n\nratio: the fraction of points that should remain after simplify. Useful as it will generalise for large collections of objects.\nnumber: the number of points that should remain after simplify. Less useful for large collections of mixed size objects.\ntol: the minimum area of a triangle made with a point and its neighboring points.\n\n\n\n\n\n","category":"type"},{"location":"#GeometryOps._det-Union{Tuple{T2}, Tuple{T1}, Tuple{Union{Tuple{T1, T1}, StaticArraysCore.StaticArray{Tuple{2}, T1, 1}}, Union{Tuple{T2, T2}, StaticArraysCore.StaticArray{Tuple{2}, T2, 1}}}} where {T1<:Real, T2<:Real}","page":"Home","title":"GeometryOps._det","text":"_det(s1::Point2{T1}, s2::Point2{T2}) where {T1 <: Real, T2 <: Real}\n\nReturns the determinant of the matrix formed by hcat'ing two points s1 and s2.\n\nSpecifically, this is: \n\ns1[1] * s2[2] - s1[2] * s2[1]\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps._equals_curves-NTuple{4, Any}","page":"Home","title":"GeometryOps._equals_curves","text":"_equals_curves(c1, c2, closed_type1, closed_type2)::Bool\n\nTwo curves are equal if they share the same set of point, representing the same geometry. Both curves must must be composed of the same set of points, however, they do not have to wind in the same direction, or start on the same point to be equivalent. Inputs: c1 first geometry c2 second geometry closedtype1::Bool true if c1 is closed by definition (polygon, linear ring) closedtype2::Bool true if c2 is closed by definition (polygon, linear ring)\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps._intersection_point-Tuple{Tuple, Tuple}","page":"Home","title":"GeometryOps._intersection_point","text":"_intersection_point(\n (a1, a2)::Tuple,\n (b1, b2)::Tuple,\n)\n\nCalculates the intersection point between two lines if it exists, and as if the line extended to infinity, and the fractional component of each line from the initial end point to the intersection point. Inputs: (a1, a2)::Tuple{Tuple{::Real, ::Real}, Tuple{::Real, ::Real}} first line (b1, b2)::Tuple{Tuple{::Real, ::Real}, Tuple{::Real, ::Real}} second line Outputs: (x, y)::Tuple{::Real, ::Real} intersection point (t, u)::Tuple{::Real, ::Real} fractional length of lines to intersection Both are ::Nothing if point doesn't exist!\n\nCalculation derivation can be found here: https://stackoverflow.com/questions/563198/\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps._line_intersects-Tuple{Tuple{Tuple{Float64, Float64}, Tuple{Float64, Float64}}, Tuple{Tuple{Float64, Float64}, Tuple{Float64, Float64}}}","page":"Home","title":"GeometryOps._line_intersects","text":"_line_intersects(\n edge_a::Edge,\n edge_b::Edge,\n)::Bool\n\nReturns true if there is at least one intersection between two edges.\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps._line_intersects-Tuple{Vector{Tuple{Tuple{Float64, Float64}, Tuple{Float64, Float64}}}, Vector{Tuple{Tuple{Float64, Float64}, Tuple{Float64, Float64}}}}","page":"Home","title":"GeometryOps._line_intersects","text":"_line_intersects(\n edges_a::Vector{Edge},\n edges_b::Vector{Edge}\n)::Bool\n\nReturns true if there is at least one intersection between edges within the two lists of edges.\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps._overlaps-Tuple{Tuple{Tuple{Float64, Float64}, Tuple{Float64, Float64}}, Tuple{Tuple{Float64, Float64}, Tuple{Float64, Float64}}}","page":"Home","title":"GeometryOps._overlaps","text":"_overlaps(\n (a1, a2)::Edge,\n (b1, b2)::Edge\n)::Bool\n\nIf the edges overlap, meaning that they are colinear but each have one endpoint outside of the other edge, return true. Else false. \n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.apply-Union{Tuple{Target}, Tuple{Any, Type{Target}, Any}} where Target","page":"Home","title":"GeometryOps.apply","text":"apply(f, target::Type{<:AbstractTrait}, obj; kw...)\n\nReconstruct a geometry, feature, feature collection, or nested vectors of either using the function f on the target trait.\n\nf(target_geom) => x where x also has the target trait, or a trait that can be substituted. For example, swapping PolgonTrait to MultiPointTrait will fail if the outer object has MultiPolygonTrait, but should work if it has FeatureTrait.\n\nObjects \"shallower\" than the target trait are always completely rebuilt, like a Vector of FeatureCollectionTrait of FeatureTrait when the target has PolygonTrait and is held in the features. But \"deeper\" objects may remain unchanged - such as points and linear rings if the target is the same PolygonTrait.\n\nThe result is a functionally similar geometry with values depending on f\n\nthreaded: true or false. Whether to use multithreading. Defaults to false.\ncrs: The CRS to attach to geometries. Defaults to nothing.\ncalc_extent: true or false. Whether to calculate the extent. Defaults to false.\n\nExample\n\nFlipped point the order in any feature or geometry, or iterables of either:\n\n```juia import GeoInterface as GI import GeometryOps as GO geom = GI.Polygon([GI.LinearRing([(1, 2), (3, 4), (5, 6), (1, 2)]), GI.LinearRing([(3, 4), (5, 6), (6, 7), (3, 4)])])\n\nflipped_geom = GO.apply(GI.PointTrait, geom) do p (GI.y(p), GI.x(p)) end\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.area-Union{Tuple{Any}, Tuple{T}, Tuple{Any, Type{T}}} where T<:AbstractFloat","page":"Home","title":"GeometryOps.area","text":"area(geom, ::Type{T} = Float64)::T\n\nReturns the area of the geometry. This is computed slighly differently for different geometries: - The area of a point/multipoint is always zero. - The area of a curve/multicurve is always zero. - The area of a polygon is the absolute value of the signed area. - The area multi-polygon is the sum of the areas of all of the sub-polygons. - The area of a geometry collection is the sum of the areas of all of the sub-geometries. \n\nResult will be of type T, where T is an optional argument with a default value of Float64.\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.centroid-Tuple{Any, Any}","page":"Home","title":"GeometryOps.centroid","text":"centroid(trait, geom)::Tuple{T, T}\n\nReturns the centroid of a polygon or multipolygon, which is calculated by weighting edges by their area component by convention.\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.centroid-Tuple{Any}","page":"Home","title":"GeometryOps.centroid","text":"centroid(geom)::Tuple{T, T}\n\nReturns the centroid of a given line segment, linear ring, polygon, or mutlipolygon.\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.centroid-Tuple{Union{GeoInterface.LineStringTrait, GeoInterface.LinearRingTrait}, Any}","page":"Home","title":"GeometryOps.centroid","text":"centroid(\n trait::Union{GI.LineStringTrait, GI.LinearRingTrait},\n geom,\n)::Tuple{T, T}\n\nReturns the centroid of a line string or linear ring, which is calculated by weighting line segments by their length by convention.\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.centroid_and_area-Tuple{Any}","page":"Home","title":"GeometryOps.centroid_and_area","text":"centroid_and_area(\n ::Union{GI.LineStringTrait, GI.LinearRingTrait}, \n geom,\n)::(::Tuple{T, T}, ::Real)\n\nReturns the centroid and area of a given geom.\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.centroid_and_area-Tuple{GeoInterface.MultiPolygonTrait, Any}","page":"Home","title":"GeometryOps.centroid_and_area","text":"centroid_and_area(::GI.MultiPolygonTrait, geom)::(::Tuple{T, T}, ::Real)\n\nReturns the centroid and area of a given multipolygon.\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.centroid_and_area-Tuple{GeoInterface.PolygonTrait, Any}","page":"Home","title":"GeometryOps.centroid_and_area","text":"centroid_and_area(::GI.PolygonTrait, geom)::(::Tuple{T, T}, ::Real)\n\nReturns the centroid and area of a given polygon.\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.centroid_and_area-Tuple{Union{GeoInterface.LineStringTrait, GeoInterface.LinearRingTrait}, Any}","page":"Home","title":"GeometryOps.centroid_and_area","text":"centroid_and_area(\n ::Union{GI.LineStringTrait, GI.LinearRingTrait},\n geom,\n)::(::Tuple{T, T}, ::Real)\n\nReturns the centroid and area of a given a line string or a linear ring. Note that this is only valid if the line segment or linear ring is closed. \n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.centroid_and_length-Tuple{Any}","page":"Home","title":"GeometryOps.centroid_and_length","text":"centroid_and_length(geom)::(::Tuple{T, T}, ::Real)\n\nReturns the centroid and length of a given line/ring. Note this is only valid for line strings and linear rings.\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.centroid_and_length-Tuple{Union{GeoInterface.LineStringTrait, GeoInterface.LinearRingTrait}, Any}","page":"Home","title":"GeometryOps.centroid_and_length","text":"centroid_and_length(geom)::(::Tuple{T, T}, ::Real)\n\nReturns the centroid and length of a given line/ring. Note this is only valid for line strings and linear rings.\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.contains-Tuple{Any, Any}","page":"Home","title":"GeometryOps.contains","text":"contains(ft1::AbstractGeometry, ft2::AbstractGeometry)::Bool\n\nReturn true if the second geometry is completely contained by the first geometry. The interiors of both geometries must intersect and, the interior and boundary of the secondary (geometry b) must not intersect the exterior of the primary (geometry a). contains returns the exact opposite result of within.\n\nExamples\n\nimport GeometryOps as GO, GeoInterface as GI\nline = GI.LineString([(1, 1), (1, 2), (1, 3), (1, 4)])\npoint = (1, 2)\n\nGO.contains(line, point)\n# output\ntrue\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.crosses-Tuple{Any, Any}","page":"Home","title":"GeometryOps.crosses","text":" crosses(geom1, geom2)::Bool\n\nReturn true if the intersection results in a geometry whose dimension is one less than the maximum dimension of the two source geometries and the intersection set is interior to both source geometries.\n\nTODO: broken\n\nExamples\n\nimport GeoInterface as GI, GeometryOps as GO\n\nline1 = GI.LineString([(1, 1), (1, 2), (1, 3), (1, 4)])\nline2 = GI.LineString([(-2, 2), (4, 2)])\n\nGO.crosses(line1, line2)\n# output\ntrue\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.disjoint-Tuple{Any, Any}","page":"Home","title":"GeometryOps.disjoint","text":"disjoint(geom1, geom2)::Bool\n\nReturn true if the intersection of the two geometries is an empty set.\n\nExamples\n\nimport GeometryOps as GO, GeoInterface as GI\n\npoly = GI.Polygon([[(-1, 2), (3, 2), (3, 3), (-1, 3), (-1, 2)]])\npoint = (1, 1)\nGO.disjoint(poly, point)\n\n# output\ntrue\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.distance-Union{Tuple{T}, Tuple{Any, Any}, Tuple{Any, Any, Type{T}}} where T<:AbstractFloat","page":"Home","title":"GeometryOps.distance","text":"distance(point, geom, ::Type{T} = Float64)::T\n\nCalculates the ditance from the geometry g1 to the point. The distance will always be positive or zero.\n\nThe method will differ based on the type of the geometry provided: - The distance from a point to a point is just the Euclidean distance between the points. - The distance from a point to a line is the minimum distance from the point to the closest point on the given line. - The distance from a point to a linestring is the minimum distance from the point to the closest segment of the linestring. - The distance from a point to a linear ring is the minimum distance from the point to the closest segment of the linear ring. - The distance from a point to a polygon is zero if the point is within the polygon and otherwise is the minimum distance from the point to an edge of the polygon. This includes edges created by holes. - The distance from a point to a multigeometry or a geometry collection is the minimum distance between the point and any of the sub-geometries.\n\nResult will be of type T, where T is an optional argument with a default value of Float64.\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.embed_extent-Tuple{Any}","page":"Home","title":"GeometryOps.embed_extent","text":"embed_extent(obj)\n\nRecursively wrap the object with a GeoInterface.jl geometry, calculating and adding an Extents.Extent to all objects.\n\nThis can improve performance when extents need to be checked multiple times, such when needing to check if many points are in geometries, and using their extents as a quick filter for obviously exterior points.\n\nKeywords\n\nthreaded: true or false. Whether to use multithreading. Defaults to false.\ncrs: The CRS to attach to geometries. Defaults to nothing.\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.equals-NTuple{4, Any}","page":"Home","title":"GeometryOps.equals","text":"equals(trait_a, geom_a, trait_b, geom_b)\n\nTwo geometries which are not of the same type cannot be equal so they always return false.\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.equals-Tuple{Any, Any}","page":"Home","title":"GeometryOps.equals","text":"equals(geom1, geom2)::Bool\n\nCompare two Geometries return true if they are the same geometry.\n\nExamples\n\nimport GeometryOps as GO, GeoInterface as GI\npoly1 = GI.Polygon([[(0,0), (0,5), (5,5), (5,0), (0,0)]])\npoly2 = GI.Polygon([[(0,0), (0,5), (5,5), (5,0), (0,0)]])\n\nGO.equals(poly1, poly2)\n# output\ntrue\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.equals-Tuple{GeoInterface.LinearRingTrait, Any, GeoInterface.LinearRingTrait, Any}","page":"Home","title":"GeometryOps.equals","text":"equals(\n ::GI.LinearRingTrait, l1,\n ::GI.LinearRingTrait, l2,\n)::Bool\n\nTwo linear rings are equal if they share the same set of points going along the curve. Note that rings are closed by definition, so they can have, but don't need, a repeated last point to be equal.\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.equals-Tuple{GeoInterface.LinearRingTrait, Any, Union{GeoInterface.LineStringTrait, GeoInterface.LineTrait}, Any}","page":"Home","title":"GeometryOps.equals","text":"equals(\n ::GI.LinearRingTrait, l1,\n ::Union{GI.LineTrait, GI.LineStringTrait}, l2,\n)::Bool\n\nA linear ring and a line/linestring are equal if they share the same set of points going along the curve. Note that lines aren't closed by defintion, but rings are, so the line must have a repeated last point to be equal\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.equals-Tuple{GeoInterface.MultiPointTrait, Any, GeoInterface.MultiPointTrait, Any}","page":"Home","title":"GeometryOps.equals","text":"equals(::GI.MultiPointTrait, mp1, ::GI.MultiPointTrait, mp2)::Bool\n\nTwo multipoints are equal if they share the same set of points.\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.equals-Tuple{GeoInterface.MultiPointTrait, Any, GeoInterface.PointTrait, Any}","page":"Home","title":"GeometryOps.equals","text":"equals(::GI.MultiPointTrait, mp1, ::GI.PointTrait, p2)::Bool\n\nA point and a multipoint are equal if the multipoint is composed of a single point that is equivalent to the given point.\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.equals-Tuple{GeoInterface.MultiPolygonTrait, Any, GeoInterface.MultiPolygonTrait, Any}","page":"Home","title":"GeometryOps.equals","text":"equals(::GI.PolygonTrait, geom_a, ::GI.PolygonTrait, geom_b)::Bool\n\nTwo multipolygons are equal if they share the same set of polygons.\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.equals-Tuple{GeoInterface.MultiPolygonTrait, Any, GeoInterface.PolygonTrait, Any}","page":"Home","title":"GeometryOps.equals","text":"equals(::GI.MultiPolygonTrait, geom_a, ::GI.PolygonTrait, geom_b)::Bool\n\nA polygon and a multipolygon are equal if the multipolygon is composed of a single polygon that is equivalent to the given polygon.\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.equals-Tuple{GeoInterface.PointTrait, Any, GeoInterface.MultiPointTrait, Any}","page":"Home","title":"GeometryOps.equals","text":"equals(::GI.PointTrait, p1, ::GI.MultiPointTrait, mp2)::Bool\n\nA point and a multipoint are equal if the multipoint is composed of a single point that is equivalent to the given point.\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.equals-Tuple{GeoInterface.PointTrait, Any, GeoInterface.PointTrait, Any}","page":"Home","title":"GeometryOps.equals","text":"equals(::GI.PointTrait, p1, ::GI.PointTrait, p2)::Bool\n\nTwo points are the same if they have the same x and y (and z if 3D) coordinates.\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.equals-Tuple{GeoInterface.PolygonTrait, Any, GeoInterface.MultiPolygonTrait, Any}","page":"Home","title":"GeometryOps.equals","text":"equals(::GI.PolygonTrait, geom_a, ::GI.MultiPolygonTrait, geom_b)::Bool\n\nA polygon and a multipolygon are equal if the multipolygon is composed of a single polygon that is equivalent to the given polygon.\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.equals-Tuple{GeoInterface.PolygonTrait, Any, GeoInterface.PolygonTrait, Any}","page":"Home","title":"GeometryOps.equals","text":"equals(::GI.PolygonTrait, geom_a, ::GI.PolygonTrait, geom_b)::Bool\n\nTwo polygons are equal if they share the same exterior edge and holes.\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.equals-Tuple{Union{GeoInterface.LineStringTrait, GeoInterface.LineTrait}, Any, GeoInterface.LinearRingTrait, Any}","page":"Home","title":"GeometryOps.equals","text":"equals(\n ::Union{GI.LineTrait, GI.LineStringTrait}, l1,\n ::GI.LinearRingTrait, l2,\n)::Bool\n\nA line/linestring and a linear ring are equal if they share the same set of points going along the curve. Note that lines aren't closed by defintion, but rings are, so the line must have a repeated last point to be equal\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.equals-Tuple{Union{GeoInterface.LineStringTrait, GeoInterface.LineTrait}, Any, Union{GeoInterface.LineStringTrait, GeoInterface.LineTrait}, Any}","page":"Home","title":"GeometryOps.equals","text":"equals(\n ::Union{GI.LineTrait, GI.LineStringTrait}, l1,\n ::Union{GI.LineTrait, GI.LineStringTrait}, l2,\n)::Bool\n\nTwo lines/linestrings are equal if they share the same set of points going along the curve. Note that lines/linestrings aren't closed by defintion.\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.equals-Union{Tuple{T}, Tuple{T, Any, T, Any}} where T","page":"Home","title":"GeometryOps.equals","text":"equals(::T, geom_a, ::T, geom_b)::Bool\n\nTwo geometries of the same type, which don't have a equals function to dispatch off of should throw an error.\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.flatten-Union{Tuple{Target}, Tuple{Type{Target}, Any}} where Target<:GeoInterface.AbstractTrait","page":"Home","title":"GeometryOps.flatten","text":"flatten(target::Type{<:GI.AbstractTrait}, obj)\nflatten(f, target::Type{<:GI.AbstractTrait}, obj)\n\nLazily flatten any AbstractArray, iterator, FeatureCollectionTrait, FeatureTrait or AbstractGeometryTrait object obj, so that objects with the target trait are returned by the iterator.\n\nIf f is passed in it will be applied to the target geometries.\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.flip-Tuple{Any}","page":"Home","title":"GeometryOps.flip","text":"flip(obj)\n\nSwap all of the x and y coordinates in obj, otherwise keeping the original structure (but not necessarily the original type).\n\nKeywords\n\nthreaded: true or false. Whether to use multithreading. Defaults to false.\ncrs: The CRS to attach to geometries. Defaults to nothing.\ncalc_extent: true or false. Whether to calculate the extent. Defaults to false.\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.get_contours-Tuple{AbstractMatrix}","page":"Home","title":"GeometryOps.get_contours","text":"get_contours(A::AbstractMatrix)\n\nReturns contours as vectors of CartesianIndex.\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.intersection-Tuple{Any, Any}","page":"Home","title":"GeometryOps.intersection","text":"intersection(geom_a, geom_b)::Union{Tuple{::Real, ::Real}, ::Nothing}\n\nReturn an intersection point between two geometries. Return nothing if none are found. Else, the return type depends on the input. It will be a union between: a point, a line, a linear ring, a polygon, or a multipolygon\n\nExample\n\nimport GeoInterface as GI, GeometryOps as GO\n\nline1 = GI.Line([(124.584961,-12.768946), (126.738281,-17.224758)])\nline2 = GI.Line([(123.354492,-15.961329), (127.22168,-14.008696)])\nGO.intersection(line1, line2)\n\n# output\n(125.58375366067547, -14.83572303404496)\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.intersection-Tuple{GeoInterface.AbstractTrait, Any, GeoInterface.AbstractTrait, Any}","page":"Home","title":"GeometryOps.intersection","text":"intersection(\n ::GI.AbstractTrait, geom_a,\n ::GI.AbstractTrait, geom_b,\n)::Union{\n ::Vector{Vector{Tuple{::Real, ::Real}}}, # is this a good return type?\n ::Nothing\n}\n\nCalculates the intersection between two line segments. Return nothing if there isn't one.\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.intersection-Tuple{GeoInterface.LineTrait, Any, GeoInterface.LineTrait, Any}","page":"Home","title":"GeometryOps.intersection","text":"intersection(\n ::GI.LineTrait, line_a,\n ::GI.LineTrait, line_b,\n)::Union{\n ::Tuple{::Real, ::Real},\n ::Nothing\n}\n\nCalculates the intersection between two line segments. Return nothing if there isn't one.\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.intersection-Tuple{GeoInterface.PolygonTrait, Any, GeoInterface.PolygonTrait, Any}","page":"Home","title":"GeometryOps.intersection","text":"intersection(\n ::GI.PolygonTrait, poly_a,\n ::GI.PolygonTrait, poly_b,\n)::Union{\n ::Vector{Vector{Tuple{::Real, ::Real}}}, # is this a good return type?\n ::Nothing\n}\n\nCalculates the intersection between two line segments. Return nothing if there isn't one.\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.intersection_points-Tuple{Any, Any}","page":"Home","title":"GeometryOps.intersection_points","text":"intersection_points(\n geom_a,\n geom_b,\n)::Union{\n ::Vector{::Tuple{::Real, ::Real}},\n ::Nothing,\n}\n\nReturn a list of intersection points between two geometries. If no intersection point was possible given geometry extents, return nothing. If none are found, return an empty list.\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.intersection_points-Tuple{GeoInterface.AbstractTrait, Any, GeoInterface.AbstractTrait, Any}","page":"Home","title":"GeometryOps.intersection_points","text":"intersection_points(\n ::GI.AbstractTrait, geom_a,\n ::GI.AbstractTrait, geom_b,\n)::Union{\n ::Vector{::Tuple{::Real, ::Real}},\n ::Nothing,\n}\n\nCalculates the list of intersection points between two geometries, inlcuding line segments, line strings, linear rings, polygons, and multipolygons. If no intersection points were possible given geometry extents, return nothing. If none are found, return an empty list.\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.intersects-Tuple{Any, Any}","page":"Home","title":"GeometryOps.intersects","text":"intersects(geom1, geom2)::Bool\n\nCheck if two geometries intersect, returning true if so and false otherwise.\n\nExample\n\nimport GeoInterface as GI, GeometryOps as GO\n\nline1 = GI.Line([(124.584961,-12.768946), (126.738281,-17.224758)])\nline2 = GI.Line([(123.354492,-15.961329), (127.22168,-14.008696)])\nGO.intersects(line1, line2)\n\n# output\ntrue\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.intersects-Tuple{GeoInterface.AbstractTrait, Any, GeoInterface.AbstractTrait, Any}","page":"Home","title":"GeometryOps.intersects","text":"intersects(::GI.AbstractTrait, a, ::GI.AbstractTrait, b)::Bool\n\nReturns true if two geometries intersect with one another and false otherwise. For all geometries but lines, convert the geometry to a list of edges and cross compare the edges for intersections.\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.intersects-Tuple{GeoInterface.LineTrait, Any, GeoInterface.LineTrait, Any}","page":"Home","title":"GeometryOps.intersects","text":"intersects(::GI.LineTrait, a, ::GI.LineTrait, b)::Bool\n\nReturns true if two line segments intersect and false otherwise.\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.isclockwise-Tuple{Any}","page":"Home","title":"GeometryOps.isclockwise","text":"isclockwise(line::Union{LineString, Vector{Position}})::Bool\n\nTake a ring and return true or false whether or not the ring is clockwise or counter-clockwise.\n\nExample\n\nimport GeoInterface as GI, GeometryOps as GO\n\nring = GI.LinearRing([(0, 0), (1, 1), (1, 0), (0, 0)])\nGO.isclockwise(ring)\n\n# output\ntrue\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.isconcave-Tuple{Any}","page":"Home","title":"GeometryOps.isconcave","text":"isconcave(poly::Polygon)::Bool\n\nTake a polygon and return true or false as to whether it is concave or not.\n\nExamples\n\nimport GeoInterface as GI, GeometryOps as GO\n\npoly = GI.Polygon([[(0, 0), (0, 1), (1, 1), (1, 0), (0, 0)]])\nGO.isconcave(poly)\n\n# output\nfalse\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.overlaps-Tuple{Any, Any}","page":"Home","title":"GeometryOps.overlaps","text":"overlaps(geom1, geom2)::Bool\n\nCompare two Geometries of the same dimension and return true if their intersection set results in a geometry different from both but of the same dimension. This means one geometry cannot be within or contain the other and they cannot be equal\n\nExamples\n\nimport GeometryOps as GO, GeoInterface as GI\npoly1 = GI.Polygon([[(0,0), (0,5), (5,5), (5,0), (0,0)]])\npoly2 = GI.Polygon([[(1,1), (1,6), (6,6), (6,1), (1,1)]])\n\nGO.overlaps(poly1, poly2)\n# output\ntrue\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.overlaps-Tuple{GeoInterface.AbstractTrait, Any, GeoInterface.AbstractTrait, Any}","page":"Home","title":"GeometryOps.overlaps","text":"overlaps(::GI.AbstractTrait, geom1, ::GI.AbstractTrait, geom2)::Bool\n\nFor any non-specified pair, all have non-matching dimensions, return false.\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.overlaps-Tuple{GeoInterface.LineTrait, Any, GeoInterface.LineTrait, Any}","page":"Home","title":"GeometryOps.overlaps","text":"overlaps(::GI.LineTrait, line1, ::GI.LineTrait, line)::Bool\n\nIf the lines overlap, meaning that they are colinear but each have one endpoint outside of the other line, return true. Else false.\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.overlaps-Tuple{GeoInterface.MultiPointTrait, Any, GeoInterface.MultiPointTrait, Any}","page":"Home","title":"GeometryOps.overlaps","text":"overlaps(\n ::GI.MultiPointTrait, points1,\n ::GI.MultiPointTrait, points2,\n)::Bool\n\nIf the multipoints overlap, meaning some, but not all, of the points within the multipoints are shared, return true.\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.overlaps-Tuple{GeoInterface.MultiPolygonTrait, Any, GeoInterface.MultiPolygonTrait, Any}","page":"Home","title":"GeometryOps.overlaps","text":"overlaps(\n ::GI.MultiPolygonTrait, polys1,\n ::GI.MultiPolygonTrait, polys2,\n)::Bool\n\nReturn true if at least one pair of polygons from multipolygons overlap. Else false.\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.overlaps-Tuple{GeoInterface.MultiPolygonTrait, Any, GeoInterface.PolygonTrait, Any}","page":"Home","title":"GeometryOps.overlaps","text":"overlaps(\n ::GI.MultiPolygonTrait, polys1,\n ::GI.PolygonTrait, poly2,\n)::Bool\n\nReturn true if polygon overlaps with at least one of the polygons within the multipolygon. Else false.\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.overlaps-Tuple{GeoInterface.PolygonTrait, Any, GeoInterface.MultiPolygonTrait, Any}","page":"Home","title":"GeometryOps.overlaps","text":"overlaps(\n ::GI.PolygonTrait, poly1,\n ::GI.MultiPolygonTrait, polys2,\n)::Bool\n\nReturn true if polygon overlaps with at least one of the polygons within the multipolygon. Else false.\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.overlaps-Tuple{GeoInterface.PolygonTrait, Any, GeoInterface.PolygonTrait, Any}","page":"Home","title":"GeometryOps.overlaps","text":"overlaps(\n trait_a::GI.PolygonTrait, poly_a,\n trait_b::GI.PolygonTrait, poly_b,\n)::Bool\n\nIf the two polygons intersect with one another, but are not equal, return true. Else false.\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.overlaps-Tuple{Union{GeoInterface.LineStringTrait, GeoInterface.Wrappers.LinearRing}, Any, Union{GeoInterface.LineStringTrait, GeoInterface.Wrappers.LinearRing}, Any}","page":"Home","title":"GeometryOps.overlaps","text":"overlaps(\n ::Union{GI.LineStringTrait, GI.LinearRing}, line1,\n ::Union{GI.LineStringTrait, GI.LinearRing}, line2,\n)::Bool\n\nIf the curves overlap, meaning that at least one edge of each curve overlaps, return true. Else false.\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.point_in_polygon-Tuple{Any, Any}","page":"Home","title":"GeometryOps.point_in_polygon","text":"point_in_polygon(point::Point, polygon::Union{Polygon, MultiPolygon}, ignore_boundary::Bool=false)::Bool\n\nTake a Point and a Polygon and determine if the point resides inside the polygon. The polygon can be convex or concave. The function accounts for holes.\n\nExamples\n\nimport GeoInterface as GI, GeometryOps as GO\n\npoint = (-77.0, 44.0)\npoly = GI.Polygon([[(-81, 41), (-81, 47), (-72, 47), (-72, 41), (-81, 41)]])\nGO.point_in_polygon(point, poly)\n\n# output\ntrue\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.point_on_line-Tuple{Any, Any}","page":"Home","title":"GeometryOps.point_on_line","text":"point_on_line(point::Point, line::LineString; ignore_end_vertices::Bool=false)::Bool\n\nReturn true if a point is on a line. Accept a optional parameter to ignore the start and end vertices of the linestring.\n\nExamples\n\nimport GeoInterface as GI, GeometryOps as GO\n\npoint = (1, 1)\nline = GI.LineString([(0, 0), (3, 3), (4, 4)])\nGO.point_on_line(point, line)\n\n# output\ntrue\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.polygon_to_line-Tuple{Any}","page":"Home","title":"GeometryOps.polygon_to_line","text":"polygon_to_line(poly::Polygon)\n\nConverts a Polygon to LineString or MultiLineString\n\nExamples\n\nimport GeometryOps as GO, GeoInterface as GI\n\npoly = GI.Polygon([[(-2.275543, 53.464547), (-2.275543, 53.489271), (-2.215118, 53.489271), (-2.215118, 53.464547), (-2.275543, 53.464547)]])\nGO.polygon_to_line(poly)\n# output\nGeoInterface.Wrappers.LineString{false, false, Vector{Tuple{Float64, Float64}}, Nothing, Nothing}([(-2.275543, 53.464547), (-2.275543, 53.489271), (-2.215118, 53.489271), (-2.215118, 53.464547), (-2.275543, 53.464547)], nothing, nothing)\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.polygonize-Tuple{AbstractMatrix}","page":"Home","title":"GeometryOps.polygonize","text":"polygonize(A; minpoints=10)\npolygonize(xs, ys, A; minpoints=10)\n\nConvert matrix A to polygons.\n\nIf xs and ys are passed in they are used as the pixel center points.\n\nKeywords\n\nminpoints: ignore polygons with less than minpoints points. \n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.rebuild-Tuple{Any, Any}","page":"Home","title":"GeometryOps.rebuild","text":"rebuild(geom, child_geoms)\n\nRebuild a geometry from child geometries.\n\nBy default geometries will be rebuilt as a GeoInterface.Wrappers geometry, but rebuild can have methods added to it to dispatch on geometries from other packages and specify how to rebuild them.\n\n(Maybe it should go into GeoInterface.jl)\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.reconstruct-Tuple{Any, Any}","page":"Home","title":"GeometryOps.reconstruct","text":"reconstruct(geom, components)\n\nReconstruct geom from an iterable of component objects that match its structure.\n\nAll objects in components must have the same GeoInterface.trait.\n\nUsusally used in combination with flatten.\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.reproject-Tuple{Any}","page":"Home","title":"GeometryOps.reproject","text":"reproject(geometry; source_crs, target_crs, transform, always_xy, time)\nreproject(geometry, source_crs, target_crs; always_xy, time)\nreproject(geometry, transform; always_xy, time)\n\nReproject any GeoInterface.jl compatible geometry from source_crs to target_crs.\n\nThe returned object will be constructed from GeoInterface.WrapperGeometry geometries, wrapping views of a Vector{Proj.Point{D}}, where D is the dimension.\n\nArguments\n\ngeometry: Any GeoInterface.jl compatible geometries.\nsource_crs: the source coordinate referece system, as a GeoFormatTypes.jl object or a string.\ntarget_crs: the target coordinate referece system, as a GeoFormatTypes.jl object or a string.\n\nIf these a passed as keywords, transform will take priority. Without it target_crs is always needed, and source_crs is needed if it is not retreivable from the geometry with GeoInterface.crs(geometry).\n\nKeywords\n\nalways_xy: force x, y coordinate order, true by default. false will expect and return points in the crs coordinate order.\ntime: the time for the coordinates. Inf by default.\nthreaded: true or false. Whether to use multithreading. Defaults to false.\ncrs: The CRS to attach to geometries. Defaults to nothing.\ncalc_extent: true or false. Whether to calculate the extent. Defaults to false.\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.signed_area-Union{Tuple{Any}, Tuple{T}, Tuple{Any, Type{T}}} where T<:AbstractFloat","page":"Home","title":"GeometryOps.signed_area","text":"signed_area(geom, ::Type{T} = Float64)::T\n\nReturns the signed area of the geometry, based on winding order. This is computed slighly differently for different geometries: - The signed area of a point is always zero. - The signed area of a curve is always zero. - The signed area of a polygon is computed with the shoelace formula and is positive if the polygon coordinates wind clockwise and negative if counterclockwise. - You cannot compute the signed area of a multipolygon as it doesn't have a meaning as each sub-polygon could have a different winding order.\n\nResult will be of type T, where T is an optional argument with a default value of Float64.\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.signed_distance-Union{Tuple{T}, Tuple{Any, Any}, Tuple{Any, Any, Type{T}}} where T<:AbstractFloat","page":"Home","title":"GeometryOps.signed_distance","text":"signed_distance(point, geom, ::Type{T} = Float64)::T\n\nCalculates the signed distance from the geometry geom to the given point. Points within geom have a negative signed distance, and points outside of geom have a positive signed distance. - The signed distance from a point to a point, line, linestring, or linear ring is equal to the distance between the two. - The signed distance from a point to a polygon is negative if the point is within the polygon and is positive otherwise. The value of the distance is the minimum distance from the point to an edge of the polygon. This includes edges created by holes. - The signed distance from a point to a multigeometry or a geometry collection is the minimum signed distance between the point and any of the sub-geometries.\n\nResult will be of type T, where T is an optional argument with a default value of Float64.\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.simplify-Tuple{Any}","page":"Home","title":"GeometryOps.simplify","text":"simplify(obj; kw...)\nsimplify(::SimplifyAlg, obj; kw...)\n\nSimplify a geometry, feature, feature collection, or nested vectors or a table of these.\n\nRadialDistance, DouglasPeucker, or VisvalingamWhyatt algorithms are available, listed in order of increasing quality but decreaseing performance.\n\nPoinTrait and MultiPointTrait are returned unchanged.\n\nThe default behaviour is simplify(DouglasPeucker(; kw...), obj). Pass in other SimplifyAlg to use other algorithms.\n\nKeywords\n\nthreaded: true or false. Whether to use multithreading. Defaults to false.\ncrs: The CRS to attach to geometries. Defaults to nothing.\ncalc_extent: true or false. Whether to calculate the extent. Defaults to false.\n\nKeywords for DouglasPeucker are allowed when no algorithm is specified:\n\nKeywords\n\nratio: the fraction of points that should remain after simplify. Useful as it will generalise for large collections of objects.\nnumber: the number of points that should remain after simplify. Less useful for large collections of mixed size objects.\n\nExample\n\nSimplify a polygon to have six points:\n\nimport GeoInterface as GI\nimport GeometryOps as GO\n\npoly = GI.Polygon([[\n [-70.603637, -33.399918],\n [-70.614624, -33.395332],\n [-70.639343, -33.392466],\n [-70.659942, -33.394759],\n [-70.683975, -33.404504],\n [-70.697021, -33.419406],\n [-70.701141, -33.434306],\n [-70.700454, -33.446339],\n [-70.694274, -33.458369],\n [-70.682601, -33.465816],\n [-70.668869, -33.472117],\n [-70.646209, -33.473835],\n [-70.624923, -33.472117],\n [-70.609817, -33.468107],\n [-70.595397, -33.458369],\n [-70.587158, -33.442901],\n [-70.587158, -33.426283],\n [-70.590591, -33.414248],\n [-70.594711, -33.406224],\n [-70.603637, -33.399918]]])\n\nsimple = GO.simplify(poly; number=6)\nGI.npoint(simple)\n\n# output\n6\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.t_value-Union{Tuple{T2}, Tuple{T1}, Tuple{N}, Tuple{Union{Tuple{Vararg{T1, N}}, StaticArraysCore.StaticArray{Tuple{N}, T1, 1}}, Union{Tuple{Vararg{T1, N}}, StaticArraysCore.StaticArray{Tuple{N}, T1, 1}}, T2, T2}} where {N, T1<:Real, T2<:Real}","page":"Home","title":"GeometryOps.t_value","text":"t_value(sᵢ, sᵢ₊₁, rᵢ, rᵢ₊₁)\n\nReturns the \"T-value\" as described in Hormann's presentation [HormannPresentation] on how to calculate the mean-value coordinate. \n\nHere, sᵢ is the vector from vertex vᵢ to the point, and rᵢ is the norm (length) of sᵢ. s must be Point and r must be real numbers.\n\ntᵢ = fracmathrmdetleft(sᵢ sᵢ₁right)rᵢ * rᵢ₁ + sᵢ sᵢ₁\n\n[HormannPresentation]: K. Hormann and N. Sukumar. Generalized Barycentric Coordinates in Computer Graphics and Computational Mechanics. Taylor & Fancis, CRC Press, 2017.\n\n```\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.to_edges-Tuple{Any}","page":"Home","title":"GeometryOps.to_edges","text":"to_edges()\n\nConvert any geometry or collection of geometries into a flat vector of Tuple{Tuple{Float64,Float64},Tuple{Float64,Float64}} edges.\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.tuples-Tuple{Any}","page":"Home","title":"GeometryOps.tuples","text":"tuples(obj)\n\nConvert all points in obj to Tuples, wherever the are nested.\n\nReturns a similar object or collection of objects using GeoInterface.jl geometries wrapping Tuple points.\n\nKeywords\n\nthreaded: true or false. Whether to use multithreading. Defaults to false.\ncrs: The CRS to attach to geometries. Defaults to nothing.\ncalc_extent: true or false. Whether to calculate the extent. Defaults to false.\n\n\n\n\n\n","category":"method"},{"location":"#GeometryOps.unwrap","page":"Home","title":"GeometryOps.unwrap","text":"unwrap(target::Type{<:AbstractTrait}, obj)\nunwrap(f, target::Type{<:AbstractTrait}, obj)\n\nUnwrap the object newst to vectors, down to the target trait.\n\nIf f is passed in it will be applied to the target geometries as they are found.\n\n\n\n\n\n","category":"function"},{"location":"#GeometryOps.weighted_mean-Union{Tuple{WT}, Tuple{WT, Any, Any}} where WT<:Real","page":"Home","title":"GeometryOps.weighted_mean","text":"weighted_mean(weight::Real, x1, x2)\n\nReturns the weighted mean of x1 and x2, where weight is the weight of x1.\n\nSpecifically, calculates x1 * weight + x2 * (1 - weight).\n\nnote: Note\nThe idea for this method is that you can override this for custom types, like Color types, in extension modules.\n\n\n\n\n\n","category":"method"}]
+}
diff --git a/previews/PR36/siteinfo.js b/previews/PR36/siteinfo.js
new file mode 100644
index 000000000..f1e252971
--- /dev/null
+++ b/previews/PR36/siteinfo.js
@@ -0,0 +1 @@
+var DOCUMENTER_CURRENT_VERSION = "previews/PR36";
diff --git a/previews/PR36/source/GeometryOps/index.html b/previews/PR36/source/GeometryOps/index.html
new file mode 100644
index 000000000..733a2781d
--- /dev/null
+++ b/previews/PR36/source/GeometryOps/index.html
@@ -0,0 +1,41 @@
+
+GeometryOps.jl · GeometryOps.jl
This document was generated with Documenter.jl version 0.27.24 on Monday 1 January 2024. Using Julia version 1.10.0.
diff --git a/previews/PR36/source/methods/area/index.html b/previews/PR36/source/methods/area/index.html
new file mode 100644
index 000000000..f270d837a
--- /dev/null
+++ b/previews/PR36/source/methods/area/index.html
@@ -0,0 +1,67 @@
+
+Area and signed area · GeometryOps.jl
Area is the amount of space occupied by a two-dimensional figure. It is always a positive value. Signed area is simply the integral over the exterior path of a polygon, minus the sum of integrals over its interior holes. It is signed such that a clockwise path has a positive area, and a counterclockwise path has a negative area. The area is the absolute value of the signed area.
To provide an example, consider this rectangle:
using GeometryOps
+using GeometryOps.GeometryBasics
+using Makie
+
+rect = Polygon([Point(0,0), Point(0,1), Point(1,1), Point(1,0), Point(0, 0)])
+f, a, p = poly(rect; axis = (; aspect = DataAspect()))
This is clearly a rectangle, etc. But now let's look at how the points look:
lines!(a, rect; color = 1:length(coordinates(rect))+1)
+f
The points are ordered in a clockwise fashion, which means that the signed area is negative. If we reverse the order of the points, we get a postive area.
This is the GeoInterface-compatible implementation. First, we implement a wrapper method that dispatches to the correct implementation based on the geometry trait. This is also used in the implementation, since it's a lot less work!
Note that area (and signed area) are zero for all points and curves, even if the curves are closed like with a linear ring. Also note that signed area really only makes sense for polygons, given with a multipolygon can have several polygons each with a different orientation and thus the absolute value of the signed area might not be the area. This is why signed area is only implemented for polygons.
"""
+ area(geom, ::Type{T} = Float64)::T
+
+Returns the area of the geometry. This is computed slighly differently for
+different geometries:
+ - The area of a point/multipoint is always zero.
+ - The area of a curve/multicurve is always zero.
+ - The area of a polygon is the absolute value of the signed area.
+ - The area multi-polygon is the sum of the areas of all of the sub-polygons.
+ - The area of a geometry collection is the sum of the areas of all of the
+ sub-geometries.
+
+Result will be of type T, where T is an optional argument with a default value
+of Float64.
+"""
+area(geom, ::Type{T} = Float64) where T <: AbstractFloat =
+ _area(T, GI.trait(geom), geom)
+
+"""
+ signed_area(geom, ::Type{T} = Float64)::T
+
+Returns the signed area of the geometry, based on winding order. This is
+computed slighly differently for different geometries:
+ - The signed area of a point is always zero.
+ - The signed area of a curve is always zero.
+ - The signed area of a polygon is computed with the shoelace formula and is
+ positive if the polygon coordinates wind clockwise and negative if
+ counterclockwise.
+ - You cannot compute the signed area of a multipolygon as it doesn't have a
+ meaning as each sub-polygon could have a different winding order.
+
+Result will be of type T, where T is an optional argument with a default value
+of Float64.
+"""
+signed_area(geom, ::Type{T} = Float64) where T <: AbstractFloat =
+ _signed_area(T, GI.trait(geom), geom)
Points, MultiPoints, Curves, MultiCurves
_area(::Type{T}, ::GI.AbstractGeometryTrait, geom) where T = zero(T)
+
+_signed_area(::Type{T}, ::GI.AbstractGeometryTrait, geom) where T = zero(T)
Polygons
_area(::Type{T}, trait::GI.PolygonTrait, poly) where T =
+ abs(_signed_area(T, trait, poly))
+
+function _signed_area(::Type{T}, ::GI.PolygonTrait, poly) where T
+ GI.isempty(poly) && return zero(T)
+ s_area = _signed_area(T, GI.getexterior(poly))
+ area = abs(s_area)
+ area == 0 && return area
Remove hole areas from total
for hole in GI.gethole(poly)
+ area -= abs(_signed_area(T, hole))
+ end
_area(
+ ::Type{T},
+ ::Union{GI.MultiPolygonTrait, GI.GeometryCollectionTrait},
+ geoms,
+) where T =
+ sum((area(geom, T) for geom in GI.getgeom(geoms)), init = zero(T))
Helper function:
Calculates the signed area of a given curve. This is equivalent to integrating to find the area under the curve. Even if curve isn't explicitly closed by repeating the first point at the end of the coordinates, curve is still assumed to be closed.
function _signed_area(::Type{T}, geom) where T
+ area = zero(T)
Close curve, even if last point isn't explicitly repeated
np = GI.npoint(geom)
+ np == 0 && return area
Integrate the area under the curve
p1 = GI.getpoint(geom, np)
+ for p2 in GI.getpoint(geom)
Generalized barycentric coordinates are a generalization of barycentric coordinates, which are typically used in triangles, to arbitrary polygons.
They provide a way to express a point within a polygon as a weighted average of the polygon's vertices.
In the case of a triangle, barycentric coordinates are a set of three numbers $(λ_1, λ_2, λ_3)$, each associated with a vertex of the triangle. Any point within the triangle can be expressed as a weighted average of the vertices, where the weights are the barycentric coordinates. The weights sum to 1, and each is non-negative.
For a polygon with $n$ vertices, generalized barycentric coordinates are a set of $n$ numbers $(λ_1, λ_2, ..., λ_n)$, each associated with a vertex of the polygon. Any point within the polygon can be expressed as a weighted average of the vertices, where the weights are the generalized barycentric coordinates.
As with the triangle case, the weights sum to 1, and each is non-negative.
In some cases, we actually want barycentric interpolation, and have no interest in the coordinates themselves.
However, the coordinates can be useful for debugging, and when performing 3D rendering, multiple barycentric values (depth, uv) are needed for depth buffering.
const _VecTypes = Union{Tuple{Vararg{T, N}}, GeometryBasics.StaticArraysCore.StaticArray{Tuple{N}, T, 1}} where {N, T}
+
+"""
+ abstract type AbstractBarycentricCoordinateMethod
+
+Abstract supertype for barycentric coordinate methods.
+The subtypes may serve as dispatch types, or may cache
+some information about the target polygon.
+
+# API
+The following methods must be implemented for all subtypes:
+- `barycentric_coordinates!(λs::Vector{<: Real}, method::AbstractBarycentricCoordinateMethod, exterior::Vector{<: Point{2, T1}}, point::Point{2, T2})`
+- `barycentric_interpolate(method::AbstractBarycentricCoordinateMethod, exterior::Vector{<: Point{2, T1}}, values::Vector{V}, point::Point{2, T2})::V`
+- `barycentric_interpolate(method::AbstractBarycentricCoordinateMethod, exterior::Vector{<: Point{2, T1}}, interiors::Vector{<: Vector{<: Point{2, T1}}} values::Vector{V}, point::Point{2, T2})::V`
+The rest of the methods will be implemented in terms of these, and have efficient dispatches for broadcasting.
+"""
+abstract type AbstractBarycentricCoordinateMethod end
+
+
+Base.@propagate_inbounds function barycentric_coordinates!(λs::Vector{<: Real}, method::AbstractBarycentricCoordinateMethod, polypoints::AbstractVector{<: Point{N1, T1}}, point::Point{N2, T2}) where {N1, N2, T1 <: Real, T2 <: Real}
+ @boundscheck @assert length(λs) == length(polypoints)
+ @boundscheck @assert length(polypoints) >= 3
+
+ @error("Not implemented yet for method $(method).")
+end
+Base.@propagate_inbounds barycentric_coordinates!(λs::Vector{<: Real}, polypoints::AbstractVector{<: Point{N1, T1}}, point::Point{N2, T2}) where {N1, N2, T1 <: Real, T2 <: Real} = barycentric_coordinates!(λs, MeanValue(), polypoints, point)
+
+Base.@propagate_inbounds function barycentric_coordinates(method::AbstractBarycentricCoordinateMethod, polypoints::AbstractVector{<: Point{N1, T1}}, point::Point{N2, T2}) where {N1, N2, T1 <: Real, T2 <: Real}
+ λs = zeros(promote_type(T1, T2), length(polypoints))
+ barycentric_coordinates!(λs, method, polypoints, point)
+ return λs
+end
+Base.@propagate_inbounds barycentric_coordinates(polypoints::AbstractVector{<: Point{N1, T1}}, point::Point{N2, T2}) where {N1, N2, T1 <: Real, T2 <: Real} = barycentric_coordinates(MeanValue(), polypoints, point)
+
+Base.@propagate_inbounds function barycentric_interpolate(method::AbstractBarycentricCoordinateMethod, polypoints::AbstractVector{<: Point{N, T1}}, values::AbstractVector{V}, point::Point{N, T2}) where {N, T1 <: Real, T2 <: Real, V}
+ @boundscheck @assert length(values) == length(polypoints)
+ @boundscheck @assert length(polypoints) >= 3
+ λs = barycentric_coordinates(method, polypoints, point)
+ return sum(λs .* values)
+end
+Base.@propagate_inbounds barycentric_interpolate(polypoints::AbstractVector{<: Point{N, T1}}, values::AbstractVector{V}, point::Point{N, T2}) where {N, T1 <: Real, T2 <: Real, V} = barycentric_interpolate(MeanValue(), polypoints, values, point)
+
+Base.@propagate_inbounds function barycentric_interpolate(method::AbstractBarycentricCoordinateMethod, exterior::AbstractVector{<: Point{N, T1}}, interiors::AbstractVector{<: Point{N, T1}}, values::AbstractVector{V}, point::Point{N, T2}) where {N, T1 <: Real, T2 <: Real, V}
+ @boundscheck @assert length(values) == length(exterior) + isempty(interiors) ? 0 : sum(length.(interiors))
+ @boundscheck @assert length(exterior) >= 3
+ λs = barycentric_coordinates(method, exterior, interiors, point)
+ return sum(λs .* values)
+end
+Base.@propagate_inbounds barycentric_interpolate(exterior::AbstractVector{<: Point{N, T1}}, interiors::AbstractVector{<: Point{N, T1}}, values::AbstractVector{V}, point::Point{N, T2}) where {N, T1 <: Real, T2 <: Real, V} = barycentric_interpolate(MeanValue(), exterior, interiors, values, point)
+
+Base.@propagate_inbounds function barycentric_interpolate(method::AbstractBarycentricCoordinateMethod, polygon::Polygon{2, T1}, values::AbstractVector{V}, point::Point{2, T2}) where {T1 <: Real, T2 <: Real, V}
+ exterior = decompose(Point{2, promote_type(T1, T2)}, polygon.exterior)
+ if isempty(polygon.interiors)
+ @boundscheck @assert length(values) == length(exterior)
+ return barycentric_interpolate(method, exterior, values, point)
+ else # the poly has interiors
+ interiors = reverse.(decompose.((Point{2, promote_type(T1, T2)},), polygon.interiors))
+ @boundscheck @assert length(values) == length(exterior) + sum(length.(interiors))
+ return barycentric_interpolate(method, exterior, interiors, values, point)
+ end
+end
+Base.@propagate_inbounds barycentric_interpolate(polygon::Polygon{2, T1}, values::AbstractVector{V}, point::Point{2, T2}) where {T1 <: Real, T2 <: Real, V} = barycentric_interpolate(MeanValue(), polygon, values, point)
3D polygons are considered to have their vertices in the XY plane, and the Z coordinate must represent some value. This is to say that the Z coordinate is interpreted as an M coordinate.
This method is the one which supports GeoInterface.
Base.@propagate_inbounds function barycentric_interpolate(method::AbstractBarycentricCoordinateMethod, polygon, values::AbstractVector{V}, point) where V
+ @assert GeoInterface.trait(polygon) isa GeoInterface.PolygonTrait
+ @assert GeoInterface.trait(point) isa GeoInterface.PointTrait
+ passable_polygon = GeoInterface.convert(GeometryBasics, polygon)
+ @assert passable_polygon isa GeometryBasics.Polygon "The polygon was converted to a $(typeof(passable_polygon)), which is not a `GeometryBasics.Polygon`."
+ # first_poly_point = GeoInterface.getpoint(GeoInterface.getexterior(polygon))
+ passable_point = GeoInterface.convert(GeometryBasics, point)
+ return barycentric_interpolate(method, passable_polygon, Point2(passable_point))
+end
+Base.@propagate_inbounds barycentric_interpolate(polygon, values::AbstractVector{V}, point) where V = barycentric_interpolate(MeanValue(), polygon, values, point)
+
+"""
+ weighted_mean(weight::Real, x1, x2)
+
+Returns the weighted mean of `x1` and `x2`, where `weight` is the weight of `x1`.
+
+Specifically, calculates `x1 * weight + x2 * (1 - weight)`.
+
+!!! note
+ The idea for this method is that you can override this for custom types, like Color types, in extension modules.
+"""
+function weighted_mean(weight::WT, x1, x2) where {WT <: Real}
+ return muladd(x1, weight, x2 * (oneunit(WT) - weight))
+end
+
+
+"""
+ MeanValue() <: AbstractBarycentricCoordinateMethod
+
+This method calculates barycentric coordinates using the mean value method.
+
+# References
+
+"""
+struct MeanValue <: AbstractBarycentricCoordinateMethod
+end
Before we go to the actual implementation, there are some quick and simple utility functions that we need to implement. These are mainly for convenience and code brevity.
"""
+ _det(s1::Point2{T1}, s2::Point2{T2}) where {T1 <: Real, T2 <: Real}
+
+Returns the determinant of the matrix formed by `hcat`'ing two points `s1` and `s2`.
+
+Specifically, this is:
+```julia
+s1[1] * s2[2] - s1[2] * s2[1]
+```
+"""
+function _det(s1::_VecTypes{2, T1}, s2::_VecTypes{2, T2}) where {T1 <: Real, T2 <: Real}
+ return s1[1] * s2[2] - s1[2] * s2[1]
+end
+
+"""
+ t_value(sᵢ, sᵢ₊₁, rᵢ, rᵢ₊₁)
+
+Returns the "T-value" as described in Hormann's presentation [^HormannPresentation] on how to calculate
+the mean-value coordinate.
+
+Here, `sᵢ` is the vector from vertex `vᵢ` to the point, and `rᵢ` is the norm (length) of `sᵢ`.
+`s` must be `Point` and `r` must be real numbers.
+
+```math
+tᵢ = \\frac{\\mathrm{det}\\left(sᵢ, sᵢ₊₁\\right)}{rᵢ * rᵢ₊₁ + sᵢ ⋅ sᵢ₊₁}
+```
+
+[^HormannPresentation]: K. Hormann and N. Sukumar. Generalized Barycentric Coordinates in Computer Graphics and Computational Mechanics. Taylor & Fancis, CRC Press, 2017.
+```
+
+"""
+function t_value(sᵢ::_VecTypes{N, T1}, sᵢ₊₁::_VecTypes{N, T1}, rᵢ::T2, rᵢ₊₁::T2) where {N, T1 <: Real, T2 <: Real}
+ return _det(sᵢ, sᵢ₊₁) / muladd(rᵢ, rᵢ₊₁, dot(sᵢ, sᵢ₊₁))
+end
+
+
+function barycentric_coordinates!(λs::Vector{<: Real}, ::MeanValue, polypoints::AbstractVector{<: Point{2, T1}}, point::Point{2, T2}) where {T1 <: Real, T2 <: Real}
+ @boundscheck @assert length(λs) == length(polypoints)
+ @boundscheck @assert length(polypoints) >= 3
+ n_points = length(polypoints)
+ # Initialize counters and register variables
+ # Points - these are actually vectors from point to vertices
+ # polypoints[i-1], polypoints[i], polypoints[i+1]
+ sᵢ₋₁ = polypoints[end] - point
+ sᵢ = polypoints[begin] - point
+ sᵢ₊₁ = polypoints[begin+1] - point
+ # radius / Euclidean distance between points.
+ rᵢ₋₁ = norm(sᵢ₋₁)
+ rᵢ = norm(sᵢ )
+ rᵢ₊₁ = norm(sᵢ₊₁)
+ # Perform the first computation explicitly, so we can cut down on
+ # a mod in the loop.
+ λs[1] = (t_value(sᵢ₋₁, sᵢ, rᵢ₋₁, rᵢ) + t_value(sᵢ, sᵢ₊₁, rᵢ, rᵢ₊₁)) / rᵢ
+ # Loop through the rest of the vertices, compute, store in λs
+ for i in 2:n_points
+ # Increment counters + set variables
+ sᵢ₋₁ = sᵢ
+ sᵢ = sᵢ₊₁
+ sᵢ₊₁ = polypoints[mod1(i+1, n_points)] - point
+ rᵢ₋₁ = rᵢ
+ rᵢ = rᵢ₊₁
+ rᵢ₊₁ = norm(sᵢ₊₁) # radius / Euclidean distance between points.
+ λs[i] = (t_value(sᵢ₋₁, sᵢ, rᵢ₋₁, rᵢ) + t_value(sᵢ, sᵢ₊₁, rᵢ, rᵢ₊₁)) / rᵢ
+ end
+ # Normalize λs to the 1-norm (sum=1)
+ λs ./= sum(λs)
+ return λs
+end
function barycentric_coordinates(::MeanValue, polypoints::NTuple{N, Point{2, T2}}, point::Point{2, T1},) where {N, T1, T2}
+ ## Initialize counters and register variables
+ ## Points - these are actually vectors from point to vertices
+ ## polypoints[i-1], polypoints[i], polypoints[i+1]
+ sᵢ₋₁ = polypoints[end] - point
+ sᵢ = polypoints[begin] - point
+ sᵢ₊₁ = polypoints[begin+1] - point
+ ## radius / Euclidean distance between points.
+ rᵢ₋₁ = norm(sᵢ₋₁)
+ rᵢ = norm(sᵢ )
+ rᵢ₊₁ = norm(sᵢ₊₁)
+ λ₁ = (t_value(sᵢ₋₁, sᵢ, rᵢ₋₁, rᵢ) + t_value(sᵢ, sᵢ₊₁, rᵢ, rᵢ₊₁)) / rᵢ
+ λs = ntuple(N) do i
+ if i == 1
+ return λ₁
+ end
+ ## Increment counters + set variables
+ sᵢ₋₁ = sᵢ
+ sᵢ = sᵢ₊₁
+ sᵢ₊₁ = polypoints[mod1(i+1, N)] - point
+ rᵢ₋₁ = rᵢ
+ rᵢ = rᵢ₊₁
+ rᵢ₊₁ = norm(sᵢ₊₁) # radius / Euclidean distance between points.
+ return (t_value(sᵢ₋₁, sᵢ, rᵢ₋₁, rᵢ) + t_value(sᵢ, sᵢ₊₁, rᵢ, rᵢ₊₁)) / rᵢ
+ end
+
+ ∑λ = sum(λs)
+
+ return ntuple(N) do i
+ λs[i] / ∑λ
+ end
+end
This performs an inplace accumulation, using less memory and is faster. That's particularly good if you are using a polygon with a large number of points...
function barycentric_interpolate(::MeanValue, polypoints::AbstractVector{<: Point{2, T1}}, values::AbstractVector{V}, point::Point{2, T2}) where {T1 <: Real, T2 <: Real, V}
+ @boundscheck @assert length(values) == length(polypoints)
+ @boundscheck @assert length(polypoints) >= 3
+
+ n_points = length(polypoints)
+ # Initialize counters and register variables
+ # Points - these are actually vectors from point to vertices
+ # polypoints[i-1], polypoints[i], polypoints[i+1]
+ sᵢ₋₁ = polypoints[end] - point
+ sᵢ = polypoints[begin] - point
+ sᵢ₊₁ = polypoints[begin+1] - point
+ # radius / Euclidean distance between points.
+ rᵢ₋₁ = norm(sᵢ₋₁)
+ rᵢ = norm(sᵢ )
+ rᵢ₊₁ = norm(sᵢ₊₁)
+ # Now, we set the interpolated value to the first point's value, multiplied
+ # by the weight computed relative to the first point in the polygon.
+ wᵢ = (t_value(sᵢ₋₁, sᵢ, rᵢ₋₁, rᵢ) + t_value(sᵢ, sᵢ₊₁, rᵢ, rᵢ₊₁)) / rᵢ
+ wₜₒₜ = wᵢ
+ interpolated_value = values[begin] * wᵢ
+ for i in 2:n_points
+ # Increment counters + set variables
+ sᵢ₋₁ = sᵢ
+ sᵢ = sᵢ₊₁
+ sᵢ₊₁ = polypoints[mod1(i+1, n_points)] - point
+ rᵢ₋₁ = rᵢ
+ rᵢ = rᵢ₊₁
+ rᵢ₊₁ = norm(sᵢ₊₁)
+ # Now, we calculate the weight:
+ wᵢ = (t_value(sᵢ₋₁, sᵢ, rᵢ₋₁, rᵢ) + t_value(sᵢ, sᵢ₊₁, rᵢ, rᵢ₊₁)) / rᵢ
+ # perform a weighted sum with the interpolated value:
+ interpolated_value += values[i] * wᵢ
+ # and add the weight to the total weight accumulator.
+ wₜₒₜ += wᵢ
+ end
+ # Return the normalized interpolated value.
+ return interpolated_value / wₜₒₜ
+end
When you have holes, then you have to be careful about the order you iterate around points.
Specifically, you have to iterate around each linear ring separately and ensure there are no degenerate/repeated points at the start and end!
The may not necessarily be what want in the end but work for now!
"""
+ isclockwise(line::Union{LineString, Vector{Position}})::Bool
+
+Take a ring and return true or false whether or not the ring is clockwise or
+counter-clockwise.
+
+# Example
+
+```jldoctest
+import GeoInterface as GI, GeometryOps as GO
+
+ring = GI.LinearRing([(0, 0), (1, 1), (1, 0), (0, 0)])
+GO.isclockwise(ring)
output
true
+```
+"""
+isclockwise(geom)::Bool = isclockwise(GI.trait(geom), geom)
+
+function isclockwise(::AbstractCurveTrait, line)::Bool
+ sum = 0.0
+ prev = GI.getpoint(line, 1)
+ for p in GI.getpoint(line)
sum will be zero for the first point as x is subtracted from itself
sum += (GI.x(p) - GI.x(prev)) * (GI.y(p) + GI.y(prev))
+ prev = p
+ end
+
+ return sum > 0.0
+end
+
+"""
+ isconcave(poly::Polygon)::Bool
+
+Take a polygon and return true or false as to whether it is concave or not.
+
+# Examples
+```jldoctest
+import GeoInterface as GI, GeometryOps as GO
+
+poly = GI.Polygon([[(0, 0), (0, 1), (1, 1), (1, 0), (0, 0)]])
+GO.isconcave(poly)
"""
+ point_on_line(point::Point, line::LineString; ignore_end_vertices::Bool=false)::Bool
+
+Return true if a point is on a line. Accept a optional parameter to ignore the
+start and end vertices of the linestring.
+
+# Examples
+
+```jldoctest
+import GeoInterface as GI, GeometryOps as GO
+
+point = (1, 1)
+line = GI.LineString([(0, 0), (3, 3), (4, 4)])
+GO.point_on_line(point, line)
output
true
+```
+"""
+function point_on_line(point, line; ignore_end_vertices::Bool=false)::Bool
+ line_points = tuple_points(line)
+ n = length(line_points)
+
+ exclude_boundary = :none
+ for i in 1:n - 1
+ if ignore_end_vertices
+ if i === 1
+ exclude_boundary = :start
+ elseif i === n - 2
+ exclude_boundary = :end
+ elseif (i === 1 && i + 1 === n - 1)
+ exclude_boundary = :both
+ end
+ end
+ if point_on_segment(point, (line_points[i], line_points[i + 1]); exclude_boundary)
+ return true
+ end
+ end
+ return false
+end
+
+function point_on_seg(point, start, stop)
if exclude_boundary === :none
+ if abs(dx1) >= abs(dy1)
+ return dx1 > 0 ? x1 <= x && x <= x2 : x2 <= x && x <= x1
+ end
+ return dy1 > 0 ? y1 <= y && y <= y2 : y2 <= y && y <= y1
+ elseif exclude_boundary === :start
+ if abs(dx1) >= abs(dy1)
+ return dx1 > 0 ? x1 < x && x <= x2 : x2 <= x && x < x1
+ end
+ return dy1 > 0 ? y1 < y && y <= y2 : y2 <= y && y < y1
+ elseif exclude_boundary === :end
+ if abs(dx1) >= abs(dy1)
+ return dx1 > 0 ? x1 <= x && x < x2 : x2 < x && x <= x1
+ end
+ return dy1 > 0 ? y1 <= y && y < y2 : y2 < y && y <= y1
+ elseif exclude_boundary === :both
+ if abs(dx1) >= abs(dy1)
+ return dx1 > 0 ? x1 < x && x < x2 : x2 < x && x < x1
+ end
+ return dy1 > 0 ? y1 < y && y < y2 : y2 < y && y < y1
+ end
+ return false
+end
+
+"""
+ point_in_polygon(point::Point, polygon::Union{Polygon, MultiPolygon}, ignore_boundary::Bool=false)::Bool
+
+Take a Point and a Polygon and determine if the point
+resides inside the polygon. The polygon can be convex or concave. The function accounts for holes.
+
+# Examples
+
+```jldoctest
+import GeoInterface as GI, GeometryOps as GO
+
+point = (-77.0, 44.0)
+poly = GI.Polygon([[(-81, 41), (-81, 47), (-72, 47), (-72, 41), (-81, 41)]])
+GO.point_in_polygon(point, poly)
on_boundary = ( # vertex to point has same slope as edge
+ yi * (xj - x) + yj * (x - xi) == y * (xj - xi) &&
+ (xi - x) * (xj - x) <= 0 && # x is between xi and xj
+ (yi - y) * (yj - y) <= 0 # y is between yi and yj
+ )
+ on_boundary && return !ignore_boundary
The centroid is the geometric center of a line string or area(s). Note that the centroid does not need to be inside of a concave area.
Further note that by convention a line, or linear ring, is calculated by weighting the line segments by their length, while polygons and multipolygon centroids are calculated by weighting edge's by their 'area components'.
To provide an example, consider this concave polygon in the shape of a 'C':
This is the GeoInterface-compatible implementation.
First, we implement a wrapper method that dispatches to the correct implementation based on the geometry trait. This is also used in the implementation, since it's a lot less work!
Note that if you call centroid on a LineString or LinearRing, the centroidandlength function will be called due to the weighting scheme described above, while centroidandarea is called for polygons and multipolygons. However, centroidandarea can still be called on a LineString or LinearRing when they are closed, for example as the interior hole of a polygon.
The helper functions centroidandlength and centroidandarea are made availible just in case the user also needs the area or length to decrease repeat computation.
"""
+ centroid(geom)::Tuple{T, T}
+
+Returns the centroid of a given line segment, linear ring, polygon, or
+mutlipolygon.
+"""
+centroid(geom) = centroid(GI.trait(geom), geom)
+
+"""
+ centroid(
+ trait::Union{GI.LineStringTrait, GI.LinearRingTrait},
+ geom,
+ )::Tuple{T, T}
+
+Returns the centroid of a line string or linear ring, which is calculated by
+weighting line segments by their length by convention.
+"""
+centroid(
+ trait::Union{GI.LineStringTrait, GI.LinearRingTrait},
+ geom,
+) = centroid_and_length(trait, geom)[1]
+
+"""
+ centroid(trait, geom)::Tuple{T, T}
+
+Returns the centroid of a polygon or multipolygon, which is calculated by
+weighting edges by their `area component` by convention.
+"""
+centroid(trait, geom) = centroid_and_area(trait, geom)[1]
+
+"""
+ centroid_and_length(geom)::(::Tuple{T, T}, ::Real)
+
+Returns the centroid and length of a given line/ring. Note this is only valid
+for line strings and linear rings.
+"""
+centroid_and_length(geom) = centroid_and_length(GI.trait(geom), geom)
+
+"""
+ centroid_and_area(
+ ::Union{GI.LineStringTrait, GI.LinearRingTrait},
+ geom,
+ )::(::Tuple{T, T}, ::Real)
+
+Returns the centroid and area of a given geom.
+"""
+centroid_and_area(geom) = centroid_and_area(GI.trait(geom), geom)
+
+"""
+ centroid_and_length(geom)::(::Tuple{T, T}, ::Real)
+
+Returns the centroid and length of a given line/ring. Note this is only valid
+for line strings and linear rings.
+"""
+function centroid_and_length(
+ ::Union{GI.LineStringTrait, GI.LinearRingTrait},
+ geom,
+)
+ T = typeof(GI.x(GI.getpoint(geom, 1)))
Advance the point buffer by 1 point to move to next line segment
point₁ = point₂
+ end
+ xcentroid /= length
+ ycentroid /= length
+ return (xcentroid, ycentroid), length
+end
+
+"""
+ centroid_and_area(
+ ::Union{GI.LineStringTrait, GI.LinearRingTrait},
+ geom,
+ )::(::Tuple{T, T}, ::Real)
+
+Returns the centroid and area of a given a line string or a linear ring.
+Note that this is only valid if the line segment or linear ring is closed.
+"""
+function centroid_and_area(
+ ::Union{GI.LineStringTrait, GI.LinearRingTrait},
+ geom,
+)
+ T = typeof(GI.x(GI.getpoint(geom, 1)))
Check that the geometry is closed
@assert(
+ GI.getpoint(geom, 1) == GI.getpoint(geom, GI.ngeom(geom)),
+ "centroid_and_area should only be used with closed geometries"
+ )
xcentroid -= xinterior * interior_area
+ ycentroid -= yinterior * interior_area
+ end
+ xcentroid /= area
+ ycentroid /= area
+ return (xcentroid, ycentroid), area
+end
+
+"""
+ centroid_and_area(::GI.MultiPolygonTrait, geom)::(::Tuple{T, T}, ::Real)
+
+Returns the centroid and area of a given multipolygon.
+"""
+function centroid_and_area(::GI.MultiPolygonTrait, geom)
First polygon's centroid and area
(xcentroid, ycentroid), area = centroid_and_area(GI.getpolygon(geom, 1))
export contains
+
+"""
+ contains(ft1::AbstractGeometry, ft2::AbstractGeometry)::Bool
+
+Return true if the second geometry is completely contained by the first geometry.
+The interiors of both geometries must intersect and, the interior and boundary of the secondary (geometry b)
+must not intersect the exterior of the primary (geometry a).
+`contains` returns the exact opposite result of `within`.
+
+# Examples
+
+```jldoctest
+import GeometryOps as GO, GeoInterface as GI
+line = GI.LineString([(1, 1), (1, 2), (1, 3), (1, 4)])
+point = (1, 2)
+
+GO.contains(line, point)
"""
+ crosses(geom1, geom2)::Bool
+
+Return `true` if the intersection results in a geometry whose dimension is one less than
+the maximum dimension of the two source geometries and the intersection set is interior to
+both source geometries.
+
+TODO: broken
+
+# Examples
+```julia
+import GeoInterface as GI, GeometryOps as GO
+
+line1 = GI.LineString([(1, 1), (1, 2), (1, 3), (1, 4)])
+line2 = GI.LineString([(-2, 2), (4, 2)])
+
+GO.crosses(line1, line2)
output
true
+```
+"""
+crosses(g1, g2)::Bool = crosses(trait(g1), g1, trait(g2), g2)::Bool
+crosses(t1::FeatureTrait, g1, t2, g2)::Bool = crosses(GI.geometry(g1), g2)
+crosses(t1, g1, t2::FeatureTrait, g2)::Bool = crosses(g1, geometry(g2))
+crosses(::MultiPointTrait, g1, ::LineStringTrait, g2)::Bool = multipoint_crosses_line(g1, g2)
+crosses(::MultiPointTrait, g1, ::PolygonTrait, g2)::Bool = multipoint_crosses_poly(g1, g2)
+crosses(::LineStringTrait, g1, ::MultiPointTrait, g2)::Bool = multipoint_crosses_lines(g2, g1)
+crosses(::LineStringTrait, g1, ::PolygonTrait, g2)::Bool = line_crosses_poly(g1, g2)
+crosses(::LineStringTrait, g1, ::LineStringTrait, g2)::Bool = line_crosses_line(g1, g2)
+crosses(::PolygonTrait, g1, ::MultiPointTrait, g2)::Bool = multipoint_crosses_poly(g2, g1)
+crosses(::PolygonTrait, g1, ::LineStringTrait, g2)::Bool = line_crosses_poly(g2, g1)
+
+function multipoint_crosses_line(geom1, geom2)
+ int_point = false
+ ext_point = false
+ i = 1
+ np2 = GI.npoint(geom2)
+
+ while i < GI.npoint(geom1) && !int_point && !ext_point
+ for j in 1:GI.npoint(geom2) - 1
+ exclude_boundary = (j === 1 || j === np2 - 2) ? :none : :both
+ if point_on_segment(GI.getpoint(geom1, i), (GI.getpoint(geom2, j), GI.getpoint(geom2, j + 1)); exclude_boundary)
+ int_point = true
+ else
+ ext_point = true
+ end
+ end
+ i += 1
+ end
+
+ return int_point && ext_point
+end
+
+function line_crosses_line(line1, line2)
+ np2 = GI.npoint(line2)
+ if intersects(line1, line2)
+ for i in 1:GI.npoint(line1) - 1
+ for j in 1:GI.npoint(line2) - 1
+ exclude_boundary = (j === 1 || j === np2 - 2) ? :none : :both
+ pa = GI.getpoint(line1, i)
+ pb = GI.getpoint(line1, i + 1)
+ p = GI.getpoint(line2, j)
+ point_on_segment(p, (pa, pb); exclude_boundary) && return true
+ end
+ end
+ end
+ return false
+end
+
+function line_crosses_poly(line, poly)
+ for l in flatten(AbstractCurveTrait, poly)
+ intersects(line, l) && return true
+ end
+ return false
+end
+
+function multipoint_crosses_poly(mp, poly)
+ int_point = false
+ ext_point = false
+
+ for p in GI.getpoint(mp)
+ if point_in_polygon(p, poly)
+ int_point = true
+ else
+ ext_point = true
+ end
+ int_point && ext_point && return true
+ end
+ return false
+end
Distance is the distance of a point to another geometry. This is always a positive number. If a point is inside of geometry, so on a curve or inside of a polygon, the distance will be zero. Signed distance is mainly used for polygons and multipolygons. If a point is outside of a geometry, signed distance has the same value as distance. However, points within the geometry have a negative distance representing the distance of a point to the closest boundary. Therefore, for all "non-filled" geometries, like curves, the distance will either be postitive or 0.
This is clearly a rectangle with one point inside and one point outside. The points are both an equal distance to the polygon. The distance to pointin is negative while the distance to pointout is positive.
This is the GeoInterface-compatible implementation. First, we implement a wrapper method that dispatches to the correct implementation based on the geometry trait. This is also used in the implementation, since it's a lot less work!
Distance and signed distance are only implemented for points to other geometries right now. This could be extended to include distance from other geometries in the future.
The distance calculated is the Euclidean distance using the Pythagorean theorem. Also note that singed_distance only makes sense for "filled-in" shapes, like polygons, so it isn't implemented for curves.
"""
+ distance(point, geom, ::Type{T} = Float64)::T
+
+Calculates the ditance from the geometry `g1` to the `point`. The distance
+will always be positive or zero.
+
+The method will differ based on the type of the geometry provided:
+ - The distance from a point to a point is just the Euclidean distance
+ between the points.
+ - The distance from a point to a line is the minimum distance from the point
+ to the closest point on the given line.
+ - The distance from a point to a linestring is the minimum distance from the
+ point to the closest segment of the linestring.
+ - The distance from a point to a linear ring is the minimum distance from
+ the point to the closest segment of the linear ring.
+ - The distance from a point to a polygon is zero if the point is within the
+ polygon and otherwise is the minimum distance from the point to an edge of
+ the polygon. This includes edges created by holes.
+ - The distance from a point to a multigeometry or a geometry collection is
+ the minimum distance between the point and any of the sub-geometries.
+
+Result will be of type T, where T is an optional argument with a default value
+of Float64.
+"""
+distance(point, geom, ::Type{T} = Float64) where T <: AbstractFloat =
+ _distance(T, GI.trait(point), point, GI.trait(geom), geom)
+
+"""
+ signed_distance(point, geom, ::Type{T} = Float64)::T
+
+Calculates the signed distance from the geometry `geom` to the given point.
+Points within `geom` have a negative signed distance, and points outside of
+`geom` have a positive signed distance.
+ - The signed distance from a point to a point, line, linestring, or linear
+ ring is equal to the distance between the two.
+ - The signed distance from a point to a polygon is negative if the point is
+ within the polygon and is positive otherwise. The value of the distance is
+ the minimum distance from the point to an edge of the polygon. This includes
+ edges created by holes.
+ - The signed distance from a point to a multigeometry or a geometry
+ collection is the minimum signed distance between the point and any of the
+ sub-geometries.
+
+Result will be of type T, where T is an optional argument with a default value
+of Float64.
+"""
+signed_distance(point, geom, ::Type{T} = Float64) where T<:AbstractFloat =
+ _signed_distance(T, GI.trait(point), point, GI.trait(geom), geom)
Swap argument order to point as first argument
_distance(
+ ::Type{T},
+ gtrait::GI.AbstractTrait, geom,
+ ptrait::GI.PointTrait, point,
+) where T = _distance(T, ptrait, point, gtrait, geom)
+
+_signed_distance(
+ ::Type{T},
+ gtrait::GI.AbstractTrait, geom,
+ ptrait::GI.PointTrait, point,
+) where T = _signed_distance(T, ptrait, point, gtrait, geom)
_distance(::Type{T}, ::GI.PointTrait, point, ::GI.PointTrait, geom) where T =
+ _euclid_distance(T, point, geom)
+
+_distance(::Type{T}, ::GI.PointTrait, point, ::GI.LineTrait, geom) where T =
+ _distance_line(T, point, GI.getpoint(geom, 1), GI.getpoint(geom, 2))
+
+_distance(::Type{T}, ::GI.PointTrait, point, ::GI.LineStringTrait, geom) where T =
+ _distance_curve(T, point, geom, close_curve = false)
+
+_distance(::Type{T}, ::GI.PointTrait, point, ::GI.LinearRingTrait, geom) where T =
+ _distance_curve(T, point, geom, close_curve = true)
+
+_signed_distance(::Type{T}, ptrait::GI.PointTrait, point, gtrait::GI.AbstractTrait, geom) where T =
+ _distance(T, ptrait, point, gtrait, geom)
Point-Polygon
function _distance(::Type{T}, ::GI.PointTrait, point, ::GI.PolygonTrait, geom) where T
+ GI.within(point, geom) && return zero(T)
+ return _distance_polygon(T, point, geom)
+end
+
+function _signed_distance(::Type{T}, ::GI.PointTrait, point, ::GI.PolygonTrait, geom) where T
+ min_dist = _distance_polygon(T, point, geom)
function _distance(
+ ::Type{T},
+ ::GI.PointTrait,
+ point,
+ ::Union{
+ GI.MultiPointTrait, GI.MultiCurveTrait,
+ GI.MultiPolygonTrait, GI.GeometryCollectionTrait,
+ },
+ geoms,
+) where T
+ min_dist = typemax(T)
+ for g in GI.getgeom(geoms)
+ dist = distance(point, g, T)
+ min_dist = dist < min_dist ? dist : min_dist
+ end
+ return min_dist
+end
+
+function _signed_distance(
+ ::Type{T},
+ ::GI.PointTrait,
+ point,
+ ::Union{
+ GI.MultiPointTrait, GI.MultiCurveTrait,
+ GI.MultiPolygonTrait, GI.GeometryCollectionTrait,
+ },
+ geoms,
+) where T
+ min_dist = typemax(T)
+ for g in GI.getgeom(geoms)
+ dist = signed_distance(point, g, T)
+ min_dist = dist < min_dist ? dist : min_dist
+ end
+ return min_dist
+end
Returns the Euclidean distance between two points.
Base.@propagate_inbounds _euclid_distance(::Type{T}, p1, p2) where T =
+ _euclid_distance(
+ T,
+ GeoInterface.x(p1), GeoInterface.y(p1),
+ GeoInterface.x(p2), GeoInterface.y(p2),
+ )
Returns the Euclidean distance between two points given their x and y values.
Base.@propagate_inbounds _euclid_distance(::Type{T}, x1, y1, x2, y2) where T =
+ T(sqrt((x2 - x1)^2 + (y2 - y1)^2))
Returns the minimum distance from point p0 to the line defined by endpoints p1 and p2.
function _distance_line(::Type{T}, p0, p1, p2) where T
+ x0, y0 = GeoInterface.x(p0), GeoInterface.y(p0)
+ x1, y1 = GeoInterface.x(p1), GeoInterface.y(p1)
+ x2, y2 = GeoInterface.x(p2), GeoInterface.y(p2)
+
+ xfirst, yfirst, xlast, ylast = x1 < x2 ?
+ (x1, y1, x2, y2) : (x2, y2, x1, y1)
+
+ #=
+ Vectors from first point to last point (v) and from first point to point of
+ interest (w) to find the projection of w onto v to find closest point
+ =#
+ v = (xlast - xfirst, ylast - yfirst)
+ w = (x0 - xfirst, y0 - yfirst)
+
+ c1 = sum(w .* v)
+ if c1 <= 0 # p0 is closest to first endpoint
+ return _euclid_distance(T, x0, y0, xfirst, yfirst)
+ end
+
+ c2 = sum(v .* v)
+ if c2 <= c1 # p0 is closest to last endpoint
+ return _euclid_distance(T, x0, y0, xlast, ylast)
+ end
+
+ b2 = c1 / c2 # projection fraction
+ return _euclid_distance(T, x0, y0, xfirst + (b2 * v[1]), yfirst + (b2 * v[2]))
+end
Returns the minimum distance from the given point to the given curve. If close_curve is true, make sure to include the edge from the first to last point of the curve, even if it isn't explicitly repeated.
function _distance_curve(::Type{T}, point, curve; close_curve = false) where T
see if linear ring has explicitly repeated last point in coordinates
Returns the minimum distance from the given point to an edge of the given polygon, including from edges created by holes. Assumes polygon isn't filled and treats the exterior and each hole as a linear ring.
function _distance_polygon(::Type{T}, point, poly) where T
+ min_dist = _distance_curve(T, point, GI.getexterior(poly); close_curve = true)
+ @inbounds for hole in GI.gethole(poly)
+ dist = _distance_curve(T, point, hole; close_curve = true)
+ min_dist = dist < min_dist ? dist : min_dist
+ end
+ return min_dist
+end
This is the GeoInterface-compatible implementation.
First, we implement a wrapper method that dispatches to the correct implementation based on the geometry trait. This is also used in the implementation, since it's a lot less work!
Note that while we need the same set of points and edges, they don't need to be provided in the same order for polygons. For for example, we need the same set points for two multipoints to be equal, but they don't have to be saved in the same order. The winding order also doesn't have to be the same to represent the same geometry. This requires checking every point against every other point in the two geometries we are comparing. Also, some geometries must be "closed" like polygons and linear rings. These will be assumed to be closed, even if they don't have a repeated last point explicity written in the coordinates. Additionally, geometries and multi-geometries can be equal if the multi-geometry only includes that single geometry.
"""
+ equals(geom1, geom2)::Bool
+
+Compare two Geometries return true if they are the same geometry.
+
+# Examples
+```jldoctest
+import GeometryOps as GO, GeoInterface as GI
+poly1 = GI.Polygon([[(0,0), (0,5), (5,5), (5,0), (0,0)]])
+poly2 = GI.Polygon([[(0,0), (0,5), (5,5), (5,0), (0,0)]])
+
+GO.equals(poly1, poly2)
output
true
+```
+"""
+equals(geom_a, geom_b) = equals(
+ GI.trait(geom_a), geom_a,
+ GI.trait(geom_b), geom_b,
+)
+
+"""
+ equals(::T, geom_a, ::T, geom_b)::Bool
+
+Two geometries of the same type, which don't have a equals function to dispatch
+off of should throw an error.
+"""
+equals(::T, geom_a, ::T, geom_b) where T = error("Cant compare $T yet")
+
+"""
+ equals(trait_a, geom_a, trait_b, geom_b)
+
+Two geometries which are not of the same type cannot be equal so they always
+return false.
+"""
+equals(trait_a, geom_a, trait_b, geom_b) = false
+
+"""
+ equals(::GI.PointTrait, p1, ::GI.PointTrait, p2)::Bool
+
+Two points are the same if they have the same x and y (and z if 3D) coordinates.
+"""
+function equals(::GI.PointTrait, p1, ::GI.PointTrait, p2)
+ GI.ncoord(p1) == GI.ncoord(p2) || return false
+ GI.x(p1) == GI.x(p2) || return false
+ GI.y(p1) == GI.y(p2) || return false
+ if GI.is3d(p1)
+ GI.z(p1) == GI.z(p2) || return false
+ end
+ return true
+end
+
+"""
+ equals(::GI.PointTrait, p1, ::GI.MultiPointTrait, mp2)::Bool
+
+A point and a multipoint are equal if the multipoint is composed of a single
+point that is equivalent to the given point.
+"""
+function equals(::GI.PointTrait, p1, ::GI.MultiPointTrait, mp2)
+ GI.npoint(mp2) == 1 || return false
+ return equals(p1, GI.getpoint(mp2, 1))
+end
+
+"""
+ equals(::GI.MultiPointTrait, mp1, ::GI.PointTrait, p2)::Bool
+
+A point and a multipoint are equal if the multipoint is composed of a single
+point that is equivalent to the given point.
+"""
+equals(trait1::GI.MultiPointTrait, mp1, trait2::GI.PointTrait, p2) =
+ equals(trait2, p2, trait1, mp1)
+
+"""
+ equals(::GI.MultiPointTrait, mp1, ::GI.MultiPointTrait, mp2)::Bool
+
+Two multipoints are equal if they share the same set of points.
+"""
+function equals(::GI.MultiPointTrait, mp1, ::GI.MultiPointTrait, mp2)
+ GI.npoint(mp1) == GI.npoint(mp2) || return false
+ for p1 in GI.getpoint(mp1)
+ has_match = false # if point has a matching point in other multipoint
+ for p2 in GI.getpoint(mp2)
+ if equals(p1, p2)
+ has_match = true
+ break
+ end
+ end
+ has_match || return false # if no matching point, can't be equal
+ end
+ return true # all points had a match
+end
+
+"""
+ _equals_curves(c1, c2, closed_type1, closed_type2)::Bool
+
+Two curves are equal if they share the same set of point, representing the same
+geometry. Both curves must must be composed of the same set of points, however,
+they do not have to wind in the same direction, or start on the same point to be
+equivalent.
+Inputs:
+ c1 first geometry
+ c2 second geometry
+ closed_type1::Bool true if c1 is closed by definition (polygon, linear ring)
+ closed_type2::Bool true if c2 is closed by definition (polygon, linear ring)
+"""
+function _equals_curves(c1, c2, closed_type1, closed_type2)
Check all remaining points are the same wrapping around line
jstep = same_direction ? 1 : -1
+ for i in 2:n1
+ ip = GI.getpoint(c1, i)
+ j = jstart + (i - 1) * jstep
+ j += (0 < j <= n2) ? 0 : (n2 * -jstep)
+ jp = GI.getpoint(c2, j)
+ equals(ip, jp) || return false
+ end
+ return true
+end
+
+"""
+ equals(
+ ::Union{GI.LineTrait, GI.LineStringTrait}, l1,
+ ::Union{GI.LineTrait, GI.LineStringTrait}, l2,
+ )::Bool
+
+Two lines/linestrings are equal if they share the same set of points going
+along the curve. Note that lines/linestrings aren't closed by defintion.
+"""
+equals(
+ ::Union{GI.LineTrait, GI.LineStringTrait}, l1,
+ ::Union{GI.LineTrait, GI.LineStringTrait}, l2,
+) = _equals_curves(l1, l2, false, false)
+
+"""
+ equals(
+ ::Union{GI.LineTrait, GI.LineStringTrait}, l1,
+ ::GI.LinearRingTrait, l2,
+ )::Bool
+
+A line/linestring and a linear ring are equal if they share the same set of
+points going along the curve. Note that lines aren't closed by defintion, but
+rings are, so the line must have a repeated last point to be equal
+"""
+equals(
+ ::Union{GI.LineTrait, GI.LineStringTrait}, l1,
+ ::GI.LinearRingTrait, l2,
+) = _equals_curves(l1, l2, false, true)
+
+"""
+ equals(
+ ::GI.LinearRingTrait, l1,
+ ::Union{GI.LineTrait, GI.LineStringTrait}, l2,
+ )::Bool
+
+A linear ring and a line/linestring are equal if they share the same set of
+points going along the curve. Note that lines aren't closed by defintion, but
+rings are, so the line must have a repeated last point to be equal
+"""
+equals(
+ ::GI.LinearRingTrait, l1,
+ ::Union{GI.LineTrait, GI.LineStringTrait}, l2,
+) = _equals_curves(l1, l2, true, false)
+
+"""
+ equals(
+ ::GI.LinearRingTrait, l1,
+ ::GI.LinearRingTrait, l2,
+ )::Bool
+
+Two linear rings are equal if they share the same set of points going along the
+curve. Note that rings are closed by definition, so they can have, but don't
+need, a repeated last point to be equal.
+"""
+equals(
+ ::GI.LinearRingTrait, l1,
+ ::GI.LinearRingTrait, l2,
+) = _equals_curves(l1, l2, true, true)
+
+"""
+ equals(::GI.PolygonTrait, geom_a, ::GI.PolygonTrait, geom_b)::Bool
+
+Two polygons are equal if they share the same exterior edge and holes.
+"""
+function equals(::GI.PolygonTrait, geom_a, ::GI.PolygonTrait, geom_b)
Check if exterior is equal
_equals_curves(
+ GI.getexterior(geom_a), GI.getexterior(geom_b),
+ true, true, # linear rings are closed by definition
+ ) || return false
for ihole in GI.gethole(geom_a)
+ has_match = false
+ for jhole in GI.gethole(geom_b)
+ if _equals_curves(
+ ihole, jhole,
+ true, true, # linear rings are closed by definition
+ )
+ has_match = true
+ break
+ end
+ end
+ has_match || return false
+ end
+ return true
+end
+
+"""
+ equals(::GI.PolygonTrait, geom_a, ::GI.MultiPolygonTrait, geom_b)::Bool
+
+A polygon and a multipolygon are equal if the multipolygon is composed of a
+single polygon that is equivalent to the given polygon.
+"""
+function equals(::GI.PolygonTrait, geom_a, ::MultiPolygonTrait, geom_b)
+ GI.npolygon(geom_b) == 1 || return false
+ return equals(geom_a, GI.getpolygon(geom_b, 1))
+end
+
+"""
+ equals(::GI.MultiPolygonTrait, geom_a, ::GI.PolygonTrait, geom_b)::Bool
+
+A polygon and a multipolygon are equal if the multipolygon is composed of a
+single polygon that is equivalent to the given polygon.
+"""
+equals(trait_a::GI.MultiPolygonTrait, geom_a, trait_b::PolygonTrait, geom_b) =
+ equals(trait_b, geom_b, trait_a, geom_a)
+
+"""
+ equals(::GI.PolygonTrait, geom_a, ::GI.PolygonTrait, geom_b)::Bool
+
+Two multipolygons are equal if they share the same set of polygons.
+"""
+function equals(::GI.MultiPolygonTrait, geom_a, ::GI.MultiPolygonTrait, geom_b)
for poly_a in GI.getpolygon(geom_a)
+ has_match = false
+ for poly_b in GI.getpolygon(geom_b)
+ if equals(poly_a, poly_b)
+ has_match = true
+ break
+ end
+ end
+ has_match || return false
+ end
+ return true
+end
The intersects methods check whether two geometries intersect with each other. The intersection methods return the geometry intersection between the two input geometries. The intersection_points method returns a list of intersection points between two geometries.
The intersects methods will always return a Boolean. However, note that the intersection methods will not all return the same type. For example, the intersection of two lines will be a point in most cases, unless the lines are parallel. On the other hand, the intersection of two polygons will be another polygon in most cases. Finally, the intersection_points method returns a list of tuple points.
This is the GeoInterface-compatible implementation.
First, we implement a wrapper method for intersects, intersection, and intersectionpoints that dispatches to the correct implementation based on the geometry trait. The two underlying helper functions that are widely used in all geometry dispatches are _lineintersects, which determines if two line segments intersect and intersectionpoint which determines the intersection point between two line segments.
"""
+ intersects(geom1, geom2)::Bool
+
+Check if two geometries intersect, returning true if so and false otherwise.
+
+# Example
+
+```jldoctest
+import GeoInterface as GI, GeometryOps as GO
+
+line1 = GI.Line([(124.584961,-12.768946), (126.738281,-17.224758)])
+line2 = GI.Line([(123.354492,-15.961329), (127.22168,-14.008696)])
+GO.intersects(line1, line2)
output
true
+```
+"""
+intersects(geom1, geom2) = intersects(
+ GI.trait(geom1),
+ geom1,
+ GI.trait(geom2),
+ geom2
+)
+
+"""
+ intersects(::GI.LineTrait, a, ::GI.LineTrait, b)::Bool
+
+Returns true if two line segments intersect and false otherwise.
+"""
+function intersects(::GI.LineTrait, a, ::GI.LineTrait, b)
+ a1 = _tuple_point(GI.getpoint(a, 1))
+ a2 = _tuple_point(GI.getpoint(a, 2))
+ b1 = _tuple_point(GI.getpoint(b, 1))
+ b2 = _tuple_point(GI.getpoint(b, 2))
+ meet_type = ExactPredicates.meet(a1, a2, b1, b2)
+ return meet_type == 0 || meet_type == 1
+end
+
+"""
+ intersects(::GI.AbstractTrait, a, ::GI.AbstractTrait, b)::Bool
+
+Returns true if two geometries intersect with one another and false
+otherwise. For all geometries but lines, convert the geometry to a list of edges
+and cross compare the edges for intersections.
+"""
+function intersects(
+ trait_a::GI.AbstractTrait, a_geom,
+ trait_b::GI.AbstractTrait, b_geom,
+) edges_a, edges_b = map(sort! ∘ to_edges, (a_geom, b_geom))
+ return _line_intersects(edges_a, edges_b) ||
+ within(trait_a, a_geom, trait_b, b_geom) ||
+ within(trait_b, b_geom, trait_a, a_geom)
+end
+
+"""
+ _line_intersects(
+ edges_a::Vector{Edge},
+ edges_b::Vector{Edge}
+ )::Bool
+
+Returns true if there is at least one intersection between edges within the
+two lists of edges.
+"""
+function _line_intersects(
+ edges_a::Vector{Edge},
+ edges_b::Vector{Edge}
+)
for edge_a in edges_a
+ for edge_b in edges_b
+ _line_intersects(edge_a, edge_b) && return true
+ end
+ end
+ return false
+end
+
+"""
+ _line_intersects(
+ edge_a::Edge,
+ edge_b::Edge,
+ )::Bool
+
+Returns true if there is at least one intersection between two edges.
+"""
+function _line_intersects(edge_a::Edge, edge_b::Edge)
+ meet_type = ExactPredicates.meet(edge_a..., edge_b...)
+ return meet_type == 0 || meet_type == 1
+end
+
+"""
+ intersection(geom_a, geom_b)::Union{Tuple{::Real, ::Real}, ::Nothing}
+
+Return an intersection point between two geometries. Return nothing if none are
+found. Else, the return type depends on the input. It will be a union between:
+a point, a line, a linear ring, a polygon, or a multipolygon
+
+# Example
+
+```jldoctest
+import GeoInterface as GI, GeometryOps as GO
+
+line1 = GI.Line([(124.584961,-12.768946), (126.738281,-17.224758)])
+line2 = GI.Line([(123.354492,-15.961329), (127.22168,-14.008696)])
+GO.intersection(line1, line2)
T = typeof(edges_a[1][1][1]) # x-coordinate of first point in first edge
+ result = Tuple{T,T}[]
Loop over pairs of edges and add any intersection points to results
for i in eachindex(edges_a)
+ for j in eachindex(edges_b)
+ point, fracs = _intersection_point(edges_a[i], edges_b[j])
+ if !isnothing(point)
+ #=
+ Determine if point is on edge (all edge endpoints excluded
+ except for the last edge for an open geometry)
+ =#
+ α, β = fracs
+ on_a_edge = (!a_closed && i == npoints_a && 0 <= α <= 1) ||
+ (0 <= α < 1)
+ on_b_edge = (!b_closed && j == npoints_b && 0 <= β <= 1) ||
+ (0 <= β < 1)
+ if on_a_edge && on_b_edge
+ push!(result, point)
+ end
+ end
+ end
+ end
+ return result
+ end
+ return nothing
+end
+
+"""
+ _intersection_point(
+ (a1, a2)::Tuple,
+ (b1, b2)::Tuple,
+ )
+
+Calculates the intersection point between two lines if it exists, and as if the
+line extended to infinity, and the fractional component of each line from the
+initial end point to the intersection point.
+Inputs:
+ (a1, a2)::Tuple{Tuple{::Real, ::Real}, Tuple{::Real, ::Real}} first line
+ (b1, b2)::Tuple{Tuple{::Real, ::Real}, Tuple{::Real, ::Real}} second line
+Outputs:
+ (x, y)::Tuple{::Real, ::Real} intersection point
+ (t, u)::Tuple{::Real, ::Real} fractional length of lines to intersection
+ Both are ::Nothing if point doesn't exist!
+
+Calculation derivation can be found here:
+ https://stackoverflow.com/questions/563198/
+"""
+function _intersection_point((a1, a2)::Tuple, (b1, b2)::Tuple)
The overlaps function checks if two geometries overlap. Two geometries can only overlap if they have the same dimension, and if they overlap, but one is not contained, within, or equal to the other.
Note that this means it is impossible for a single point to overlap with a single point and a line only overlaps with another line if only a section of each line is colinear.
To provide an example, consider these two lines:
using GeometryOps
+using GeometryOps.GeometryBasics
+using Makie
+using CairoMakie
+
+l1 = GI.LineString([(0.0, 0.0), (0.0, 10.0)])
+l2 = GI.LineString([(0.0, -10.0), (0.0, 3.0)])
+f, a, p = lines(GI.getpoint(l1), color = :blue)
+scatter!(GI.getpoint(l1), color = :blue)
+lines!(GI.getpoint(l2), color = :orange)
+scatter!(GI.getpoint(l2), color = :orange)
We can see that the two lines overlap in the plot:
This is the GeoInterface-compatible implementation.
First, we implement a wrapper method that dispatches to the correct implementation based on the geometry trait. This is also used in the implementation, since it's a lot less work!
Note that that since only elements of the same dimension can overlap, any two geometries with traits that are of different dimensions autmoatically can return false.
For geometries with the same trait dimension, we must make sure that they share a point, an edge, or area for points, lines, and polygons/multipolygons respectivly, without being contained.
"""
+ overlaps(geom1, geom2)::Bool
+
+Compare two Geometries of the same dimension and return true if their
+intersection set results in a geometry different from both but of the same
+dimension. This means one geometry cannot be within or contain the other and
+they cannot be equal
+
+# Examples
+```jldoctest
+import GeometryOps as GO, GeoInterface as GI
+poly1 = GI.Polygon([[(0,0), (0,5), (5,5), (5,0), (0,0)]])
+poly2 = GI.Polygon([[(1,1), (1,6), (6,6), (6,1), (1,1)]])
+
+GO.overlaps(poly1, poly2)
output
true
+```
+"""
+overlaps(geom1, geom2)::Bool = overlaps(
+ GI.trait(geom1),
+ geom1,
+ GI.trait(geom2),
+ geom2,
+)
+
+"""
+ overlaps(::GI.AbstractTrait, geom1, ::GI.AbstractTrait, geom2)::Bool
+
+For any non-specified pair, all have non-matching dimensions, return false.
+"""
+overlaps(::GI.AbstractTrait, geom1, ::GI.AbstractTrait, geom2) = false
+
+"""
+ overlaps(
+ ::GI.MultiPointTrait, points1,
+ ::GI.MultiPointTrait, points2,
+ )::Bool
+
+If the multipoints overlap, meaning some, but not all, of the points within the
+multipoints are shared, return true.
+"""
+function overlaps(
+ ::GI.MultiPointTrait, points1,
+ ::GI.MultiPointTrait, points2,
+)
+ one_diff = false # assume that all the points are the same
+ one_same = false # assume that all points are different
+ for p1 in GI.getpoint(points1)
+ match_point = false
+ for p2 in GI.getpoint(points2)
+ if equals(p1, p2) # Point is shared
+ one_same = true
+ match_point = true
+ break
+ end
+ end
+ one_diff |= !match_point # Point isn't shared
+ one_same && one_diff && return true
+ end
+ return false
+end
+
+"""
+ overlaps(::GI.LineTrait, line1, ::GI.LineTrait, line)::Bool
+
+If the lines overlap, meaning that they are colinear but each have one endpoint
+outside of the other line, return true. Else false.
+"""
+overlaps(::GI.LineTrait, line1, ::GI.LineTrait, line) =
+ _overlaps((a1, a2), (b1, b2))
+
+"""
+ overlaps(
+ ::Union{GI.LineStringTrait, GI.LinearRing}, line1,
+ ::Union{GI.LineStringTrait, GI.LinearRing}, line2,
+ )::Bool
+
+If the curves overlap, meaning that at least one edge of each curve overlaps,
+return true. Else false.
+"""
+function overlaps(
+ ::Union{GI.LineStringTrait, GI.LinearRing}, line1,
+ ::Union{GI.LineStringTrait, GI.LinearRing}, line2,
+)
+ edges_a, edges_b = map(sort! ∘ to_edges, (line1, line2))
+ for edge_a in edges_a
+ for edge_b in edges_b
+ _overlaps(edge_a, edge_b) && return true
+ end
+ end
+ return false
+end
+
+"""
+ overlaps(
+ trait_a::GI.PolygonTrait, poly_a,
+ trait_b::GI.PolygonTrait, poly_b,
+ )::Bool
+
+If the two polygons intersect with one another, but are not equal, return true.
+Else false.
+"""
+function overlaps(
+ trait_a::GI.PolygonTrait, poly_a,
+ trait_b::GI.PolygonTrait, poly_b,
+)
+ edges_a, edges_b = map(sort! ∘ to_edges, (poly_a, poly_b))
+ return _line_intersects(edges_a, edges_b) &&
+ !equals(trait_a, poly_a, trait_b, poly_b)
+end
+
+"""
+ overlaps(
+ ::GI.PolygonTrait, poly1,
+ ::GI.MultiPolygonTrait, polys2,
+ )::Bool
+
+Return true if polygon overlaps with at least one of the polygons within the
+multipolygon. Else false.
+"""
+function overlaps(
+ ::GI.PolygonTrait, poly1,
+ ::GI.MultiPolygonTrait, polys2,
+)
+ for poly2 in GI.getgeom(polys2)
+ overlaps(poly1, poly2) && return true
+ end
+ return false
+end
+
+"""
+ overlaps(
+ ::GI.MultiPolygonTrait, polys1,
+ ::GI.PolygonTrait, poly2,
+ )::Bool
+
+Return true if polygon overlaps with at least one of the polygons within the
+multipolygon. Else false.
+"""
+overlaps(trait1::GI.MultiPolygonTrait, polys1, trait2::GI.PolygonTrait, poly2) =
+ overlaps(trait2, poly2, trait1, polys1)
+
+"""
+ overlaps(
+ ::GI.MultiPolygonTrait, polys1,
+ ::GI.MultiPolygonTrait, polys2,
+ )::Bool
+
+Return true if at least one pair of polygons from multipolygons overlap. Else
+false.
+"""
+function overlaps(
+ ::GI.MultiPolygonTrait, polys1,
+ ::GI.MultiPolygonTrait, polys2,
+)
+ for poly1 in GI.getgeom(polys1)
+ overlaps(poly1, polys2) && return true
+ end
+ return false
+end
+
+"""
+ _overlaps(
+ (a1, a2)::Edge,
+ (b1, b2)::Edge
+ )::Bool
+
+If the edges overlap, meaning that they are colinear but each have one endpoint
+outside of the other edge, return true. Else false.
+"""
+function _overlaps(
+ (a1, a2)::Edge,
+ (b1, b2)::Edge
+)
This returns a list of GeometryBasics.Polygon, which can be plotted immediately, or wrapped directly in a GeometryBasics.MultiPolygon. Let's see how these look:
f, a, p = poly(polygons; label = "Polygonized polygons", axis = (; aspect = DataAspect()))
Finally, let's plot the Makie contour lines on top, to see how well the polygonization worked:
"""
+ polygonize(A; minpoints=10)
+ polygonize(xs, ys, A; minpoints=10)
+
+Convert matrix `A` to polygons.
+
+If `xs` and `ys` are passed in they are used as the pixel center points.
Keywords
- `minpoints`: ignore polygons with less than `minpoints` points.
+"""
+polygonize(A::AbstractMatrix; kw...) = polygonize(axes(A)..., A; kw...)
+
+function polygonize(xs, ys, A::AbstractMatrix; minpoints=10)
+ # This function uses a lazy map to get contours.
+ contours = Iterators.map(get_contours(A)) do contour
+ poly = map(contour) do xy
+ x, y = Tuple(xy)
+ Point2f(x + first(xs) - 1, y + first(ys) - 1)
+ end
+ end
+ # If we filter off the minimum points, then it's a hair more efficient
+ # not to convert contours with length < missingpoints to polygons.
+ if minpoints > 1
+ contours = Iterators.filter(contours) do contour
+ length(contour) > minpoints
+ end
+ return map(Polygon, contours)
+ else
+ return map(Polygon, contours)
+ end
+end
+
+# rotate direction clockwise
+rot_clockwise(dir) = (dir) % 8 + 1
+# rotate direction counterclockwise
+rot_counterclockwise(dir) = (dir + 6) % 8 + 1
+
+# move from current pixel to next in given direction
+function move(pixel, image, dir, dir_delta)
+ newp = pixel + dir_delta[dir]
+ height, width = size(image)
+ if (0 < newp[1] <= height) && (0 < newp[2] <= width)
+ if image[newp] != 0
+ return newp
+ end
+ end
+ return CartesianIndex(0, 0)
+end
+
+# finds direction between two given pixels
+function from_to(from, to, dir_delta)
+ delta = to - from
+ return findall(x -> x == delta, dir_delta)[1]
+end
+
+function detect_move(image, p0, p2, nbd, border, done, dir_delta)
+ dir = from_to(p0, p2, dir_delta)
+ moved = rot_clockwise(dir)
+ p1 = CartesianIndex(0, 0)
+ while moved != dir ## 3.1
+ newp = move(p0, image, moved, dir_delta)
+ if newp[1] != 0
+ p1 = newp
+ break
+ end
+ moved = rot_clockwise(moved)
+ end
+
+ if p1 == CartesianIndex(0, 0)
+ return
+ end
+
+ p2 = p1 ## 3.2
+ p3 = p0 ## 3.2
+ done .= false
+ while true
+ dir = from_to(p3, p2, dir_delta)
+ moved = rot_counterclockwise(dir)
+ p4 = CartesianIndex(0, 0)
+ done .= false
+ while true ## 3.3
+ p4 = move(p3, image, moved, dir_delta)
+ if p4[1] != 0
+ break
+ end
+ done[moved] = true
+ moved = rot_counterclockwise(moved)
+ end
+ push!(border, p3) ## 3.4
+ if p3[1] == size(image, 1) || done[3]
+ image[p3] = -nbd
+ elseif image[p3] == 1
+ image[p3] = nbd
+ end
+
+ if (p4 == p0 && p3 == p1) ## 3.5
+ break
+ end
+ p2 = p3
+ p3 = p4
+ end
+end
+
+"""
+ get_contours(A::AbstractMatrix)
+
+Returns contours as vectors of `CartesianIndex`.
+"""
+function get_contours(image::AbstractMatrix)
+ nbd = 1
+ lnbd = 1
+ image = Float64.(image)
+ contour_list = Vector{typeof(CartesianIndex[])}()
+ done = [false, false, false, false, false, false, false, false]
+
+ # Clockwise Moore neighborhood.
+ dir_delta = (CartesianIndex(-1, 0), CartesianIndex(-1, 1), CartesianIndex(0, 1), CartesianIndex(1, 1),
+ CartesianIndex(1, 0), CartesianIndex(1, -1), CartesianIndex(0, -1), CartesianIndex(-1, -1))
+
+ height, width = size(image)
+
+ for i = 1:height
+ lnbd = 1
+ for j = 1:width
+ fji = image[i, j]
+ is_outer = (image[i, j] == 1 && (j == 1 || image[i, j-1] == 0)) ## 1 (a)
+ is_hole = (image[i, j] >= 1 && (j == width || image[i, j+1] == 0))
+
+ if is_outer || is_hole
+ # 2
+ border = CartesianIndex[]
+ from = CartesianIndex(i, j)
+
+ if is_outer
+ nbd += 1
+ from -= CartesianIndex(0, 1)
+
+ else
+ nbd += 1
+ if fji > 1
+ lnbd = fji
+ end
+ from += CartesianIndex(0, 1)
+ end
+
+ p0 = CartesianIndex(i, j)
+ detect_move(image, p0, from, nbd, border, done, dir_delta) ## 3
+ if isempty(border) ##TODO
+ push!(border, p0)
+ image[p0] = -nbd
+ end
+ push!(contour_list, border)
+ end
+ if fji != 0 && fji != 1
+ lnbd = abs(fji)
+ end
+
+ end
+ end
+
+ return contour_list
+end
export within
+
+
+"""
+ within(geom1, geom)::Bool
+
+Return `true` if the first geometry is completely within the second geometry.
+The interiors of both geometries must intersect and, the interior and boundary of the primary (geometry a)
+must not intersect the exterior of the secondary (geometry b).
+`within` returns the exact opposite result of `contains`.
+
+# Examples
+```jldoctest setup=:(using GeometryOps, GeometryBasics)
+import GeometryOps as GO, GeoInterface as GI
+
+line = GI.LineString([(1, 1), (1, 2), (1, 3), (1, 4)])
+point = (1, 2)
+GO.within(point, line)
const THREADED_KEYWORD = "- `threaded`: `true` or `false`. Whether to use multithreading. Defaults to `false`."
+const CRS_KEYWORD = "- `crs`: The CRS to attach to geometries. Defaults to `nothing`."
+const CALC_EXTENT_KEYWORD = "- `calc_extent`: `true` or `false`. Whether to calculate the extent. Defaults to `false`."
+
+const APPLY_KEYWORDS = """
+$THREADED_KEYWORD
+$CRS_KEYWORD
+$CALC_EXTENT_KEYWORD
+"""
apply applies some function to every geometry matching the Target GeoInterface trait, in some arbitrarily nested object made up of:
AbstractArrays (we also try to iterate other non-GeoInteface compatible object)
FeatureCollectionTrait objects
FeatureTrait objects
AbstractGeometryTrait objects
apply recursively calls itself through these nested layers until it reaches objects with the Target GeoInterface trait. When found apply applies the function f, and stops.
The outer recursive functions then progressively rebuild the object using GeoInterface objects matching the original traits.
If PointTrait is found but it is not the Target, an error is thrown. This likely means the object contains a different geometry trait to the target, such as MultiPointTrait when LineStringTrait was specified.
To handle this possibility it may be necessary to make Target a Union of traits found at the same level of nesting, and define methods of f to handle all cases.
Be careful making a union across "levels" of nesting, e.g. Union{FeatureTrait,PolygonTrait}, as _apply will just never reach PolygonTrait when all the polygons are wrapped in a FeatureTrait object.
extent and crs can be embedded in all geometries, features, and feature collections as part of apply. Geometries deeper than Target will of course not have new extent or crs embedded.
calc_extent signals to recalculate an Extent and embed it.
Threading is used at the outermost level possible - over an array, feature collection, or e.g. a MultiPolygonTrait where each PolygonTrait sub-geometry may be calculated on a different thread.
"""
+ apply(f, target::Type{<:AbstractTrait}, obj; kw...)
+
+Reconstruct a geometry, feature, feature collection, or nested vectors of
+either using the function `f` on the `target` trait.
+
+`f(target_geom) => x` where `x` also has the `target` trait, or a trait that can
+be substituted. For example, swapping `PolgonTrait` to `MultiPointTrait` will fail
+if the outer object has `MultiPolygonTrait`, but should work if it has `FeatureTrait`.
+
+Objects "shallower" than the target trait are always completely rebuilt, like
+a `Vector` of `FeatureCollectionTrait` of `FeatureTrait` when the target
+has `PolygonTrait` and is held in the features. But "deeper" objects may remain
+unchanged - such as points and linear rings if the target is the same `PolygonTrait`.
+
+The result is a functionally similar geometry with values depending on `f`
+
+$APPLY_KEYWORDS
Example
Flipped point the order in any feature or geometry, or iterables of either:
+
+```juia
+import GeoInterface as GI
+import GeometryOps as GO
+geom = GI.Polygon([GI.LinearRing([(1, 2), (3, 4), (5, 6), (1, 2)]),
+ GI.LinearRing([(3, 4), (5, 6), (6, 7), (3, 4)])])
+
+flipped_geom = GO.apply(GI.PointTrait, geom) do p
+ (GI.y(p), GI.x(p))
+end
+"""
+apply(f, ::Type{Target}, geom; kw...) where Target = _apply(f, Target, geom; kw...)
There is no trait and this is an AbstractArray - so just iterate over it calling _apply on the contents
function _apply(f, ::Type{Target}, ::Nothing, A::AbstractArray; threaded=false, kw...) where Target
For an Array there is nothing else to do but map _apply over all values maptasks may run this level threaded if threaded==true, but deeper `apply` called in the closure will not be threaded
_maptasks(eachindex(A); threaded) do i
+ _apply(f, Target, A[i]; threaded=false, kw...)
+ end
+end
There is no trait and this is not an AbstractArray. Try to call _apply over it. We can't use threading as we don't know if we can can index into it. So just map. (TODO: maybe collect first if threaded=true so we can thread?)
Rewrap all FeatureCollectionTrait feature collections as GI.FeatureCollection
function _apply(f, ::Type{Target}, ::GI.FeatureCollectionTrait, fc;
+ crs=GI.crs(fc), calc_extent=false, threaded=false
+) where Target
Run _apply on all features in the feature collection, possibly threaded
features = _maptasks(1:GI.nfeature(fc); threaded) do i
+ feature = GI.getfeature(fc, i)
+ _apply(f, Target, feature; crs, calc_extent, threaded=false)::GI.Feature
+ end
+ if calc_extent
Return a new geometry of the same trait as geom, holding tnew geoms with crs and calcualted extent
return rebuild(geom, geoms; crs, extent)
+ else
Return a new geometryof the same trait as geom, holding the new geoms with crs
return rebuild(geom, geoms; crs)
+ end
+end
Fail loudly if we hit PointTrait without running f (after PointTrait there is no further to dig with _apply)
_apply(f, ::Type{Target}, trait::GI.PointTrait, geom; crs=nothing, kw...) where Target =
+ throw(ArgumentError("target $Target not found, but reached a `PointTrait` leaf"))
Finally, these short methods are the main purpose of apply. The Trait is a subtype of the Target (or identical to it) So the Target is found. We apply f to geom and return it to previous _apply calls to be wrapped with the outer geometries/feature/featurecollection/array.
_apply(f, ::Type{Target}, ::Trait, geom; crs=GI.crs(geom), kw...) where {Target,Trait<:Target} = f(geom)
Define some specific cases of this match to avoid method ambiguity
_apply(f, ::Type{GI.PointTrait}, trait::GI.PointTrait, geom; kw...) = f(geom)
+_apply(f, ::Type{GI.FeatureTrait}, ::GI.FeatureTrait, feature; kw...) = f(feature)
+_apply(f, ::Type{GI.FeatureCollectionTrait}, ::GI.FeatureCollectionTrait, fc; kw...) = f(fc)
+
+"""
+ unwrap(target::Type{<:AbstractTrait}, obj)
+ unwrap(f, target::Type{<:AbstractTrait}, obj)
+
+Unwrap the object newst to vectors, down to the target trait.
+
+If `f` is passed in it will be applied to the target geometries
+as they are found.
+"""
+function unwrap end
+unwrap(target::Type, geom) = unwrap(identity, target, geom)
unwrap(f, ::Type{Target}, ::Trait, geom) where {Target,Trait<:Target} = f(geom)
Fail if we hit PointTrait
unwrap(f, target::Type, trait::GI.PointTrait, geom) =
+ throw(ArgumentError("target $target not found, but reached a `PointTrait` leaf"))
Specific cases to avoid method ambiguity
unwrap(f, target::Type{GI.PointTrait}, trait::GI.PointTrait, geom) = f(geom)
+unwrap(f, target::Type{GI.FeatureTrait}, ::GI.FeatureTrait, feature) = f(feature)
+unwrap(f, target::Type{GI.FeatureCollectionTrait}, ::GI.FeatureCollectionTrait, fc) = f(fc)
+
+"""
+ flatten(target::Type{<:GI.AbstractTrait}, obj)
+ flatten(f, target::Type{<:GI.AbstractTrait}, obj)
+
+Lazily flatten any `AbstractArray`, iterator, `FeatureCollectionTrait`,
+`FeatureTrait` or `AbstractGeometryTrait` object `obj`, so that objects
+with the `target` trait are returned by the iterator.
+
+If `f` is passed in it will be applied to the target geometries.
+"""
+flatten(::Type{Target}, geom) where {Target<:GI.AbstractTrait} = flatten(identity, Target, geom)
+flatten(f, ::Type{Target}, geom) where {Target<:GI.AbstractTrait} = _flatten(f, Target, geom)
+
+_flatten(f, ::Type{Target}, geom) where Target = _flatten(f, Target, GI.trait(geom), geom)
_flatten(f, ::Type{Target}, trait::GI.PointTrait, geom) where Target =
+ throw(ArgumentError("target $Target not found, but reached a `PointTrait` leaf"))
Specific cases to avoid method ambiguity
_flatten(f, ::Type{<:GI.PointTrait}, ::GI.PointTrait, geom) = (f(geom),)
+_flatten(f, ::Type{<:GI.FeatureTrait}, ::GI.FeatureTrait, feature) = (f(feature),)
+_flatten(f, ::Type{<:GI.FeatureCollectionTrait}, ::GI.FeatureCollectionTrait, fc) = (f(fc),)
+
+
+"""
+ reconstruct(geom, components)
+
+Reconstruct `geom` from an iterable of component objects that match its structure.
+
+All objects in `components` must have the same `GeoInterface.trait`.
+
+Ususally used in combination with `flatten`.
+"""
+function reconstruct(geom, components)
+ obj, iter = _reconstruct(geom, components)
+ return obj
+end
+
+_reconstruct(geom, components) =
+ _reconstruct(typeof(GI.trait(first(components))), geom, components, 1)
+_reconstruct(::Type{Target}, geom, components, iter) where Target =
+ _reconstruct(Target, GI.trait(geom), geom, components, iter)
Try to reconstruct over iterables
function _reconstruct(::Type{Target}, ::Nothing, iterable, components, iter) where Target
+ vect = map(iterable) do x
iter is updated by _reconstruct here
obj, iter = _reconstruct(Target, x, components, iter)
+ obj
+ end
+ return vect, iter
+end
Reconstruct feature collections
function _reconstruct(::Type{Target}, ::GI.FeatureCollectionTrait, fc, components, iter) where Target
+ features = map(GI.getfeature(fc)) do feature
iter is updated by _reconstruct here
newfeature, iter = _reconstruct(Target, feature, components, iter)
+ newfeature
+ end
+ return GI.FeatureCollection(features; crs=GI.crs(fc)), iter
+end
+function _reconstruct(::Type{Target}, ::GI.FeatureTrait, feature, components, iter) where Target
+ geom, iter = _reconstruct(Target, GI.geometry(feature), components, iter)
+ return GI.Feature(geom; properties=GI.properties(feature), crs=GI.crs(feature)), iter
+end
+function _reconstruct(::Type{Target}, trait, geom, components, iter) where Target
+ geoms = map(GI.getgeom(geom)) do subgeom
iter is updated by _reconstruct here
subgeom1, iter = _reconstruct(Target, GI.trait(subgeom), subgeom, components, iter)
+ subgeom1
+ end
+ return rebuild(geom, geoms), iter
+end
_reconstruct(::Type{Target}, trait::GI.PointTrait, geom, components, iter) where Target =
+ throw(ArgumentError("target $Target not found, but reached a `PointTrait` leaf"))
+
+
+const BasicsGeoms = Union{GB.AbstractGeometry,GB.AbstractFace,GB.AbstractPoint,GB.AbstractMesh,
+ GB.AbstractPolygon,GB.LineString,GB.MultiPoint,GB.MultiLineString,GB.MultiPolygon,GB.Mesh}
+
+"""
+ rebuild(geom, child_geoms)
+
+Rebuild a geometry from child geometries.
+
+By default geometries will be rebuilt as a `GeoInterface.Wrappers`
+geometry, but `rebuild` can have methods added to it to dispatch
+on geometries from other packages and specify how to rebuild them.
+
+(Maybe it should go into GeoInterface.jl)
+"""
+rebuild(geom, child_geoms; kw...) = rebuild(GI.trait(geom), geom, child_geoms; kw...)
+function rebuild(trait::GI.AbstractTrait, geom, child_geoms; crs=GI.crs(geom), extent=nothing)
+ T = GI.geointerface_geomtype(trait)
+ if GI.is3d(geom)
The Boolean type parameters here indicate 3d-ness and measure coordinate presence respectively.
"""
+ embed_extent(obj)
+
+Recursively wrap the object with a GeoInterface.jl geometry,
+calculating and adding an `Extents.Extent` to all objects.
+
+This can improve performance when extents need to be checked multiple times,
+such when needing to check if many points are in geometries, and using their extents
+as a quick filter for obviously exterior points.
This is a simple example of how to use the apply functionality in a function, by flipping the x and y coordinates of a geometry.
"""
+ flip(obj)
+
+Swap all of the x and y coordinates in obj, otherwise
+keeping the original structure (but not necessarily the
+original type).
+
+# Keywords
+
+$APPLY_KEYWORDS
+"""
+function flip(geom; kw...)
+ if _is3d(geom)
+ return apply(PointTrait, geom; kw...) do p
+ (GI.y(p), GI.x(p), GI.z(p))
+ end
+ else
+ return apply(PointTrait, geom; kw...) do p
+ (GI.y(p), GI.x(p))
+ end
+ end
+end
This file is pretty simple - it simply reprojects a geometry pointwise from one CRS to another. It uses the Proj package for the transformation, but this could be moved to an extension if needed.
This works using the apply functionality.
"""
+ reproject(geometry; source_crs, target_crs, transform, always_xy, time)
+ reproject(geometry, source_crs, target_crs; always_xy, time)
+ reproject(geometry, transform; always_xy, time)
+
+Reproject any GeoInterface.jl compatible `geometry` from `source_crs` to `target_crs`.
+
+The returned object will be constructed from `GeoInterface.WrapperGeometry`
+geometries, wrapping views of a `Vector{Proj.Point{D}}`, where `D` is the dimension.
+
+# Arguments
+
+- `geometry`: Any GeoInterface.jl compatible geometries.
+- `source_crs`: the source coordinate referece system, as a GeoFormatTypes.jl object or a string.
+- `target_crs`: the target coordinate referece system, as a GeoFormatTypes.jl object or a string.
+
+If these a passed as keywords, `transform` will take priority.
+Without it `target_crs` is always needed, and `source_crs` is
+needed if it is not retreivable from the geometry with `GeoInterface.crs(geometry)`.
+
+# Keywords
+
+- `always_xy`: force x, y coordinate order, `true` by default.
+ `false` will expect and return points in the crs coordinate order.
+- `time`: the time for the coordinates. `Inf` by default.
+$APPLY_KEYWORDS
+"""
+function reproject(geom;
+ source_crs=nothing, target_crs=nothing, transform=nothing, kw...
+)
+ if isnothing(transform)
+ if isnothing(source_crs)
+ source_crs = if GI.trait(geom) isa Nothing && geom isa AbstractArray
+ GeoInterface.crs(first(geom))
+ else
+ GeoInterface.crs(geom)
+ end
+ end
If its still nothing, error
isnothing(source_crs) && throw(ArgumentError("geom has no crs attatched. Pass a `source_crs` keyword"))
Otherwise reproject
reproject(geom, source_crs, target_crs; kw...)
+ else
+ reproject(geom, transform; kw...)
+ end
+end
+function reproject(geom, source_crs, target_crs;
+ time=Inf,
+ always_xy=true,
+ transform=Proj.Transformation(Proj.CRS(source_crs), Proj.CRS(target_crs); always_xy),
+ kw...
+)
+ reproject(geom, transform; time, target_crs, kw...)
+end
+function reproject(geom, transform::Proj.Transformation; time=Inf, target_crs=nothing, kw...)
+ if _is3d(geom)
+ return apply(PointTrait, geom; crs=target_crs, kw...) do p
+ transform(GI.x(p), GI.y(p), GI.z(p))
+ end
+ else
+ return apply(PointTrait, geom; crs=target_crs, kw...) do p
+ transform(GI.x(p), GI.y(p))
+ end
+ end
+end
This file holds implementations for the Douglas-Peucker and Visvalingam-Whyatt algorithms for simplifying geometries (specifically polygons and lines).
export simplify, VisvalingamWhyatt, DouglasPeucker, RadialDistance
+
+
+"""
+ abstract type SimplifyAlg
+
+Abstract type for simplification algorithms.
+
+# API
+
+For now, the algorithm must hold the `number`, `ratio` and `tol` properties.
+
+Simplification algorithm types can hook into the interface by implementing
+the `_simplify(trait, alg, geom)` methods for whichever traits are necessary.
+"""
+abstract type SimplifyAlg end
+
+const SIMPLIFY_ALG_KEYWORDS = """
+# Keywords
+
+- `ratio`: the fraction of points that should remain after `simplify`.
+ Useful as it will generalise for large collections of objects.
+- `number`: the number of points that should remain after `simplify`.
+ Less useful for large collections of mixed size objects.
+"""
+
+const MIN_POINTS = 3
+
+function checkargs(number, ratio, tol)
+ count(isnothing, (number, ratio, tol)) == 2 ||
+ error("Must provide one of `number`, `ratio` or `tol` keywords")
+ if !isnothing(ratio)
+ if ratio <= 0 || ratio > 1
+ error("`ratio` must be 0 < ratio <= 1. Got $ratio")
+ end
+ end
+ if !isnothing(number)
+ if number < MIN_POINTS
+ error("`number` must be $MIN_POINTS or larger. Got $number")
+ end
+ end
+ return nothing
+end
+
+"""
+ simplify(obj; kw...)
+ simplify(::SimplifyAlg, obj; kw...)
+
+Simplify a geometry, feature, feature collection,
+or nested vectors or a table of these.
+
+`RadialDistance`, `DouglasPeucker`, or
+`VisvalingamWhyatt` algorithms are available,
+listed in order of increasing quality but decreaseing performance.
+
+`PoinTrait` and `MultiPointTrait` are returned unchanged.
+
+The default behaviour is `simplify(DouglasPeucker(; kw...), obj)`.
+Pass in other `SimplifyAlg` to use other algorithms.
Keywords
$APPLY_KEYWORDS
+
+Keywords for DouglasPeucker are allowed when no algorithm is specified:
+
+$SIMPLIFY_ALG_KEYWORDS
"""
+ tuples(obj)
+
+Convert all points in `obj` to `Tuple`s, wherever the are nested.
+
+Returns a similar object or collection of objects using GeoInterface.jl
+geometries wrapping `Tuple` points.
Keywords
$APPLY_KEYWORDS
+"""
+function tuples(geom; kw...)
+ if _is3d(geom)
+ return apply(PointTrait, geom; kw...) do p
+ (Float64(GI.x(p)), Float64(GI.y(p)), Float64(GI.z(p)))
+ end
+ else
+ return apply(PointTrait, geom; kw...) do p
+ (Float64(GI.x(p)), Float64(GI.y(p)))
+ end
+ end
+end