Skip to content

Commit

Permalink
Merge pull request #1 from reilem/fix-position-type
Browse files Browse the repository at this point in the history
Add dimensionality
  • Loading branch information
reilem authored Oct 7, 2024
2 parents 69551d4 + 4cac3c9 commit 2b63150
Show file tree
Hide file tree
Showing 36 changed files with 870 additions and 381 deletions.
71 changes: 70 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const schema = GeoJSONSchema.parse({
});
```

This library also exposes schemas for the individual GeoJSON types:
This library exposes schemas for the individual GeoJSON types:

```typescript
import {
Expand Down Expand Up @@ -72,6 +72,33 @@ import type {
} from "zod-geojson";
```

### Dimensionality

This library exports specific schemas for 2D and 3D geometries, and their accompanying types:

```typescript
import type {
GeoJSON2DFeatureSchema,
GeoJSON2DFeature,
// ...
GeoJSON2DPointSchema,
GeoJSON2DPoint,
} from "zod-geojson";
```

If you wish the use a different dimension, the generic schemas are also exposed and you can
use them to create your own schemas and types:

```typescript
import { GeoJSONGeometryGenericSchema } from "zod-geojson";

const GeoJSON4DPositionSchema = z.tuple([z.number(), z.number(), z.number(), z.number()]);
type GeoJSON4DPosition = z.infer<typeof GeoJSON4DPositionSchema>;

const GeoJSON4DGeometrySchema = GeoJSONGeometryGenericSchema(GeoJSON4DPositionSchema);
type GeoJSON4DGeometry = z.infer<typeof GeoJSON4DGeometrySchema>;
```

## Error Cases

This library will throw an error if the GeoJSON object is not valid. For example, where the coordinates type does
Expand Down Expand Up @@ -102,6 +129,48 @@ const schema = GeoJSONSchema.parse({
});
```

## Shortcomings

### Error messages

The error messages are currently very big and not user-friendly due to the default handling of failures in
nested zod unions. This is something I hope to improve in the future.

### Nested Geometry Collections

This library does not support the validation of nested geometry collections. E.g.

```typescript
// This will fail
const schema = GeoJSONSchema.parse({
type: "GeometryCollection",
geometries: [
{
type: "GeometryCollection",
geometries: [
{
type: "Point",
coordinates: [0, 0],
},
],
},
],
bbox: [0, 0, 1, 1],
});
```

This is per the GeoJSON RFC [recommendation](https://datatracker.ietf.org/doc/html/rfc7946#section-3.1.8):

> To maximize interoperability, implementations SHOULD avoid nested GeometryCollections.
and also because the implementation of recursive zod schemas together with generics is quite cumbersome and would
needlessy complicate both the implementation and usage of this library. If you need to validate nested geometry
collections feel free to open an issue and we can discuss possible solutions.

## Contributing

If you find any issues with the schemas or want to add new features, feel free to open an issue or a pull request.

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
24 changes: 19 additions & 5 deletions examples/feature_collection.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { GeoJSONFeatureCollection } from "../src";
import { GeoJSON2DFeatureCollection, GeoJSON3DFeatureCollection, GeoJSONFeatureCollection } from "../src";

export const singleGeoJsonFeatureCollection: GeoJSONFeatureCollection = {
export const singleGeoJsonFeatureCollection2D: GeoJSON2DFeatureCollection = {
type: "FeatureCollection",
features: [
{
Expand All @@ -14,7 +14,21 @@ export const singleGeoJsonFeatureCollection: GeoJSONFeatureCollection = {
],
};

export const multiGeoJsonFeatureCollection: GeoJSONFeatureCollection = {
export const singleGeoJsonFeatureCollection3D: GeoJSON3DFeatureCollection = {
type: "FeatureCollection",
features: [
{
type: "Feature",
properties: {},
geometry: {
type: "Point",
coordinates: [0.0, 0.0, 1.0],
},
},
],
};

export const multiGeoJsonFeatureCollection2D: GeoJSON2DFeatureCollection = {
type: "FeatureCollection",
features: [
{
Expand All @@ -39,7 +53,7 @@ export const multiGeoJsonFeatureCollection: GeoJSONFeatureCollection = {
],
};

export const multiGeoJsonFeatureCollectionWithBbox: GeoJSONFeatureCollection = {
...multiGeoJsonFeatureCollection,
export const multiGeoJsonFeatureCollectionWithBbox2D: GeoJSONFeatureCollection = {
...multiGeoJsonFeatureCollection2D,
bbox: [0.0, 0.0, 10.0, 10.0],
};
24 changes: 7 additions & 17 deletions examples/geometry/geometry_collection.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,37 @@
import { GeoJSONGeometryCollection } from "../../src";
import { GeoJSON2DGeometryCollection, GeoJSON3DGeometryCollection } from "../../src";
import { geoJsonLineString3D } from "./line_string";
import { multiGeoJsonMultiLineString2D } from "./multi_line_string";
import { geoJsonMultiPoint2D } from "./multi_point";
import { singleGeoJsonMultiPolygon3D } from "./multi_polygon";
import { geoJsonPoint2D, geoJsonPoint2DWithBbox, geoJsonPoint3D } from "./point";
import { geoJsonPolygon2D } from "./polygon";

export const singleGeoJsonGeometryCollection2D: GeoJSONGeometryCollection = {
export const singleGeoJsonGeometryCollection2D: GeoJSON2DGeometryCollection = {
type: "GeometryCollection",
geometries: [geoJsonPoint2D],
};

export const multiGeoJsonGeometryCollection2D: GeoJSONGeometryCollection = {
export const multiGeoJsonGeometryCollection2D: GeoJSON2DGeometryCollection = {
type: "GeometryCollection",
geometries: [geoJsonPoint2D, geoJsonMultiPoint2D, geoJsonPolygon2D, multiGeoJsonMultiLineString2D],
};

export const multiGeoJsonGeometryCollection3D: GeoJSONGeometryCollection = {
export const multiGeoJsonGeometryCollection3D: GeoJSON3DGeometryCollection = {
type: "GeometryCollection",
geometries: [geoJsonPoint3D, geoJsonLineString3D, singleGeoJsonMultiPolygon3D],
};

export const nestedGeoJsonGeometryCollection: GeoJSONGeometryCollection = {
type: "GeometryCollection",
geometries: [geoJsonPoint2D, multiGeoJsonGeometryCollection2D],
};

export const singleGeoJsonGeometryCollection2DWithBbox: GeoJSONGeometryCollection = {
export const singleGeoJsonGeometryCollection2DWithBbox: GeoJSON2DGeometryCollection = {
...singleGeoJsonGeometryCollection2D,
bbox: geoJsonPoint2DWithBbox.bbox,
};

export const multiGeoJsonGeometryCollection2DWithBbox: GeoJSONGeometryCollection = {
export const multiGeoJsonGeometryCollection2DWithBbox: GeoJSON2DGeometryCollection = {
...multiGeoJsonGeometryCollection2D,
bbox: [-3.0, -2.0, 30.0, 30.0],
};

export const multiGeoJsonGeometryCollection3DWithBbox: GeoJSONGeometryCollection = {
export const multiGeoJsonGeometryCollection3DWithBbox: GeoJSON3DGeometryCollection = {
...multiGeoJsonGeometryCollection3D,
bbox: [0.0, 0.0, 0.0, 20.0, 10.0, 10.0],
};

export const nestedGeoJsonGeometryCollectionWithBbox: GeoJSONGeometryCollection = {
...nestedGeoJsonGeometryCollection,
bbox: [-3.0, -2.0, 30.0, 30.0],
};
10 changes: 5 additions & 5 deletions examples/geometry/line_string.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { GeoJSONLineString } from "../../src";
import { GeoJSON2DLineString, GeoJSON3DLineString } from "../../src";

export const geoJsonLineString2D: GeoJSONLineString = {
export const geoJsonLineString2D: GeoJSON2DLineString = {
type: "LineString",
coordinates: [
[1.0, 2.0],
[3.0, 4.0],
],
};

export const geoJsonLineString3D: GeoJSONLineString = {
export const geoJsonLineString3D: GeoJSON3DLineString = {
type: "LineString",
coordinates: [
[0.0, 0.0, 0.0],
Expand All @@ -17,12 +17,12 @@ export const geoJsonLineString3D: GeoJSONLineString = {
],
};

export const geoJsonLineString2DWithBbox: GeoJSONLineString = {
export const geoJsonLineString2DWithBbox: GeoJSON2DLineString = {
...geoJsonLineString2D,
bbox: [1.0, 2.0, 3.0, 4.0],
};

export const geoJsonLineString3DWithBbox: GeoJSONLineString = {
export const geoJsonLineString3DWithBbox: GeoJSON3DLineString = {
...geoJsonLineString3D,
bbox: [0.0, 0.0, 0.0, 20.0, 10.0, 2.0],
};
14 changes: 7 additions & 7 deletions examples/geometry/multi_line_string.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { GeoJSONMultiLineString } from "../../src";
import { GeoJSON2DMultiLineString, GeoJSON3DMultiLineString } from "../../src";
import {
geoJsonLineString2D,
geoJsonLineString2DWithBbox,
geoJsonLineString3D,
geoJsonLineString3DWithBbox,
} from "./line_string";

export const singleGeoJsonMultiLineString2D: GeoJSONMultiLineString = {
export const singleGeoJsonMultiLineString2D: GeoJSON2DMultiLineString = {
type: "MultiLineString",
coordinates: [geoJsonLineString2D.coordinates],
};
export const multiGeoJsonMultiLineString2D: GeoJSONMultiLineString = {
export const multiGeoJsonMultiLineString2D: GeoJSON2DMultiLineString = {
type: "MultiLineString",
coordinates: [
geoJsonLineString2D.coordinates,
Expand All @@ -20,22 +20,22 @@ export const multiGeoJsonMultiLineString2D: GeoJSONMultiLineString = {
],
],
};
export const singleGeoJsonMultiLineString3D: GeoJSONMultiLineString = {
export const singleGeoJsonMultiLineString3D: GeoJSON3DMultiLineString = {
type: "MultiLineString",
coordinates: [geoJsonLineString3D.coordinates],
};

export const singleGeoJsonMultiLineString2DWithBbox: GeoJSONMultiLineString = {
export const singleGeoJsonMultiLineString2DWithBbox: GeoJSON2DMultiLineString = {
...singleGeoJsonMultiLineString2D,
bbox: geoJsonLineString2DWithBbox.bbox,
};

export const multiGeoJsonMultiLineString2DWithBbox: GeoJSONMultiLineString = {
export const multiGeoJsonMultiLineString2DWithBbox: GeoJSON2DMultiLineString = {
...multiGeoJsonMultiLineString2D,
bbox: [1.0, 2.0, 30.0, 30.0],
};

export const singleGeoJsonMultiLineString3DWithBbox: GeoJSONMultiLineString = {
export const singleGeoJsonMultiLineString3DWithBbox: GeoJSON3DMultiLineString = {
...singleGeoJsonMultiLineString3D,
bbox: geoJsonLineString3DWithBbox.bbox,
};
10 changes: 5 additions & 5 deletions examples/geometry/multi_point.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { GeoJSONMultiPoint } from "../../src";
import { GeoJSON2DMultiPoint, GeoJSON3DMultiPoint } from "../../src";

export const geoJsonMultiPoint2D: GeoJSONMultiPoint = {
export const geoJsonMultiPoint2D: GeoJSON2DMultiPoint = {
type: "MultiPoint",
coordinates: [
[0.0, 0.0],
Expand All @@ -9,7 +9,7 @@ export const geoJsonMultiPoint2D: GeoJSONMultiPoint = {
],
};

export const geoJsonMultiPoint3D: GeoJSONMultiPoint = {
export const geoJsonMultiPoint3D: GeoJSON3DMultiPoint = {
type: "MultiPoint",
coordinates: [
[0.0, 0.0, 0.0],
Expand All @@ -18,12 +18,12 @@ export const geoJsonMultiPoint3D: GeoJSONMultiPoint = {
],
};

export const geoJsonMultiPoint2DWithBbox: GeoJSONMultiPoint = {
export const geoJsonMultiPoint2DWithBbox: GeoJSON2DMultiPoint = {
...geoJsonMultiPoint2D,
bbox: [-3.0, -2.0, 8.0, 4.0],
};

export const geoJsonMultiPoint3DWithBbox: GeoJSONMultiPoint = {
export const geoJsonMultiPoint3DWithBbox: GeoJSON3DMultiPoint = {
...geoJsonMultiPoint3D,
bbox: [-3.0, -2.0, 0.0, 8.0, 4.0, 5.0],
};
14 changes: 7 additions & 7 deletions examples/geometry/multi_polygon.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GeoJSONMultiPolygon } from "../../src";
import { GeoJSON2DMultiPolygon, GeoJSON3DMultiPolygon } from "../../src";
import {
geoJsonPolygon2D,
geoJsonPolygon2DWithBbox,
Expand All @@ -8,32 +8,32 @@ import {
geoJsonPolygon3DWithBbox,
} from "./polygon";

export const singleGeoJsonMultiPolygon2D: GeoJSONMultiPolygon = {
export const singleGeoJsonMultiPolygon2D: GeoJSON2DMultiPolygon = {
type: "MultiPolygon",
coordinates: [geoJsonPolygon2D.coordinates],
};

export const multiGeoJsonMultiPolygon2D: GeoJSONMultiPolygon = {
export const multiGeoJsonMultiPolygon2D: GeoJSON2DMultiPolygon = {
type: "MultiPolygon",
coordinates: [geoJsonPolygon2D.coordinates, geoJsonPolygon2DWithHole.coordinates],
};

export const singleGeoJsonMultiPolygon3D: GeoJSONMultiPolygon = {
export const singleGeoJsonMultiPolygon3D: GeoJSON3DMultiPolygon = {
type: "MultiPolygon",
coordinates: [geoJsonPolygon3D.coordinates],
};

export const singleGeoJsonMultiPolygon2DWithBbox: GeoJSONMultiPolygon = {
export const singleGeoJsonMultiPolygon2DWithBbox: GeoJSON2DMultiPolygon = {
...singleGeoJsonMultiPolygon2D,
bbox: geoJsonPolygon2DWithBbox.bbox,
};

export const multiGeoJsonMultiPolygon2DWithBbox: GeoJSONMultiPolygon = {
export const multiGeoJsonMultiPolygon2DWithBbox: GeoJSON2DMultiPolygon = {
...multiGeoJsonMultiPolygon2D,
bbox: geoJsonPolygon2DWithHoleAndBbox.bbox,
};

export const singleGeoJsonMultiPolygon3DWithBbox: GeoJSONMultiPolygon = {
export const singleGeoJsonMultiPolygon3DWithBbox: GeoJSON3DMultiPolygon = {
...singleGeoJsonMultiPolygon3D,
bbox: geoJsonPolygon3DWithBbox.bbox,
};
10 changes: 5 additions & 5 deletions examples/geometry/point.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { GeoJSONPoint } from "../../src";
import { GeoJSON2DPoint, GeoJSON3DPoint } from "../../src";

export const geoJsonPoint2D: GeoJSONPoint = {
export const geoJsonPoint2D: GeoJSON2DPoint = {
type: "Point",
coordinates: [1.0, 2.0],
};

export const geoJsonPoint3D: GeoJSONPoint = {
export const geoJsonPoint3D: GeoJSON3DPoint = {
type: "Point",
coordinates: [1.0, 2.0, 10.0],
};

export const geoJsonPoint2DWithBbox: GeoJSONPoint = {
export const geoJsonPoint2DWithBbox: GeoJSON2DPoint = {
...geoJsonPoint2D,
bbox: [1.0, 2.0, 1.0, 2.0],
};

export const geoJsonPoint3DWithBbox: GeoJSONPoint = {
export const geoJsonPoint3DWithBbox: GeoJSON3DPoint = {
...geoJsonPoint3D,
bbox: [1.0, 2.0, 10.0, 1.0, 2.0, 10.0],
};
Loading

0 comments on commit 2b63150

Please sign in to comment.