Exemple #1
0
        /**
         * Takes a set of features, calculates the bbox of all input features, and returns a bounding box.
         *
         * @name bbox
         * @param {(Feature|FeatureCollection)} geojson input features
         * @return {Array<number>} bbox extent in [minX, minY, maxX, maxY] order
         * @example
         * var pt1 = point([114.175329, 22.2524])
         * var pt2 = point([114.170007, 22.267969])
         * var pt3 = point([114.200649, 22.274641])
         * var pt4 = point([114.200649, 22.274641])
         * var pt5 = point([114.186744, 22.265745])
         * var features = featureCollection([pt1, pt2, pt3, pt4, pt5])
         *
         * var bbox = turf.bbox(features);
         *
         * var bboxPolygon = turf.bboxPolygon(bbox);
         *
         * //=bbox
         *
         * //=bboxPolygon
         */
        public static List <double> Bbox(IGeoJSONObject geojson)
        {
            var bbox = new List <double>()
            {
                double.PositiveInfinity, double.PositiveInfinity, double.NegativeInfinity, double.NegativeInfinity
            };

            Turf.CoordEach(geojson, (List <double> coord) => {
                if (bbox[0] > coord[0])
                {
                    bbox[0] = coord[0];
                }
                if (bbox[1] > coord[1])
                {
                    bbox[1] = coord[1];
                }
                if (bbox[2] < coord[0])
                {
                    bbox[2] = coord[0];
                }
                if (bbox[3] < coord[1])
                {
                    bbox[3] = coord[1];
                }
            });
            return(bbox);
        }
Exemple #2
0
        /**
         * Takes one or more features and calculates the centroid using
         * the mean of all vertices.
         * This lessens the effect of small islands and artifacts when calculating
         * the centroid of a set of polygons.
         *
         * @name centroid
         * @param {(Feature|FeatureCollection)} features input features
         * @return {Feature<Point>} the centroid of the input features
         * @example
         * var poly = {
         *   "type": "Feature",
         *   "properties": {},
         *   "geometry": {
         *     "type": "Polygon",
         *     "coordinates": [[
         *       [105.818939,21.004714],
         *       [105.818939,21.061754],
         *       [105.890007,21.061754],
         *       [105.890007,21.004714],
         *       [105.818939,21.004714]
         *     ]]
         *   }
         * };
         *
         * var centroidPt = turf.centroid(poly);
         *
         * var result = {
         *   "type": "FeatureCollection",
         *   "features": [poly, centroidPt]
         * };
         *
         * //=result
         */
        public static Feature Centroid(IGeoJSONObject features)
        {
            double xSum = 0;
            double ySum = 0;
            int    len  = 0;

            Turf.CoordEach(features, (List <double> coord) => {
                xSum += coord[0];
                ySum += coord[1];
                len++;
            }, true);
            return(Turf.Point(new double[] { xSum / (double)len, ySum / (double)len }));
        }