@comapeo/geometry
Advanced tools
Comparing version 1.0.2 to 1.1.0
@@ -8,2 +8,6 @@ import { Geometry as GeometryProto, GeometryType } from './proto/geometry.js'; | ||
return GeometryProto.encode(encodePoint(message), writer); | ||
case 'LineString': | ||
return GeometryProto.encode(encodeLineString(message), writer); | ||
case 'MultiLineString': | ||
return GeometryProto.encode(encodeMultiLineString(message), writer); | ||
case 'MultiPoint': | ||
@@ -27,2 +31,6 @@ return GeometryProto.encode(encodeMultiPoint(message), writer); | ||
return decodePoint(geometryProto); | ||
case GeometryType.LINE_STRING: | ||
return decodeLineString(geometryProto); | ||
case GeometryType.MULTI_LINE_STRING: | ||
return decodeMultiLineString(geometryProto); | ||
case GeometryType.MULTI_POINT: | ||
@@ -52,2 +60,47 @@ return decodeMultiPoint(geometryProto); | ||
} | ||
function decodeLineString({ coordinates: rawCoords, }) { | ||
return { | ||
type: 'LineString', | ||
coordinates: readPositionArray(rawCoords, { | ||
start: 0, | ||
length: rawCoords.length / 2, | ||
}), | ||
}; | ||
} | ||
function encodeLineString({ coordinates }) { | ||
return { | ||
lengths: [], | ||
type: GeometryType.LINE_STRING, | ||
coordinates: coordinates.flat(), | ||
}; | ||
} | ||
function decodeMultiLineString({ coordinates: rawCoords, lengths, }) { | ||
let coordinates; | ||
if (lengths.length === 0) { | ||
coordinates = [ | ||
readPositionArray(rawCoords, { start: 0, length: rawCoords.length / 2 }), | ||
]; | ||
} | ||
else { | ||
let start = 0; | ||
coordinates = lengths.map((length) => { | ||
const line = readPositionArray(rawCoords, { start, length }); | ||
start += length * 2; | ||
return line; | ||
}); | ||
} | ||
return { | ||
type: 'MultiLineString', | ||
coordinates, | ||
}; | ||
} | ||
function encodeMultiLineString({ coordinates, }) { | ||
return { | ||
lengths: | ||
// For simple (most common?) case, omit lengths | ||
coordinates.length === 1 ? [] : coordinates.map((line) => line.length), | ||
type: GeometryType.MULTI_LINE_STRING, | ||
coordinates: coordinates.flat(3), | ||
}; | ||
} | ||
function decodeMultiPoint({ coordinates: rawCoords, }) { | ||
@@ -144,3 +197,18 @@ return { | ||
} | ||
for (let i = 0; i < rawCoords.length; i++) { | ||
if (i % 2 === 0) { | ||
validateLongitude(rawCoords[i]); | ||
} | ||
else { | ||
validateLatitude(rawCoords[i]); | ||
} | ||
} | ||
} | ||
const rangeValidator = (max) => (value) => { | ||
if (Math.abs(value) > max) { | ||
throw new Error(`Coordinate value must be between -${max} and ${max}`); | ||
} | ||
}; | ||
const validateLatitude = rangeValidator(90); | ||
const validateLongitude = rangeValidator(180); | ||
function validateLinearRing(ring) { | ||
@@ -147,0 +215,0 @@ if (ring.length < 4) { |
@@ -11,3 +11,3 @@ /* eslint-disable */ | ||
*/ | ||
export type Geometry = Point | Polygon | MultiPoint | MultiPolygon; | ||
export type Geometry = Point | LineString | MultiLineString | Polygon | MultiPoint | MultiPolygon; | ||
/** | ||
@@ -27,2 +27,10 @@ * @minItems 2 | ||
} | ||
export interface LineString { | ||
type: "LineString"; | ||
coordinates: Position[]; | ||
} | ||
export interface MultiLineString { | ||
type: "MultiLineString"; | ||
coordinates: Position[][]; | ||
} | ||
export interface Polygon { | ||
@@ -29,0 +37,0 @@ type: "Polygon"; |
@@ -13,5 +13,16 @@ { | ||
"maxItems": 2, | ||
"items": { | ||
"type": "number" | ||
} | ||
"items": [ | ||
{ | ||
"type": "number", | ||
"description": "longitude", | ||
"minimum": -180, | ||
"maximum": 180 | ||
}, | ||
{ | ||
"type": "number", | ||
"description": "latitude", | ||
"minimum": -90, | ||
"maximum": 90 | ||
} | ||
] | ||
}, | ||
@@ -44,2 +55,43 @@ "linearRing": { | ||
{ | ||
"title": "LineString", | ||
"type": "object", | ||
"required": ["type", "coordinates"], | ||
"properties": { | ||
"type": { | ||
"type": "string", | ||
"const": "LineString" | ||
}, | ||
"coordinates": { | ||
"type": "array", | ||
"items": { | ||
"$ref": "#/definitions/position" | ||
}, | ||
"minLength": 2 | ||
} | ||
}, | ||
"additionalProperties": false | ||
}, | ||
{ | ||
"title": "MultiLineString", | ||
"type": "object", | ||
"required": ["type", "coordinates"], | ||
"properties": { | ||
"type": { | ||
"type": "string", | ||
"const": "MultiLineString" | ||
}, | ||
"coordinates": { | ||
"type": "array", | ||
"items": { | ||
"type": "array", | ||
"items": { | ||
"$ref": "#/definitions/position" | ||
}, | ||
"minLength": 2 | ||
} | ||
} | ||
}, | ||
"additionalProperties": false | ||
}, | ||
{ | ||
"title": "Polygon", | ||
@@ -46,0 +98,0 @@ "type": "object", |
{ | ||
"name": "@comapeo/geometry", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "GeoJSON Geometry encoding and decoding to protobuf", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -19,3 +19,3 @@ # CoMapeo Geometry Encoding | ||
- Only `Point`, `Polygon`, `MultiPoint` and `MultiPolygon` are supported. | ||
- Only `Point`, `LineString`, `MultiLineString`, `Polygon`, `MultiPoint` and `MultiPolygon` are supported. | ||
- Only 2D positions (coordinates) are supported. | ||
@@ -22,0 +22,0 @@ - No support for `bbox`. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22697
611