Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions geom.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,79 @@ func GetCoordinates(g Geometry) (pts []Point, err error) {
err = getCoordinates(g, &pts)
return pts, err
}

// extractLines is a helper function for ExtractLines to avoid too many
// array copies and still provide a convenient interface to the user.
func extractLines(g Geometry, lines *[]Line) error {
switch gg := g.(type) {

default:

return ErrUnknownGeometry

case Pointer:

return nil

case MultiPointer:

return nil

case LineStringer:

v := gg.Verticies()
for i := 0; i < len(v) - 1; i++ {
*lines = append(*lines, Line{v[i], v[i + 1]})
}
return nil

case MultiLineStringer:

for _, ls := range gg.LineStrings() {
if err := extractLines(LineString(ls), lines); err != nil {
return err
}
}
return nil

case Polygoner:

for _, ls := range gg.LinearRings() {
if err := extractLines(LineString(ls), lines); err != nil {
return err
}
}
return nil

case MultiPolygoner:

for _, p := range gg.Polygons() {
if err := extractLines(Polygon(p), lines); err != nil {
return err
}
}
return nil

case Collectioner:

for _, child := range gg.Geometries() {
if err := extractLines(child, lines); err != nil {
return err
}
}
return nil

}
}

/*
ExtractLines extracts all linear components from a geometry (line segements).
If the geometry contains no line segements (e.g. empty geometry or
point), then an empty array will be returned.

Duplicate lines will not be removed.
*/
func ExtractLines(g Geometry) (lines []Line, err error) {
err = extractLines(g, &lines)
return lines, err
}
119 changes: 119 additions & 0 deletions geom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,122 @@ func TestGetCoordinates(t *testing.T) {
t.Run(strconv.FormatInt(int64(i), 10), func(t *testing.T) { fn(t, tc) })
}
}

func TestExtractLines(t *testing.T) {

type tcase struct {
geom Geometry
expected []Line
err error
}

fn := func(t *testing.T, tc tcase) {
r, err := ExtractLines(tc.geom)
if err != tc.err {
t.Errorf("error, expected %v got %v", tc.err, err)
}
if !(len(r) == 0 && len(tc.expected) == 0) && !reflect.DeepEqual(r, tc.expected) {
t.Errorf("error, expected %v got %v", tc.expected, r)
}
}
testcases := []tcase{
{
geom: Point{10, 20},
expected: []Line{},
err: nil,
},
{
geom: MultiPoint{
{10, 20},
{30, 40},
{-10, -5},
},
expected: []Line{},
err: nil,
},
{
geom: LineString{
{10, 20},
{30, 40},
{-10, -5},
},
expected: []Line{{{10, 20}, {30, 40}}, {{30, 40}, {-10, -5}}},
err: nil,
},
{
geom: MultiLineString{
{
{10, 20},
{30, 40},
},
{
{-10, -5},
{15, 20},
},
},
expected: []Line{{{10, 20}, {30, 40}}, {{-10, -5}, {15, 20}}},
err: nil,
},
{
geom: Polygon{
{
{10, 20},
{30, 40},
{-10, -5},
},
{
{1, 2},
{3, 4},
},
},
expected: []Line{{{10, 20}, {30, 40}}, {{30, 40}, {-10, -5}}, {{1, 2}, {3, 4}}},
err: nil,
},
{
geom: MultiPolygon{
{
{
{10, 20},
{30, 40},
{-10, -5},
},
{
{1, 2},
{3, 4},
},
},
{
{
{5, 6},
{7, 8},
{9, 10},
},
},
},
expected: []Line{{{10, 20}, {30, 40}}, {{30, 40}, {-10, -5}}, {{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}, {{7, 8}, {9, 10}}},
err: nil,
},
{
geom: Collection{
Point{10, 20},
MultiPoint{
{10, 20},
{30, 40},
{-10, -5},
},
LineString{
{1, 2},
{3, 4},
{5, 6},
},
},
expected: []Line{{{1, 2}, {3, 4}}, {{3, 4}, {5, 6}}},
err: nil,
},
}

for i, tc := range testcases {
tc := tc
t.Run(strconv.FormatInt(int64(i), 10), func(t *testing.T) { fn(t, tc) })
}
}
30 changes: 30 additions & 0 deletions planar/algorithm/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Eclipse Distribution License - v 1.0

Copyright (c) 2007, Eclipse Foundation, Inc. and its licensors.

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

Neither the name of the Eclipse Foundation, Inc. nor the names of its
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Loading