hmz2ele

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2026 License: MIT Imports: 11 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

hmz2ele

hmz2ele samples a dem2hm heightmap (format 1.1) at the centers and corners of a flat-top hex grid and writes the elevations as JSON, with an optional PNG preview.

Usage

go run ./cmd/hmz2ele [flags] <input.hmz>

Flags:

  • -apothem <px> sets the hex apothem in raster pixels. A hex is twice this tall. The default is 48, the campaign grid's apothem.
  • -output <file> is the JSON file to write. Required.
  • -preview <file> is a PNG preview to write. Optional.
  • -preview-scale <n> sets how many raster pixels, in each direction, one preview pixel covers. The default is 8.
  • -version prints the version.

The command prints a summary: grid size, hex count, and the number of land, low, no-data, and coastal sea hexes.

Grid

Hexes are flat-top, with even columns shifted down by half a hex. Geometry uses continuous raster coordinates: pixel (i, j) covers [i, i+1) × [j, j+1). For apothem a and side s = 2a / sqrt(3), the center of the hex in column c and row r is:

x = s + 1.5 × s × c
y = a + 2a × r + (a if c is even, else 0)

The first column's west corner is at x = 0. A hex is in the grid if its center pixel, (floor(x), floor(y)), is inside the raster.

Sampling

Each sample point gets the median of the valid pixels in the 3 × 3 block centered on the pixel nearest the point. With an even number of valid pixels, it is the mean of the two middle values, rounded half away from zero. If 5 or more of the 9 pixels are no-data, the sample is no-data. Block pixels outside the raster count as no-data.

Output

Elevations are int16 meters; null means no-data.

{
  "hmz2ele_version": "0.1.0",
  "heightmap": { "file_name": "pandemokh.hmz", "metadata": { ... } },
  "grid": { "apothem_px": 48, "side_px": 55.43, "columns": 106, "rows": 222, "hex_count": 23479, ... },
  "sampling": { "window": 3, "statistic": "...", "no_data_majority": 5 },
  "hexes": [ { "col": 18, "row": 47, "center": 81, "west": 148, "east": null }, ... ],
  "boundary_vertices": [ { "col": 0, "row": -1, "corner": "east", "elevation": null }, ... ]
}

heightmap.metadata is the heightmap's dem2hm metadata, copied unchanged.

Each hex owns its west and east corners, so every vertex is stored exactly once. A hex's other corners are owned by its neighbors:

Corner Owner
North-east the north-east neighbor's west corner
South-east the south-east neighbor's west corner
South-west the south-west neighbor's east corner
North-west the north-west neighbor's east corner

Corners used by hexes in the grid but owned by hexes outside it are listed in boundary_vertices, keyed by their owner's column and row, which may be negative or past the grid's edge.

Hexes are ordered by row, then column. Not every column and row within columns × rows is in the grid: in the last row, even columns may fall outside the raster.

Preview

The preview fills each hex with the color of its center elevation, using a hypsometric ramp with more color stops at low elevations, and darkens hex outlines. No-data hexes are dark blue, hexes at or below 0 m are teal, and areas outside the grid are white.

Package

The hmz2ele package holds the grid geometry that other pipeline tools share, so they always agree with the sampled elevations:

  • NewGrid, Grid.Center, Grid.West, Grid.East, Grid.Contains, and Neighbor lay out the grid.
  • Grid.HexAt and Grid.NearestVertex map a raster point to a hex and to the nearest vertex.
  • CornerOwner, VertexHexes, and VertexNeighbors walk the vertex graph; each vertex has three neighbors along hex edges.
  • EdgeBetween and EdgeVertices convert between pairs of vertices and edges, identified by the hex that owns them (n, ne, or se).
  • RenderPreview draws the preview image so callers can draw over it; WritePreview encodes it as PNG.

License

MIT. See LICENSE.

Documentation

Index

Constants

View Source
const (
	// SampleWindow is the side of the square block of pixels sampled around
	// a point.
	SampleWindow = 3
	// NoDataMajority is the number of no-data pixels in the block that makes
	// the sample no-data.
	NoDataMajority = 5
)

Variables

Directions lists every Direction, clockwise from North.

Functions

func EdgeVertices added in v0.2.0

func EdgeVertices(e EdgeKey) (VertexKey, VertexKey)

EdgeVertices returns the edge's two vertices, in clockwise order around the hex that owns it.

func Neighbor

func Neighbor(col, row int, d Direction) (int, int)

Neighbor returns the column and row of the hex next to (col, row) in the given direction. The result may be outside the grid.

func RenderPreview added in v0.2.0

func RenderPreview(g Grid, hexes []Hex, scale int) (*image.RGBA, error)

RenderPreview draws the preview that WritePreview writes, so callers can draw over it. Preview pixel (px, py) covers raster pixels [px × scale, (px+1) × scale) × [py × scale, (py+1) × scale).

func Version

