-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add the addFeatures method to allow adding of external data
- Loading branch information
1 parent
157c1b3
commit b6e0043
Showing
31 changed files
with
1,314 additions
and
306 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { | ||
validLatitude, | ||
validLongitude, | ||
coordinateIsValid, | ||
getDecimalPlaces, | ||
} from "./is-valid-coordinate"; | ||
|
||
describe("validLatitude", () => { | ||
it("should return true for valid latitude", () => { | ||
expect(validLatitude(45)).toBe(true); | ||
}); | ||
|
||
it("should return false for latitude greater than 90", () => { | ||
expect(validLatitude(91)).toBe(false); | ||
}); | ||
|
||
it("should return false for latitude less than -90", () => { | ||
expect(validLatitude(-91)).toBe(false); | ||
}); | ||
|
||
it("should return true for boundary values -90 and 90", () => { | ||
expect(validLatitude(-90)).toBe(true); | ||
expect(validLatitude(90)).toBe(true); | ||
}); | ||
|
||
it("should return true for valid longitude", () => { | ||
expect(validLongitude(90)).toBe(true); | ||
}); | ||
|
||
it("should return false for longitude greater than 180", () => { | ||
expect(validLongitude(181)).toBe(false); | ||
}); | ||
|
||
it("should return false for longitude less than -180", () => { | ||
expect(validLongitude(-181)).toBe(false); | ||
}); | ||
|
||
it("should return true for boundary values -180 and 180", () => { | ||
expect(validLongitude(-180)).toBe(true); | ||
expect(validLongitude(180)).toBe(true); | ||
}); | ||
}); | ||
|
||
describe("coordinateIsValid", () => { | ||
it("should return true for valid coordinate", () => { | ||
expect(coordinateIsValid([45, 90], 9)).toBe(true); | ||
}); | ||
|
||
it("should return false for coordinate with incorrect length", () => { | ||
expect(coordinateIsValid([45], 9)).toBe(false); | ||
expect(coordinateIsValid([45, 90, 100], 9)).toBe(false); | ||
}); | ||
|
||
it("should return false for coordinate with non-number elements", () => { | ||
expect(coordinateIsValid(["45", 90], 9)).toBe(false); | ||
expect(coordinateIsValid([45, "90"], 9)).toBe(false); | ||
expect(coordinateIsValid(["45", "90"], 9)).toBe(false); | ||
}); | ||
|
||
it("should return false for coordinate with invalid longitude and latitude", () => { | ||
expect(coordinateIsValid([181, 90], 9)).toBe(false); | ||
expect(coordinateIsValid([45, 91], 9)).toBe(false); | ||
expect(coordinateIsValid([-181, -91], 9)).toBe(false); | ||
}); | ||
}); | ||
|
||
describe("getDecimalPlaces", () => { | ||
it("returns 0 for an integer", () => { | ||
expect(getDecimalPlaces(10)).toBe(0); | ||
expect(getDecimalPlaces(0)).toBe(0); | ||
}); | ||
|
||
it("returns the correct number of decimal places for a float", () => { | ||
expect(getDecimalPlaces(0.1)).toBe(1); | ||
expect(getDecimalPlaces(0.01)).toBe(2); | ||
expect(getDecimalPlaces(0.123456)).toBe(6); | ||
}); | ||
|
||
it("returns the correct number of decimal places for a float greater than 1", () => { | ||
expect(getDecimalPlaces(1.23)).toBe(2); | ||
}); | ||
|
||
it("returns the correct number of decimal places for a float less than 0", () => { | ||
expect(getDecimalPlaces(-0.123)).toBe(3); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
export function validLatitude(lat: number) { | ||
return lat >= -90 && lat <= 90; | ||
} | ||
|
||
export function validLongitude(lng: number) { | ||
return lng >= -180 && lng <= 180; | ||
} | ||
|
||
export function coordinateIsValid( | ||
coordinate: unknown[], | ||
coordinatePrecision: number | ||
) { | ||
return ( | ||
coordinate.length === 2 && | ||
typeof coordinate[0] === "number" && | ||
typeof coordinate[1] === "number" && | ||
coordinate[0] !== Infinity && | ||
coordinate[1] !== Infinity && | ||
validLongitude(coordinate[0]) && | ||
validLatitude(coordinate[1]) && | ||
getDecimalPlaces(coordinate[0]) <= coordinatePrecision && | ||
getDecimalPlaces(coordinate[1]) <= coordinatePrecision | ||
); | ||
} | ||
|
||
export function getDecimalPlaces(value: number): number { | ||
let current = 1; | ||
let precision = 0; | ||
while (Math.round(value * current) / current !== value) { | ||
current *= 10; | ||
precision++; | ||
} | ||
|
||
return precision; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { Feature, LineString } from "geojson"; | ||
import { isValidLineStringFeature } from "./is-valid-linestring-feature"; | ||
|
||
describe("isValidLineStringFeature", () => { | ||
it("returns true for a valid LineString feature with correct coordinate precision", () => { | ||
const validFeature = { | ||
type: "Feature", | ||
properties: {}, | ||
geometry: { | ||
type: "LineString", | ||
coordinates: [ | ||
[45, 80], | ||
[46, 81], | ||
], | ||
}, | ||
} as Feature<LineString, Record<string, any>>; | ||
expect(isValidLineStringFeature(validFeature, 9)).toBe(true); | ||
}); | ||
|
||
it("returns false for a non-LineString feature", () => { | ||
const nonLineStringFeature = { | ||
type: "Feature", | ||
properties: {}, | ||
geometry: { | ||
type: "Polygon", | ||
coordinates: [ | ||
[45, 80], | ||
[46, 81], | ||
[45, 80], | ||
], | ||
}, | ||
} as any; | ||
expect(isValidLineStringFeature(nonLineStringFeature, 9)).toBe(false); | ||
}); | ||
|
||
it("returns false for a LineString feature with less than 2 coordinates", () => { | ||
const lessCoordinatesFeature = { | ||
type: "Feature", | ||
properties: {}, | ||
geometry: { | ||
type: "LineString", | ||
coordinates: [[45, 90]], | ||
}, | ||
} as Feature<LineString, Record<string, any>>; | ||
expect(isValidLineStringFeature(lessCoordinatesFeature, 9)).toBe(false); | ||
}); | ||
|
||
it("returns false for a LineString feature with incorrect coordinate precision", () => { | ||
const validFeature = { | ||
type: "Feature", | ||
properties: {}, | ||
geometry: { | ||
type: "LineString", | ||
coordinates: [ | ||
[45.123, 80.123], | ||
[46.123, 81.123], | ||
], | ||
}, | ||
} as Feature<LineString, Record<string, any>>; | ||
expect(isValidLineStringFeature(validFeature, 2)).toBe(false); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { GeoJSONStoreFeatures } from "../../terra-draw"; | ||
import { coordinateIsValid } from "./is-valid-coordinate"; | ||
|
||
export function isValidLineStringFeature( | ||
feature: GeoJSONStoreFeatures, | ||
coordinatePrecision: number | ||
): boolean { | ||
return ( | ||
feature.geometry.type === "LineString" && | ||
feature.geometry.coordinates.length >= 2 && | ||
feature.geometry.coordinates.every((coordinate) => | ||
coordinateIsValid(coordinate, coordinatePrecision) | ||
) | ||
); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { Feature, Point } from "geojson"; | ||
import { isValidPoint } from "./is-valid-point"; | ||
|
||
describe("isValidPoint", () => { | ||
it("returns true for a valid Point with correct coordinate precision", () => { | ||
const validPoint = { | ||
type: "Feature", | ||
properties: {}, | ||
geometry: { | ||
type: "Point", | ||
coordinates: [45, 90], | ||
}, | ||
} as Feature<Point, Record<string, any>>; | ||
expect(isValidPoint(validPoint, 2)).toBe(true); | ||
}); | ||
|
||
it("returns false for a non-Point feature", () => { | ||
const nonPointFeature = { | ||
type: "Feature", | ||
properties: {}, | ||
geometry: { | ||
type: "LineString", | ||
coordinates: [ | ||
[45, 90], | ||
[46, 91], | ||
], | ||
}, | ||
} as any; | ||
expect(isValidPoint(nonPointFeature, 2)).toBe(false); | ||
}); | ||
|
||
it("returns false for a Point with incorrect coordinate precision", () => { | ||
const invalidPoint = { | ||
type: "Feature", | ||
properties: {}, | ||
geometry: { | ||
type: "Point", | ||
coordinates: [45.123, 90.123], | ||
}, | ||
} as Feature<Point, Record<string, any>>; | ||
expect(isValidPoint(invalidPoint, 2)).toBe(false); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { GeoJSONStoreFeatures } from "../../terra-draw"; | ||
import { coordinateIsValid } from "./is-valid-coordinate"; | ||
|
||
export function isValidPoint( | ||
feature: GeoJSONStoreFeatures, | ||
coordinatePrecision: number | ||
): boolean { | ||
return ( | ||
feature.geometry.type === "Point" && | ||
coordinateIsValid(feature.geometry.coordinates, coordinatePrecision) | ||
); | ||
} |
Oops, something went wrong.