Skip to content

Commit

Permalink
supporting geometry collections
Browse files Browse the repository at this point in the history
  • Loading branch information
ambrazasp committed Aug 25, 2023
1 parent d84b394 commit 650af48
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 17 deletions.
75 changes: 63 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ const result: ValidationResult = validate(featureCollection);

### parse

Accepts array or object of `type`, `coordinates` and `properties` (optional)
Accepts array or object of `type`, `coordinates` and `properties` (optional).

Also you can parse `GeometryCollection` (object or array of objects). In that case instead of `coordinates` and `properties` it uses `geometries`.

Returns [feature collection](#feature-collection)

Expand All @@ -82,21 +84,27 @@ Returns [feature collection](#feature-collection)
```js
import { parse } from 'geojsonjs';

const featureCollection = parse({ type: 'Point', coordinates: [11, 22] });
const featureCollection2 = parse([
{ type: 'Point', coordinates: [11, 22], properties: { prop1: 'prop1' } },
]);
const point1 = { type: 'Point', coordinates: [11, 22] };
const point2 = {
type: 'Point',
coordinates: [11, 22],
properties: { prop1: 'prop1' },
};
const geometryCollection = {
type: 'GeometryCollection',
geometries: [{ type: 'Point', coordinates: [11, 22] }],
};

const featureCollection = parse(point1);
const featureCollection2 = parse([point1, point2]);
const featureCollection3 = parse(geometryCollection);

// TypeScript
import { parse, FeatureCollection } from 'geojsonjs';

const featureCollection: FeatureCollection = parse({
type: 'Point',
coordinates: [11, 22],
});
const featureCollection2: FeatureCollection = parse([
{ type: 'Point', coordinates: [11, 22], properties: { prop1: 'prop1' } },
]);
const featureCollection: FeatureCollection = parse(point1);
const featureCollection2: FeatureCollection = parse([point1, point2]);
const featureCollection3: FeatureCollection = parse(geometryCollection);
```

### getGeometries
Expand Down Expand Up @@ -336,6 +344,8 @@ const result: ValidationResult = validateGeometryTypes(
- [Features (array)](#features-array)
- [Geometry](#geometry)
- [Geometries (array)](#geometries-array)
- [Geometry collection](#geometry-collection)
- [Geometry collections (array)](#geometry-collections-array)
- [Geometry item types](#geometry-types)
- Point
- MultiPoint
Expand Down Expand Up @@ -451,6 +461,47 @@ const result: ValidationResult = validateGeometryTypes(
]
```

### Geometry Collection

```json
{
"type": "GeometryCollection",
"geometries": [
{ "type": "Point", "coordinates": [11, 22] },
{
"type": "LineString",
"coordinates": [
[11, 22],
[22, 33]
]
}
]
}
```

### Geometry Collections (array)

```json
[
{
"type": "GeometryCollection",
"geometries": [{ "type": "Point", "coordinates": [11, 22] }]
},
{
"type": "GeometryCollection",
"geometries": [
{
"type": "LineString",
"coordinates": [
[11, 22],
[22, 33]
]
}
]
}
]
```

### Geometry types

```js
Expand Down
3 changes: 3 additions & 0 deletions src/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
GEOMETRY_COLLECTION,
GenericObject,
Geometry,
GeometryCollection,
GeometryType,
} from './types';
import * as utils from './utils';
Expand Down Expand Up @@ -45,6 +46,8 @@ export function getFeatures(geom: AllTypes): Feature[] {
return geom
.map((g) => getFeatures(g))
.reduce((acc: Feature[], item: Feature[]) => [...acc, ...item], []);
} else if (geom.type === GEOMETRY_COLLECTION) {
return getFeatures((geom as GeometryCollection).geometries);
} else if (geom.type === FEATURE_COLLECTION_TYPE) {
return (geom as FeatureCollection).features;
} else if (geom.type === FEATURE_TYPE) {
Expand Down
17 changes: 12 additions & 5 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ export type CoordinatesMultiLineString = CoordinatesLineString[];
export type CoordinatesPolygon = CoordinatesLineString[];
export type CoordinatesMultiPolygon = CoordinatesPolygon[];

export const FEATURE_COLLECTION_TYPE = 'FeatureCollection';
export const FEATURE_TYPE = 'Feature';
export const GEOMETRY_COLLECTION = 'GeometryCollection';

export type CoordinatesTypes =
| CoordinatesPoint
| CoordinatesLineString
Expand All @@ -20,6 +24,11 @@ export type Geometry = {
coordinates: CoordinatesTypes;
};

export type GeometryCollection = {
type: 'GeometryCollection';
geometries: Geometry[];
};

export type FeatureCollection = {
type: 'FeatureCollection';
features: Feature[];
Expand All @@ -31,10 +40,6 @@ export type Feature = {
properties?: GenericObject;
};

export const FEATURE_COLLECTION_TYPE = 'FeatureCollection';
export const FEATURE_TYPE = 'Feature';
export const GEOMETRY_COLLECTION = 'GeometryCollection';

export const ValidationError = {
EMTPY: 'EMTPY',
EMPTY_FEATURES: 'EMPTY_FEATURES',
Expand All @@ -61,7 +66,9 @@ export type AllTypes =
| Feature
| Feature[]
| FeatureCollection
| FeatureCollection[];
| FeatureCollection[]
| GeometryCollection
| GeometryCollection[];

export type ValidationResult = {
valid: boolean;
Expand Down

0 comments on commit 650af48

Please sign in to comment.