Skip to main content

parseShapeFile

Reads the shapes in a GeoJSON file, in Well-Known Text (WKT) or in a CSV, ready for the draw reducer's import action. Pure and bounded, and never throws: text from a file is hostile until proven otherwise.

This is a function exported by @mapcairn/core, checked against version 0.2.0.

Signature​

import { parseShapeFile } from "@mapcairn/core";

function parseShapeFile(text: string, options?: ShapeImportOptions): ShapeImport;

Parameters​

  • text (string)
  • options? (ShapeImportOptions)

Returns​

ShapeImport

Remarks​

The format is told from the content. GeoJSON may be a FeatureCollection, a Feature or a bare geometry; Multi geometries give one shape per part, a GeometryCollection is flattened once, and a polygon keeps its outer ring, its holes left out and counted. A feature whose properties name a circle with radius_m and centre, as the exporter writes, is a circle again, and so is a CSV row with radius_m, centre_lng and centre_lat; where the radius is not above zero, as an older export wrote for a circle under 5 mm, the shape is read from its geometry instead, as a polygon. Every shape must pass the draw tool's own rules with positions inside the world, or it is skipped and counted.

WKT may be one geometry or several one after another (one to a line is usual), each read like its GeoJSON twin, with the order of its two numbers set by options.wktAxisOrder. A geometry that cannot be used is skipped and counted, and the geometries after it are still read: one that holds a measure (M), a curved type or an EWKT SRID= prefix other than SRID=4326; is skipped to its closing bracket (counted as unsupported, or other-srid), and damaged text is skipped up to the next line that starts a geometry (counted as malformed). Only WKT text in which not one geometry could be used is refused whole, as wkt-unreadable, with the reader's own words (and position, where it has one) for its first fault in detail; text that starts with neither a WKT type nor an SRID= prefix is not-recognised. A CSV that is not the draw tool's own is read as WKT when its header has a column named wkt (any case, comma-delimited only), whose cells are read as WKT in the same way, each stretch of a cell that cannot be used counting as one skip.

The text is refused whole when it is longer than MAX_IMPORT_BYTES characters, nests deeper than MAX_IMPORT_DEPTH (measured before parsing), or holds more than MAX_IMPORT_SHAPES shapes or MAX_IMPORT_POINTS points; a shape of more than MAX_IMPORT_SHAPE_POINTS points is skipped. Keys are read only when they are an object's own, so a __proto__ key is inert. The work is linear in the text, apart from each polygon's check for crossing edges, which the per-shape cap bounds.

See also​

Source: packages/core/src/draw/import.ts:788