Skip to content

Commit

Permalink
Merge pull request #31 from pelias/line_centroid
Browse files Browse the repository at this point in the history
linestring centroids
  • Loading branch information
missinglink authored Aug 4, 2016
2 parents e96d10f + 80e19f1 commit 8fe1117
Show file tree
Hide file tree
Showing 17 changed files with 9,205 additions and 9,081 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ pbf2json.createReadStream( config )

Make sure `Go` is installed and configured on your system, see: https://gist.github.com/missinglink/4212a81a7d9c125b68d9

**Note:** You should install the latest version of Golang, at least `1.5+`, last tested on `1.6.2`

```bash
sudo apt-get install mercurial;
go get;
Expand All @@ -189,7 +191,7 @@ If you would like to compile a version of this lib for an architecture which isn

```bash
go get;
go build pbf2json.go;
go build;
chmod +x pbf2json;
mv pbf2json build/pbf2json.{platform}-{arch};
```
Expand All @@ -205,4 +207,4 @@ $ node
'x64'
```

Then submit a pull request, you are awesome ;)
Then submit a pull request, you are awesome ;)
Binary file modified build/pbf2json.darwin-x64
Binary file not shown.
Binary file modified build/pbf2json.linux-arm
Binary file not shown.
Binary file modified build/pbf2json.linux-x64
Binary file not shown.
Binary file modified build/pbf2json.win32-x64
Binary file not shown.
31 changes: 14 additions & 17 deletions compile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,67 +2,64 @@

# note: you will need to follow the guide here to install the tools required
# to cross-compile for different architectures on your host machine.
# http://dave.cheney.net/2013/07/09/an-introduction-to-cross-compilation-with-go-1-1

# change this path to match your env
source /var/www/golang-crosscompile/crosscompile.bash
# http://dave.cheney.net/2015/08/22/cross-compilation-with-go-1-5

echo "[compile] linux arm";
go-linux-arm build pbf2json.go;
env GOOD=linux GOARCH=arm go build;
chmod +x pbf2json;
mv pbf2json build/pbf2json.linux-arm;

# echo "[compile] linux i386";
# go-linux-386 build pbf2json.go;
# env GOOD=linux GOARCH=386 go build;
# chmod +x pbf2json;
# mv pbf2json build/pbf2json.linux-ia32;

echo "[compile] linux x64";
go-linux-amd64 build pbf2json.go;
env GOOD=linux GOARCH=amd64 go build;
chmod +x pbf2json;
mv pbf2json build/pbf2json.linux-x64;

# echo "[compile] darwin i386";
# go-darwin-386 build pbf2json.go;
# env GOOD=darwin GOARCH=386 go build;
# chmod +x pbf2json;
# mv pbf2json build/pbf2json.darwin-ia32;

echo "[compile] darwin x64";
go-darwin-amd64 build pbf2json.go;
env GOOD=darwin GOARCH=amd64 go build;
chmod +x pbf2json;
mv pbf2json build/pbf2json.darwin-x64;

# echo "[compile] windows i386";
# go-windows-386 build pbf2json.go;
# env GOOD=windows GOARCH=386 go build;
# chmod +x pbf2json.exe;
# mv pbf2json.exe build/pbf2json.win32-ia32;

echo "[compile] windows x64";
go-windows-amd64 build pbf2json.go;
env GOOD=windows GOARCH=amd64 go build -o pbf2json.exe;
chmod +x pbf2json.exe;
mv pbf2json.exe build/pbf2json.win32-x64;

# echo "[compile] freebsd arm";
# go-freebsd-arm build pbf2json.go;
# env GOOD=freebsd GOARCH=arm go build;
# chmod +x pbf2json;
# mv pbf2json build/pbf2json.freebsd-arm;

# echo "[compile] freebsd i386";
# go-freebsd-386 build pbf2json.go;
# env GOOD=freebsd GOARCH=386 go build;
# chmod +x pbf2json;
# mv pbf2json build/pbf2json.freebsd-ia32;

