Skip to content

Commit 2b5e154

Browse files
committed
Merge branch 'v3.1.0'
2 parents 3a43ca1 + 76551dd commit 2b5e154

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+2439
-798
lines changed

.gitignore

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,5 @@
1-
mbtiles
2-
openmaptiles
3-
tippecanoe
4-
tilemaker
5-
pbf
6-
coastline
7-
water-polygons*
81
test.json
9-
my.json
10-
tmp
112
data
123
main
13-
REPORT.txt
14-
.vscode
15-
16-
!/configs/tilemaker
17-
!third_party/*
4+
bin
5+
.vscode

.gitmodules

Lines changed: 0 additions & 12 deletions
This file was deleted.

CONTRIBUTING.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
## All contributions are welcome.
2+
3+
#### We could particularly use help with:
4+
5+
1. macOS testing / improvements.
6+
7+
### Dev:
8+
9+
- Build using 'make' and remember to set the $(VERSION) variable appropriately.
10+
- Testing may have to be done on built binaries if your golang and docker permissions clash.

Makefile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
VERSION := v3.1.0
2+
LDF := "-X github.com/lambdajack/sequentially-generate-planet-mbtiles/cmd/sequentially-generate-planet-mbtiles.sgpmVersion=$(VERSION)"
3+
4+
all: clean darwin linux windows
5+
6+
darwin:
7+
GOOS=darwin GOARCH=amd64 go build -o bin/sequentially-generate-planet-mbtiles--darwin-amd64-$(VERSION) -ldflags $(LDF)
8+
9+
linux:
10+
GOOS=linux GOARCH=amd64 go build -o bin/sequentially-generate-planet-mbtiles--unix-amd64-$(VERSION) -ldflags $(LDF)
11+
GOOS=linux GOARCH=arm go build -o bin/sequentially-generate-planet-mbtiles--unix-arm64-$(VERSION) -ldflags $(LDF)
12+
13+
windows:
14+
GOOS=windows GOARCH=amd64 go build -o bin/sequentially-generate-planet-mbtiles--win-amd64-$(VERSION).exe -ldflags $(LDF)
15+
16+
clean:
17+
rm -rf bin/

README.md

Lines changed: 107 additions & 49 deletions
Large diffs are not rendered by default.

build/osmium/Dockerfile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
FROM ubuntu:20.04
2-
ARG DEBIAN_FRONTEND=noninteractive
1+
FROM debian:bookworm-slim as builder
32

43
RUN apt-get update
54
RUN apt-get update && apt-get install -y \
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
package sequentiallygenerateplanetmbtiles
2+
3+
import (
4+
"bufio"
5+
"log"
6+
"os"
7+
"path/filepath"
8+
"regexp"
9+
"strconv"
10+
"strings"
11+
12+
"github.com/lambdajack/lj_go/pkg/lj_json"
13+
)
14+
15+
type configuration struct {
16+
srcFileProvided bool
17+
PbfFile string `json:"pbfFile"`
18+
WorkingDir string `json:"workingDir"`
19+
OutDir string `json:"outDir"`
20+
ExcludeOcean bool `json:"excludeOcean"`
21+
ExcludeLanduse bool `json:"excludeLanduse"`
22+
TilemakerConfig string `json:"TilemakerConfig"`
23+
TilemakerProcess string `json:"TilemakerProcess"`
24+
MaxRamMb uint64 `json:"maxRamMb"`
25+
OutAsDir bool `json:"outAsDir"`
26+
SkipSlicing bool `json:"skipSlicing"`
27+
MergeOnly bool `json:"mergeOnly"`
28+
SkipDownload bool `json:"skipDownload"`
29+
}
30+
31+
func initConfig() {
32+
if fl.config == "" {
33+
setConfigByFlags()
34+
} else {
35+
setConfigByJSON()
36+
}
37+
38+
verifyPaths()
39+
40+
cfg.PbfFile = convertAbs(cfg.PbfFile)
41+
cfg.WorkingDir = convertAbs(cfg.WorkingDir)
42+
cfg.OutDir = convertAbs(cfg.OutDir)
43+
}
44+
45+
func setConfigByJSON() {
46+
err := lj_json.DecodeTo(cfg, fl.config, 1000)
47+
if err != nil {
48+
log.Printf("Unable to decode config: %s", err)
49+
os.Exit(exitInvalidJSON)
50+
}
51+
if cfg.MaxRamMb == 0 {
52+
cfg.MaxRamMb = getRam()
53+
}
54+
}
55+
56+
func setConfigByFlags() {
57+
cfg.PbfFile = fl.pbfFile
58+
cfg.WorkingDir = fl.workingDir
59+
cfg.OutDir = fl.outDir
60+
cfg.ExcludeOcean = fl.excludeOcean
61+
cfg.ExcludeLanduse = fl.excludeLanduse
62+
cfg.TilemakerConfig = fl.tilemakerConfig
63+
cfg.TilemakerProcess = fl.tilemakerProcess
64+
cfg.MaxRamMb = getRam()
65+
cfg.OutAsDir = fl.outAsDir
66+
cfg.SkipSlicing = fl.skipSlicing
67+
cfg.MergeOnly = fl.mergeOnly
68+
cfg.SkipDownload = fl.skipDownload
69+
}
70+
71+
func setTmPaths() {
72+
if cfg.TilemakerProcess == "" || strings.ToLower(cfg.TilemakerProcess) == "tileserver-gl-basic" {
73+
cfg.TilemakerProcess = filepath.Join(pth.temp, "tilemaker", "resources", "process-openmaptiles.lua")
74+
log.Println("using tileserver-gl-basic style target")
75+
}
76+
77+
if cfg.TilemakerProcess == "sgpm-bright" {
78+
cfg.TilemakerProcess = filepath.Join(pth.temp, "tilemaker", "resources", "process-bright.lua")
79+
log.Println("using sgpm-bright style target")
80+
}
81+
82+
if cfg.TilemakerConfig == "" {
83+
cfg.TilemakerConfig = filepath.Join(pth.temp, "tilemaker", "resources", "config-openmaptiles.json")
84+
}
85+
86+
cfg.TilemakerConfig = convertAbs(cfg.TilemakerConfig)
87+
cfg.TilemakerProcess = convertAbs(cfg.TilemakerProcess)
88+
}
89+
90+
func verifyPaths() {
91+
if cfg.PbfFile != "" {
92+
if _, err := os.Stat(cfg.PbfFile); os.IsNotExist(err) {
93+
log.Fatalf("planet file does not exist: %s", cfg.PbfFile)
94+
}
95+
cfg.srcFileProvided = true
96+
} else {
97+
if fl.test {
98+
cfg.PbfFile = filepath.Join(cfg.WorkingDir, "pbf", "morocco-latest.osm.pbf")
99+
} else {
100+
cfg.PbfFile = filepath.Join(cfg.WorkingDir, "pbf", "planet-latest.osm.pbf")
101+
}
102+
}
103+
104+
if cfg.TilemakerConfig != "" {
105+
if _, err := os.Stat(cfg.TilemakerConfig); os.IsNotExist(err) {
106+
log.Fatalf("tilemaker config does not exist: %s", cfg.TilemakerConfig)
107+
}
108+
}
109+
110+
if cfg.TilemakerProcess != "" && cfg.TilemakerProcess != "sgpm-bright" && cfg.TilemakerProcess != "tileserver-gl-basic" {
111+
if _, err := os.Stat(cfg.TilemakerProcess); os.IsNotExist(err) {
112+
log.Fatalf("tilemaker process does not exist: %s", cfg.TilemakerProcess)
113+
}
114+
}
115+
}
116+
117+
func getRam() uint64 {
118+
if fl.maxRamMb != 0 {
119+
return fl.maxRamMb
120+
}
121+
122+
var memTotalKb uint64
123+
124+
f, err := os.Open("/proc/meminfo")
125+
if err != nil {
126+
log.Printf("Unable to open /proc/meminfo: %s", err)
127+
memTotalKb = 4096 * 1024
128+
} else {
129+
defer f.Close()
130+
131+
scanner := bufio.NewScanner(f)
132+
133+
memTotalStr := ""
134+
135+
for scanner.Scan() {
136+
if strings.HasPrefix(scanner.Text(), "MemTotal:") {
137+
memTotalStr = regexp.MustCompile(`[0-9]+`).FindString(scanner.Text())
138+
}
139+
}
140+
141+
memTotalKb, err = strconv.ParseUint(memTotalStr, 10, 64)
142+
if err != nil {
143+
log.Printf("Unable to parse MemTotal: %s", err)
144+
memTotalKb = 4096 * 1024
145+
}
146+
}
147+
148+
if (memTotalKb / 1024) < 2048 {
149+
log.Printf("system ram is less than 2 GB; this is dangerously low, but we will do our best!")
150+
return 1024
151+
}
152+
153+
memTotalMb := memTotalKb / 1024
154+
155+
totalRamToUse := memTotalMb - 2048
156+
157+
if totalRamToUse < 1024 {
158+
totalRamToUse = 1024
159+
}
160+
161+
log.Printf("attempting to use not more than %d MB of ram", totalRamToUse)
162+
163+
return totalRamToUse
164+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package sequentiallygenerateplanetmbtiles
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
"os"
7+
"os/exec"
8+
"path/filepath"
9+
"testing"
10+
)
11+
12+
func init() {
13+
14+
tmpDir := filepath.Join(os.TempDir(), "sequentially-generate-planet-mbtiles")
15+
16+
err := os.MkdirAll(tmpDir, os.ModePerm)
17+
if err != nil {
18+
panic("Could not make os.TempDir/sequentially-generate-planet-mbtiles. Permissions issue? TempleOS? Expect many tests to fail.")
19+
}
20+
21+
if _, err := os.Stat(tmpDir); os.IsNotExist(err) {
22+
panic("Could not find os.TempDir. Permissions issue? TempleOS? Expect many tests to fail.")
23+
}
24+
25+
cfg = &configuration{
26+
PbfFile: "", // CHANGE TO TEST FILE ONCE SET U
27+
WorkingDir: tmpDir,
28+
OutDir: filepath.Join(tmpDir, "out"),
29+
ExcludeOcean: true,
30+
ExcludeLanduse: true,
31+
TilemakerConfig: "",
32+
TilemakerProcess: "",
33+
MaxRamMb: 1,
34+
}
35+
36+
fmt.Printf("cfg initialised: %+v\n", cfg)
37+
}
38+
39+
func TestSetInvalidConfigByJSON(t *testing.T) {
40+
// Test run in separate processes so as not to pollute other tests
41+
42+
invalidConfig := "../../configs/test/TEST_INVALID.json"
43+
44+
if _, err := os.Stat(invalidConfig); os.IsNotExist(err) {
45+
t.Errorf("TestSetConfigByJSON test config file does not exist")
46+
}
47+
48+
if os.Getenv("TEST_SET_CONFIG_BY_JSON") == "1" {
49+
os.Args = append(os.Args, "-c", invalidConfig)
50+
flag.Parse()
51+
setConfigByJSON()
52+
return
53+
}
54+
55+
cmd := exec.Command(os.Args[0], "-test.run=TestSetInvalidConfigByJSON")
56+
cmd.Env = append(os.Environ(), "TEST_SET_CONFIG_BY_JSON=1")
57+
defer os.Unsetenv("TEST_SET_CONFIG_BY_JSON")
58+
err := cmd.Run()
59+
60+
if e, ok := err.(*exec.ExitError); ok && !e.Success() {
61+
if e.ExitCode() == exitInvalidJSON {
62+
return
63+
}
64+
}
65+
66+
t.Fatalf("process ran with err %v, want exit status %v", err, exitInvalidJSON)
67+
}
68+
69+
func TestSetValidConfigByJSON(t *testing.T) {
70+
validConfig := "../../configs/test/TEST_VALID.json"
71+
72+
if _, err := os.Stat(validConfig); os.IsNotExist(err) {
73+
t.Errorf("TestSetConfigByJSON test config file does not exist")
74+
}
75+
76+
if os.Getenv("TEST_SET_VALID_CONFIG_BY_JSON") == "1" {
77+
t.Log(os.Args)
78+
os.Args = append(os.Args, "-c", validConfig)
79+
flag.Parse()
80+
setConfigByJSON()
81+
return
82+
}
83+
84+
cmd := exec.Command(os.Args[0], "-test.run=TestSetValidConfigByJSON")
85+
cmd.Env = append(os.Environ(), "TEST_SET_VALID_CONFIG_BY_JSON=1")
86+
defer os.Unsetenv("TEST_SET_VALID_CONFIG_BY_JSON")
87+
err := cmd.Run()
88+
if err != nil {
89+
t.Fatalf("process ran with err %v, want %v", err, nil)
90+
}
91+
}

0 commit comments

Comments
 (0)