raster

package
v0.0.0-...-88a5242 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 23, 2024 License: MIT Imports: 12 Imported by: 0

README

raster

Much of the data under OS OpenData. is available in a "raster" format, with image files representing tiles.

One such dataset is the OS VectorMap District set, which this package was developed against, though it may work with other raster datasets, with no or minimal modification - I haven't tested that.

This package provides a way to work with a raster dataset, querying images and pixel locations corresponding to grid references.

To use it, download (all, or some of) the OS VectorMap District data in "GeoTIFF Full Colour" format.

package main

import (
	"fmt"
	"image"
	"image/draw"
	"image/png"
	"os"

	"github.com/usedbytes/osgrid"
	"github.com/usedbytes/osgrid/osdata/raster"
)

func main() {
	if len(os.Args) != 2 {
		fmt.Println("Provide path to raster data as only argument")
		return
	}

	db, err := raster.OpenDatabase(os.Args[1], 10 * osgrid.Kilometre)
	if err != nil {
		panic(err)
	}

	summit, _ := osgrid.ParseGridRef("SH 60986 54375")

	tile, _ := db.GetImageTile(summit)

	x, y, _ := tile.GetPixelCoord(summit)

	out := image.NewRGBA(image.Rect(0, 0, 500, 500))

	draw.Draw(out, out.Bounds(), tile.GetImage(), image.Pt(x - 250, y - 250), draw.Over)

	f, err := os.Create("out.png")
	if err != nil {
		panic(err)
	}
	defer f.Close()

	png.Encode(f, out)
}

A tile cache is used (with 16 entries by default), storing the parsed data for the 16 most-recently-used tiles so that queries which are geographically close to each other are fast, and to ensure memory usage doesn't grow unbounded.

The GenerateSurface() function in lib/geometry provides the functionality to query elevation data for a rectangular region.

Known Issues

The golang.org/x/image/tiff package which is used for decoding the TIFF images has an issue where it throws "unexpected EOF" for some images. For example tile NO08 exhibits this problem.

I've posted a patch which appears to fix it: https://github.com/golang/go/issues/30827#issuecomment-774469551

I probably should either make a vendored version of that module and pin it in this package, or try out a different TIFF decoder package.

Documentation

Index

Constants

View Source
const (
	ModelPixelScaleTag uint16 = 33550
	ModelTiepointTag   uint16 = 33922
)

Variables

This section is empty.

Functions

func OpenDatabase

func OpenDatabase(path string, tileSize osgrid.Distance) (osdata.ImageDatabase, error)

Types

type Database

type Database struct {
	// contains filtered or unexported fields
}

func (*Database) GetImageTile

func (d *Database) GetImageTile(ref osgrid.GridRef) (osdata.ImageTile, error)

func (*Database) GetTile

func (d *Database) GetTile(ref osgrid.GridRef) (osdata.Tile, error)

func (*Database) Precision

func (d *Database) Precision() osgrid.Distance

type PixelScaleTag

type PixelScaleTag struct {
	ScaleX, ScaleY float64
}

func (*PixelScaleTag) Decode

func (pt *PixelScaleTag) Decode(data []byte, order binary.ByteOrder) error

type TiePoint

type TiePoint struct {
	PixX, PixY     int
	ModelX, ModelY int
}

type TiePointTag

type TiePointTag struct {
	TiePoints []TiePoint
}

func (*TiePointTag) Decode

func (tt *TiePointTag) Decode(data []byte, order binary.ByteOrder) error

type Tile

type Tile struct {
	// contains filtered or unexported fields
}

func OpenTile

func OpenTile(path string) (*Tile, error)

func (*Tile) BottomLeft

func (t *Tile) BottomLeft() osgrid.GridRef

func (*Tile) GetImage

func (t *Tile) GetImage() image.Image

func (*Tile) GetPixelCoord

func (t *Tile) GetPixelCoord(ref osgrid.GridRef) (int, int, error)

func (*Tile) GridRef

func (t *Tile) GridRef() osgrid.GridRef

func (*Tile) Height

func (t *Tile) Height() osgrid.Distance

func (*Tile) Image

func (t *Tile) Image() image.Image

func (*Tile) PixelPrecision

func (t *Tile) PixelPrecision() int

func (*Tile) Precision

func (t *Tile) Precision() osgrid.Distance

func (*Tile) String

func (t *Tile) String() string

func (*Tile) Width

func (t *Tile) Width() osgrid.Distance

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL