From 2f51e457204306d1afb87759d2e13f431560c414 Mon Sep 17 00:00:00 2001 From: gamzeozgul Date: Sun, 13 Jul 2025 02:10:25 +0300 Subject: [PATCH] docs: add intersection examples to README and example directory - Add comprehensive intersection example in README.md showing both line-polygon and line-line intersections - Create example/example_intersection.go with working code examples - Address issue #105 by providing clear documentation on how to calculate geometric intersections - Examples include error handling and expected output for better user understanding --- README.md | 50 +++++++++++++++++++++++++++++++++ example/example_intersection.go | 44 +++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 example/example_intersection.go diff --git a/README.md b/README.md index 3d1f505..f119b02 100644 --- a/README.md +++ b/README.md @@ -76,6 +76,56 @@ func main() { Example: geoencoding [example_encoding.go](https://github.com/spatial-go/geoos/example/example_encoding.go) +Example: Calculating `intersection` of two geometries via `Geoos` +```go +package main + +import ( + "fmt" + + "github.com/spatial-go/geoos/geoencoding" + "github.com/spatial-go/geoos/planar" +) + +func main() { + // First, choose the default algorithm. + strategy := planar.NormalStrategy() + + // Create two geometries for intersection + // Example 1: Line intersecting with a polygon + polygon, _ := geoencoding.Decode([]byte(`POLYGON((0 0,0 1,0 2,0 3,1 3,1 2,1 1,2 1,2 2,3 2,3 1,4 1,4 2,5 2,5 1,5 0,4 0,3 0,2 0,1 0,0 0))`), geoencoding.WKT) + line, _ := geoencoding.Decode([]byte(`LINESTRING(-5 0, 5 5)`), geoencoding.WKT) + + // Calculate intersection + intersection, err := strategy.Intersection(polygon, line) + if err != nil { + fmt.Printf("Error: %v\n", err) + return + } + + // Output result + result := string(geoencoding.Encode(intersection, geoencoding.WKT)) + fmt.Printf("Intersection result: %s\n", result) + // Output: LINESTRING(0 2.5,1 3) + + // Example 2: Two lines intersecting at a point + line1, _ := geoencoding.Decode([]byte(`LINESTRING(0 0, 2 2)`), geoencoding.WKT) + line2, _ := geoencoding.Decode([]byte(`LINESTRING(0 2, 2 0)`), geoencoding.WKT) + + intersection2, err := strategy.Intersection(line1, line2) + if err != nil { + fmt.Printf("Error: %v\n", err) + return + } + + result2 := string(geoencoding.Encode(intersection2, geoencoding.WKT)) + fmt.Printf("Lines intersection: %s\n", result2) + // Output: POINT(1 1) +} +``` + +[example_intersection.go](https://github.com/spatial-go/geoos/example/example_intersection.go) + ## Maintainer [@spatial-go](https://github.com/spatial-go)。 diff --git a/example/example_intersection.go b/example/example_intersection.go new file mode 100644 index 0000000..907fd8f --- /dev/null +++ b/example/example_intersection.go @@ -0,0 +1,44 @@ +package main + +import ( +"fmt" + +"github.com/spatial-go/geoos/geoencoding" +"github.com/spatial-go/geoos/planar" +) + +func main() { +// First, choose the default algorithm. +strategy := planar.NormalStrategy() + +// Create two geometries for intersection +// Example 1: Line intersecting with a polygon +polygon, _ := geoencoding.Decode([]byte(`POLYGON((0 0,0 1,0 2,0 3,1 3,1 2,1 1,2 1,2 2,3 2,3 1,4 1,4 2,5 2,5 1,5 0,4 0,3 0,2 0,1 0,0 0))`), geoencoding.WKT) +line, _ := geoencoding.Decode([]byte(`LINESTRING(-5 0, 5 5)`), geoencoding.WKT) + +// Calculate intersection +intersection, err := strategy.Intersection(polygon, line) +if err != nil { +fmt.Printf("Error: %v\n", err) +return +} + +// Output result +result := string(geoencoding.Encode(intersection, geoencoding.WKT)) +fmt.Printf("Intersection result: %s\n", result) +// Output: LINESTRING(0 2.5,1 3) + +// Example 2: Two lines intersecting at a point +line1, _ := geoencoding.Decode([]byte(`LINESTRING(0 0, 2 2)`), geoencoding.WKT) +line2, _ := geoencoding.Decode([]byte(`LINESTRING(0 2, 2 0)`), geoencoding.WKT) + +intersection2, err := strategy.Intersection(line1, line2) +if err != nil { +fmt.Printf("Error: %v\n", err) +return +} + +result2 := string(geoencoding.Encode(intersection2, geoencoding.WKT)) +fmt.Printf("Lines intersection: %s\n", result2) +// Output: POINT(1 1) +}