forked from novalabio/react-native-maps-super-cluster
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil.js
More file actions
162 lines (138 loc) · 4.39 KB
/
util.js
File metadata and controls
162 lines (138 loc) · 4.39 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import {Platform} from 'react-native';
import SuperCluster from 'supercluster';
import GeoViewport from '@mapbox/geo-viewport';
export const IS_ANDROID = Platform.OS === 'android';
/**
* Compute clusters and return clustered data.
*
* @param {Object} index Supercluster instance
* @param {Object} region map's region
* @param {Object} size map's size
* @param {Object} config
* @returns {Array} clustered data
*/
export const computeClusters = (index, region, {width, height}, {minZoom}) => {
const bbox = regionToBoundingBox(region);
const size = toGeoViewportFormat(width, height);
const viewport =
region.longitudeDelta >= 40
? {zoom: minZoom}
: GeoViewport.viewport(bbox, size);
return index.getClusters(bbox, viewport.zoom);
};
/**
* Load given dataset into a newly created
* Supercluster instance
*
* @param {Array} dataset data to clusterize
* @param {Object} region map's region
* @param {Object} config various config
* @returns {Object} Supercluster instace
*/
export const createIndex = (
dataset,
{extent, radius, minZoom, maxZoom, width, accessor},
) => {
const index = new SuperCluster({
// eslint-disable-line new-cap
extent,
minZoom,
maxZoom,
radius: radius || width * 0.045, // 4.5% of screen width
});
// get formatted GeoPoints for cluster
const rawData = dataset.map((item) => itemToGeoJSONFeature(item, accessor));
// load geopoints into SuperCluster
index.load(rawData);
return index;
};
/**
* Format width and height for `GeoViewport`
*/
export const toGeoViewportFormat = (width, height) => {
return [width, height];
};
/**
* Compute bounding box for the given region
* @param {Object} region - Google Maps/MapKit region
* @returns {Object} - Region's bounding box as WSEN array
*/
export const regionToBoundingBox = (region) => {
let lngD = region.longitudeDelta;
if (lngD < 0) {
lngD += 360;
}
return [
region.longitude - lngD, // westLng - min lng
region.latitude - region.latitudeDelta, // southLat - min lat
region.longitude + lngD, // eastLng - max lng
region.latitude + region.latitudeDelta, // northLat - max lat
];
};
/**
* Calculate region from the given bounding box.
* Bounding box must be represented as WSEN:
* {
* ws: { longitude: minLon, latitude: minLat }
* en: { longitude: maxLon, latitude: maxLat }
* }
* @param {Object} bbox - Bounding box
* @returns {Object} - Google Maps/MapKit compliant region
*/
export const boundingBoxToRegion = (bbox) => {
const minLon = (bbox.ws.longitude * Math.PI) / 180;
const maxLon = (bbox.en.longitude * Math.PI) / 180;
const minLat = (bbox.ws.latitude * Math.PI) / 180;
const maxLat = (bbox.en.latitude * Math.PI) / 180;
const dLon = maxLon - minLon;
const dLat = maxLat - minLat;
const x = Math.cos(maxLat) * Math.cos(dLon);
const y = Math.cos(maxLat) * Math.sin(dLon);
const latRad = Math.atan2(
Math.sin(minLat) + Math.sin(maxLat),
Math.sqrt((Math.cos(minLat) + x) * (Math.cos(minLat) + x) + y * y),
);
const lonRad = minLon + Math.atan2(y, Math.cos(minLat) + x);
const latitude = (latRad * 180) / Math.PI;
const longitude = (lonRad * 180) / Math.PI;
return {
latitude,
longitude,
latitudeDelta: (dLat * 180) / Math.PI,
longitudeDelta: (dLon * 180) / Math.PI,
};
};
export const getCoordinatesFromItem = (item, accessor, asArray = true) => {
let coordinates = [];
if (typeof accessor === 'string') {
coordinates = [item[accessor].longitude, item[accessor].latitude];
} else if (typeof accessor === 'function') {
coordinates = accessor(item);
}
if (asArray) {
return coordinates;
}
return {
latitude: coordinates[1],
longitude: coordinates[0],
};
};
/**
* Compute a RFC-compliant GeoJSON Feature object
* from the given JS object
* RFC7946: https://tools.ietf.org/html/rfc7946#section-3.2
* @param {Object} item - JS object containing marker data
* @param {Function|String} accessor - accessor for item coordinate values. Could be a string (field name) or a function (that describe how to access to coordinate data).
* @returns {Object} - GeoJSON Feature object
*/
export const itemToGeoJSONFeature = (item, accessor) => {
const coordinates = getCoordinatesFromItem(item, accessor);
return {
type: 'Feature',
geometry: {
coordinates,
type: 'Point',
},
properties: {point_count: 0, item}, // eslint-disable-line camelcase
};
};