func Version() semver.Version

func WritePreview

func WritePreview(w io.Writer, g Grid, hexes []Hex, scale int) error

WritePreview writes a PNG of the grid with each hex filled by the color of its center elevation and hex outlines drawn darker. Each preview pixel covers scale × scale raster pixels.

Types

type Corner

type Corner int

Corner names a corner a hex owns.

const (
	WestCorner Corner = iota
	EastCorner
)

func (Corner) MarshalText

func (c Corner) MarshalText() ([]byte, error)

MarshalText encodes the corner as "west" or "east".

func (Corner) String

func (c Corner) String() string

type Direction

type Direction int

Direction names a neighboring hex. Flat-top hexes have no east or west neighbors.

const (
	North Direction = iota
	NorthEast
	SouthEast
	South
	SouthWest
	NorthWest
)

type EdgeKey added in v0.2.0

type EdgeKey struct {
	Col, Row int
	Side     Side
}

EdgeKey identifies an edge by the hex that owns it and which of that hex's three owned sides it is.

func EdgeBetween added in v0.2.0

func EdgeBetween(u, v VertexKey) (EdgeKey, bool)

EdgeBetween returns the edge joining two vertices, or false if they are not neighbors.

type Elevations

type Elevations struct {
	Hmz2eleVersion   string        `json:"hmz2ele_version"`
	Heightmap        HeightmapInfo `json:"heightmap"`
	Grid             GridInfo      `json:"grid"`
	Sampling         SamplingInfo  `json:"sampling"`
	Hexes            []Hex         `json:"hexes"`
	BoundaryVertices []Vertex      `json:"boundary_vertices"`
}

Elevations is the hex grid's sampled elevations, as written to JSON. Elevations are in meters; nil means no-data.

func NewElevations

func NewElevations(fileName string, hm *dem2hm.HeightMap16, g Grid) *Elevations

NewElevations samples the grid and returns the complete JSON document.

type Grid

type Grid struct {
	// Apothem is the distance from a hex's center to the middle of a side,
	// in pixels. A hex is 2 × Apothem pixels tall.
	Apothem int
	// Side is the length of a hex side, which is also the distance from the
	// center to a corner, in pixels.
	Side float64
	// Columns and Rows bound the column and row numbers of hexes in the
	// grid. Not every (column, row) inside those bounds is in the grid; use
	// Contains.
	Columns, Rows int
	// Width and Height are the raster's dimensions in pixels.
	Width, Height int
}

Grid is a flat-top hex grid laid over a raster, with even columns shifted down by half a hex. Hex (0, 0) is in the top-left corner and the first column's west corner is at x = 0.

Geometry uses continuous raster coordinates: pixel (i, j) covers [i, i+1) × [j, j+1).

func NewGrid

func NewGrid(apothem, width, height int) (Grid, error)

NewGrid returns the grid of every hex whose center pixel lies inside a width × height raster.

func (Grid) Center

func (g Grid) Center(col, row int) (x, y float64)

Center returns the center of the hex in the given column and row. It is defined for any column and row, including hexes outside the grid.

func (Grid) Contains

func (g Grid) Contains(col, row int) bool

Contains reports whether the hex's center pixel is inside the raster.

func (Grid) East

func (g Grid) East(col, row int) (x, y float64)

func (Grid) HexAt added in v0.2.0

func (g Grid) HexAt(x, y float64) (col, row int)

HexAt returns the column and row of the hex containing the point. The hex may be outside the grid.

func (Grid) NearestVertex added in v0.2.0

func (g Grid) NearestVertex(x, y float64) VertexKey

NearestVertex returns the vertex nearest the point. The nearest vertex is always a corner of the hex containing the point.

func (Grid) Vertex

func (g Grid) Vertex(k VertexKey) (x, y float64)

Vertex returns the position of the vertex.

func (Grid) West

func (g Grid) West(col, row int) (x, y float64)

West and East return the hex's west (180°) and east (0°) corners, which are the two corners each hex owns.

type GridInfo

type GridInfo struct {
	Layout     string  `json:"layout"`
	ApothemPx  int     `json:"apothem_px"`
	SidePx     float64 `json:"side_px"`
	Columns    int     `json:"columns"`
	Rows       int     `json:"rows"`
	WidthPx    int     `json:"width_px"`
	HeightPx   int     `json:"height_px"`
	HexCount   int     `json:"hex_count"`
	CornerNote string  `json:"corner_note"`
}

GridInfo describes the hex grid. See Grid.

type HeightmapInfo

type HeightmapInfo struct {
	FileName string          `json:"file_name"`
	Metadata dem2hm.Metadata `json:"metadata"`
}

HeightmapInfo identifies the heightmap the elevations were sampled from.

type Hex

type Hex struct {
	Col    int    `json:"col"`
	Row    int    `json:"row"`
	Center *int16 `json:"center"`
	West   *int16 `json:"west"`
	East   *int16 `json:"east"`
}