# echo "[compile] freebsd x64";
# go-freebsd-amd64 build pbf2json.go;
# env GOOD=freebsd GOARCH=amd64 go build;
# chmod +x pbf2json;
# mv pbf2json build/pbf2json.freebsd-x64;

# echo "[compile] openbsd i386";
# go-openbsd-386 build pbf2json.go;
# env GOOD=openbsd GOARCH=386 go build;
# chmod +x pbf2json;
# mv pbf2json build/pbf2json.openbsd-ia32;

# echo "[compile] openbsd x64";
# go-openbsd-amd64 build pbf2json.go;
# env GOOD=openbsd GOARCH=amd64 go build;
# chmod +x pbf2json;
# mv pbf2json build/pbf2json.openbsd-x64;
# mv pbf2json build/pbf2json.openbsd-x64;
29 changes: 29 additions & 0 deletions line_centroid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import "github.com/paulmach/go.geo"

// GetLineCentroid - compute the centroid of a line string
func GetLineCentroid(ps *geo.PointSet) *geo.Point {

path := geo.NewPath()
path.PointSet = *ps

halfDistance := path.Distance() / 2
travelled := 0.0

for i := 0; i < len(path.PointSet)-1; i++ {

segment := geo.NewLine(&path.PointSet[i], &path.PointSet[i+1])
distance := segment.Distance()

// middle line segment
if (travelled + distance) > halfDistance {
var remainder = halfDistance - travelled
return segment.Interpolate(remainder / distance)
}

travelled += distance
}

return ps.GeoCentroid()
}
46 changes: 46 additions & 0 deletions line_centroid_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package main

import (
"testing"

"github.com/paulmach/go.geo"
"github.com/stretchr/testify/assert"
)

// http://www.openstreetmap.org/way/46340228
func TestGetLineCentroid(t *testing.T) {

var poly = geo.NewPointSet()
poly.Push(geo.NewPoint(-74.001559, 40.719743))
poly.Push(geo.NewPoint(-73.999914, 40.721679))
poly.Push(geo.NewPoint(-73.997783, 40.724195))
poly.Push(geo.NewPoint(-73.997318, 40.724745))
poly.Push(geo.NewPoint(-73.996797, 40.725375))
poly.Push(geo.NewPoint(-73.995203, 40.727239))
poly.Push(geo.NewPoint(-73.993927, 40.728737))
poly.Push(geo.NewPoint(-73.992407, 40.730535))
poly.Push(geo.NewPoint(-73.991545, 40.731566))
poly.Push(geo.NewPoint(-73.991417, 40.731843))
poly.Push(geo.NewPoint(-73.990745, 40.734738))
poly.Push(geo.NewPoint(-73.990199, 40.737495))
poly.Push(geo.NewPoint(-73.989630, 40.739735))
poly.Push(geo.NewPoint(-73.989370, 40.741459))
poly.Push(geo.NewPoint(-73.989219, 40.742233))
poly.Push(geo.NewPoint(-73.989119, 40.743025))
poly.Push(geo.NewPoint(-73.988699, 40.745262))
poly.Push(geo.NewPoint(-73.987904, 40.749446))
poly.Push(geo.NewPoint(-73.987417, 40.752149))
poly.Push(geo.NewPoint(-73.986938, 40.754016))
poly.Push(geo.NewPoint(-73.986833, 40.754345))
poly.Push(geo.NewPoint(-73.986321, 40.755897))
poly.Push(geo.NewPoint(-73.986117, 40.756513))
poly.Push(geo.NewPoint(-73.985720, 40.757348))
poly.Push(geo.NewPoint(-73.985433, 40.757980))
poly.Push(geo.NewPoint(-73.983607, 40.760503))
poly.Push(geo.NewPoint(-73.979957, 40.765504))
poly.Push(geo.NewPoint(-73.979264, 40.766480))

var centroid = GetLineCentroid(poly)
assert.Equal(t, 40.74239780132512, centroid.Lat())
assert.Equal(t, -73.98919819175188, centroid.Lng())
}
Loading

0 comments on commit 8fe1117

Please sign in to comment.