Skip to main content

Drawing

Part of Architecture, which is built from ARCHITECTURE.md in the repository, where it is edited.

How shapes are drawn, edited, imported, exported and shared.

The draw tool follows the same split. packages/core/src/draw holds a pure reducer over the shapes (point, line, polygon, circle by centre and radius), the selection and a bounded undo stack. It also holds the rules for a valid shape (a line needs two different places, a polygon three that are not on one line and whose edges do not cross or touch, a circle a radius up to 10,000 km), measures that reuse the Measure tool's geodesic maths and unit formatters, the wording of every announcement, the key commands, GeoJSON, CSV and WKT text, and the link encoding. Ids come from a counter in the state, so the reducer needs no clock or randomness.

  • State. useDraw (in packages/map/src/tools/DrawToolbar) runs the reducer and adds what needs the map and the ports: the map centre and projection for "at map centre", arrow-key nudges and picking a shape by tapping its edge; the announcer; the file saver and clipboard for export; and the file opener for import. Shapes belong to the app, not to the toolbar, so they stay on the map when the tool closes. An unfinished shape is dropped when the tool closes, except while the sheet is open over it. Nothing is stored.
  • Surface. draw is a surface like measure, so one tool is active at a time. Opening the sheet over a drawing sets resumeDraw, and closing the sheet goes back to the draw tool with focus on its Shapes button.
  • Drawing. Shapes, vertices, midpoint handles, the selection and the dashed preview are drawn in the single SVG overlay (DrawOverlay), so they survive a basemap swap with no engine calls. DrawMapBindings hands the handles of the selected shape to the adapter and passes its handle events to useDraw. A midpoint becomes a new vertex under the pointer for the rest of the drag, and a whole drag, or a run of arrow presses, is one undo step. The overlay is not part of the engine canvas, so an image export of the map has to composite it.
  • Keys. MapView takes an onKeyCommand that runs before the map's own keys. The arrows only move a point while one is selected, so they pan the map otherwise.
  • Export. shapesToGeoJsonText (the collection from shapesToGeoJson, indented), shapesToCsv and shapesToWkt build the text in core, and the FileSaver port offers it as a download. The CSV has id, type, area_m2, length_m, the geometry as GeoJSON text, and a circle's radius_m, centre_lng and centre_lat. The WKT is one geometry to a line, longitude first, with a circle written as the polygon of its outline (formatWkt, in packages/core/src/geometry, writes each one). Positions are rounded to six decimals, about 10 cm. A shape that six decimals would fold in on itself, such as a circle with a radius of a metre or two, is written with the fewest more decimals, up to ten, that keep it valid. A shape that not even ten decimals keep valid, such as a circle a few micrometres across, is written unrounded, so no valid finished shape is left out of an export. A circle whose outline is not valid in the plane to begin with, because it reaches over a pole or the antimeridian, is written at six decimals as before. A circle's centre and radius_m (and the CSV's centre_lng, centre_lat and radius_m) follow its outline: six decimals and two where the outline is written at six, as they always were, and exactly as they are where the outline needed more, so a circle of a centimetre or two keeps its centre and radius through a round trip. A circle over a pole or the antimeridian keeps six and two, so one under 5 mm there is written with a radius_m of 0. Every number in the three texts is plain decimal text and never has an exponent (0.00000078, not 7.8e-7), because some readers of WKT and CSV do not accept one; the digits are those String gives, so a number that has no exponent is written as it always was. JSON.stringify cannot do this, which is why the GeoJSON file is written by shapesToGeoJsonText and the number writer is fixedDecimal in packages/core/src/geometry (also behind formatWkt). Nothing is sent anywhere.
  • Import. The optional FileOpener port asks for a file and reads it in the browser (a host without one offers no import); it rejects only with a FileOpenError (cancelled, too-large, unreadable, unsupported), and its adapter checks the size before reading. parseShapeFile in core reads GeoJSON (a collection, a feature or a geometry; Multi geometries split into parts, a geometry collection flattened once, a polygon's holes left out and counted), the CSV the exporter writes, so an exported file comes back as the same shapes, circles included (a CSV from before the circle columns still imports, with a circle as its polygon, and so does a circle whose radius_m is not above zero, as an older export wrote for a circle under 5 mm), and WKT. WKT is read by readWktRecovering, the unchecked and forgiving sibling of parseWkt in packages/core/src/geometry (readWktSequence is the same reader that gives up at the first fault): one geometry or several one after another, as the text of the file or in the cells of a CSV column headed wkt (any case; the draw tool's own header is tried first). Each geometry goes through the same per-shape rules as GeoJSON, a Multi geometry splitting into parts and a collection flattening once. Its two numbers are longitude then latitude unless parseShapeFile is given wktAxisOrder: "lat-lng", which ShapesSection offers as a native select kept in useDraw for the session (nothing is stored). suggestWktAxisOrder finds the first two numbers after the first bracket that follows a WKT geometry type (so a bracketed pair in a CSV note is not one) and, when they fall outside the world in the order in use but inside it in the other, names the other order. It reads only the first MAX_AXIS_SCAN_CHARACTERS (4,096) characters in one linear pass, and gives no suggestion for text over MAX_IMPORT_BYTES, so its work is bounded however large the file is. useDraw then imports nothing, keeps the text, says so (describeAxisSuggestion) and sets readAs on the import feedback, and rereadImport reads the kept text in that order. The kept text is dropped when the shapes change, when the select is changed by hand or when the next file cannot be opened, and focus moves to the select when the re-read button goes. A database export's EWKT prefix, SRID=4326;, is read and dropped, at the start of a whole geometry only and written as a plain run of at most nine digits. Any other SRID is an UnsupportedSridError (an unsupported GeometryError that carries the SRID), which the importer counts as other-srid, worded "in a coordinate system other than SRID 4326", apart from the "of a type that cannot be drawn" of a measure or a curve. The reader goes on after a geometry it cannot use, and readWktRecovering gives each stretch of text in turn, as a geometry or as the GeometryError for that stretch, one at a time, so the importer can stop at its caps. A geometry that is valid WKT the reader leaves out (a measure, a curve, another SRID) is skipped to the bracket that closes it, so the geometries after it are kept even on the same line, and counts as one unsupported (or other-srid). Outside a bracket the skip ends at the end of the line the geometry began on; once a bracket of it is open the skip goes on over lines, so a collection or a curve written over several lines is skipped whole, and a closing bracket left over after it is unreadable text. If the end of the text, or a character the reader does not know, ends the skip inside a bracket, the skip is made again as if it ended at the end of its line, and the damaged-text rule below applies. The tokens read for nothing in such skips are added up, and once they are more than the length of the text every later skip ends at the end of its line, so the work stays linear however many geometries are left open, and a few stray brackets do not change how the geometries after them are read. Text that cannot be read is skipped up to the next line that starts with a geometry type or an SRID= prefix (the = may come after a line break, as the reader allows) (or, when the broken geometry was cut short by such a line, from that line), and counts as one malformed; so one bad line in a file of one geometry to a line costs one skip, and the lines that carry on unreadable text are skipped with it as one stretch, counted once, unless one of them starts with a geometry type or SRID=. Reading goes back over text only after a skip that ran out, and only within a budget. Only text in which not one geometry could be used is refused whole as wkt-unreadable, with the reader's own words for its first fault, and its position where it has one, carried in the refusal's detail (startsLikeWkt tells such a file from one that is not WKT at all). A CSV cell is read the same way, each stretch of it that cannot be used counting as one skipped shape. The wkt column is found in a comma-delimited CSV only. It is pure, never throws, tells the format from the content, and is bounded before it walks: 1 MB, 32 levels of nesting (measured by one pass before JSON.parse), 500 shapes, 1,000 points a shape and 10,000 in all. A file over a cap is refused whole; a shape that breaks the draw rules or lies outside the world is skipped and counted. Keys are read only when they are an object's own, so __proto__ is inert. The reducer's import action appends the shapes as one undo step, ids and labels continuing from the drawing, before any shape still being drawn. An import that would take the drawing past 1,000 shapes (MAX_DRAWING_SHAPES) is refused whole. ShapesSection shows the result in words and fits the map to what was added (shapeInputBounds, the short way across the antimeridian), clear of the sheet, the search pill and the end stack (fitPadding, shared with Directions), no closer than FIT_MAX_ZOOM.
  • Sharing. Shapes are never written to the address bar and never stored. "Copy link with shapes" calls UrlState.link with the current address state plus shapes=, a text of link-safe characters (A-Z a-z 0-9 . - _): one letter for the kind, then positions in units of 0.00001 degrees (coarser than the file exports, which keep six decimals or more) and a circle's radius in whole metres, the first absolute and each later one as a zigzag step, packed five bits to a character, with shapes separated by a full stop. A link is limited to 2,000 characters and 50 shapes, and is refused with a message rather than cut short for its size. A shape the link cannot carry is left out, and the message says how many were and offers a file instead (every shape too small gives an error and copies nothing): a circle under a metre, since a radius in whole metres would read back up to twice the size, or as nothing, and a line or polygon that does not read back as a valid shape once its positions are whole units, such as a line whose two ends fall on the same unit. unsharableShapes finds them by encoding each shape and decoding it again with the rules the reader uses, so the writer and the reader cannot disagree. The radius is not scaled, so a link written before keeps its meaning. Reading a link skips a part that cannot be read or that fails the draw rules, and keeps the rest; the text as a whole is refused when it is outside the alphabet, too long, over 50 parts or holds no valid shape. Reading a link is bounded and linear in its length, and the next write of the address drops shapes=, because only the view, basemap and layers are written.