onmap

package module
v1.4.2 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2022 License: MIT Imports: 9 Imported by: 0

README

onmap

Go package onmap puts pins on a world map image.

The images (mercator.jpg, pin.png and pin-shadow.png) are embedded in the package, no need to ship them separately.

Image licensing

The default world map used is:

https://en.wikipedia.org/wiki/File:Mercator_projection_SW.jpg
License: Strebe, CC BY-SA 3.0 <https://creativecommons.org/licenses/by-sa/3.0>, via Wikimedia Commons

Pin images are created by the author of the package, licensed under this package license (MIT).

Example

package main

import (
    "os"
    "log"
    "image/png"

    "github.com/dchest/onmap"
)

func main() {
	coords := []onmap.Coord{
		{42.1, 19.1},             // Bar
		{55.755833, 37.617222},   // Moscow
		{41.9097306, 12.2558141}, // Rome
		{-31.952222, 115.858889}, // Perth
		{42.441286, 19.262892},   // Podgorica
		{38.615925, -27.226598},  // Azores
		{45.4628329, 9.1076924},  // Milano
		{43.7800607, 11.170928},  // Florence
		{37.7775, -122.416389},   // San Francisco
	}

	m := onmap.Pins(coords, onmap.StandardCrop)
	f, err := os.Create("out.png")
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()
	if err := png.Encode(f, m); err != nil {
		log.Fatal(err)
	}
}

Result (shown as webp, not png):

World map with pins, cropped

Just one coordinate for San Francisco and using StandardCrop:

World map with pins, cropped

Other projections

You can use a different projection by defining the following interface for it:

// Projection is an interface for converting coordinates.
type Projection interface {
	// Convert converts coordinates into a point on a map.
	Convert(coord Coord, mapWidth, mapHeight int) image.Point
}

and using MapPinsProjection with your own world map in that projection.

Documentation

Overview

Package onmap puts pins into a world map image.

Index

Constants

This section is empty.

Variables

View Source
var Mercator = mercatorProjection(0)

Mercator provides the Mercator projection.

View Source
var StandardCrop = &CropOption{
	Bound:         100,
	MinWidth:      640,
	MinHeight:     543,
	PreserveRatio: true,
}

StandardCrop is the standard crop.

Functions

func DefaultMap added in v1.4.0

func DefaultMap() image.Image

DefaultMap returns the default map (Mercator projection).

func DefaultPin added in v1.4.0

func DefaultPin() []image.Image

DefaultPin returns default pin images.

func MapPins

func MapPins(worldMap image.Image, pinParts []image.Image, coords []Coord, crop *CropOption) image.Image

MapPins is like MapPinsProjection with Mercator projection. The world map must be in the same projection.

func MapPinsProjection added in v1.1.0

func MapPinsProjection(proj Projection, worldMap image.Image, pinParts []image.Image, coords []Coord, crop *CropOption) image.Image

MapPinsProjection returns an image with the given coordinates marked as pins on the given world map. If crop is nil, doesn't crop the image.

World map must be in the given projection.

Pin parts are arbitrary pin images, usually a shadow of the pin and the pin itself. Pin parts are drawn on top of each other from the bottom of the map to the top by first drawing pinParts[n], then pinParts[n+1], etc. The coordinate point is at the bottom center of each pin part image.

func Pins

func Pins(coords []Coord, crop *CropOption) image.Image

Pins is like MapPins but uses the embedded world map and pin images.

Types

type Coord

type Coord struct {
	// Latitude
	Lat float64

	// Longitude
	Long float64
}

Coord describes decimal coordinates.

type CropOption

type CropOption struct {
	// Bound is a minimum distance from the pin to the image boundary.
	Bound int

	// MinWidth is a minimum width of image.
	MinWidth int

	// MinHeight is a minimum height of image.
	MinHeight int

	// If PreserveRatio is true, the image preserves the ratio between
	// MinWidth and MinHeight.
	//
	// MinHeight must be less than MinWidth for this to work correctly.
	PreserveRatio bool
}

CropOptions defines options for cropping the map image.

type Projection added in v1.1.0

type Projection interface {
	// Convert converts coordinates into a point on a map.
	Convert(coord Coord, mapWidth, mapHeight int) image.Point
}

Projection is an interface for converting coordinates.

Jump to

Keyboard shortcuts

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