Skip to content

Commit

Permalink
add minzoom option to extract command (#120)
Browse files Browse the repository at this point in the history
* add minzoom option to extract command

* Error if minzoom > maxzoom
  • Loading branch information
JesseCrocker authored Jan 17, 2024
1 parent 541c8e0 commit fde7b36
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ var cli struct {
Bucket string `help:"Remote bucket of input archive."`
Region string `help:"local GeoJSON Polygon or MultiPolygon file for area of interest." type:"existingfile"`
Bbox string `help:"bbox area of interest: min_lon,min_lat,max_lon,max_lat" type:"string"`
Minzoom int8 `default:-1 help:"Minimum zoom level, inclusive."`
Maxzoom int8 `default:-1 help:"Maximum zoom level, inclusive."`
DownloadThreads int `default:4 help:"Number of download threads."`
DryRun bool `help:"Calculate tiles to extract, but don't download them."`
Expand Down Expand Up @@ -147,7 +148,7 @@ func main() {
logger.Printf("Serving %s %s on port %d and interface %s with Access-Control-Allow-Origin: %s\n", cli.Serve.Bucket, cli.Serve.Path, cli.Serve.Port, cli.Serve.Interface, cli.Serve.Cors)
logger.Fatal(http.ListenAndServe(cli.Serve.Interface+":"+strconv.Itoa(cli.Serve.Port), nil))
case "extract <input> <output>":
err := pmtiles.Extract(logger, cli.Extract.Bucket, cli.Extract.Input, cli.Extract.Maxzoom, cli.Extract.Region, cli.Extract.Bbox, cli.Extract.Output, cli.Extract.DownloadThreads, cli.Extract.Overfetch, cli.Extract.DryRun)
err := pmtiles.Extract(logger, cli.Extract.Bucket, cli.Extract.Input, cli.Extract.Minzoom, cli.Extract.Maxzoom, cli.Extract.Region, cli.Extract.Bbox, cli.Extract.Output, cli.Extract.DownloadThreads, cli.Extract.Overfetch, cli.Extract.DryRun)
if err != nil {
logger.Fatalf("Failed to extract, %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions pmtiles/bitmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func bitmapMultiPolygon(zoom uint8, multipolygon orb.MultiPolygon) (*roaring64.B
return boundary_set, interior_set
}

func generalizeOr(r *roaring64.Bitmap) {
func generalizeOr(r *roaring64.Bitmap, minzoom uint8) {
if r.GetCardinality() == 0 {
return
}
Expand All @@ -56,7 +56,7 @@ func generalizeOr(r *roaring64.Bitmap) {
temp = roaring64.New()
to_iterate = r

for current_z := int(max_z); current_z > 0; current_z-- {
for current_z := int(max_z); current_z > int(minzoom); current_z-- {
iter := to_iterate.Iterator()
for iter.HasNext() {
parent_id := ParentId(iter.Next())
Expand Down
4 changes: 2 additions & 2 deletions pmtiles/bitmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ func TestGeneralizeAnd(t *testing.T) {

func TestGeneralizeOr(t *testing.T) {
b := roaring64.New()
generalizeOr(b)
generalizeOr(b, 0)
assert.Equal(t, uint64(0), b.GetCardinality())
b = roaring64.New()
b.Add(ZxyToId(3, 0, 0))
generalizeOr(b)
generalizeOr(b, 0)
assert.Equal(t, uint64(4), b.GetCardinality())
assert.True(t, b.Contains(ZxyToId(2, 0, 0)))
assert.True(t, b.Contains(ZxyToId(1, 0, 0)))
Expand Down
14 changes: 11 additions & 3 deletions pmtiles/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ func MergeRanges(ranges []SrcDstRange, overfetch float32) (*list.List, uint64) {
// 10. write the leaf directories (if any)
// 11. Get all tiles, and write directly to the output.

func Extract(logger *log.Logger, bucketURL string, key string, maxzoom int8, region_file string, bbox string, output string, download_threads int, overfetch float32, dry_run bool) error {
func Extract(logger *log.Logger, bucketURL string, key string, minzoom int8, maxzoom int8, region_file string, bbox string, output string, download_threads int, overfetch float32, dry_run bool) error {
// 1. fetch the header
start := time.Now()
ctx := context.Background()
Expand Down Expand Up @@ -291,10 +291,18 @@ func Extract(logger *log.Logger, bucketURL string, key string, maxzoom int8, reg
source_metadata_offset := header.MetadataOffset
source_tile_data_offset := header.TileDataOffset

if minzoom == -1 || int8(header.MinZoom) > minzoom {
minzoom = int8(header.MinZoom)
}

if maxzoom == -1 || int8(header.MaxZoom) < maxzoom {
maxzoom = int8(header.MaxZoom)
}

if minzoom > maxzoom {
return fmt.Errorf("Error: minzoom cannot be greater than maxzoom.")
}

var relevant_set *roaring64.Bitmap
if region_file != "" || bbox != "" {
if region_file != "" && bbox != "" {
Expand Down Expand Up @@ -324,7 +332,7 @@ func Extract(logger *log.Logger, bucketURL string, key string, maxzoom int8, reg
boundary_set, interior_set := bitmapMultiPolygon(uint8(maxzoom), multipolygon)
relevant_set = boundary_set
relevant_set.Or(interior_set)
generalizeOr(relevant_set)
generalizeOr(relevant_set, uint8(minzoom))

header.MinLonE7 = int32(bound.Left() * 10000000)
header.MinLatE7 = int32(bound.Bottom() * 10000000)
Expand All @@ -334,7 +342,7 @@ func Extract(logger *log.Logger, bucketURL string, key string, maxzoom int8, reg
header.CenterLatE7 = int32(bound.Center().Y() * 10000000)
} else {
relevant_set = roaring64.New()
relevant_set.AddRange(0, ZxyToId(uint8(maxzoom)+1, 0, 0))
relevant_set.AddRange(ZxyToId(uint8(minzoom), 0, 0), ZxyToId(uint8(maxzoom)+1, 0, 0))
}

// 3. get relevant entries from root
Expand Down

0 comments on commit fde7b36

Please sign in to comment.