Skip to content

Commit

Permalink
fix lint errors, upgrade mbtiles library
Browse files Browse the repository at this point in the history
  • Loading branch information
polastre committed Sep 8, 2023
1 parent 459afb2 commit d641429
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 29 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/klauspost/compress v1.16.7
github.com/paulmach/orb v0.10.0
github.com/stretchr/testify v1.8.4
github.com/twpayne/go-mbtiles v0.0.0-20230824001319-b2b036ce9e05
github.com/twpayne/go-mbtiles v0.0.1
golang.org/x/exp v0.0.0-20230811145659-89c5cff77bcb
)

Expand Down
7 changes: 5 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/alecthomas/assert/v2 v2.3.0 h1:mAsH2wmvjsuvyBvAmCtm7zFsBlb8mIHx5ySLVdDZXL0=
github.com/alecthomas/repr v0.2.0 h1:HAzS41CIzNW5syS8Mf9UwXhNH1J9aix/BvDRf1Ml2Yk=
github.com/alexflint/go-arg v1.4.3 h1:9rwwEBpMXfKQKceuZfYcwuc/7YY7tWJbFsgG5cAU/uo=
github.com/alexflint/go-arg v1.4.3/go.mod h1:3PZ/wp/8HuqRZMUUgu7I+e1qcpUbvmS258mRXkFH4IA=
github.com/alexflint/go-scalar v1.1.0 h1:aaAouLLzI9TChcPXotr6gUhq+Scr8rl0P9P4PnltbhM=
Expand All @@ -12,6 +14,7 @@ github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEW
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=
Expand Down Expand Up @@ -51,8 +54,8 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/twpayne/go-mbtiles v0.0.0-20230824001319-b2b036ce9e05 h1:hRyZ+Hu/Y8Awb0BQh7pwKZa6OXcq5KxW+v4f7wfGrTc=
github.com/twpayne/go-mbtiles v0.0.0-20230824001319-b2b036ce9e05/go.mod h1:u1fVCS6etm3AsoQo2Sp2Dthop30PLV7pIvvnn0oTulc=
github.com/twpayne/go-mbtiles v0.0.1 h1:A0p5QSoGpvugK1nPX7vVHgcSCoNki7BL2XAEwMy/ERQ=
github.com/twpayne/go-mbtiles v0.0.1/go.mod h1:AYk1UngFPDnDhTqYd71ppiHzYiXCmZAxiwBOhN0Wmgs=
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
github.com/xdg-go/scram v1.1.1/go.mod h1:RaEWvsqvNKKvBPvcKeFjrG2cJqOkHTiyTpzz23ni57g=
github.com/xdg-go/stringprep v1.0.3/go.mod h1:W3f5j4i+9rC0kuIEJL0ky1VpHXQU3ocBgklLGvcBnW8=
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var (
)

