Esempio n. 1
0
        /// <summary>
        /// Expand a geometry's extent - Help Map zoom to the geometry
        /// </summary>
        /// <param name="geometry">Geometry parameter: Point, Polyline, or Polygon</param>
        /// <param name="percent">Percentage to expand the geometry's extent</param>
        /// <returns>An expanded envelope</returns>
        public static Envelope ExpandGeometryExtent(Geometry geometry, double percent)
        {
            if (geometry != null && Application.Current.RootVisual != null)
            {
                Envelope ext = geometry.Extent;
                ScaleLine.ScaleLineUnit mapUnits = (Application.Current.RootVisual as MapPage).MapUnits;

                double radiusX = MINIMUM_MAP_WIDTH / 2.0;
                double radiusY = MINIMUM_MAP_WIDTH / 2.0;
                double minimum = MINIMUM_MAP_WIDTH;

                if (mapUnits == ScaleLine.ScaleLineUnit.DecimalDegrees)
                {
                    double[] conversion = UnitsHelper.GetConversionToDegree((ext.YMin + ext.YMax) / 2.0, ScaleLine.ScaleLineUnit.Meters);
                    radiusX *= conversion[0];
                    radiusY *= conversion[1];
                    minimum *= conversion[0];
                }
                else
                {
                    double conversion = UnitsHelper.GetConversionFromMeter(mapUnits);
                    radiusX *= conversion;
                    radiusY *= conversion;
                    minimum *= conversion;
                }

                double bufferX = (ext.Width < minimum) ? radiusX : (ext.Width * percent / 2.0);
                double bufferY = (ext.Height < minimum) ? radiusY : (ext.Height * percent / 2.0);

                ext.XMin = ext.XMin - bufferX;
                ext.XMax = ext.XMax + bufferX;
                ext.YMin = ext.YMin - bufferY;
                ext.YMax = ext.YMax + bufferY;

                return(ext);
            }
            else
            {
                return(null);
            }
        }
        private Polygon CreateBufferCircle(MapPoint center, double radius, bool toMercator)
        {
            double pi36 = Math.PI / 180.0;
            double rx = radius, ry = radius;
            double dx = 1.0, dy = 1.0;

            ScaleLine.ScaleLineUnit units = UnitsHelper.GetUnitsByWKID(center.SpatialReference.WKID);
            if (units == ScaleLine.ScaleLineUnit.DecimalDegrees)
            {
                double[] conversion = UnitsHelper.GetConversionToDegree(center.Y, ScaleLine.ScaleLineUnit.Meters);
                rx = radius * conversion[0];
                ry = radius * conversion[1];
            }
            else
            {
                double conversion = UnitsHelper.GetConversionFromMeter(units);
                rx = radius * conversion;
                ry = radius * conversion;
            }

            PointCollection pointSet      = new PointCollection();

            for (int i = 0; i <= 36; i++)
            {
                dx = center.X + rx * Math.Cos(i * 10 * pi36);
                dy = center.Y + ry * Math.Sin(i * 10 * pi36);
                MapPoint p = new MapPoint(dx, dy, new SpatialReference(center.SpatialReference.WKID));
                pointSet.Add((center.SpatialReference.WKID == 4326 && toMercator) ? p.GeographicToWebMercator() : p);
            }

            Polygon circle                = new Polygon();

            circle.Rings.Add(pointSet);
            circle.SpatialReference = new SpatialReference((center.SpatialReference.WKID == 4326 && toMercator) ? 102113 : center.SpatialReference.WKID);

            return(circle);
        }