Hex is one hex's center elevation and the elevations of the two corners it owns.

func Build

func Build(r Raster, g Grid) []Hex

Build samples the center and owned corners of every hex in the grid, plus every corner that a hex in the grid uses but a hex outside the grid owns. Hexes are ordered by row, then column.

func (Hex) IsLand

func (h Hex) IsLand() bool

IsLand reports whether the hex's center elevation is above sea level.

type HexCorner added in v0.2.0

type HexCorner struct {
	Col, Row, Corner int
}

HexCorner is a hex and one of its corners, numbered clockwise from east (0°) in y-down raster coordinates: 0 east, 1 south-east, 2 south-west, 3 west, 4 north-west, 5 north-east.

func VertexHexes added in v0.2.0

func VertexHexes(k VertexKey) [3]HexCorner

VertexHexes returns the three hexes that meet at the vertex, with the vertex's corner number in each.

type Raster

type Raster struct {
	Width, Height int
	Data          []int16
}

Raster is the elevation data a sample reads: a row-major raster of elevations in meters, with dem2hm.NoDataPixel16 for no-data.

func RasterFromHeightMap

func RasterFromHeightMap(hm *dem2hm.HeightMap16) Raster

RasterFromHeightMap returns a Raster that shares the heightmap's data.

func (Raster) Sample

func (r Raster) Sample(x, y float64) (int16, bool)

Sample returns the elevation at (x, y), in meters, taken from the 3 × 3 block of pixels centered on the pixel nearest the point.

The elevation is the median of the valid pixels in the block. With an even number of valid pixels it is the mean of the two middle values, rounded half away from zero. Pixels outside the raster count as no-data. If 5 or more of the 9 pixels are no-data, Sample returns false.

type SamplingInfo

type SamplingInfo struct {
	Window         int    `json:"window"`
	Statistic      string `json:"statistic"`
	NoDataMajority int    `json:"no_data_majority"`
}

SamplingInfo describes how each elevation was sampled. See Raster.Sample.

type Side added in v0.2.0

type Side int

Side names an edge a hex owns. Each hex owns its north, north-east, and south-east edges; its south, south-west, and north-west edges are the north, north-east, and south-east edges of its south, south-west, and north-west neighbors.

const (
	NorthSide Side = iota
	NorthEastSide
	SouthEastSide
)

func (Side) MarshalText added in v0.2.0

func (s Side) MarshalText() ([]byte, error)

MarshalText encodes the side as "n", "ne", or "se".

func (Side) String added in v0.2.0

func (s Side) String() string

type Summary

type Summary struct {
	// Hexes is the number of hexes in the grid.
	Hexes int
	// Land hexes have a center elevation above sea level (0 m).
	Land int
	// Low hexes have a valid center elevation at or below sea level.
	Low int
	// NoData hexes have a no-data center: sea, or foreign land at a border
	// cut.
	NoData int
	// CoastalSea hexes are Low or NoData hexes next to at least one Land hex.
	CoastalSea int
}

Summary counts hexes by what their center sample shows.

func Summarize

func Summarize(g Grid, hexes []Hex) Summary

Summarize counts the hexes.

type Vertex

type Vertex struct {
	Col       int    `json:"col"`
	Row       int    `json:"row"`
	Corner    Corner `json:"corner"`
	Elevation *int16 `json:"elevation"`
}

Vertex is a corner used by a hex in the grid but owned by a hex outside it.

func BuildBoundaryVertices

func BuildBoundaryVertices(r Raster, g Grid) []Vertex

BuildBoundaryVertices samples every corner that a hex in the grid uses but a hex outside the grid owns. They are ordered by owner row, owner column, then corner.

type VertexKey

type VertexKey struct {
	Col, Row int
	Corner   Corner
}

VertexKey identifies a vertex by the hex that owns it and which of that hex's two owned corners it is.

func CornerOwner

func CornerOwner(col, row int, corner int) VertexKey

CornerOwner returns the owner of the hex's corner that lies between the given direction and the next direction clockwise. The north-east corner is between NorthEast and SouthEast, and so on. The owner of a hex's own east and west corners is the hex itself.

Every vertex touches exactly three hexes, and exactly one of them has the vertex as its west or east corner, so each vertex has exactly one owner.

func VertexNeighbors added in v0.2.0

func VertexNeighbors(k VertexKey) [3]VertexKey

VertexNeighbors returns the three vertices joined to the vertex by a hex edge.

Directories

Path Synopsis
cmd
hmz2ele command
Command hmz2ele samples a dem2hm heightmap (format 1.1) at the centers and corners of a flat-top hex grid and writes the elevations as JSON, with an optional PNG preview.
Command hmz2ele samples a dem2hm heightmap (format 1.1) at the centers and corners of a flat-top hex grid and writes the elevations as JSON, with an optional PNG preview.

Jump to

Keyboard shortcuts

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