type Args struct {
TileJson string `arg:"positional,required" help:"input tilejson file"`
TileJSON string `arg:"positional,required" help:"input tilejson file"`
Output string `arg:"-o,--output" help:"output file or directory"`
MbTiles bool `arg:"--mbtiles" help:"output mbtiles instead of files (automatically selected if output filename ends in '.mbtiles')"`
Dsn string `arg:"-d,--dsn" help:"database connection string (dsn) for postgis"`
Expand Down Expand Up @@ -212,7 +212,7 @@ func main() {
}

// read tilejson
tileJson, tileMap, err := ParseTileJson(args.TileJson)
tileJson, tileMap, err := ParseTileJSON(args.TileJSON)

Check failure on line 215 in main.go

View workflow job for this annotation

GitHub Actions / golang-test

var tileJson should be tileJSON (ST1003)
if err != nil {
panic(err)
}
Expand Down
20 changes: 10 additions & 10 deletions mbtiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type CreateMetadataOptions struct {

// CreateMetadata generates the (name,value) metadata pairs for .mbtiles files.
// Since name is required, it falls back to the filename if not provided.
func CreateMetadata(tj *TileJson, opts CreateMetadataOptions) MbTilesMetadata {
func CreateMetadata(tj *TileJSON, opts CreateMetadataOptions) MbTilesMetadata {
meta := MbTilesMetadata{
"name": tj.Name,
"format": "pbf",
Expand All @@ -36,7 +36,7 @@ func CreateMetadata(tj *TileJson, opts CreateMetadataOptions) MbTilesMetadata {
if tj.Version != "" {
meta["version"] = tj.Version
}
// overwrite TileJson version with passed in version, since that might be set in the command line
// overwrite TileJSON version with passed in version, since that might be set in the command line
if opts.Version != "" {
meta["version"] = opts.Version
}
Expand All @@ -59,27 +59,27 @@ func CreateMetadata(tj *TileJson, opts CreateMetadataOptions) MbTilesMetadata {
}
meta["center"] = center
}
metaJsonField := CreateMetadataJson(tj)
if metaJsonBytes, err := json.Marshal(metaJsonField); err == nil {
meta["json"] = string(metaJsonBytes)
metaJSONField := CreateMetadataJSON(tj)
if metaJSONBytes, err := json.Marshal(metaJSONField); err == nil {
meta["json"] = string(metaJSONBytes)
}
return meta
}

// CreateMetadataJson generates a mbtiles MetadataJson object based on the TileJson input
func CreateMetadataJson(tj *TileJson) *mbtiles.MetadataJson {
// CreateMetadataJSON generates a mbtiles MetadataJson object based on the TileJSON input
func CreateMetadataJSON(tj *TileJSON) *mbtiles.MetadataJson {
meta := mbtiles.MetadataJson{
VectorLayers: extractLayersFromTileJson(tj),
VectorLayers: extractLayersFromTileJSON(tj),
}
return &meta
}

func extractLayersFromTileJson(tj *TileJson) []mbtiles.MetadataJsonVectorLayer {
func extractLayersFromTileJSON(tj *TileJSON) []mbtiles.MetadataJsonVectorLayer {
layers := make([]mbtiles.MetadataJsonVectorLayer, 0, len(tj.VectorLayers))
for _, layer := range tj.VectorLayers {
l := layer // create local variable copy
layer := mbtiles.MetadataJsonVectorLayer{
ID: &l.Id,
ID: &l.ID,
Fields: map[string]string{},
}
minzoom := -1
Expand Down
18 changes: 9 additions & 9 deletions tilejson.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strings"
)

type TileJson struct {
type TileJSON struct {
Attribution string `json:"attribution,omitempty"`
Name string `json:"name"`
Description string `json:"description"`
Expand All @@ -20,29 +20,29 @@ type TileJson struct {
}

type VectorLayer struct {
Id string `json:"id"`
ID string `json:"id"`
Queries []VectorQuery `json:"queries"`
}

type VectorQuery struct {
MinZoom int `json:"minzoom"`
MaxZoom int `json:"maxzoom"`
Sql string `json:"sql"`
SQL string `json:"sql"`
}

// ZoomLayerInfo is a mapped index of queries at each zoom.
// map[int] where int is the zoom level.
// map[string][]string where string1 is the layer name/id and []string is the list of queries.
type ZoomLayerInfo map[int]map[string][]string

func ParseTileJson(filename string) (*TileJson, ZoomLayerInfo, error) {
func ParseTileJSON(filename string) (*TileJSON, ZoomLayerInfo, error) {
// read the file
jsonBytes, err := os.ReadFile(filename)
if err != nil {
return nil, nil, err
}
// parse the tilejson content
tj := TileJson{
tj := TileJSON{
MinZoom: -1,
MaxZoom: -1,
}
Expand All @@ -56,14 +56,14 @@ func ParseTileJson(filename string) (*TileJson, ZoomLayerInfo, error) {
for _, q := range layer.Queries {
for i := q.MinZoom; i < q.MaxZoom; i++ {
// replace $zoom with the zoom level
sql := strings.ReplaceAll(q.Sql, "$zoom", strconv.Itoa(i))
sql := strings.ReplaceAll(q.SQL, "$zoom", strconv.Itoa(i))
if _, ok := zooms[i]; !ok {
zooms[i] = map[string][]string{}
}
if _, ok := zooms[i][layer.Id]; !ok {
zooms[i][layer.Id] = []string{}
if _, ok := zooms[i][layer.ID]; !ok {
zooms[i][layer.ID] = []string{}
}
zooms[i][layer.Id] = append(zooms[i][layer.Id], sql)
zooms[i][layer.ID] = append(zooms[i][layer.ID], sql)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions tilejson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"github.com/stretchr/testify/assert"
)

func TestParseTileJson(t *testing.T) {
func TestParseTileJSON(t *testing.T) {
assert := assert.New(t)
tj, lq, err := ParseTileJson("./testdata/tiles.json")
tj, lq, err := ParseTileJSON("./testdata/tiles.json")
assert.Nil(err)

assert.Equal(tj.Attribution, "for me")
Expand Down
2 changes: 1 addition & 1 deletion tileutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type BoundingBox struct {
Bottom float64
}

func listTiles(zooms []int, tj *TileJson) []TileCoords {
func listTiles(zooms []int, tj *TileJSON) []TileCoords {
tiles := make([]TileCoords, 0, 2<<zooms[len(zooms)-1])
for _, z := range zooms {
newTiles := tilesInBbox(BoundingBox{
Expand Down
4 changes: 2 additions & 2 deletions writers.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func (w *MbTilesWriter) New() (TileWriter, func(), error) {
}

// 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) {
func NewWriters(args Args, tj *TileJSON) (writer TileWriter, bulkWriter TileBulkWriter, close func(), err error) {
var mbWriter *MbTilesWriter
if args.Output == "" {
writer = &DummyWriter{}
Expand All @@ -171,7 +171,7 @@ func NewWriters(args Args, tj *TileJson) (writer TileWriter, bulkWriter TileBulk
return
}
meta := CreateMetadata(tj, CreateMetadataOptions{
Filename: args.TileJson,
Filename: args.TileJSON,
Version: args.Version,
})
err = mbWriter.BulkWriteMetadata(meta)
Expand Down

0 comments on commit d641429

Please sign in to comment.