API Reference#

papermap.papermap Module#

class papermap.papermap.PaperMap(lat, lon, tile_server='OpenStreetMap', api_key=None, size='a4', use_landscape=False, margin_top=10, margin_right=10, margin_bottom=10, margin_left=10, scale=25000, dpi=300, background_color='#fff', add_grid=False, grid_size=1000)#

A paper map.

>>> from papermap import PaperMap
>>> pm = PaperMap(13.75889, 100.49722)
>>> pm.render()
>>> pm.save("Bangkok.pdf")
Parameters:
  • lat (float) – Latitude of the center of the map.

  • lon (float) – Longitude of the center of the map

  • tile_server (str) – Tile server to serve as the base of the paper map. Defaults to OpenStreetMap.

  • api_key (str | None) – API key for the chosen tile server (if applicable). Defaults to None.

  • size (str) – Size of the paper map. Defaults to a4.

  • landscape – Use landscape orientation. Defaults to False.

  • margin_top (int) – Top margin (in mm). Defaults to 10.

  • margin_right (int) – Right margin (in mm). Defaults to 10.

  • margin_bottom (int) – Bottom margin (in mm). Defaults to 10.

  • margin_left (int) – Left margin (in mm). Defaults to 10.

  • scale (int) – Scale of the paper map. Defaults to 25000.

  • dpi (int) – Dots per inch. Defaults to 300.

  • background_color (str) – Background color of the paper map. Defaults to #fff.

  • add_grid (bool) – Add a coordinate grid overlay to the paper map. Defaults to False.

  • grid_size (int) – Size of the grid squares (if applicable, in meters). Defaults to 1000.

Raises:
  • ValueError – If the tile server is invalid.

  • ValueError – If no API key is specified (when applicable).

  • ValueError – If the paper size is invalid.

  • ValueError – If the scale is “out of bounds”.

render()#

Render the paper map, consisting of the map image, grid (if applicable), attribution and scale.

save(file, title='PaperMap', author='PaperMap')#

Save the paper map to a file.

Parameters:
  • file (str | Path) – The file to save the paper map to.

  • title (str) – The title of the PDF document. Defaults to PaperMap.

  • author (str) – The author of the PDF document. Defaults to PaperMap.

papermap.utils Module#

papermap.utils.cartesian_to_spherical(x, y, z)#

Convert cartesian coordinates (i.e. x, y, z) to spherical coordinates (i.e. lat, lon).

Adapted from: https://github.com/chrisveness/geodesy

Parameters:
  • x (float) – The x coordinate.

  • y (float) – The y coordinate.

  • z (float) – The z coordinate.

Returns:

The spherical coordinates (i.e. lat, lon).

Return type:

Tuple[float, float]

papermap.utils.clip(val, lower, upper)#

Clips a value to [lower, upper] range.

Parameters:
  • val (float) – The value.

  • lower (float) – The lower bound.

  • upper (float) – The upper bound.

Returns:

The value clipped to [lower, upper] range.

Return type:

float

papermap.utils.compute_central_lon(zone)#

Compute the central longitude of a given UTM zone number.

Parameters:

zone (int) – The UTM zone number.

Returns:

The central longitude.

Return type:

float

papermap.utils.dd_to_dms(dd)#

Convert Decimal Degrees (DD) to Degrees, Minutes, and Seconds (DMS).

Parameters:

dd (float) – The Decimal Degrees.

Returns:

The Degrees, Minutes, and Seconds.

Return type:

Tuple[int, int, float]

papermap.utils.dms_to_dd(dms)#

Convert Degrees, Minutes, and Seconds (DMS) to Decimal Degrees (DD).

Parameters:

dms (Tuple[int, int, float]) – The Degrees, Minutes, and Seconds.

Returns:

The Decimal Degrees.

Return type:

float

papermap.utils.lat_to_y(lat, zoom)#

Converts latitude to y (tile coordinate), given a zoom level.

Parameters:
  • lat (float) – The latitude.

  • zoom (int) – The zoom level.

Returns:

The y (tile coordinate).

Return type:

float

papermap.utils.lon_to_x(lon, zoom)#

Converts longitude to x (tile coordinate), given a zoom level.

Parameters:
  • lon (float) – The longitude.

  • zoom (int) – The zoom level.

Returns:

The x (tile coordinate).

Return type:

float

papermap.utils.mm_to_pt(mm)#

Convert millimeters to points.

Parameters:

mm (float) – The millimeters.

Returns:

The points.

Return type:

float

papermap.utils.mm_to_px(mm, dpi=300)#

Convert millimeters to pixels, given the dpi.

Parameters:
  • mm (float) – The millimeters.

  • dpi (int) – Dots per inch. Defaults to 300.

Returns:

The pixels.

Return type:

int

papermap.utils.pt_to_mm(pt)#

Convert points to millimeters.

Parameters:

pt (float) – The points.

Returns:

The millimeters.

Return type:

float

papermap.utils.px_to_mm(px, dpi=300)#

Convert pixels to millimeters, given the dpi.

Parameters:
  • px (int) – The pixels.

  • dpi (int) – Dots per inch. Defaults to 300.

Returns:

The millimeters.

Return type:

float

papermap.utils.scale_to_zoom(scale, lat, dpi=300)#

Compute the zoom level, given the latitude, scale and dpi.

Parameters:
  • scale (int) – The scale.

  • lat (float) – The latitude.

  • dpi (int) – Dots per inch. Defaults to 300.

Returns:

The zoom level.

Return type:

float

papermap.utils.spherical_to_cartesian(lat, lon, r=6378137)#

Convert spherical coordinates (i.e. lat, lon) to cartesian coordinates (i.e. x, y, z).

Adapted from: https://github.com/chrisveness/geodesy

Parameters:
  • lat (float) – The latitude.

  • lon (float) – The longitude.

  • r (float) – The radius of the sphere. Defaults to 6378137

  • radius). ((equatorial Earth) –

Returns:

The cartesian coordinates (x, y, z).

Return type:

Tuple[float, float, float]

papermap.utils.spherical_to_utm(lat, lon)#

Convert a spherical coordinate (i.e. lat, lon) to a UTM coordinate.

Based on formulas from (Karney, 2011).

Adapted from: https://github.com/chrisveness/geodesy/blob/master/utm.js

Parameters:
  • lat (float) – The latitude.

  • lon (float) – The longitude.

References

Karney, C. F. (2011). Transverse Mercator with an accuracy of a few nanometers. Journal of Geodesy, 85(8), 475-485.

papermap.utils.spherical_to_zone(lat, lon)#

Compute the UTM zone number of a given spherical coordinate (i.e. lat, lon).

Parameters:
  • lat (float) – The latitude.

  • lon (float) – The longitude.

Returns:

The UTM zone number.

Return type:

int

papermap.utils.utm_to_spherical(x, y, zone, hemisphere)#

Convert a UTM coordinate to a spherical coordinate (i.e. lat, lon).

Based on formulas from (Karney, 2011).

Adapted from: https://github.com/chrisveness/geodesy/blob/master/utm.js

Parameters:
  • x (float) – The easting.

  • y (float) – The northing.

  • z – The zone number.

  • l – The hemisphere.

References

Karney, C. F. (2011). Transverse Mercator with an accuracy of a few nanometers. Journal of Geodesy, 85(8), 475-485.

papermap.utils.wrap(angle, limit)#

Wraps an angle to [-limit, limit] range.

Parameters:
  • angle (float) – The angle.

  • limit (float) – The lower and upper limit.

Returns:

The angle wrapped to [-limit, limit] range.

Return type:

float

papermap.utils.wrap180(angle)#

Wraps an angle to [-180, 180] range

Parameters:

angle (float) – The angle.

Returns:

The angle wrapped to [-180, 180] range.

Return type:

float

papermap.utils.wrap360(angle)#

Wraps an angle to [0, 360) range

Parameters:

angle (float) – The angle.

Returns:

The angle wrapped to [0, 360) range.

Return type:

float

papermap.utils.wrap90(angle)#

Wraps an angle to [-90, 90] range.

Parameters:

angle (float) – The angle.

Returns:

The angle wrapped to [-90, 90] range.

Return type:

float

papermap.utils.x_to_lon(x, zoom)#

Converts x (tile coordinate) to longitude, given a zoom level.

Parameters:
  • x (int | float) – The tile coordinate.

  • zoom (int) – The zoom level.

Returns:

The longitude.

Return type:

float

papermap.utils.x_to_px(x, x_center, width, tile_size=256)#

Convert x (tile coordinate) to pixels.

Parameters:
  • x (int) – The tile coordinate.

  • x_center (int) – Tile coordinate of the center tile.

  • width (int) – The image width.

  • tile_size (int) – The tile size. Defaults to 256.

Returns:

The pixels.

Return type:

int

papermap.utils.y_to_lat(y, zoom)#

Converts y (tile coordinate) to latitude, given a zoom level.

Parameters:
  • y (int | float) – The tile coordinate.

  • zoom (int) – The zoom level.

Returns:

The latitude.

Return type:

float

papermap.utils.y_to_px(y, y_center, height, tile_size=256)#

Convert y (tile coordinate) to pixel

Parameters:
  • y (int) – The tile coordinate.

  • y_center (int) – Tile coordinate of the center tile.

  • height (int) – The image height.

  • tile_size (int) – The tile size. Defaults to 256.

Returns:

The pixels.

Return type:

int

papermap.utils.zoom_to_scale(zoom, lat, dpi=300)#

Compute the scale, given the latitude, zoom level and dpi

Parameters:
  • zoom (int) – The zoom level.

  • lat (float) – The latitude.

  • dpi (int) – Dots per inch. Defaults to 300.

Returns:

The scale.

Return type:

float

papermap.tile Module#

class papermap.tile.Tile(x, y, zoom, bbox, image=None)#

A tile from a tile server.

Parameters:
  • x (int) – The x coordinate of the tile.

  • y (int) – The y coordinate of the tile.

  • zoom (int) – The zoom level of the tile.

  • bbox (Tuple[int, int, int, int]) – The bounding box of the tile.

  • image (Image | None) – The image of the tile. Defaults to None.

format_url_template(url_template, mirror=None, api_key=None)#

Format a URL template with the tile’s coordinates and zoom level.

Parameters:
  • url_template (str) – The URL template to format.

  • mirror (str | None) – The mirror to use. Defaults to None.

  • api_key (str | None) – The API key to use. Defaults to None.

Returns:

The formatted URL template.

Return type:

str

property success: bool#

Whether the tile was successfully downloaded or not.

papermap.tile_server Module#

class papermap.tile_server.TileServer(attribution, url_template, zoom_min, zoom_max, mirrors=None)#

A tile server.

Parameters:
  • attribution (str) – The attribution of the tile server.

  • url_template (str) – The URL template of the tile server. Allowed placeholders are {x}, {y}, {zoom}, {mirror} and {api_key}, where {x} refers to the x coordinate of the tile, {y} refers to the y coordinate of the tile, {zoom} to the zoom level, {mirror} to the mirror (optional) and {api_key} to the API key (optional). See https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#Tile_servers for more information.

  • zoom_min (int) – The minimum zoom level of the tile server.

  • zoom_max (int) – The maximum zoom level of the tile server.

  • mirrors (List[str | int | None] | None) – The mirrors of the tile server. Defaults to None.

format_url_template(tile, api_key=None)#

Format the URL template with the tile’s coordinates and zoom level.

Parameters:
  • tile (Tile) – The tile to format the URL template with.

  • api_key (str | None) – The API key to use. Defaults to None.

Returns:

The formatted URL template.

Return type:

str

papermap.typing Module#

Type information used throughout papermap.

papermap.typing.Angle#

Angle in either degrees or radians.

papermap.typing.Cartesian_2D#

Two-dimensional Cartesian (x, y) coordinates.

alias of Tuple[float, float]

papermap.typing.Cartesian_3D#

Thee-dimensional Cartesian (x, y, z) coordinates.

alias of Tuple[float, float, float]

papermap.typing.DMS#

Degrees, Minutes, and Seconds (DMS).

alias of Tuple[int, int, float]

papermap.typing.Degree#

Angle in degrees.

papermap.typing.Pixel#

Number of pixels.

papermap.typing.Radian#

Angle in radians.

papermap.typing.Spherical_2D#

Two-dimensional Spherical (lat, lon) coordinates.

alias of Tuple[float, float]

papermap.typing.Spherical_3D#

Thee-dimensional Spherical (lat, lon, height) coordinates.

alias of Tuple[float, float, float]

papermap.typing.UTM_Coordinate#

UTM coordinate (easting, northing, zone, hemisphere).

alias of Tuple[float, float, int, str]