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 @@ -128,3 +128,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{g}

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
}
129 changes: 129 additions & 0 deletions geom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ func TestGetCoordinates(t *testing.T) {
}
}
testcases := []tcase{
{
geom: Extent{},
expected: nil,
err: ErrUnknownGeometry{Extent{}},
},
{
geom: Point{10, 20},
expected: []Point{{10, 20}},
Expand Down Expand Up @@ -124,3 +129,127 @@ 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: Extent{},
expected: nil,
err: ErrUnknownGeometry{Extent{}},
},
{
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) })
}
}
5 changes: 3 additions & 2 deletions planar/triangulate/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ porting are:
* Stay true to JTS's implementation, but make the necessary changes to stay
go-ish and fit nicely within the geom package.
* Only implement what is needed as it is needed.
* When possible, keep modifications to the functionality segregated into
* When reasonable, keep modifications to the functionality segregated into
different directories. This will help minimize the cost of porting more of
JTS's functionality or porting JTS's future changes.
JTS's functionality or porting JTS's future changes. When it isn't
reasonable, make a specific note of the difference in the comments.

To make porting easier, the original Java code will be kept in the source
files until the specific function has been ported at which time the Java
Expand Down
21 changes: 21 additions & 0 deletions planar/triangulate/constraineddelaunay/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 go-spatial

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
12 changes: 12 additions & 0 deletions planar/triangulate/constraineddelaunay/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@


This constrained delaunay triangulation implementation is based on:

Domiter, Vid. "Constrained Delaunay triangulation using plane subdivision."
Proceedings of the 8th central European seminar on computer graphics.
Budmerice. 2004.
http://old.cescg.org/CESCG-2004/web/Domiter-Vid/CDT.pdf

While this code makes heavy use of the JTS port, it is all new code. As such
it can fall under the MIT license.

Loading