Skip to content

useConvertToGeoJSON

Package
Share - File
Category
Description
Easily convert raw input data into GeoJSON format, with automatic geometry detection, validation.

Easily convert raw input data into GeoJSON format, with automatic geometry detection, validation.

Demo

[
  {
    "name": "Test Line",
    "geometry": [
      [
        105.8,
        21
      ],
      [
        105.9,
        21.1
      ]
    ]
  },
  {
    "name": "Test Polygon",
    "geometry": [
      [
        [
          105.8,
          20.8
        ],
        [
          105.9,
          20.8
        ],
        [
          105.9,
          20.9
        ],
        [
          105.8,
          20.9
        ],
        [
          105.8,
          20.8
        ]
      ]
    ]
  }
]
    

GeoJSON Converter

null
    

Return Values

NameDescription
convertFeatureConvert a single item with geometry into a valid GeoJSON Feature
convertListConvert an array of items into a GeoJSON FeatureCollection

Input Format

Each input item should follow this structure:

  • geometry (any)
    An array representing the coordinates. The geometry type is auto-detected based on its structure.
  • Additional key-value pairs will be preserved in the GeoJSON properties.

Example Input

ts
const rawData = [
  {
    name: 'Line A',
    geometry: [
      [105.8, 20.9],
      [105.9, 21.0],
    ],
  },
  {
    name: 'Polygon B',
    geometry: [
      [
        [105.8, 20.8],
        [105.9, 20.8],
        [105.9, 20.9],
        [105.8, 20.9],
        [105.8, 20.8],
      ],
    ],
  },
];

Usage

ts
import { useConvertToGeoJSON } from '@hungpvq/shared-file';

const { convertFeature, convertList } = useConvertToGeoJSON();

const feature = convertFeature({
  name: 'Test Line',
  geometry: [
    [105.8, 20.9],
    [105.9, 21.0],
  ],
});

const collection = convertList([
  {
    name: 'Test Line',
    geometry: [
      [105.8, 20.9],
      [105.9, 21.0],
    ],
  },
]);