{"version":3,"file":"geojson.mjs","names":[],"sources":["../src/core/geojson.ts"],"sourcesContent":["/**\n * GeoJSON-shaped value types used by the PostGIS codec.\n *\n * The codec speaks GeoJSON-style objects to JavaScript callers and\n * EWKT/EWKB on the wire. This file is the single source of truth for the\n * value shapes; the public type re-export lives in `exports/geojson.ts`.\n */\n\nexport type Position = readonly [number, number];\n\nexport type GeometryPoint = {\n  readonly type: 'Point';\n  readonly coordinates: Position;\n  readonly srid?: number;\n};\n\nexport type GeometryLineString = {\n  readonly type: 'LineString';\n  readonly coordinates: ReadonlyArray<Position>;\n  readonly srid?: number;\n};\n\nexport type GeometryPolygon = {\n  readonly type: 'Polygon';\n  readonly coordinates: ReadonlyArray<ReadonlyArray<Position>>;\n  readonly srid?: number;\n};\n\nexport type GeometryMultiPoint = {\n  readonly type: 'MultiPoint';\n  readonly coordinates: ReadonlyArray<Position>;\n  readonly srid?: number;\n};\n\nexport type GeometryMultiLineString = {\n  readonly type: 'MultiLineString';\n  readonly coordinates: ReadonlyArray<ReadonlyArray<Position>>;\n  readonly srid?: number;\n};\n\nexport type GeometryMultiPolygon = {\n  readonly type: 'MultiPolygon';\n  readonly coordinates: ReadonlyArray<ReadonlyArray<ReadonlyArray<Position>>>;\n  readonly srid?: number;\n};\n\nexport type Geometry =\n  | GeometryPoint\n  | GeometryLineString\n  | GeometryPolygon\n  | GeometryMultiPoint\n  | GeometryMultiLineString\n  | GeometryMultiPolygon;\n\n/**\n * Construct a Point. Convenience for the common \"lng/lat\" case.\n *\n * @example\n *   point(-122.4194, 37.7749, 4326)\n */\nexport function point(longitude: number, latitude: number, srid?: number): GeometryPoint {\n  if (!Number.isFinite(longitude) || !Number.isFinite(latitude)) {\n    throw new RangeError('point: coordinates must be finite numbers');\n  }\n  return srid !== undefined\n    ? { type: 'Point', coordinates: [longitude, latitude], srid }\n    : { type: 'Point', coordinates: [longitude, latitude] };\n}\n\n/**\n * Construct a Polygon from a single outer ring of `[lng, lat]` pairs.\n * If the first and last positions differ, the ring is closed automatically.\n */\nexport function polygon(ring: ReadonlyArray<Position>, srid?: number): GeometryPolygon {\n  if (ring.length < 3) {\n    throw new Error('polygon: ring must contain at least 3 positions');\n  }\n  const first = ring[0];\n  const last = ring[ring.length - 1];\n  if (!first || !last) {\n    throw new Error('polygon: ring positions cannot be undefined');\n  }\n  for (const position of ring) {\n    if (!Number.isFinite(position[0]) || !Number.isFinite(position[1])) {\n      throw new RangeError('polygon: coordinates must be finite numbers');\n    }\n  }\n  const closed = first[0] === last[0] && first[1] === last[1] ? ring : [...ring, first];\n  const distinct = new Set(closed.slice(0, -1).map(([x, y]) => `${x},${y}`));\n  if (distinct.size < 3) {\n    throw new Error('polygon: ring must contain at least 3 distinct positions');\n  }\n  return srid !== undefined\n    ? { type: 'Polygon', coordinates: [closed], srid }\n    : { type: 'Polygon', coordinates: [closed] };\n}\n\n/**\n * Construct a rectangular Polygon from a bounding box.\n *\n * @param bbox - `[minLng, minLat, maxLng, maxLat]`\n */\nexport function bboxPolygon(\n  bbox: readonly [number, number, number, number],\n  srid?: number,\n): GeometryPolygon {\n  const [minX, minY, maxX, maxY] = bbox;\n  if (\n    !Number.isFinite(minX) ||\n    !Number.isFinite(minY) ||\n    !Number.isFinite(maxX) ||\n    !Number.isFinite(maxY)\n  ) {\n    throw new RangeError('bboxPolygon: coordinates must be finite numbers');\n  }\n  if (minX > maxX || minY > maxY) {\n    throw new Error(\n      `bboxPolygon: inverted bbox [${minX}, ${minY}, ${maxX}, ${maxY}] (expected minX <= maxX and minY <= maxY)`,\n    );\n  }\n  return polygon(\n    [\n      [minX, minY],\n      [maxX, minY],\n      [maxX, maxY],\n      [minX, maxY],\n      [minX, minY],\n    ],\n    srid,\n  );\n}\n"],"mappings":";;;;;;;AA4DA,SAAgB,MAAM,WAAmB,UAAkB,MAA8B;CACvF,IAAI,CAAC,OAAO,SAAS,SAAS,KAAK,CAAC,OAAO,SAAS,QAAQ,GAC1D,MAAM,IAAI,WAAW,2CAA2C;CAElE,O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