Skip to content

Commit

Permalink
Change structure to separate application from library (#4)
Browse files Browse the repository at this point in the history
* Change structure to separate application from library

Move application to cmd/main.go and update documentation
Move a few things into library code and name it a new package "tileutils"

* update setup-go action

* cmd/baremaps-exporter folder

* change structure per feedback

* fix test
  • Loading branch information
nathreed authored May 17, 2024
1 parent 2bca1ba commit dcf7146
Show file tree
Hide file tree
Showing 15 changed files with 74 additions and 71 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup golang
uses: actions/setup-go@v4
uses: actions/setup-go@v5
with:
go-version: ${{ vars.GO_VERSION }}
- name: Install staticcheck
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Go must be installed, version 1.20 or later.
Then:

```
go install github.com/flightaware/baremaps-exporter
go install github.com/flightaware/baremaps-exporter/cmd/baremaps-exporter
```

Now you're ready to go.
Expand All @@ -41,7 +41,7 @@ Now you're ready to go.

Run the application with:
```
go run github.com/flightaware/baremaps-exporter --help
go run github.com/flightaware/baremaps-exporter/cmd/baremaps-exporter --help
```

All of the options:
Expand All @@ -68,7 +68,7 @@ Options:

Typical usage:
```
go run github.com/flightaware/baremaps-exporter -o ./tiles/ -d 'postgres://baremaps:baremaps@localhost:5432/baremaps' tiles.json
go run github.com/flightaware/baremaps-exporter/cmd/baremaps-exporter -o ./tiles/ -d 'postgres://baremaps:baremaps@localhost:5432/baremaps' tiles.json
```

## LICENSE
Expand Down
68 changes: 48 additions & 20 deletions main.go → cmd/baremaps-exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"sync"
"time"

"github.com/flightaware/baremaps-exporter/pkg/tileutils"

"github.com/alexflint/go-arg"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/twpayne/go-mbtiles"
Expand Down Expand Up @@ -40,22 +42,48 @@ func (Args) Description() string {
return "export baremaps-compatible tilesets from a postgis server"
}

type TileCoords struct {
Z int
X int
Y int
type WorkerParams struct {
Num int // worker number
Wg *sync.WaitGroup // waitgroup to signal when completed
Args Args // input args
TileList []tileutils.TileCoords // coords that this worker should process
QueryMap tileutils.ZoomLayerInfo // a map of the queries relevant at each zoom level
GzipCompression bool // true if gzip compression should be used
Writer tileutils.TileWriter // writer to use for output
BulkWriter tileutils.TileBulkWriter // bulk writer if available
Pool *pgxpool.Pool // postgres connection pool
}

type WorkerParams struct {
Num int // worker number
Wg *sync.WaitGroup // waitgroup to signal when completed
Args Args // input args
TileList []TileCoords // coords that this worker should process
QueryMap ZoomLayerInfo // a map of the queries relevant at each zoom level
GzipCompression bool // true if gzip compression should be used
Writer TileWriter // writer to use for output
BulkWriter TileBulkWriter // bulk writer if available
Pool *pgxpool.Pool // postgres connection pool
// newWriters creates a TileWriter and TileBulkWriter based on the input arguments
func newWriters(args Args, tj *tileutils.TileJSON) (writer tileutils.TileWriter, bulkWriter tileutils.TileBulkWriter, close func(), err error) {
var mbWriter *tileutils.MbTilesWriter
if args.Output == "" {
writer = &tileutils.DummyWriter{}
return
}
if args.MbTiles {
mbWriter = &tileutils.MbTilesWriter{
Filename: args.Output,
}
writer = mbWriter
bulkWriter = mbWriter
writer, close, err = mbWriter.New()
if err != nil {
return
}
meta := tileutils.CreateMetadata(tj, tileutils.CreateMetadataOptions{
Filename: args.TileJSON,
Version: args.Version,
Format: tileutils.MbTilesFormatPbf,
})
err = mbWriter.BulkWriteMetadata(meta)
return
}
writer = &tileutils.FileWriter{
Path: args.Output,
}
writer, close, err = writer.New()
return
}

func connectWithRetries(pool *pgxpool.Pool, numRetries int) (*pgxpool.Conn, error) {
Expand Down Expand Up @@ -147,7 +175,7 @@ func tileWorker(params WorkerParams) {
continue
}
if params.GzipCompression {
compressed, err := gzip(mvtTile)
compressed, err := tileutils.Gzip(mvtTile)
if err != nil {
fmt.Printf("error compressing tile: %v\n", err)
}
Expand Down Expand Up @@ -212,7 +240,7 @@ func main() {
}

// read tilejson
tileJSON, tileMap, err := ParseTileJSON(args.TileJSON)
tileJSON, tileMap, err := tileutils.ParseTileJSON(args.TileJSON)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -249,9 +277,9 @@ func main() {
tileJSON.MinZoom = zooms[0]
tileJSON.MaxZoom = zooms[len(zooms)-1]

tiles := ListTiles(zooms, tileJSON)
tiles := tileutils.ListTiles(zooms, tileJSON)
if args.TilesFile != "" {
extraTiles, err := tilesFromFile(args.TilesFile)
extraTiles, err := tileutils.TilesFromFile(args.TilesFile)
if err != nil {
panic(err)
}
Expand All @@ -261,7 +289,7 @@ func main() {
tileLen := len(tiles)
fmt.Printf("number of tiles: %d\n", tileLen)

writer, bulkWriter, close, err := NewWriters(args, tileJSON)
writer, bulkWriter, close, err := newWriters(args, tileJSON)
if err != nil {
panic(err)
}
Expand All @@ -271,7 +299,7 @@ func main() {
numWorkers = tileLen
}
// round robin the tiles so workers are hitting similar geospatial entries and zoom at the same time
rrTiles := RoundRobinTiles(tiles, numWorkers)
rrTiles := tileutils.RoundRobinTiles(tiles, numWorkers)
for i := 0; i < numWorkers; i++ {
wg.Add(1)
workerTiles := rrTiles[i]
Expand Down
6 changes: 3 additions & 3 deletions gzip.go → pkg/tileutils/gzip.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package main
package tileutils

import (
"bytes"

gziplib "github.com/klauspost/compress/gzip"
)

// gzip is a utility function to zip up a tile for storage
func gzip(data []byte) ([]byte, error) {
// Gzip is a utility function to zip up a tile for storage
func Gzip(data []byte) ([]byte, error) {
var buf bytes.Buffer
g, err := gziplib.NewWriterLevel(&buf, gziplib.BestCompression)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions gzip_test.go → pkg/tileutils/gzip_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package tileutils

import (
"bytes"
Expand All @@ -12,7 +12,7 @@ import (

func TestGzip(t *testing.T) {
data := []byte{1, 2, 3, 4, 5}
output, err := gzip(data)
output, err := Gzip(data)
assert.Nil(t, err)

// decode the gzip and make sure it is the same as the original input
Expand Down
2 changes: 1 addition & 1 deletion mbtiles.go → pkg/tileutils/mbtiles.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package tileutils

import (
"encoding/json"
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion tilejson.go → pkg/tileutils/tilejson.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package tileutils

import (
"encoding/json"
Expand Down
2 changes: 1 addition & 1 deletion tilejson_test.go → pkg/tileutils/tilejson_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package tileutils

import (
"testing"
Expand Down
13 changes: 10 additions & 3 deletions tileutils.go → pkg/tileutils/tileutils.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package main
// Package tileutils provides types and functions for working with slippy map tiles, MBTiles, and TileJSON
package tileutils

import (
"fmt"
Expand All @@ -19,6 +20,12 @@ func latToY(lat float64, zoom int) int {
return int(math.Floor((1 - math.Log(math.Tan(latRad)+1/math.Cos(latRad))/math.Pi) / 2 * n))
}

type TileCoords struct {
Z int
X int
Y int
}

// BoundingBox is a lat/lon set of coordinates for a bounding box
type BoundingBox struct {
Left float64
Expand Down Expand Up @@ -101,8 +108,8 @@ func RoundRobinTiles(input []TileCoords, numWorkers int) [][]TileCoords {
return out
}

// tilesFromFile reads the tile coordinates to generate from a file
func tilesFromFile(filename string) ([]TileCoords, error) {
// TilesFromFile reads the tile coordinates to generate from a file
func TilesFromFile(filename string) ([]TileCoords, error) {
data, err := os.ReadFile(filename)
if err != nil {
return nil, fmt.Errorf("unable to read file (%s): %w", filename, err)
Expand Down
2 changes: 1 addition & 1 deletion tileutils_test.go → pkg/tileutils/tileutils_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package tileutils

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion utils.go → pkg/tileutils/utils.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package tileutils

import (
"encoding/json"
Expand Down
2 changes: 1 addition & 1 deletion utils_test.go → pkg/tileutils/utils_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package tileutils

import (
"os"
Expand Down
34 changes: 1 addition & 33 deletions writers.go → pkg/tileutils/writers.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package tileutils

import (
"fmt"
Expand Down Expand Up @@ -152,35 +152,3 @@ func (w *MbTilesWriter) New() (TileWriter, func(), error) {
},
nil
}

// NewWriters creates a TileWriter and TileBulkWriter based on the input arguments
func NewWriters(args Args, tj *TileJSON) (writer TileWriter, bulkWriter TileBulkWriter, close func(), err error) {
var mbWriter *MbTilesWriter
if args.Output == "" {
writer = &DummyWriter{}
return
}
if args.MbTiles {
mbWriter = &MbTilesWriter{
Filename: args.Output,
}
writer = mbWriter
bulkWriter = mbWriter
writer, close, err = mbWriter.New()
if err != nil {
return
}
meta := CreateMetadata(tj, CreateMetadataOptions{
Filename: args.TileJSON,
Version: args.Version,
Format: MbTilesFormatPbf,
})
err = mbWriter.BulkWriteMetadata(meta)
return
}
writer = &FileWriter{
Path: args.Output,
}
writer, close, err = writer.New()
return
}

0 comments on commit dcf7146

Please sign in to comment.