-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from reilem/fix-position-and-bbox
Fix position and bbox
- Loading branch information
Showing
47 changed files
with
719 additions
and
392 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,5 @@ | ||
import { GeoJSONBbox } from "../src"; | ||
import { GeoJSON2DBbox, GeoJSON3DBbox } from "../src/bbox"; | ||
|
||
export const bbox2D: GeoJSONBbox = [0.0, 0.0, 1.0, 1.0]; | ||
export const bbox2D: GeoJSON2DBbox = [0.0, 0.0, 1.0, 1.0]; | ||
|
||
export const bbox3D: GeoJSONBbox = [0.0, 0.0, 1.0, 1.0, 2.0, 2.0]; | ||
|
||
export const bbox4D: GeoJSONBbox = [0.0, 0.0, 1.0, 3.0, 1.0, 2.0, 2.0, 4.0]; | ||
export const bbox3D: GeoJSON3DBbox = [0.0, 0.0, 1.0, 1.0, 2.0, 2.0]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,26 @@ | ||
import { z } from "zod"; | ||
import { GeoJSONBbox, GeoJSONBboxSchema, GeoJSONBboxSchemaType } from "./bbox"; | ||
import { GeoJSONBboxGenericSchema, GeoJSONBboxSchemaType, GeoJSONBboxGeneric } from "./bbox"; | ||
import { GeoJSONPosition } from "./geometry/position"; | ||
|
||
export type GeoJSONBase = { | ||
bbox?: GeoJSONBbox; | ||
export type GeoJSONBase<P extends GeoJSONPosition> = { | ||
bbox?: GeoJSONBboxGeneric<P>; | ||
}; | ||
|
||
export type GeoJSONBaseSchemaInnerType = { | ||
bbox: z.ZodOptional<GeoJSONBboxSchemaType>; | ||
export type GeoJSONBaseSchemaInnerType<P extends GeoJSONPosition> = { | ||
bbox: z.ZodOptional<GeoJSONBboxSchemaType<P>>; | ||
}; | ||
|
||
export type GeoJSONBaseSchemaType = z.ZodObject< | ||
GeoJSONBaseSchemaInnerType, | ||
export type GeoJSONBaseSchemaType<P extends GeoJSONPosition> = z.ZodObject< | ||
GeoJSONBaseSchemaInnerType<P>, | ||
"strip", | ||
z.ZodTypeAny, | ||
GeoJSONBase, | ||
GeoJSONBase | ||
GeoJSONBase<P>, | ||
GeoJSONBase<P> | ||
>; | ||
|
||
export const GeoJSONBaseSchema: GeoJSONBaseSchemaType = z.object({ | ||
bbox: GeoJSONBboxSchema.optional(), | ||
}); | ||
export const GeoJSONBaseSchema = <P extends GeoJSONPosition>( | ||
positionSchema: z.ZodSchema<P>, | ||
): GeoJSONBaseSchemaType<P> => | ||
z.object({ | ||
bbox: GeoJSONBboxGenericSchema(positionSchema).optional(), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,45 @@ | ||
import { z } from "zod"; | ||
import { z, ZodSchema } from "zod"; | ||
import { | ||
GeoJSON2DPosition, | ||
GeoJSON2DPositionSchema, | ||
GeoJSON3DPosition, | ||
GeoJSON3DPositionSchema, | ||
GeoJSONPosition, | ||
GeoJSONPositionSchema, | ||
} from "./geometry/position"; | ||
|
||
export type GeoJSONBbox = [number, number, number, number, ...number[]]; | ||
export type GeoJSONBboxGeneric<P extends GeoJSONPosition> = P extends GeoJSON3DPosition | ||
? [number, number, number, number, number, number] | ||
: P extends GeoJSON2DPosition | ||
? [number, number, number, number] | ||
: [number, number, number, number] | [number, number, number, number, number, number]; | ||
|
||
export type GeoJSONBboxSchemaInnerType = z.ZodTuple<[z.ZodNumber, z.ZodNumber, z.ZodNumber, z.ZodNumber], z.ZodNumber>; | ||
export type GeoJSONBboxSchemaType<P extends GeoJSONPosition> = ZodSchema<GeoJSONBboxGeneric<P>>; | ||
|
||
export type GeoJSONBboxSchemaType = z.ZodEffects<GeoJSONBboxSchemaInnerType, GeoJSONBbox, GeoJSONBbox>; | ||
const _2DBboxSchema = z.tuple([z.number(), z.number(), z.number(), z.number()]); | ||
const _3DBboxSchema = z.tuple([z.number(), z.number(), z.number(), z.number(), z.number(), z.number()]); | ||
|
||
export const GeoJSONBboxSchema: GeoJSONBboxSchemaType = z | ||
.tuple([z.number(), z.number(), z.number(), z.number()]) | ||
.rest(z.number()) | ||
.refine((bbox) => bbox.length % 2 === 0, "Bounding box must have an even number of elements"); | ||
/** | ||
* Because zod cannot do conditional typing we need to do some hacky type casts to make this work | ||
*/ | ||
export const GeoJSONBboxGenericSchema = <P extends GeoJSONPosition>( | ||
positionSchema: z.ZodSchema<P>, | ||
): GeoJSONBboxSchemaType<P> => { | ||
// If the position is not a tuple, we can't infer the dimension, and we return a union of 2D and 3D bbox | ||
if (!(positionSchema instanceof z.ZodTuple)) { | ||
return z.union([_2DBboxSchema, _3DBboxSchema]) as unknown as ZodSchema<GeoJSONBboxGeneric<P>>; | ||
} | ||
if (positionSchema.items.length === 2) { | ||
return _2DBboxSchema as unknown as ZodSchema<GeoJSONBboxGeneric<P>>; | ||
} | ||
return _3DBboxSchema as unknown as ZodSchema<GeoJSONBboxGeneric<P>>; | ||
}; | ||
|
||
export const GeoJSON2DBboxSchema = GeoJSONBboxGenericSchema(GeoJSON2DPositionSchema); | ||
export type GeoJSON2DBbox = z.infer<typeof GeoJSON2DBboxSchema>; | ||
|
||
export const GeoJSON3DBboxSchema = GeoJSONBboxGenericSchema(GeoJSON3DPositionSchema); | ||
export type GeoJSON3DBbox = z.infer<typeof GeoJSON3DBboxSchema>; | ||
|
||
export const GeoJSONBboxSchema = GeoJSONBboxGenericSchema(GeoJSONPositionSchema); | ||
export type GeoJSONBbox = z.infer<typeof GeoJSONBboxSchema>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.