Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use v3 as a library #20

Closed
wieringen opened this issue Oct 12, 2022 · 7 comments
Closed

Use v3 as a library #20

wieringen opened this issue Oct 12, 2022 · 7 comments

Comments

@wieringen
Copy link

wieringen commented Oct 12, 2022

I was using the writer interface of v2 combined with github.com/paulmach/orb to create my pmtiles file.

	writer := pmtiles.NewWriter("./base.pmtiles")
	for z := minZoom; z <= maxZoom; z++ {
		covering := tilecover.Bound(bound, z)
		for tile := range covering {
		    layers := mvt.Layers{.....layers.....}
		    tileData, _ := mvt.Marshal(layers)
    
		    writer.WriteTile(
			    pmtiles.Zxy{
				    Z: uint8(tile.Z),
				    X: tile.X,
				    Y: tile.Y,
			    },
			    tileData,
		    )
		}
	}

	metadata, err := json.Marshal(pmtiles.Metadata{
           ...metadata...
         })
	if err != nil {
		return err
	}
	writer.Finalize(metadata)

Is something similar still possible using v3?

To create a better separation of concerns, it might be a good idea to split the pmtiles folder. The pmtiles folder can hold all the library code for reading and writing pmtiles. Next to a cmd folder that can hold the cli related commands and logic for handling flags, etc.

@wieringen wieringen changed the title Using v3 as a library Use v3 as a library Oct 12, 2022
@bdon
Copy link
Member

bdon commented Oct 13, 2022

Yes, at some point I was planning on making that separation like you suggested. Does your use case need deduplication and compression or not? We may need to expose multiple writer interfaces: the simplest one would be "dumb" which assumes the data you are passing in is already compressed.

@bdon
Copy link
Member

bdon commented Oct 13, 2022

What about splitting into two folders, one called cmd and one called util (or lib ?) that handles low-level serialization, has a Writer interface, etc

I'd like to rename from pmtiles as that is the new preferred name of the compiled executable (instead of go-pmtiles)

@wieringen
Copy link
Author

wieringen commented Oct 13, 2022

A common pattern I see a lot is something like this: https://github.com/golang-standards/project-layout
Examples:

So in your case, that would be something like this:

cmd/pmtiles/app/*.go // Hold cli commands, parsing flags, uploading to bucket, converting, serving etc. Leverages pkg/pmtiles for core functionality.
cmd/pmtiles/main.go // cli entry point.
pkg/pmtiles // Lib that exposes a normal writer/reader interface, and handles reading, writing, compressing, and deduplication.

Having the same name for both the lib and the cli is not a problem (in my opinion), both are valid entry points. An alternative might be dropping the pkg dir and using encoding/pmtiles? It's similar to what orb is doing (https://github.com/paulmach/orb/tree/master/encoding/mvt) and the std lib (https://pkg.go.dev/encoding/json).

orb can gzip the mvt tileData, it exposes both a Marshal and MarshalGzipped method. Not sure how and if it supports deduplication.

@bdon
Copy link
Member

bdon commented Nov 2, 2022

seems like https://github.com/alecthomas/kong or https://github.com/spf13/cobra are the preferred way to structure CLI applications, any opinion on either? #26

@wipfli
Copy link
Contributor

wipfli commented Nov 15, 2022

Having go-pmtiles as a library could also be useful in the future for example for a MapLibre GL Native app which allows you to download a pmtiles extract and save the map data on your mobile device. The area downloads would require something like #31...

@tyrauber
Copy link

MapLibre GL Native app which allows you to download a pmtiles extract and save the map data on your mobile device.

@wipfli, very happy to see you helping out here! We, @TheOutbound, would very much like to see this happen.

bdon added a commit that referenced this issue Jan 28, 2024
]

* Use revive with the default configuration.
bdon added a commit that referenced this issue Jan 28, 2024
#123)

* Make all variable and field names consistent with style guide. [#20, #46]

* Use revive with the default configuration.

* also do error string and code style cleanup (revive defaults)
bdon added a commit that referenced this issue Jan 29, 2024
…#46]

* Also correct ordering of function arguments to put errors last
bdon added a commit that referenced this issue Jan 29, 2024
#124)

* refactor names to only export intentional parts of functionality [#20, #46]

* Also correct ordering of function arguments to put errors last

* add go vet to CI, fix CLI syntax
@bdon
Copy link
Member

bdon commented Jan 29, 2024

As of 1.14 there is now a more reasonable interface to package methods outlined here:

https://pkg.go.dev/github.com/protomaps/go-pmtiles@v1.14.0

closing this as a duplicate of #46 - we should still evolve the internal cmd/package structure

@bdon bdon closed this as completed Jan 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants