Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
createObj: ({ h3Idx }) => {
const obj = new THREE.Mesh();
obj.__hexCenter = h3ToGeo(h3Idx);
obj.__hexGeoJson = h3ToGeoBoundary(h3Idx, true);
// stitch longitudes at the anti-meridian
const centerLng = obj.__hexCenter[1];
obj.__hexGeoJson.forEach(d => {
const edgeLng = d[0];
if (Math.abs(centerLng - edgeLng) > 170) {
// normalize large lng distances
d[0] += (centerLng > edgeLng ? 360 : -360);
}
});
return obj;
},
updateObj: obj => {
export function scalePolygon(hexId, vertices, factor) {
const [lat, lng] = h3ToGeo(hexId);
const actualCount = vertices.length;
// normalize with respect to center
normalizeLongitudes(vertices, lng);
// `h3ToGeoBoundary` returns same array object for first and last vertex (closed polygon),
// if so skip scaling the last vertex
const vertexCount = vertices[0] === vertices[actualCount - 1] ? actualCount - 1 : actualCount;
for (let i = 0; i < vertexCount; i++) {
vertices[i][0] = lerp(lng, vertices[i][0], factor);
vertices[i][1] = lerp(lat, vertices[i][1], factor);
}
}
export function getCentroid({id}) {
// always reverse it to [lng, lat]
return h3ToGeo(id).reverse();
}
function createObj(d) {
const obj = new THREE.Mesh();
obj.__hexCenter = h3ToGeo(d.h3Idx);
obj.__hexGeoJson = h3ToGeoBoundary(d.h3Idx, true);
// stitch longitudes at the anti-meridian
const centerLng = obj.__hexCenter[1];
obj.__hexGeoJson.forEach(d => {
const edgeLng = d[0];
if (Math.abs(centerLng - edgeLng) > 170) {
// normalize large lng distances
d[0] += (centerLng > edgeLng ? 360 : -360);
}
});
obj.__globeObjType = 'hexbin'; // Add object type
return obj;
}
verify: (vertices, hexId) => {
const [lat, lng] = h3ToGeo(hexId);
return vertices.every(vertex => vertex[0] === lng || vertex[1] === lat);
}
},
function getHexagonCentroid(getHexagon, object, objectInfo) {
const hexagonId = getHexagon(object, objectInfo);
const [lat, lng] = h3ToGeo(hexagonId);
return [lng, lat];
}
const {resolution, edgeLengthKM, centerHex} = this.state;
if (resolution < 0) {
return;
}
const hex = geoToH3(viewport.latitude, viewport.longitude, resolution);
if (
centerHex === hex ||
(centerHex && h3Distance(centerHex, hex) * edgeLengthKM < UPDATE_THRESHOLD_KM)
) {
return;
}
const {unitsPerMeter} = viewport.distanceScales;
let vertices = h3ToPolygon(hex);
const [centerLat, centerLng] = h3ToGeo(hex);
const [centerX, centerY] = viewport.projectFlat([centerLng, centerLat]);
vertices = vertices.map(p => {
const worldPosition = viewport.projectFlat(p);
worldPosition[0] = (worldPosition[0] - centerX) / unitsPerMeter[0];
worldPosition[1] = (worldPosition[1] - centerY) / unitsPerMeter[1];
return worldPosition;
});
this.setState({centerHex: hex, vertices});
}