Improve docs and types

This commit is contained in:
pietervdvn 2022-09-21 02:09:41 +02:00
parent 02278af985
commit bdb5833a1c

View file

@ -8,11 +8,12 @@ import {
booleanWithin, booleanWithin,
Coord, Coord,
Feature, Feature,
Geometry, Geometry, Lines,
MultiPolygon, MultiPolygon,
Polygon, Polygon,
Properties, Properties,
} from "@turf/turf" } from "@turf/turf"
import {GeoJSON, LineString} from "geojson";
export class GeoOperations { export class GeoOperations {
private static readonly _earthRadius = 6378137 private static readonly _earthRadius = 6378137
@ -270,7 +271,8 @@ export class GeoOperations {
} }
/** /**
* Generates the closest point on a way from a given point * Generates the closest point on a way from a given point.
* If the passed-in geojson object is a polygon, the outer ring will be used as linestring
* *
* The properties object will contain three values: * The properties object will contain three values:
// - `index`: closest point was found on nth line part, // - `index`: closest point was found on nth line part,
@ -279,15 +281,15 @@ export class GeoOperations {
* @param way The road on which you want to find a point * @param way The road on which you want to find a point
* @param point Point defined as [lon, lat] * @param point Point defined as [lon, lat]
*/ */
public static nearestPoint(way, point: [number, number]) { public static nearestPoint(way: Feature<LineString | Polygon>, point: [number, number]) {
if (way.geometry.type === "Polygon") { if (way.geometry.type === "Polygon") {
way = { ...way } way = { ...way }
way.geometry = { ...way.geometry } way.geometry = { ...way.geometry }
way.geometry.type = "LineString" way.geometry.type = "LineString"
way.geometry.coordinates = way.geometry.coordinates[0] way.geometry.coordinates = (<Polygon> way.geometry).coordinates[0]
} }
return turf.nearestPointOnLine(way, point, { units: "kilometers" }) return turf.nearestPointOnLine(<Feature<LineString>> way, point, { units: "kilometers" })
} }
public static toCSV(features: any[]): string { public static toCSV(features: any[]): string {