make-geo-json
Version:
Converts polylines, wkts, wkbs, polygons, circles, rectangles, as well as standard geoJSON into geoJSON MultiPolygon Feature Objects. Converts wkb_list and FeatureCollection to arrays.
77 lines (62 loc) • 2.56 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = generateCircleApprox;
var _mathjs = require('mathjs');
var _mathjs2 = _interopRequireDefault(_mathjs);
var _map = require('lodash/map');
var _map2 = _interopRequireDefault(_map);
var _range = require('lodash/range');
var _range2 = _interopRequireDefault(_range);
var _reverse = require('lodash/reverse');
var _reverse2 = _interopRequireDefault(_reverse);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
// convert degrees (lat/long) to radians
// MAKE A CIRCLE
// Using Doc's code
// https://gist.github.com/otherchris/c529f2f39fd9af273455933eb3a50e90
// generateCircleApprox()
const radians = deg => deg / 360 * 2 * Math.PI;
// create a set of points around the origin at the given radius
// 24 is usually a good number of sides, consider hard coding that
const rotatedPointsOrigin = (radius, sides) => {
const z = new _mathjs2.default.complex({ phi: 0, r: radius });
const angle = 2 * Math.PI / sides;
return (0, _map2.default)((0, _range2.default)(sides), x => {
const i = _mathjs2.default.complex(0, 1);
const point = _mathjs2.default.multiply(z, _mathjs2.default.exp(_mathjs2.default.multiply(x, angle, _mathjs2.default.complex(0, 1))));
return [_mathjs2.default.re(point), _mathjs2.default.im(point)];
});
};
// scale points to coordinate system
// const latFactor = 69.172 // if miles
const latFactor = 111321.543; // if meters
const scalePoints = (points, lat) => {
const lngFactor = _mathjs2.default.cos(radians(lat)) * latFactor;
return (0, _map2.default)(points, x => [x[0] / lngFactor, x[1] / latFactor]);
};
// move this circle to the correct place on the map
const translatePoints = (points, center) => (0, _map2.default)(points, point => [point[0] + center[0], point[1] + center[1]]);
// create GeoJSON feature from circle
// (using a 24 sided approximation by default)
function generateCircleApprox(radius, center, sides = 24) {
const points = rotatedPointsOrigin(radius, sides);
const scaledPoints = scalePoints(points, center[0]);
const translatedPoints = translatePoints(scaledPoints, center);
return translatedPoints;
/* const reversedPoints = map(translatedPoints, (x) => reverse(x));
* return reversedPoints; */
/* return {
* type: 'Feature',
* properties: {
* radius,
* center,
* sides,
* },
* geometry: {
* type: 'MultiPolygon',
* coordinates: [[reversedPoints]],
* },
* }; */
}