Documentation
¶
Overview ¶
Package dem2hm reads heightmaps produced by the dem2hm converter.
Index ¶
Examples ¶
Constants ¶
const ( // MaxMetadataSize is the largest version 1.1 JSON metadata block in bytes. MaxMetadataSize = 1 << 20 // NoDataPixel16 marks a version 1.1 pixel outside the source data. It // does not imply sea; the application decides what such a pixel means. NoDataPixel16 int16 = -32768 )
const ( // NoDataPixel is the value reserved for a pixel with no elevation data. NoDataPixel int32 = -2147483648 // ErrInvalidByteOrder indicates that the file does not start with the // expected magic value in little-endian byte order. ErrInvalidByteOrder = Error("invalid heightmap byte order or format") // ErrInvalidHeader indicates that the heightmap header is incomplete or, // for version 1.1, declares an invalid metadata length. ErrInvalidHeader = Error("invalid heightmap header") // ErrInvalidMetadata indicates that the version 1.1 JSON metadata block is // not valid UTF-8 JSON or does not describe a consistent heightmap. ErrInvalidMetadata = Error("invalid heightmap metadata") // ErrInvalidDimensions indicates that the height or width is not positive, // or that the resulting payload size cannot be represented safely. ErrInvalidDimensions = Error("invalid heightmap dimensions") // ErrInvalidGzip indicates that the payload is not a valid gzip stream. ErrInvalidGzip = Error("invalid heightmap gzip stream") // ErrInvalidPayload indicates that the decompressed pixel payload does not // conform to the heightmap format. ErrInvalidPayload = Error("invalid heightmap payload") // ErrMultipleGzipMembers indicates that a second gzip member follows the // required payload member. ErrMultipleGzipMembers = Error("multiple gzip members") // ErrTrailingData indicates that data follows the single gzip member. ErrTrailingData = Error("trailing heightmap data") // ErrOutOfBounds indicates that a requested pixel or row is outside the map. ErrOutOfBounds = Error("heightmap coordinates out of bounds") // ErrLimitExceeded indicates that the heightmap exceeds a configured limit. ErrLimitExceeded = Error("heightmap resource limit exceeded") // ErrCanceled indicates that reading was canceled or its deadline expired. ErrCanceled = Error("heightmap read canceled") )
Variables ¶
This section is empty.
Functions ¶
Types ¶
type ElevationMetadata ¶ added in v1.1.0
type ElevationMetadata struct {
Minimum int16 `json:"minimum"`
Maximum int16 `json:"maximum"`
// VerticalDatum is the datum elevations are measured from, such as
// "EGM96" or "EPSG:5773", or nil when unknown.
VerticalDatum *string `json:"vertical_datum"`
}
ElevationMetadata summarizes the valid elevations in meters.
type HeightMap ¶
HeightMap is a normalized raster stored in top-to-bottom row-major order.
func Read ¶
Read reads a version 1 heightmap from r. It forwards to ReadHeightMap.
Example ¶
package main
import (
"bytes"
"fmt"
_ "embed"
"github.com/maloquacious/dem2hm"
)
//go:embed testdata/example.hmz
var exampleHMZ []byte
func main() {
heightmap, err := dem2hm.Read(bytes.NewReader(exampleHMZ))
if err != nil {
fmt.Println("error:", err)
return
}
emptyPixels := 0
for _, pixel := range heightmap.Data {
if pixel == dem2hm.NoDataPixel {
emptyPixels++
}
}
fmt.Println("height:", heightmap.Height)
fmt.Println("width:", heightmap.Width)
fmt.Println("empty pixels:", emptyPixels)
}
Output: height: 2 width: 3 empty pixels: 2
func ReadHeightMap ¶ added in v1.1.0
ReadHeightMap reads a version 1 heightmap from r without an application-defined resource limit or cancellation context.
func ReadHeightMapWithOptions ¶ added in v1.1.0
ReadHeightMapWithOptions reads a version 1 heightmap from r using options.
func ReadWithOptions ¶
ReadWithOptions reads a version 1 heightmap from r using options. It forwards to ReadHeightMapWithOptions.
func (*HeightMap) At ¶
At returns the pixel at column x and row y. It panics if the coordinates are outside the heightmap.
type HeightMap16 ¶ added in v1.1.0
HeightMap16 is a raster of elevations in meters, stored in top-to-bottom row-major order, with the metadata recorded by the converter.
func ReadHeightMap16 ¶ added in v1.1.0
func ReadHeightMap16(r io.Reader) (*HeightMap16, error)
ReadHeightMap16 reads a version 1.1 heightmap from r without an application-defined resource limit or cancellation context.
Example ¶
package main
import (
"bytes"
"fmt"
_ "embed"
"github.com/maloquacious/dem2hm"
)
//go:embed testdata/example16.hmz
var example16HMZ []byte
func main() {
heightmap, err := dem2hm.ReadHeightMap16(bytes.NewReader(example16HMZ))
if err != nil {
fmt.Println("error:", err)
return
}
aboveSeaLevel, outsideSource := 0, 0
for _, elevation := range heightmap.Data {
switch {
case elevation == dem2hm.NoDataPixel16:
outsideSource++
case elevation > 0:
aboveSeaLevel++
}
}
lon, lat, err := heightmap.LonLat(1, 1)
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Println("height:", heightmap.Height)
fmt.Println("width:", heightmap.Width)
fmt.Println("elevation (1, 1):", heightmap.At(1, 1), "m")
fmt.Println("above sea level:", aboveSeaLevel)
fmt.Println("outside source:", outsideSource)
fmt.Println("vertical datum:", *heightmap.Metadata.Elevation.VerticalDatum)
fmt.Printf("center of (1, 1): %.3f, %.3f\n", lon, lat)
}
Output: height: 2 width: 3 elevation (1, 1): 1830 m above sea level: 2 outside source: 2 vertical datum: EGM96 center of (1, 1): -79.625, 8.625
func ReadHeightMap16WithOptions ¶ added in v1.1.0
func ReadHeightMap16WithOptions(r io.Reader, options Options) (*HeightMap16, error)
ReadHeightMap16WithOptions reads a version 1.1 heightmap from r using options.
func (*HeightMap16) At ¶ added in v1.1.0
func (hm *HeightMap16) At(x, y int) int16
At returns the elevation at column x and row y. It panics if the coordinates are outside the heightmap.
func (*HeightMap16) LonLat ¶ added in v1.1.0
func (hm *HeightMap16) LonLat(x, y int) (float64, float64, error)
LonLat returns the longitude and latitude in degrees of the center of output pixel (x, y).
func (*HeightMap16) Pixel ¶ added in v1.1.0
func (hm *HeightMap16) Pixel(x, y int) (int16, error)
Pixel returns the elevation at column x and row y.
func (*HeightMap16) Row ¶ added in v1.1.0
func (hm *HeightMap16) Row(y int) ([]int16, error)
Row returns row y as a view into the heightmap's backing data. Changes to the returned slice change the heightmap.
func (*HeightMap16) SourcePixel ¶ added in v1.1.0
func (hm *HeightMap16) SourcePixel(x, y int) (int, int, error)
SourcePixel returns the source GeoTIFF column and row of output pixel (x, y).
type Metadata ¶ added in v1.1.0
type Metadata struct {
// Height and Width are the output raster dimensions.
Height int32 `json:"height"`
Width int32 `json:"width"`
// Elevation summarizes the valid elevations.
Elevation ElevationMetadata `json:"elevation"`
// PixelSizeMeters is the nominal north–south ground size of one pixel at
// the source raster's center latitude on the WGS 84 ellipsoid.
PixelSizeMeters float64 `json:"pixel_size_m"`
// Source describes the GeoTIFF the heightmap was converted from.
Source SourceMetadata `json:"source"`
// Transform is the rotation and flips applied to the source raster.
Transform TransformMetadata `json:"transform"`
// Dem2hmVersion is the version of the converter that wrote the file.
Dem2hmVersion string `json:"dem2hm_version"`
}
Metadata is the version 1.1 JSON metadata block.
type Options ¶
type Options struct {
// Context is checked while reading and decoding. A nil Context is treated
// as context.Background(). Cancellation cannot interrupt an underlying
// io.Reader blocked in Read unless that reader also honors cancellation,
// deadlines, or closure.
Context context.Context
// MaxPixels is the largest decoded heightmap to accept. Zero disables the
// additional limit; format and address-space limits still apply.
MaxPixels uint64
}
Options controls resource use while reading a heightmap.
type SourceMetadata ¶ added in v1.1.0
type SourceMetadata struct {
FileName string `json:"file_name"`
SHA256 string `json:"sha256"`
Width int32 `json:"width"`
Height int32 `json:"height"`
// NoData is the source's declared no-data value, or nil if it had none.
NoData *int64 `json:"no_data"`
// GeoTransform maps the top-left corner of source pixel (column, row) to
// degrees: longitude = g[0] + column*g[1] + row*g[2] and
// latitude = g[3] + column*g[4] + row*g[5].
GeoTransform [6]float64 `json:"geotransform"`
}
SourceMetadata describes the source GeoTIFF.
type TransformMetadata ¶ added in v1.1.0
type TransformMetadata struct {
Rotate int `json:"rotate"`
FlipHorizontal bool `json:"flip_horizontal"`
FlipVertical bool `json:"flip_vertical"`
}
TransformMetadata is the transformation from source to output pixels: a clockwise rotation, followed by flips in output coordinates.