Skip to content

Find intersection of two geometries? #105

Open

Description

@rmx90210

Hello, and thank you for this project!

Is it possible to calculate the spatial intersection of two geometries? Say, the point where two lines intersect? I wasn't able to find it in the docs.

Activity

  1. ewsterrenburg commented on Jan 7, 2025

    @ewsterrenburg

    Agreed that having only the https://pkg.go.dev/github.com/spatial-go/geoos as documentation makes things a bit of a guessing game to find the right functions (and how to apply them). For an intersection, you could use the following:

    package main
    
    import (
    	"fmt"
    
    	"github.com/spatial-go/geoos/geoencoding"
    	"github.com/spatial-go/geoos/planar"
    )
    
    func main() {
    	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)
    
    	strategy := planar.NormalStrategy()
    	i, _ := strategy.Intersection(polygon, line)
    	fmt.Println(string(geoencoding.Encode(i, geoencoding.WKT)))
    }

    (prints "LINESTRING(0 2.5,1 3)")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions