Skip to contents

Places a pic drawing on the Earth and returns it as a GeoJSON FeatureCollection (RFC 7946, WGS84 longitude/latitude), through the engine's geopic-core. The drawing is registered by a similarity — declared in the source with the variables geo_lon, geo_lat (the position of the pic origin), geo_scale (metres per inch, default 100), geo_bearing (azimuth of the drawing's +y axis, degrees clockwise from north) and geo_frame (0 = enu, 1 = mercator) — or recovered from control points: a place A: (x, y) with A_lon/A_lat (one point anchors, two fix scale and bearing exactly, three or more are fitted by least squares). Every argument here overrides the corresponding source variable (the caller wins, like -D in troff).

Usage

rpic_geojson(
  src,
  frame = c("enu", "mercator"),
  anchor = NULL,
  scale = NULL,
  bearing = NULL,
  features = NULL,
  flatten = 0.001,
  view_scale = NULL,
  circuits = NULL,
  texlabels = NULL,
  name_col = "name"
)

Arguments

src

pic source code: a pic() object or a single string.

frame

projection frame: "enu" (local tangent plane at the anchor: true distances, geodesic straight lines) or "mercator" (Web Mercator: lines stay straight on a web map). When omitted the source's geo_frame decides (default enu).

anchor

c(lon, lat) of the pic origin (0, 0), overriding geo_lon/geo_lat.

scale

metres per inch of the drawing as authored, overriding geo_scale.

bearing

azimuth of the drawing's +y axis in degrees clockwise from north, overriding geo_bearing.

features

host-provided named features (see the Features section).

flatten

curve tolerance in inches (cubic curves are flattened to within this distance before densification).

view_scale

pixels per inch, needed when the source sets geo_px = 1 (lengths in pixels, geo_scale in metres per pixel).

circuits

load the native circuit-element library (or write copy "circuits" in the source itself). NULL (the default) takes the pic() object's setting, FALSE for a string.

texlabels

typeset fully $...$-delimited labels as TeX math (initializer only — the source can still set texlabels = 0). NULL (the default) takes the pic() object's setting, FALSE for a string.

name_col

for an sf features, the column holding the names.

Value

a JSON string of class c("geojson", "json") with attributes "warnings" (see above) and "registration" — the similarity that placed the drawing: frame, anchor (lon, lat), origin (the canvas point at the anchor, inches), scale_m_per_in, bearing_deg, method ("anchor", "two_points", "helmert"), control_points and, for fits, rms_m. The same registration rides inside the GeoJSON as the foreign member geopic.

Details

Straight segments are densified in the plane before they are carried to longitude/latitude (no step longer than 100 m), so a circle of radius 2 km is a geodesic circle, not a circle in degree space. Each drawn object becomes one or more features (boxes and circles polygons, lines line strings, dots and text points, arrowheads separate features, and a shape with text an extra title point at the text's centre) whose properties are the simplestyle keys (stroke, stroke-width, fill, stroke-opacity, fill-opacity, title) and the geopic: namespace (geopic:id, geopic:kind, geopic:text, geopic:placement, ...); the full vocabulary is in the engine's docs/geopic.md (and, with the column names rpic_sf() gives them, in rpic_sf_names()).

Features

features names places the source may use without defining them (arrow from Recife to Olinda). Give it as

  • a named list of c(lon, lat) (or c(lon, lat, west, south, east, north) for a feature with extent);

  • a data.frame with columns name, lon, lat;

  • an sf object: the name comes from column name_col, the point is the geometry's centroid (sf::st_centroid()) and the extent its bounding box (sf::st_bbox()); data in another CRS is transformed to EPSG:4326.

Features the source itself defines (a place or a labelled object) act as control points of its own geometry. The others are placed by a first pass's registration and injected before the source — as places, or as invisible boxes spanning their extent so Name.n/Name.e follow it — and the drawing is georeferenced again with all of them. Names must be pic labels (an uppercase letter, then letters, digits or underscores); any other name is an error, never written into the source. Diagnostic positions stay relative to your src (a position inside the injected lines has file = "features").

Warnings and errors

Anything that cannot be represented exactly (a gradient fill, an animation, a control point that names no label, ...) is reported, never dropped silently: one R warning per distinct kind, of class rpic_geo_warning (which inherits rpic_warning), whose info field lists the warnings of that kind; all of them are also in the "warnings" attribute of the result (each a list of kind, message, hint, id, line, col, end_col, file; absent values are NA).

A drawing that cannot be registered (no anchor and no control points, a scale out of range, coincident control points, ...) raises an rpic_geo_error condition (inheriting rpic_error) whose info holds kind ("no_registration", "invalid_scale", "invalid_anchor", "invalid_flatten", "view_scale_required", "degenerate_control_points", "invalid_options"), message and hint. pic errors raise the usual rpic_error.

See also

rpic_sf() for an sf data frame; vignette("maps").

Examples

gj <- pic('geo_lon = -34.8711; geo_lat = -8.0631
geo_scale = 1000     # one inch is one kilometre
circle rad 2 at (0, 0)
"Recife" at (0, 0)') |>
  rpic_geojson()
attr(gj, "registration")$method   # "anchor"
#> [1] "anchor"
substr(gj, 1, 60)
#> {"type":"FeatureCollection","features":[{"type":"Feature","g 

# an arrow between two places the source does not define
gj <- rpic_geojson("arrow from Recife to Olinda",
  features = list(Recife = c(-34.8711, -8.0631), Olinda = c(-34.8553, -8.0089)))
attr(gj, "registration")$method   # "two_points"
#> [1] "two_points"

# no anchor and no control points: a classed error with a hint
tryCatch(rpic_geojson("box"), rpic_geo_error = function(e) e$info$kind)
#> [1] "no_registration"