-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
127 lines (113 loc) · 4.9 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import CheapRuler from 'cheap-ruler';
import {Feature, FeatureCollection, GeoJsonProperties, Point, Polygon} from 'geojson';
import {getGeohashAsBBox, getGeohashAsLatLon} from './helpers/geohash';
import {polygon, featureCollection, point} from './helpers/geojson';
import {createCheapRuler, createCircleFromGeohash, getDistance} from './helpers/utils';
/**
* Converts geohash to polygon Feature
*
* @export
* @param {string} geohash Geohash to convert to Polygon Feature
* @param {GeoJsonProperties} [properties={}] Properties to embed to the resulting feature
* @returns {Feature<Polygon>}
*/
export function geohashToPolygonFeature(geohash: string, properties: GeoJsonProperties = {}): Feature<Polygon> {
const [minLon, minLat, maxLon, maxLat] = getGeohashAsBBox(geohash);
const sw = [minLon, minLat];
const se = [maxLon, minLat];
const nw = [minLon, maxLat];
const ne = [maxLon, maxLat];
const coordinates = [[sw, se, ne, nw, sw]];
return polygon(coordinates, properties);
}
/**
* Converts geohash to polygon Geometry
*
* @export
* @param {string} geohash Geohash to get as Polygon Geometry
* @returns {Polygon} The Polygon Geometry representing the geohash
*/
export function geohashToPolygonGeometry(geohash: string): Polygon {
return geohashToPolygonFeature(geohash).geometry;
}
/**
* Converts geohash to point feature, for centroid coordinates
*
* @export
* @param {string} geohash Geohash to convert to Point Feature
* @param {GeoJsonProperties} [properties={}] Properties to embed to the resulting feature
* @returns {Point} The geohash centroid as a Point Feature
*/
export function geohashToPointFeature(geohash: string, properties: GeoJsonProperties = {}): Feature<Point> {
const {latitude, longitude} = getGeohashAsLatLon(geohash);
return point([longitude, latitude], {
geohash,
...properties,
});
}
export type CircleOptions = {
ruler?: CheapRuler;
steps?: number;
};
/**
* Converts geohash to a circle Feature, based on % of the size desired
*
* @export
* @param {string} geohash Geohash to convert to circle
* @param {number} percentage Percentage of the geohash area to cover with the circle
* @param {GeoJsonProperties} [properties={}] Properties to embed to the resulting feature
* @param {CircleOptions} [options] Options for circle rendering
* @param {CheapRuler} [options.ruler] A CheapRuler instance to make computing distances faster
* @param {number} [options.steps=32] Number of steps for converting circle to polygon
* @return {Feature<Polygon>} The geohash as a circle Polygon Feature
*/
export function geohashToCircleFeature(geohash: string, percentage: number, properties: GeoJsonProperties = {}, options?: CircleOptions): Feature<Polygon> {
const [minLon, minLat, maxLon, maxLat] = getGeohashAsBBox(geohash);
const sw = [minLon, minLat];
const se = [maxLon, minLat];
const nw = [minLon, maxLat];
let rulerToUse = options?.ruler;
if (!rulerToUse) {
rulerToUse = createCheapRuler(minLat);
}
const minDistance = Math.min(getDistance(sw, se, rulerToUse), getDistance(sw, nw, rulerToUse));
const half = 2;
const radiusDistance = minDistance / half;
const total = 100;
const radiusCalc = radiusDistance * (percentage / total);
return createCircleFromGeohash(geohash, radiusCalc, properties, options?.steps);
}
/**
* Converts geohash to a circle Polygon Geometry, based on % of the size desired
*
* @export
* @param {string} geohash Geohash to convert to circle
* @param {number} percentage Percentage of the geohash area to cover with the circle
* @param {CircleOptions} [options] Options for circle rendering
* @param {CheapRuler} [options.ruler] A CheapRuler instance to make computing distances faster
* @param {number} [options.steps=32] Number of steps for converting circle to polygon
* @return {Feature<Polygon>} The geohash as a circle Polygon Geometry
*/
export function geohashToCircleGeometry(geohash: string, percentage: number, options?: CircleOptions): Polygon {
return geohashToCircleFeature(geohash, percentage, options).geometry;
}
/**
* Helper function to wrap geohash features converted using geohashToPolygonFeature in a FeatureCollection
*
* @export
* @param {Feature[]} featuresArray Features array to wrap inside the FeatureCollection
* @returns {FeatureCollection} FeatureCollection wrapping the Features
*/
export function wrapAsFeatureCollection(featuresArray: Feature[]): FeatureCollection {
return featureCollection(featuresArray);
}
/**
* Converts array of geohashes to GeoJSON FeatureCollection
*
* @export
* @param {string[]} hashes Geohashes to wrap into FeatureCollection
* @returns {FeatureCollection} FeatureCollection with each geohash as a Polygon Feature inside
*/
export function geohashesToFeatureCollection(hashes: string[]): FeatureCollection {
return wrapAsFeatureCollection(hashes.map((hash) => geohashToPolygonFeature(hash)));
}